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.

Legendre.h 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. #include "RootFinder.h"
  33. namespace Dsp
  34. {
  35. /*
  36. * Filters with Legendre / "Optimum-L" response characteristics
  37. *
  38. */
  39. namespace Legendre
  40. {
  41. // Numerical computation of Legendre "Optimum-L" polynomials
  42. class PolynomialFinderBase
  43. {
  44. public:
  45. void solve(int n);
  46. double* coef() { return m_w; }
  47. private:
  48. void legendre(double* p, int n);
  49. protected:
  50. int m_maxN = 0;
  51. double* m_w = nullptr;
  52. double* m_a = nullptr;
  53. double* m_p = nullptr;
  54. double* m_s = nullptr;
  55. double* m_v = nullptr;
  56. double* m_aa = nullptr;
  57. double* m_bb = nullptr;
  58. };
  59. template <int maxN>
  60. class PolynomialFinder : public PolynomialFinderBase
  61. {
  62. public:
  63. PolynomialFinder()
  64. {
  65. m_maxN = maxN;
  66. m_w = m_ws;
  67. m_a = m_as;
  68. m_p = m_ps;
  69. m_s = m_ss;
  70. m_v = m_vs;
  71. m_aa = m_aas;
  72. m_bb = m_bbs;
  73. }
  74. void solve(int n)
  75. {
  76. assert(n <= maxN);
  77. PolynomialFinderBase::solve(n);
  78. }
  79. private:
  80. double m_ws [2 * maxN + 1];
  81. double m_as [ maxN + 1];
  82. double m_ps [2 * maxN + 1];
  83. double m_ss [2 * maxN + 1];
  84. double m_vs [2 * maxN + 4];
  85. double m_aas [ maxN + 1];
  86. double m_bbs [ maxN + 1];
  87. };
  88. //------------------------------------------------------------------------------
  89. // A Workspace is necessary to construct the polynomial and find its roots
  90. struct WorkspaceBase
  91. {
  92. WorkspaceBase(PolynomialFinderBase* polyBase, RootFinderBase* rootsBase) : poly(*polyBase), roots(*rootsBase) { }
  93. PolynomialFinderBase& poly;
  94. RootFinderBase& roots;
  95. private:
  96. WorkspaceBase(WorkspaceBase&);
  97. WorkspaceBase& operator=(WorkspaceBase&);
  98. };
  99. template <int MaxOrder>
  100. struct Workspace : WorkspaceBase
  101. {
  102. Workspace() : WorkspaceBase(&m_poly, &m_roots) { }
  103. private:
  104. PolynomialFinder<MaxOrder> m_poly;
  105. RootFinder<MaxOrder * 2> m_roots;
  106. };
  107. //------------------------------------------------------------------------------
  108. // Half-band analog prototypes (s-plane)
  109. class AnalogLowPass : public LayoutBase
  110. {
  111. public:
  112. AnalogLowPass();
  113. void design(int numPoles, WorkspaceBase* w);
  114. private:
  115. int m_numPoles = 0;
  116. };
  117. //------------------------------------------------------------------------------
  118. // Factored implementations to reduce template instantiations
  119. struct LowPassBase : PoleFilterBase<AnalogLowPass>
  120. {
  121. void setup(int order, double sampleRate, double cutoffFrequency, WorkspaceBase* w);
  122. };
  123. struct HighPassBase : PoleFilterBase<AnalogLowPass>
  124. {
  125. void setup(int order, double sampleRate, double cutoffFrequency, WorkspaceBase* w);
  126. };
  127. struct BandPassBase : PoleFilterBase<AnalogLowPass>
  128. {
  129. void setup(int order, double sampleRate, double centerFrequency, double widthFrequency, WorkspaceBase* w);
  130. };
  131. struct BandStopBase : PoleFilterBase<AnalogLowPass>
  132. {
  133. void setup(int order, double sampleRate, double centerFrequency, double widthFrequency, WorkspaceBase* w);
  134. };
  135. //------------------------------------------------------------------------------
  136. //
  137. // Raw filters
  138. //
  139. template <int MaxOrder>
  140. struct LowPass : PoleFilter<LowPassBase, MaxOrder>
  141. {
  142. void setup(int order, double sampleRate, double cutoffFrequency)
  143. {
  144. Workspace<MaxOrder> w;
  145. LowPassBase::setup(order, sampleRate, cutoffFrequency, &w);
  146. }
  147. };
  148. template <int MaxOrder>
  149. struct HighPass : PoleFilter<HighPassBase, MaxOrder>
  150. {
  151. void setup(int order, double sampleRate, double cutoffFrequency)
  152. {
  153. Workspace<MaxOrder> w;
  154. HighPassBase::setup(order, sampleRate, cutoffFrequency, &w);
  155. }
  156. };
  157. template <int MaxOrder>
  158. struct BandPass : PoleFilter<BandPassBase, MaxOrder, MaxOrder * 2>
  159. {
  160. void setup(int order, double sampleRate, double centerFrequency, double widthFrequency)
  161. {
  162. Workspace<MaxOrder> w;
  163. BandPassBase::setup(order, sampleRate, centerFrequency, widthFrequency, &w);
  164. }
  165. };
  166. template <int MaxOrder>
  167. struct BandStop : PoleFilter<BandStopBase, MaxOrder, MaxOrder * 2>
  168. {
  169. void setup(int order, double sampleRate, double centerFrequency, double widthFrequency)
  170. {
  171. Workspace<MaxOrder> w;
  172. BandStopBase::setup(order, sampleRate, centerFrequency, widthFrequency, &w);
  173. }
  174. };
  175. //------------------------------------------------------------------------------
  176. //
  177. // Gui-friendly Design layer
  178. //
  179. namespace Design
  180. {
  181. struct TypeIBase : DesignBase
  182. {
  183. enum
  184. {
  185. NumParams = 3
  186. };
  187. static int getNumParams() { return 3; }
  188. static ParamInfo getParamInfo_2() { return ParamInfo::defaultCutoffFrequencyParam(); }
  189. };
  190. template <class FilterClass>
  191. struct TypeI : TypeIBase, FilterClass
  192. {
  193. void setParams(const Params& params) { FilterClass::setup(int(params[1]), params[0], params[2]); }
  194. };
  195. struct TypeIIBase : DesignBase
  196. {
  197. enum
  198. {
  199. NumParams = 4
  200. };
  201. static int getNumParams() { return 4; }
  202. static ParamInfo getParamInfo_2() { return ParamInfo::defaultCenterFrequencyParam(); }
  203. static ParamInfo getParamInfo_3() { return ParamInfo::defaultBandwidthHzParam(); }
  204. };
  205. template <class FilterClass>
  206. struct TypeII : TypeIIBase, FilterClass
  207. {
  208. void setParams(const Params& params) { FilterClass::setup(int(params[1]), params[0], params[2], params[3]); }
  209. };
  210. // Factored kind and name
  211. struct LowPassDescription
  212. {
  213. static Kind getKind() { return kindLowPass; }
  214. static const char* getName() { return "Legendre Low Pass"; }
  215. };
  216. struct HighPassDescription
  217. {
  218. static Kind getKind() { return kindHighPass; }
  219. static const char* getName() { return "Legendre High Pass"; }
  220. };
  221. struct BandPassDescription
  222. {
  223. static Kind getKind() { return kindHighPass; }
  224. static const char* getName() { return "Legendre Band Pass"; }
  225. };
  226. struct BandStopDescription
  227. {
  228. static Kind getKind() { return kindHighPass; }
  229. static const char* getName() { return "Legendre Band Stop"; }
  230. };
  231. // This glues on the Order parameter
  232. template <int MaxOrder, template <class> class TypeClass, template <int> class FilterClass>
  233. struct OrderBase : TypeClass<FilterClass<MaxOrder>>
  234. {
  235. ParamInfo getParamInfo_1() const
  236. {
  237. return ParamInfo(idOrder, "Order", "Order", 1, MaxOrder, 2, &ParamInfo::Int_toControlValue, &ParamInfo::Int_toNativeValue,
  238. &ParamInfo::Int_toString);
  239. }
  240. };
  241. //------------------------------------------------------------------------------
  242. //
  243. // Design filters
  244. //
  245. template <int MaxOrder>
  246. struct LowPass : OrderBase<MaxOrder, TypeI, Legendre::LowPass>, LowPassDescription {};
  247. template <int MaxOrder>
  248. struct HighPass : OrderBase<MaxOrder, TypeI, Legendre::HighPass>, HighPassDescription {};
  249. template <int MaxOrder>
  250. struct BandPass : OrderBase<MaxOrder, TypeII, Legendre::BandPass>, BandPassDescription {};
  251. template <int MaxOrder>
  252. struct BandStop : OrderBase<MaxOrder, TypeII, Legendre::BandStop>, BandStopDescription {};
  253. } // namespace Design
  254. } // namespace Legendre
  255. } // namespace Dsp