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.

ChebyshevII.h 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 Inverse Chebyshev response characteristics
  36. *
  37. */
  38. namespace ChebyshevII
  39. {
  40. // Half-band analog prototypes (s-plane)
  41. class AnalogLowPass : public LayoutBase
  42. {
  43. public:
  44. AnalogLowPass();
  45. void design(int numPoles, double stopBandDb);
  46. private:
  47. int m_numPoles = 0;
  48. double m_stopBandDb = 0;
  49. };
  50. //------------------------------------------------------------------------------
  51. class AnalogLowShelf : public LayoutBase
  52. {
  53. public:
  54. AnalogLowShelf();
  55. void design(int numPoles, double gainDb, double stopBandDb);
  56. private:
  57. int m_numPoles = 0;
  58. double m_stopBandDb = 0;
  59. double m_gainDb = 0;
  60. };
  61. //------------------------------------------------------------------------------
  62. // Factored implementations to reduce template instantiations
  63. struct LowPassBase : PoleFilterBase<AnalogLowPass>
  64. {
  65. void setup(int order, double sampleRate, double cutoffFrequency, double stopBandDb);
  66. };
  67. struct HighPassBase : PoleFilterBase<AnalogLowPass>
  68. {
  69. void setup(int order, double sampleRate, double cutoffFrequency, double stopBandDb);
  70. };
  71. struct BandPassBase : PoleFilterBase<AnalogLowPass>
  72. {
  73. void setup(int order, double sampleRate, double centerFrequency, double widthFrequency, double stopBandDb);
  74. };
  75. struct BandStopBase : PoleFilterBase<AnalogLowPass>
  76. {
  77. void setup(int order, double sampleRate, double centerFrequency, double widthFrequency, double stopBandDb);
  78. };
  79. struct LowShelfBase : PoleFilterBase<AnalogLowShelf>
  80. {
  81. void setup(int order, double sampleRate, double cutoffFrequency, double gainDb, double stopBandDb);
  82. };
  83. struct HighShelfBase : PoleFilterBase<AnalogLowShelf>
  84. {
  85. void setup(int order, double sampleRate, double cutoffFrequency, double gainDb, double stopBandDb);
  86. };
  87. struct BandShelfBase : PoleFilterBase<AnalogLowShelf>
  88. {
  89. void setup(int order, double sampleRate, double centerFrequency, double widthFrequency, double gainDb, double stopBandDb);
  90. };
  91. //------------------------------------------------------------------------------
  92. //
  93. // Raw filters
  94. //
  95. template <int MaxOrder>
  96. struct LowPass : PoleFilter<LowPassBase, MaxOrder> {};
  97. template <int MaxOrder>
  98. struct HighPass : PoleFilter<HighPassBase, MaxOrder> {};
  99. template <int MaxOrder>
  100. struct BandPass : PoleFilter<BandPassBase, MaxOrder, MaxOrder * 2> {};
  101. template <int MaxOrder>
  102. struct BandStop : PoleFilter<BandStopBase, MaxOrder, MaxOrder * 2> {};
  103. template <int MaxOrder>
  104. struct LowShelf : PoleFilter<LowShelfBase, MaxOrder> {};
  105. template <int MaxOrder>
  106. struct HighShelf : PoleFilter<HighShelfBase, MaxOrder> {};
  107. template <int MaxOrder>
  108. struct BandShelf : PoleFilter<BandShelfBase, MaxOrder, MaxOrder * 2> {};
  109. //------------------------------------------------------------------------------
  110. //
  111. // Gui-friendly Design layer
  112. //
  113. namespace Design
  114. {
  115. struct TypeIBase : DesignBase
  116. {
  117. enum
  118. {
  119. NumParams = 4
  120. };
  121. static int getNumParams() { return 4; }
  122. static ParamInfo getParamInfo_2() { return ParamInfo::defaultCutoffFrequencyParam(); }
  123. static ParamInfo getParamInfo_3() { return ParamInfo::defaultStopDbParam(); }
  124. };
  125. template <class FilterClass>
  126. struct TypeI : TypeIBase, FilterClass
  127. {
  128. void setParams(const Params& params) { FilterClass::setup(int(params[1]), params[0], params[2], params[3]); }
  129. };
  130. struct TypeIIBase : DesignBase
  131. {
  132. enum
  133. {
  134. NumParams = 5
  135. };
  136. static int getNumParams() { return 5; }
  137. static ParamInfo getParamInfo_2() { return ParamInfo::defaultCenterFrequencyParam(); }
  138. static ParamInfo getParamInfo_3() { return ParamInfo::defaultBandwidthHzParam(); }
  139. static ParamInfo getParamInfo_4() { return ParamInfo::defaultStopDbParam(); }
  140. };
  141. template <class FilterClass>
  142. struct TypeII : TypeIIBase, FilterClass
  143. {
  144. void setParams(const Params& params) { FilterClass::setup(int(params[1]), params[0], params[2], params[3], params[4]); }
  145. };
  146. struct TypeIIIBase : DesignBase
  147. {
  148. enum
  149. {
  150. NumParams = 5
  151. };
  152. static int getNumParams() { return 5; }
  153. static ParamInfo getParamInfo_2() { return ParamInfo::defaultCutoffFrequencyParam(); }
  154. static ParamInfo getParamInfo_3() { return ParamInfo::defaultGainParam(); }
  155. static ParamInfo getParamInfo_4() { return ParamInfo::defaultStopDbParam(); }
  156. };
  157. template <class FilterClass>
  158. struct TypeIII : TypeIIIBase, FilterClass
  159. {
  160. void setParams(const Params& params) { FilterClass::setup(int(params[1]), params[0], params[2], params[3], params[4]); }
  161. };
  162. struct TypeIVBase : DesignBase
  163. {
  164. enum
  165. {
  166. NumParams = 6
  167. };
  168. static int getNumParams() { return 6; }
  169. static ParamInfo getParamInfo_2() { return ParamInfo::defaultCenterFrequencyParam(); }
  170. static ParamInfo getParamInfo_3() { return ParamInfo::defaultBandwidthHzParam(); }
  171. static ParamInfo getParamInfo_4() { return ParamInfo::defaultGainParam(); }
  172. static ParamInfo getParamInfo_5() { return ParamInfo::defaultStopDbParam(); }
  173. };
  174. template <class FilterClass>
  175. struct TypeIV : TypeIVBase, FilterClass
  176. {
  177. void setParams(const Params& params) { FilterClass::setup(int(params[1]), params[0], params[2], params[3], params[4], params[5]); }
  178. };
  179. // Factored kind and name
  180. struct LowPassDescription
  181. {
  182. static Kind getKind() { return kindLowPass; }
  183. static const char* getName() { return "Chebyshev II Low Pass"; }
  184. };
  185. struct HighPassDescription
  186. {
  187. static Kind getKind() { return kindHighPass; }
  188. static const char* getName() { return "Chebyshev II High Pass"; }
  189. };
  190. struct BandPassDescription
  191. {
  192. static Kind getKind() { return kindHighPass; }
  193. static const char* getName() { return "Chebyshev II Band Pass"; }
  194. };
  195. struct BandStopDescription
  196. {
  197. static Kind getKind() { return kindHighPass; }
  198. static const char* getName() { return "Chebyshev II Band Stop"; }
  199. };
  200. struct LowShelfDescription
  201. {
  202. static Kind getKind() { return kindLowShelf; }
  203. static const char* getName() { return "Chebyshev II Low Shelf"; }
  204. };
  205. struct HighShelfDescription
  206. {
  207. static Kind getKind() { return kindHighShelf; }
  208. static const char* getName() { return "Chebyshev II High Shelf"; }
  209. };
  210. struct BandShelfDescription
  211. {
  212. static Kind getKind() { return kindBandShelf; }
  213. static const char* getName() { return "Chebyshev II Band Shelf"; }
  214. };
  215. // This glues on the Order parameter
  216. template <int MaxOrder, template <class> class TypeClass, template <int> class FilterClass>
  217. struct OrderBase : TypeClass<FilterClass<MaxOrder>>
  218. {
  219. ParamInfo getParamInfo_1() const
  220. {
  221. return ParamInfo(idOrder, "Order", "Order", 1, MaxOrder, 2, &ParamInfo::Int_toControlValue, &ParamInfo::Int_toNativeValue,
  222. &ParamInfo::Int_toString);
  223. }
  224. };
  225. //------------------------------------------------------------------------------
  226. //
  227. // Design Filters
  228. //
  229. template <int MaxOrder>
  230. struct LowPass : OrderBase<MaxOrder, TypeI, ChebyshevII::LowPass>, LowPassDescription {};
  231. template <int MaxOrder>
  232. struct HighPass : OrderBase<MaxOrder, TypeI, ChebyshevII::HighPass>, HighPassDescription {};
  233. template <int MaxOrder>
  234. struct BandPass : OrderBase<MaxOrder, TypeII, ChebyshevII::BandPass>, BandPassDescription {};
  235. template <int MaxOrder>
  236. struct BandStop : OrderBase<MaxOrder, TypeII, ChebyshevII::BandStop>, BandStopDescription {};
  237. template <int MaxOrder>
  238. struct LowShelf : OrderBase<MaxOrder, TypeIII, ChebyshevII::LowShelf>, LowShelfDescription {};
  239. template <int MaxOrder>
  240. struct HighShelf : OrderBase<MaxOrder, TypeIII, ChebyshevII::HighShelf>, HighShelfDescription {};
  241. template <int MaxOrder>
  242. struct BandShelf : OrderBase<MaxOrder, TypeIV, ChebyshevII::BandShelf>, BandShelfDescription {};
  243. } // namespace Design
  244. } // namespace ChebyshevII
  245. } // namespace Dsp