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.

Elliptic.h 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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 "Cascade.h"
  29. #include "Design.h"
  30. #include "Filter.h"
  31. #include "PoleFilter.h"
  32. namespace Dsp
  33. {
  34. /*
  35. * Filters with Elliptic response characteristics
  36. *
  37. */
  38. namespace Elliptic
  39. {
  40. // Solves for Jacobi elliptics
  41. class Solver
  42. {
  43. public:
  44. static double ellipticK(double k);
  45. };
  46. // Half-band analog prototype (s-plane)
  47. class AnalogLowPass : public LayoutBase
  48. {
  49. public:
  50. AnalogLowPass();
  51. void design(int numPoles, double rippleDb, double rolloff);
  52. private:
  53. void prodpoly(int sn);
  54. void calcfz2(int i);
  55. void calcfz();
  56. void calcqz();
  57. double findfact(int t);
  58. double calcsn(double u);
  59. #if 0
  60. template<int n>
  61. struct CalcArray
  62. {
  63. double& operator[](size_t index)
  64. {
  65. assert( index<n );
  66. return m_a[index];
  67. }
  68. private:
  69. double m_a[n];
  70. };
  71. #else
  72. #endif
  73. double m_p0 = 0;
  74. double m_q = 0;
  75. double m_K = 0;
  76. double m_Kprime = 0;
  77. double m_e = 0;
  78. int m_nin = 0;
  79. int m_m = 0;
  80. int m_n2 = 0;
  81. int m_em = 0;
  82. double m_zeros[100];
  83. double m_c1[100];
  84. double m_b1[100];
  85. double m_a1[100];
  86. double m_d1[100];
  87. double m_q1[100];
  88. double m_z1[100];
  89. double m_f1[100];
  90. double m_s1[100];
  91. double m_p [100];
  92. double m_zw1[100];
  93. double m_zf1[100];
  94. double m_zq1[100];
  95. double m_rootR[100];
  96. double m_rootI[100];
  97. int m_numPoles = 0;
  98. double m_rippleDb = 0;
  99. double m_rolloff = 0;
  100. };
  101. //------------------------------------------------------------------------------
  102. // Factored implementations to reduce template instantiations
  103. struct LowPassBase : PoleFilterBase<AnalogLowPass>
  104. {
  105. void setup(int order, double sampleRate, double cutoffFrequency, double rippleDb, double rolloff);
  106. };
  107. struct HighPassBase : PoleFilterBase<AnalogLowPass>
  108. {
  109. void setup(int order, double sampleRate, double cutoffFrequency, double rippleDb, double rolloff);
  110. };
  111. struct BandPassBase : PoleFilterBase<AnalogLowPass>
  112. {
  113. void setup(int order, double sampleRate, double centerFrequency, double widthFrequency, double rippleDb, double rolloff);
  114. };
  115. struct BandStopBase : PoleFilterBase<AnalogLowPass>
  116. {
  117. void setup(int order, double sampleRate, double centerFrequency, double widthFrequency, double rippleDb, double rolloff);
  118. };
  119. //------------------------------------------------------------------------------
  120. //
  121. // Raw filters
  122. //
  123. template <int MaxOrder>
  124. struct LowPass : PoleFilter<LowPassBase, MaxOrder> {};
  125. template <int MaxOrder>
  126. struct HighPass : PoleFilter<HighPassBase, MaxOrder> {};
  127. template <int MaxOrder>
  128. struct BandPass : PoleFilter<BandPassBase, MaxOrder, MaxOrder * 2> {};
  129. template <int MaxOrder>
  130. struct BandStop : PoleFilter<BandStopBase, MaxOrder, MaxOrder * 2> {};
  131. //------------------------------------------------------------------------------
  132. //
  133. // Gui-friendly Design layer
  134. //
  135. namespace Design
  136. {
  137. struct TypeIBase : DesignBase
  138. {
  139. enum
  140. {
  141. NumParams = 5
  142. };
  143. static int getNumParams() { return 5; }
  144. static ParamInfo getParamInfo_2() { return ParamInfo::defaultCutoffFrequencyParam(); }
  145. static ParamInfo getParamInfo_3() { return ParamInfo::defaultRippleDbParam(); }
  146. static ParamInfo getParamInfo_4() { return ParamInfo::defaultRolloffParam(); }
  147. };
  148. template <class FilterClass>
  149. struct TypeI : TypeIBase, FilterClass
  150. {
  151. void setParams(const Params& params) { FilterClass::setup(int(params[1]), params[0], params[2], params[3], params[4]); }
  152. };
  153. struct TypeIIBase : DesignBase
  154. {
  155. enum
  156. {
  157. NumParams = 6
  158. };
  159. static int getNumParams() { return 6; }
  160. static ParamInfo getParamInfo_2() { return ParamInfo::defaultCenterFrequencyParam(); }
  161. static ParamInfo getParamInfo_3() { return ParamInfo::defaultBandwidthHzParam(); }
  162. static ParamInfo getParamInfo_4() { return ParamInfo::defaultRippleDbParam(); }
  163. static ParamInfo getParamInfo_5() { return ParamInfo::defaultRolloffParam(); }
  164. };
  165. template <class FilterClass>
  166. struct TypeII : TypeIIBase, FilterClass
  167. {
  168. void setParams(const Params& params) { FilterClass::setup(int(params[1]), params[0], params[2], params[3], params[4], params[5]); }
  169. };
  170. // Factored kind and name
  171. struct LowPassDescription
  172. {
  173. static Kind getKind() { return kindLowPass; }
  174. static const char* getName() { return "Elliptic Low Pass"; }
  175. };
  176. struct HighPassDescription
  177. {
  178. static Kind getKind() { return kindHighPass; }
  179. static const char* getName() { return "Elliptic High Pass"; }
  180. };
  181. struct BandPassDescription
  182. {
  183. static Kind getKind() { return kindHighPass; }
  184. static const char* getName() { return "Elliptic Band Pass"; }
  185. };
  186. struct BandStopDescription
  187. {
  188. static Kind getKind() { return kindHighPass; }
  189. static const char* getName() { return "Elliptic Band Stop"; }
  190. };
  191. // This glues on the Order parameter
  192. template <int MaxOrder, template <class> class TypeClass, template <int> class FilterClass>
  193. struct OrderBase : TypeClass<FilterClass<MaxOrder>>
  194. {
  195. const ParamInfo getParamInfo_1() const
  196. {
  197. return ParamInfo(idOrder, "Order", "Order", 1, MaxOrder, 2, &ParamInfo::Int_toControlValue, &ParamInfo::Int_toNativeValue,
  198. &ParamInfo::Int_toString);
  199. }
  200. };
  201. //------------------------------------------------------------------------------
  202. //
  203. // Design filters
  204. //
  205. template <int MaxOrder>
  206. struct LowPass : OrderBase<MaxOrder, TypeI, Elliptic::LowPass>, LowPassDescription {};
  207. template <int MaxOrder>
  208. struct HighPass : OrderBase<MaxOrder, TypeI, Elliptic::HighPass>, HighPassDescription {};
  209. template <int MaxOrder>
  210. struct BandPass : OrderBase<MaxOrder, TypeII, Elliptic::BandPass>, BandPassDescription {};
  211. template <int MaxOrder>
  212. struct BandStop : OrderBase<MaxOrder, TypeII, Elliptic::BandStop>, BandStopDescription {};
  213. } // namespace Design
  214. } // namespace Elliptic
  215. } // namespace Dsp