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.

PoleFilter.cpp 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 "PoleFilter.h"
  28. namespace Dsp
  29. {
  30. //------------------------------------------------------------------------------
  31. complex_t LowPassTransform::transform(complex_t c) const
  32. {
  33. if (c == infinity()) { return complex_t(-1, 0); }
  34. // frequency transform
  35. c = f * c;
  36. // bilinear low pass transform
  37. return (1. + c) / (1. - c);
  38. }
  39. LowPassTransform::LowPassTransform(double fc, LayoutBase& digital, LayoutBase const& analog)
  40. {
  41. digital.reset();
  42. // prewarp
  43. f = tan(doublePi * fc);
  44. const int numPoles = analog.getNumPoles();
  45. const int pairs = numPoles / 2;
  46. for (int i = 0; i < pairs; ++i)
  47. {
  48. const PoleZeroPair& pair = analog[i];
  49. digital.addPoleZeroConjugatePairs(transform(pair.poles.first), transform(pair.zeros.first));
  50. }
  51. if (numPoles & 1)
  52. {
  53. const PoleZeroPair& pair = analog[pairs];
  54. digital.add(transform(pair.poles.first), transform(pair.zeros.first));
  55. }
  56. digital.setNormal(analog.getNormalW(), analog.getNormalGain());
  57. }
  58. //------------------------------------------------------------------------------
  59. complex_t HighPassTransform::transform(complex_t c) const
  60. {
  61. if (c == infinity()) { return complex_t(1, 0); }
  62. // frequency transform
  63. c = f * c;
  64. // bilinear high pass transform
  65. return - (1. + c) / (1. - c);
  66. }
  67. HighPassTransform::HighPassTransform(double fc, LayoutBase& digital, LayoutBase const& analog)
  68. {
  69. digital.reset();
  70. // prewarp
  71. f = 1. / tan(doublePi * fc);
  72. const int numPoles = analog.getNumPoles();
  73. const int pairs = numPoles / 2;
  74. for (int i = 0; i < pairs; ++i)
  75. {
  76. const PoleZeroPair& pair = analog[i];
  77. digital.addPoleZeroConjugatePairs(transform(pair.poles.first), transform(pair.zeros.first));
  78. }
  79. if (numPoles & 1)
  80. {
  81. const PoleZeroPair& pair = analog[pairs];
  82. digital.add(transform(pair.poles.first), transform(pair.zeros.first));
  83. }
  84. digital.setNormal(doublePi - analog.getNormalW(), analog.getNormalGain());
  85. }
  86. //------------------------------------------------------------------------------
  87. BandPassTransform::BandPassTransform(double fc, double fw, LayoutBase& digital, LayoutBase const& analog)
  88. {
  89. // handle degenerate cases efficiently
  90. // THIS DOESNT WORK because the cascade states won't match
  91. #if 0
  92. const double fw_2 = fw / 2;
  93. if (fc - fw_2 < 0) { LowPassTransform::transform (fc + fw_2, digital, analog); }
  94. else if (fc + fw_2 >= 0.5) { HighPassTransform::transform (fc - fw_2, digital, analog); }
  95. else
  96. #endif
  97. digital.reset();
  98. const double ww = 2 * doublePi * fw;
  99. // pre-calcs
  100. wc2 = 2 * doublePi * fc - (ww / 2);
  101. wc = wc2 + ww;
  102. // what is this crap?
  103. if (wc2 < 1e-8) { wc2 = 1e-8; }
  104. if (wc > doublePi - 1e-8) { wc = doublePi - 1e-8; }
  105. a = cos((wc + wc2) * 0.5) /
  106. cos((wc - wc2) * 0.5);
  107. b = 1 / tan((wc - wc2) * 0.5);
  108. a2 = a * a;
  109. b2 = b * b;
  110. ab = a * b;
  111. ab_2 = 2 * ab;
  112. const int numPoles = analog.getNumPoles();
  113. const int pairs = numPoles / 2;
  114. for (int i = 0; i < pairs; ++i)
  115. {
  116. const PoleZeroPair& pair = analog[i];
  117. ComplexPair p1 = transform(pair.poles.first);
  118. const ComplexPair z1 = transform(pair.zeros.first);
  119. //
  120. // Optimize out the calculations for conjugates for Release builds
  121. //
  122. #ifndef NDEBUG
  123. ComplexPair p2 = transform(pair.poles.second);
  124. assert(p2.first == std::conj (p1.first));
  125. assert(p2.second == std::conj (p1.second));
  126. #endif
  127. digital.addPoleZeroConjugatePairs(p1.first, z1.first);
  128. digital.addPoleZeroConjugatePairs(p1.second, z1.second);
  129. }
  130. if (numPoles & 1)
  131. {
  132. const ComplexPair poles = transform(analog[pairs].poles.first);
  133. const ComplexPair zeros = transform(analog[pairs].zeros.first);
  134. digital.add(poles, zeros);
  135. }
  136. const double wn = analog.getNormalW();
  137. digital.setNormal(2 * atan(sqrt(tan((wc + wn) * 0.5) * tan((wc2 + wn) * 0.5))), analog.getNormalGain());
  138. }
  139. ComplexPair BandPassTransform::transform(complex_t c) const
  140. {
  141. if (c == infinity()) { return ComplexPair(-1, 1); }
  142. c = (1. + c) / (1. - c); // bilinear
  143. complex_t v = 0;
  144. v = addmul(v, 4 * (b2 * (a2 - 1) + 1), c);
  145. v += 8 * (b2 * (a2 - 1) - 1);
  146. v *= c;
  147. v += 4 * (b2 * (a2 - 1) + 1);
  148. v = std::sqrt(v);
  149. complex_t u = -v;
  150. u = addmul(u, ab_2, c);
  151. u += ab_2;
  152. v = addmul(v, ab_2, c);
  153. v += ab_2;
  154. complex_t d = 0;
  155. d = addmul(d, 2 * (b - 1), c) + 2 * (1 + b);
  156. return ComplexPair(u / d, v / d);
  157. }
  158. //------------------------------------------------------------------------------
  159. BandStopTransform::BandStopTransform(double fc, double fw, LayoutBase& digital, LayoutBase const& analog)
  160. {
  161. digital.reset();
  162. const double ww = 2 * doublePi * fw;
  163. wc2 = 2 * doublePi * fc - (ww / 2);
  164. wc = wc2 + ww;
  165. // this is crap
  166. if (wc2 < 1e-8) { wc2 = 1e-8; }
  167. if (wc > doublePi - 1e-8) { wc = doublePi - 1e-8; }
  168. a = cos((wc + wc2) * .5) /
  169. cos((wc - wc2) * .5);
  170. b = tan((wc - wc2) * .5);
  171. a2 = a * a;
  172. b2 = b * b;
  173. const int numPoles = analog.getNumPoles();
  174. const int pairs = numPoles / 2;
  175. for (int i = 0; i < pairs; ++i)
  176. {
  177. const PoleZeroPair& pair = analog[i];
  178. const ComplexPair p = transform(pair.poles.first);
  179. ComplexPair z = transform(pair.zeros.first);
  180. //
  181. // Optimize out the calculations for conjugates for Release builds
  182. //
  183. // trick to get the conjugate
  184. if (z.second == z.first) { z.second = std::conj(z.first); }
  185. digital.addPoleZeroConjugatePairs(p.first, z.first);
  186. digital.addPoleZeroConjugatePairs(p.second, z.second);
  187. }
  188. if (numPoles & 1)
  189. {
  190. const ComplexPair poles = transform(analog[pairs].poles.first);
  191. const ComplexPair zeros = transform(analog[pairs].zeros.first);
  192. digital.add(poles, zeros);
  193. }
  194. if (fc < 0.25) { digital.setNormal(doublePi, analog.getNormalGain()); }
  195. else { digital.setNormal(0, analog.getNormalGain()); }
  196. }
  197. ComplexPair BandStopTransform::transform(complex_t c) const
  198. {
  199. if (c == infinity()) { c = -1; }
  200. else { c = (1. + c) / (1. - c); }// bilinear
  201. complex_t u(0);
  202. u = addmul(u, 4 * (b2 + a2 - 1), c);
  203. u += 8 * (b2 - a2 + 1);
  204. u *= c;
  205. u += 4 * (a2 + b2 - 1);
  206. u = std::sqrt(u);
  207. complex_t v = u * -.5;
  208. v += a;
  209. v = addmul(v, -a, c);
  210. u *= .5;
  211. u += a;
  212. u = addmul(u, -a, c);
  213. complex_t d(b + 1);
  214. d = addmul(d, b - 1, c);
  215. return ComplexPair(u / d, v / d);
  216. }
  217. } // namespace Dsp