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.cpp 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. #include "Common.h"
  27. #include "Legendre.h"
  28. #include "RootFinder.h"
  29. #include <sstream>
  30. #include <iostream>
  31. #include <iomanip>
  32. namespace Dsp
  33. {
  34. namespace Legendre
  35. {
  36. static double m_sqrt2() { return 1.41421356237309504880; }
  37. // Optimum 'L' Filter algorithm.
  38. // (C) 2004, C. Bond.
  39. //
  40. // Based on discussion in Kuo, "Network Analysis and Synthesis",
  41. // pp. 379-383. Original method due to A.Papoulis."On Monotonic
  42. // Response Filters", Proc. IRE, 47, Feb. 1959.
  43. //
  44. // Rewritten by Vinnie Falco to change the way temporary
  45. // storage is allocated
  46. //
  47. //
  48. // This routine calculates the coefficients of the Legendre polynomial
  49. // of the 1st kind. It uses a recursion relation. The first few polynomials
  50. // are hard coded and the rest are found by recursion.
  51. //
  52. // (n+1)Pn+1 = (2n+1)xPn - nPn-1 Recursion relation.
  53. //
  54. void PolynomialFinderBase::legendre(double* p, int n)
  55. {
  56. int i, j;
  57. if (n == 0)
  58. {
  59. p[0] = 1.0;
  60. return;
  61. }
  62. if (n == 1)
  63. {
  64. p[0] = 0.0;
  65. p[1] = 1.0;
  66. return;
  67. }
  68. p[0] = -0.5;
  69. p[1] = 0.0;
  70. p[2] = 1.5;
  71. if (n == 2) { return; }
  72. for (i = 0; i <= n; ++i) { m_aa[i] = m_bb[i] = 0.0; }
  73. m_bb[1] = 1.0;
  74. for (i = 3; i <= n; ++i)
  75. {
  76. for (j = 0; j <= i; ++j)
  77. {
  78. m_aa[j] = m_bb[j];
  79. m_bb[j] = p[j];
  80. p[j] = 0.0;
  81. }
  82. for (j = i - 2; j >= 0; j -= 2) { p[j] -= (i - 1) * m_aa[j] / i; }
  83. for (j = i - 1; j >= 0; j -= 2) { p[j + 1] += (2 * i - 1) * m_bb[j] / i; }
  84. }
  85. }
  86. //
  87. //
  88. // In the following routine n = 2k + 1 for odd 'n' and n = 2k + 2 for
  89. // even 'n'.
  90. //
  91. //
  92. // n k
  93. // -----
  94. // 1 0
  95. // 2 0
  96. // 3 1
  97. // 4 1
  98. // 5 2
  99. // 6 2
  100. //
  101. void PolynomialFinderBase::solve(int n)
  102. {
  103. assert(n <= m_maxN);
  104. double c1;
  105. int i, j;
  106. int k = (n - 1) / 2;
  107. //
  108. // form vector of 'a' constants
  109. //
  110. if (n & 1)
  111. { // odd
  112. for (i = 0; i <= k; ++i) { m_a[i] = (2.0 * i + 1.0) / (m_sqrt2() * (k + 1.0)); }
  113. } // even
  114. else
  115. {
  116. for (i = 0; i < k + 1; ++i) { m_a[i] = 0.0; }
  117. if (k & 1) { for (i = 1; i <= k; i += 2) { m_a[i] = (2 * i + 1) / sqrt(double((k + 1) * (k + 2))); } }
  118. else { for (i = 0; i <= k; i += 2) { m_a[i] = (2 * i + 1) / sqrt(double((k + 1) * (k + 2))); } }
  119. }
  120. for (i = 0; i <= n; ++i)
  121. {
  122. m_s[i] = 0.0;
  123. m_w[i] = 0.0;
  124. }
  125. //
  126. // form s[] = sum of a[i]*P[i]
  127. //
  128. m_s[0] = m_a[0];
  129. m_s[1] = m_a[1];
  130. for (i = 2; i <= k; ++i)
  131. {
  132. legendre(m_p, i);
  133. for (j = 0; j <= i; ++j) { m_s[j] += m_a[i] * m_p[j]; }
  134. }
  135. //
  136. // form v[] = square of s[]
  137. //
  138. for (i = 0; i <= 2 * k + 2; ++i) { m_v[i] = 0.0; }
  139. for (i = 0; i <= k; ++i) { for (j = 0; j <= k; ++j) { m_v[i + j] += m_s[i] * m_s[j]; } }
  140. //
  141. // modify integrand for even 'n'
  142. //
  143. m_v[2 * k + 1] = 0.0;
  144. if ((n & 1) == 0) { for (i = n; i >= 0; i--) { m_v[i + 1] += m_v[i]; } }
  145. //
  146. // form integral of v[]
  147. //
  148. for (i = n + 1; i >= 0; i--) { m_v[i + 1] = m_v[i] / double(i + 1.0); }
  149. m_v[0] = 0.0;
  150. //
  151. // clear s[] for use in computing definite integral
  152. //
  153. for (i = 0; i < (n + 2); ++i) { m_s[i] = 0.0; }
  154. m_s[0] = -1.0;
  155. m_s[1] = 2.0;
  156. //
  157. // calculate definite integral
  158. //
  159. for (i = 1; i <= n; ++i)
  160. {
  161. if (i > 1)
  162. {
  163. double c0 = -m_s[0];
  164. for (j = 1; j < i + 1; ++j)
  165. {
  166. c1 = -m_s[j] + 2.0 * m_s[j - 1];
  167. m_s[j - 1] = c0;
  168. c0 = c1;
  169. }
  170. c1 = 2.0 * m_s[i];
  171. m_s[i] = c0;
  172. m_s[i + 1] = c1;
  173. }
  174. for (j = i; j > 0; j--) { m_w[j] += (m_v[i] * m_s[j]); }
  175. }
  176. if ((n & 1) == 0) { m_w[1] = 0.0; }
  177. }
  178. //------------------------------------------------------------------------------
  179. AnalogLowPass::AnalogLowPass()
  180. : m_numPoles(-1) { setNormal(0, 1); }
  181. void AnalogLowPass::design(int numPoles,
  182. WorkspaceBase* w)
  183. {
  184. if (m_numPoles != numPoles)
  185. {
  186. m_numPoles = numPoles;
  187. reset();
  188. PolynomialFinderBase& poly(w->poly);
  189. RootFinderBase& poles(w->roots);
  190. poly.solve(numPoles);
  191. int degree = numPoles * 2;
  192. poles.coef()[0] = 1 + poly.coef()[0];
  193. poles.coef()[1] = 0;
  194. for (int i = 1; i <= degree; ++i)
  195. {
  196. poles.coef()[2 * i] = poly.coef()[i] * ((i & 1) ? -1 : 1);
  197. poles.coef()[2 * i + 1] = 0;
  198. }
  199. poles.solve(degree);
  200. int j = 0;
  201. for (int i = 0; i < degree; ++i) { if (poles.root()[i].real() <= 0) { poles.root()[j++] = poles.root()[i]; } }
  202. // sort descending imag() and cut degree in half
  203. poles.sort(degree / 2);
  204. const int pairs = numPoles / 2;
  205. for (int i = 0; i < pairs; ++i)
  206. {
  207. complex_t c = poles.root()[i];
  208. addPoleZeroConjugatePairs(c, infinity());
  209. }
  210. if (numPoles & 1) { add(poles.root()[pairs].real(), infinity()); }
  211. }
  212. }
  213. //------------------------------------------------------------------------------
  214. void LowPassBase::setup(int order,
  215. double sampleRate,
  216. double cutoffFrequency,
  217. WorkspaceBase* w)
  218. {
  219. m_analogProto.design(order, w);
  220. LowPassTransform(cutoffFrequency / sampleRate,
  221. m_digitalProto,
  222. m_analogProto);
  223. setLayout(m_digitalProto);
  224. }
  225. void HighPassBase::setup(int order,
  226. double sampleRate,
  227. double cutoffFrequency,
  228. WorkspaceBase* w)
  229. {
  230. m_analogProto.design(order, w);
  231. HighPassTransform(cutoffFrequency / sampleRate,
  232. m_digitalProto,
  233. m_analogProto);
  234. setLayout(m_digitalProto);
  235. }
  236. void BandPassBase::setup(int order,
  237. double sampleRate,
  238. double centerFrequency,
  239. double widthFrequency,
  240. WorkspaceBase* w)
  241. {
  242. m_analogProto.design(order, w);
  243. BandPassTransform(centerFrequency / sampleRate,
  244. widthFrequency / sampleRate,
  245. m_digitalProto,
  246. m_analogProto);
  247. setLayout(m_digitalProto);
  248. }
  249. void BandStopBase::setup(int order,
  250. double sampleRate,
  251. double centerFrequency,
  252. double widthFrequency,
  253. WorkspaceBase* w)
  254. {
  255. m_analogProto.design(order, w);
  256. BandStopTransform(centerFrequency / sampleRate,
  257. widthFrequency / sampleRate,
  258. m_digitalProto,
  259. m_analogProto);
  260. setLayout(m_digitalProto);
  261. }
  262. } // namespace Legendre
  263. } // namespace Dsp