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.

Bessel.h 9.5KB

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