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.

Biquad.cpp 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*******************************************************************************
  2. "A Collection of Useful C++ Classes for Digital Signal Processing"
  3. By Vinnie Falco
  4. Official project location:
  5. https://github.com/vinniefalco/DSPFilters
  6. See Documentation.cpp for contact information, notes, and bibliography.
  7. --------------------------------------------------------------------------------
  8. License: MIT License (http://www.opensource.org/licenses/mit-license.php)
  9. Copyright (c) 2009 by Vinnie Falco
  10. Permission is hereby granted, free of charge, to any person obtaining a copy
  11. of this software and associated documentation files (the "Software"), to deal
  12. in the Software without restriction, including without limitation the rights
  13. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. copies of the Software, and to permit persons to whom the Software is
  15. furnished to do so, subject to the following conditions:
  16. The above copyright notice and this permission notice shall be included in
  17. all copies or substantial portions of the Software.
  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. #include "Common.h"
  27. #include "MathSupplement.h"
  28. #include "Biquad.h"
  29. namespace Dsp
  30. {
  31. BiquadPoleState::BiquadPoleState(const BiquadBase& s)
  32. {
  33. const double a0 = s.getA0();
  34. const double a1 = s.getA1();
  35. const double a2 = s.getA2();
  36. const double b0 = s.getB0();
  37. const double b1 = s.getB1();
  38. const double b2 = s.getB2();
  39. if (a2 == 0 && b2 == 0)
  40. {
  41. // single pole
  42. poles.first = -a1;
  43. zeros.first = -b0 / b1;
  44. poles.second = 0;
  45. zeros.second = 0;
  46. }
  47. else
  48. {
  49. {
  50. const complex_t c = sqrt(complex_t(a1 * a1 - 4 * a0 * a2, 0));
  51. double d = 2. * a0;
  52. poles.first = -(a1 + c) / d;
  53. poles.second = (c - a1) / d;
  54. assert(!poles.is_nan());
  55. }
  56. {
  57. const complex_t c = sqrt(complex_t(b1 * b1 - 4 * b0 * b2, 0));
  58. double d = 2. * b0;
  59. zeros.first = -(b1 + c) / d;
  60. zeros.second = (c - b1) / d;
  61. assert(!zeros.is_nan());
  62. }
  63. }
  64. gain = b0 / a0;
  65. }
  66. //------------------------------------------------------------------------------
  67. complex_t BiquadBase::response(double normalizedFrequency) const
  68. {
  69. const double a0 = getA0();
  70. const double a1 = getA1();
  71. const double a2 = getA2();
  72. const double b0 = getB0();
  73. const double b1 = getB1();
  74. const double b2 = getB2();
  75. const double w = 2 * doublePi * normalizedFrequency;
  76. const complex_t czn1 = std::polar(1., -w);
  77. const complex_t czn2 = std::polar(1., -2 * w);
  78. complex_t ch(1);
  79. complex_t cbot(1);
  80. complex_t ct(b0 / a0);
  81. complex_t cb(1);
  82. ct = addmul(ct, b1 / a0, czn1);
  83. ct = addmul(ct, b2 / a0, czn2);
  84. cb = addmul(cb, a1 / a0, czn1);
  85. cb = addmul(cb, a2 / a0, czn2);
  86. ch *= ct;
  87. cbot *= cb;
  88. return ch / cbot;
  89. }
  90. std::vector<PoleZeroPair> BiquadBase::getPoleZeros() const
  91. {
  92. std::vector<PoleZeroPair> vpz;
  93. BiquadPoleState bps(*this);
  94. vpz.push_back(bps);
  95. return vpz;
  96. }
  97. void BiquadBase::setCoefficients(double a0, double a1, double a2,
  98. double b0, double b1, double b2)
  99. {
  100. assert(!Dsp::is_nan (a0) && !Dsp::is_nan (a1) && !Dsp::is_nan (a2) &&
  101. !Dsp::is_nan (b0) && !Dsp::is_nan (b1) && !Dsp::is_nan (b2));
  102. m_a0 = a0;
  103. m_a1 = a1 / a0;
  104. m_a2 = a2 / a0;
  105. m_b0 = b0 / a0;
  106. m_b1 = b1 / a0;
  107. m_b2 = b2 / a0;
  108. }
  109. void BiquadBase::setOnePole(complex_t pole, complex_t zero)
  110. {
  111. #if 0
  112. pole = adjust_imag (pole);
  113. zero = adjust_imag (zero);
  114. #else
  115. assert(pole.imag() == 0);
  116. assert(zero.imag() == 0);
  117. #endif
  118. const double a0 = 1;
  119. const double a1 = -pole.real();
  120. const double a2 = 0;
  121. const double b0 = 1;
  122. const double b1 = -zero.real();
  123. const double b2 = 0;
  124. setCoefficients(a0, a1, a2, b0, b1, b2);
  125. }
  126. void BiquadBase::setTwoPole(complex_t pole1, complex_t zero1,
  127. complex_t pole2, complex_t zero2)
  128. {
  129. #if 0
  130. pole1 = adjust_imag (pole1);
  131. pole2 = adjust_imag (pole2);
  132. zero1 = adjust_imag (zero1);
  133. zero2 = adjust_imag (zero2);
  134. #endif
  135. const double a0 = 1;
  136. double a1;
  137. double a2;
  138. if (pole1.imag() != 0)
  139. {
  140. assert(pole2 == std::conj (pole1));
  141. a1 = -2 * pole1.real();
  142. a2 = std::norm(pole1);
  143. }
  144. else
  145. {
  146. assert(pole2.imag() == 0);
  147. a1 = -(pole1.real() + pole2.real());
  148. a2 = pole1.real() * pole2.real();
  149. }
  150. const double b0 = 1;
  151. double b1;
  152. double b2;
  153. if (zero1.imag() != 0)
  154. {
  155. assert(zero2 == std::conj (zero1));
  156. b1 = -2 * zero1.real();
  157. b2 = std::norm(zero1);
  158. }
  159. else
  160. {
  161. assert(zero2.imag() == 0);
  162. b1 = -(zero1.real() + zero2.real());
  163. b2 = zero1.real() * zero2.real();
  164. }
  165. setCoefficients(a0, a1, a2, b0, b1, b2);
  166. }
  167. void BiquadBase::setPoleZeroForm(const BiquadPoleState& bps)
  168. {
  169. setPoleZeroPair(bps);
  170. applyScale(bps.gain);
  171. }
  172. void BiquadBase::setIdentity() { setCoefficients(1, 0, 0, 1, 0, 0); }
  173. void BiquadBase::applyScale(double scale)
  174. {
  175. m_b0 *= scale;
  176. m_b1 *= scale;
  177. m_b2 *= scale;
  178. }
  179. //------------------------------------------------------------------------------
  180. Biquad::Biquad() {}
  181. // Construct a second order section from a pair of poles and zeroes
  182. Biquad::Biquad(const BiquadPoleState& bps) { setPoleZeroForm(bps); }
  183. //------------------------------------------------------------------------------
  184. } // namespace Dsp