您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

filter_biquad.h 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* Audio Library for Teensy 3.X
  2. * Copyright (c) 2014, Paul Stoffregen, paul@pjrc.com
  3. *
  4. * Development of this audio library was funded by PJRC.COM, LLC by sales of
  5. * Teensy and Audio Adaptor boards. Please support PJRC's efforts to develop
  6. * open source software by purchasing Teensy or other PJRC products.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice, development funding notice, and this permission
  16. * notice shall be included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #ifndef filter_biquad_h_
  27. #define filter_biquad_h_
  28. #include "Arduino.h"
  29. #include "AudioStream.h"
  30. class AudioFilterBiquad : public AudioStream
  31. {
  32. public:
  33. AudioFilterBiquad(void) : AudioStream(1, inputQueueArray) {
  34. // by default, the filter will not pass anything
  35. for (int i=0; i<32; i++) definition[i] = 0;
  36. }
  37. virtual void update(void);
  38. // Set the biquad coefficients directly
  39. void setCoefficients(uint32_t stage, const int *coefficients);
  40. void setCoefficients(uint32_t stage, const double *coefficients) {
  41. int coef[5];
  42. coef[0] = coefficients[0] * 1073741824.0;
  43. coef[1] = coefficients[1] * 1073741824.0;
  44. coef[2] = coefficients[2] * 1073741824.0;
  45. coef[3] = coefficients[3] * 1073741824.0;
  46. coef[4] = coefficients[4] * 1073741824.0;
  47. setCoefficients(stage, coef);
  48. }
  49. // Compute common filter functions
  50. // http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt
  51. void setLowpass(uint32_t stage, float frequency, float q = 0.7071) {
  52. int coef[5];
  53. double w0 = frequency * (2 * 3.141592654 / AUDIO_SAMPLE_RATE_EXACT);
  54. double sinW0 = sin(w0);
  55. double alpha = sinW0 / ((double)q * 2.0);
  56. double cosW0 = cos(w0);
  57. double scale = 1073741824.0 / (1.0 + alpha);
  58. /* b0 */ coef[0] = ((1.0 - cosW0) / 2.0) * scale;
  59. /* b1 */ coef[1] = (1.0 - cosW0) * scale;
  60. /* b2 */ coef[2] = coef[0];
  61. /* a1 */ coef[3] = (-2.0 * cosW0) * scale;
  62. /* a2 */ coef[4] = (1.0 - alpha) * scale;
  63. setCoefficients(stage, coef);
  64. }
  65. void setHighpass(uint32_t stage, float frequency, float q = 0.7071) {
  66. int coef[5];
  67. double w0 = frequency * (2 * 3.141592654 / AUDIO_SAMPLE_RATE_EXACT);
  68. double sinW0 = sin(w0);
  69. double alpha = sinW0 / ((double)q * 2.0);
  70. double cosW0 = cos(w0);
  71. double scale = 1073741824.0 / (1.0 + alpha);
  72. /* b0 */ coef[0] = ((1.0 + cosW0) / 2.0) * scale;
  73. /* b1 */ coef[1] = -(1.0 + cosW0) * scale;
  74. /* b2 */ coef[2] = coef[0];
  75. /* a1 */ coef[3] = (-2.0 * cosW0) * scale;
  76. /* a2 */ coef[4] = (1.0 - alpha) * scale;
  77. setCoefficients(stage, coef);
  78. }
  79. void setBandpass(uint32_t stage, float frequency, float q = 1.0) {
  80. int coef[5];
  81. double w0 = frequency * (2 * 3.141592654 / AUDIO_SAMPLE_RATE_EXACT);
  82. double sinW0 = sin(w0);
  83. double alpha = sinW0 / ((double)q * 2.0);
  84. double cosW0 = cos(w0);
  85. double scale = 1073741824.0 / (1.0 + alpha);
  86. /* b0 */ coef[0] = alpha * scale;
  87. /* b1 */ coef[1] = 0;
  88. /* b2 */ coef[2] = (-alpha) * scale;
  89. /* a1 */ coef[3] = (-2.0 * cosW0) * scale;
  90. /* a2 */ coef[4] = (1.0 - alpha) * scale;
  91. setCoefficients(stage, coef);
  92. }
  93. void setNotch(uint32_t stage, float frequency, float q = 1.0) {
  94. int coef[5];
  95. double w0 = frequency * (2 * 3.141592654 / AUDIO_SAMPLE_RATE_EXACT);
  96. double sinW0 = sin(w0);
  97. double alpha = sinW0 / ((double)q * 2.0);
  98. double cosW0 = cos(w0);
  99. double scale = 1073741824.0 / (1.0 + alpha);
  100. /* b0 */ coef[0] = scale;
  101. /* b1 */ coef[1] = (-2.0 * cosW0) * scale;
  102. /* b2 */ coef[2] = coef[0];
  103. /* a1 */ coef[3] = (-2.0 * cosW0) * scale;
  104. /* a2 */ coef[4] = (1.0 - alpha) * scale;
  105. setCoefficients(stage, coef);
  106. }
  107. void setLowShelf(uint32_t stage, float frequency, float gain, float slope = 1.0f) {
  108. int coef[5];
  109. double a = pow(10.0, gain/40.0);
  110. double w0 = frequency * (2 * 3.141592654 / AUDIO_SAMPLE_RATE_EXACT);
  111. double sinW0 = sin(w0);
  112. //double alpha = (sinW0 * sqrt((a+1/a)*(1/slope-1)+2) ) / 2.0;
  113. double cosW0 = cos(w0);
  114. //generate three helper-values (intermediate results):
  115. double sinsq = sinW0 * sqrt( (pow(a,2.0)+1.0)*(1.0/slope-1.0)+2.0*a );
  116. double aMinus = (a-1.0)*cosW0;
  117. double aPlus = (a+1.0)*cosW0;
  118. double scale = 1073741824.0 / ( (a+1.0) + aMinus + sinsq);
  119. /* b0 */ coef[0] = a * ( (a+1.0) - aMinus + sinsq ) * scale;
  120. /* b1 */ coef[1] = 2.0*a * ( (a-1.0) - aPlus ) * scale;
  121. /* b2 */ coef[2] = a * ( (a+1.0) - aMinus - sinsq ) * scale;
  122. /* a1 */ coef[3] = -2.0* ( (a-1.0) + aPlus ) * scale;
  123. /* a2 */ coef[4] = ( (a+1.0) + aMinus - sinsq ) * scale;
  124. setCoefficients(stage, coef);
  125. }
  126. void setHighShelf(uint32_t stage, float frequency, float gain, float slope = 1.0f) {
  127. int coef[5];
  128. double a = pow(10.0, gain/40.0);
  129. double w0 = frequency * (2 * 3.141592654 / AUDIO_SAMPLE_RATE_EXACT);
  130. double sinW0 = sin(w0);
  131. //double alpha = (sinW0 * sqrt((a+1/a)*(1/slope-1)+2) ) / 2.0;
  132. double cosW0 = cos(w0);
  133. //generate three helper-values (intermediate results):
  134. double sinsq = sinW0 * sqrt( (pow(a,2.0)+1.0)*(1.0/slope-1.0)+2.0*a );
  135. double aMinus = (a-1.0)*cosW0;
  136. double aPlus = (a+1.0)*cosW0;
  137. double scale = 1073741824.0 / ( (a+1.0) - aMinus + sinsq);
  138. /* b0 */ coef[0] = a * ( (a+1.0) + aMinus + sinsq ) * scale;
  139. /* b1 */ coef[1] = -2.0*a * ( (a-1.0) + aPlus ) * scale;
  140. /* b2 */ coef[2] = a * ( (a+1.0) + aMinus - sinsq ) * scale;
  141. /* a1 */ coef[3] = 2.0* ( (a-1.0) - aPlus ) * scale;
  142. /* a2 */ coef[4] = ( (a+1.0) - aMinus - sinsq ) * scale;
  143. setCoefficients(stage, coef);
  144. }
  145. private:
  146. int32_t definition[32]; // up to 4 cascaded biquads
  147. audio_block_t *inputQueueArray[1];
  148. };
  149. #endif