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.

Butterworth.h 9.0KB

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