You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

filter_biquad.h 4.4KB

10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. private:
  108. int32_t definition[32]; // up to 4 cascaded biquads
  109. audio_block_t *inputQueueArray[1];
  110. };
  111. #endif