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.h 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. #pragma once
  27. #include "Common.h"
  28. #include "MathSupplement.h"
  29. #include "Cascade.h"
  30. namespace Dsp
  31. {
  32. /*
  33. * Base for filters designed via algorithmic placement of poles and zeros.
  34. *
  35. * Typically, the filter is first designed as a half-band low pass or
  36. * low shelf analog filter (s-plane). Then, using a transformation such
  37. * as the ones from Constantinides, the poles and zeros of the analog filter
  38. * are calculated in the z-plane.
  39. *
  40. */
  41. // Factored implementations to reduce template instantiations
  42. class PoleFilterBase2 : public Cascade
  43. {
  44. public:
  45. // This gets the poles/zeros directly from the digital
  46. // prototype. It is used to double check the correctness
  47. // of the recovery of pole/zeros from biquad coefficients.
  48. //
  49. // It can also be used to accelerate the interpolation
  50. // of pole/zeros for parameter modulation, since a pole
  51. // filter already has them calculated
  52. #if 1
  53. // Commenting this out will pass the call to the Cascade,
  54. // which tries to compute the poles and zeros from the biquad
  55. // coefficients.
  56. std::vector<PoleZeroPair> getPoleZeros() const
  57. {
  58. std::vector<PoleZeroPair> vpz;
  59. const int pairs = (m_digitalProto.getNumPoles() + 1) / 2;
  60. for (int i = 0; i < pairs; ++i) { vpz.push_back(m_digitalProto[i]); }
  61. return vpz;
  62. }
  63. #endif
  64. protected:
  65. LayoutBase m_digitalProto;
  66. };
  67. // Serves a container to hold the analog prototype
  68. // and the digital pole/zero layout.
  69. template <class AnalogPrototype>
  70. class PoleFilterBase : public PoleFilterBase2
  71. {
  72. protected:
  73. void setPrototypeStorage(const LayoutBase& analogStorage, const LayoutBase& digitalStorage)
  74. {
  75. m_analogProto.setStorage(analogStorage);
  76. m_digitalProto = digitalStorage;
  77. }
  78. AnalogPrototype m_analogProto;
  79. };
  80. //------------------------------------------------------------------------------
  81. // Storage for pole filters
  82. template <class BaseClass,
  83. int MaxAnalogPoles,
  84. int MaxDigitalPoles = MaxAnalogPoles>
  85. struct PoleFilter : BaseClass
  86. , CascadeStages<(MaxDigitalPoles + 1) / 2>
  87. {
  88. PoleFilter()
  89. {
  90. // This glues together the factored base classes
  91. // with the templatized storage classes.
  92. BaseClass::setCascadeStorage(this->getCascadeStorage());
  93. BaseClass::setPrototypeStorage(m_analogStorage, m_digitalStorage);
  94. }
  95. private:
  96. Layout<MaxAnalogPoles> m_analogStorage;
  97. Layout<MaxDigitalPoles> m_digitalStorage;
  98. };
  99. //------------------------------------------------------------------------------
  100. /*
  101. * s-plane to z-plane transforms
  102. *
  103. * For pole filters, an analog prototype is created via placement of
  104. * poles and zeros in the s-plane. The analog prototype is either
  105. * a halfband low pass or a halfband low shelf. The poles, zeros,
  106. * and normalization parameters are transformed into the z-plane
  107. * using variants of the bilinear transformation.
  108. *
  109. */
  110. // low pass to low pass
  111. class LowPassTransform
  112. {
  113. public:
  114. LowPassTransform(double fc, LayoutBase& digital, LayoutBase const& analog);
  115. private:
  116. complex_t transform(complex_t c) const;
  117. double f = 0;
  118. };
  119. //------------------------------------------------------------------------------
  120. // low pass to high pass
  121. class HighPassTransform
  122. {
  123. public:
  124. HighPassTransform(double fc, LayoutBase& digital, LayoutBase const& analog);
  125. private:
  126. complex_t transform(complex_t c) const;
  127. double f = 0;
  128. };
  129. //------------------------------------------------------------------------------
  130. // low pass to band pass transform
  131. class BandPassTransform
  132. {
  133. public:
  134. BandPassTransform(double fc, double fw, LayoutBase& digital, LayoutBase const& analog);
  135. private:
  136. ComplexPair transform(complex_t c) const;
  137. double wc = 0;
  138. double wc2 = 0;
  139. double a = 0;
  140. double b = 0;
  141. double a2 = 0;
  142. double b2 = 0;
  143. double ab = 0;
  144. double ab_2 = 0;
  145. };
  146. //------------------------------------------------------------------------------
  147. // low pass to band stop transform
  148. class BandStopTransform
  149. {
  150. public:
  151. BandStopTransform(double fc, double fw, LayoutBase& digital, LayoutBase const& analog);
  152. private:
  153. ComplexPair transform(complex_t c) const;
  154. double wc = 0;
  155. double wc2 = 0;
  156. double a = 0;
  157. double b = 0;
  158. double a2 = 0;
  159. double b2 = 0;
  160. };
  161. } // namespace Dsp