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.

Elliptic.cpp 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 "Elliptic.h"
  28. namespace Dsp
  29. {
  30. namespace Elliptic
  31. {
  32. // shit ton of math in here
  33. // approximation to complete elliptic integral of the first kind.
  34. // fast convergence, peak error less than 2e-16.
  35. double Solver::ellipticK(double k)
  36. {
  37. double m = k * k;
  38. double a = 1;
  39. double b = sqrt(1 - m);
  40. double c = a - b;
  41. double co;
  42. do
  43. {
  44. co = c;
  45. c = (a - b) / 2;
  46. double ao = (a + b) / 2;
  47. b = sqrt(a * b);
  48. a = ao;
  49. } while (c < co);
  50. return doublePi / (a + a);
  51. }
  52. //------------------------------------------------------------------------------
  53. AnalogLowPass::AnalogLowPass()
  54. : m_numPoles(-1) { setNormal(0, 1); }
  55. void AnalogLowPass::design(int numPoles,
  56. double rippleDb,
  57. double rolloff)
  58. {
  59. if (m_numPoles != numPoles ||
  60. m_rippleDb != rippleDb ||
  61. m_rolloff != rolloff)
  62. {
  63. m_numPoles = numPoles;
  64. m_rippleDb = rippleDb;
  65. m_rolloff = rolloff;
  66. reset();
  67. // calculate
  68. //const double ep = rippleDb; // passband ripple
  69. const int n = numPoles;
  70. double e2 = pow(10., rippleDb / 10) - 1;
  71. //double xi = rolloff + 1;
  72. double xi = 5 * exp(rolloff - 1) + 1;
  73. m_K = Solver::ellipticK(1 / xi);
  74. m_Kprime = Solver::ellipticK(sqrt(1 - 1 / (xi * xi)));
  75. int ni = ((n & 1) == 1) ? 0 : 1;
  76. int i;
  77. double f[100]; // HACK!!!
  78. for (i = 1; i <= n / 2; ++i)
  79. {
  80. double u = (2 * i - ni) * m_K / n;
  81. double sn = calcsn(u);
  82. sn *= 2 * doublePi / m_K;
  83. f[i] = m_zeros[i - 1] = 1 / sn;
  84. }
  85. m_zeros[n / 2] = std::numeric_limits<double>::infinity();
  86. double fb = 1 / (2 * doublePi);
  87. m_nin = n % 2;
  88. m_n2 = n / 2;
  89. for (i = 1; i <= m_n2; ++i)
  90. {
  91. double x = f[m_n2 + 1 - i];
  92. m_z1[i] = sqrt(1 - 1 / (x * x));
  93. }
  94. double ee = e2;//pow(10., rippleDb/20)-1;
  95. m_e = sqrt(ee);
  96. double fbb = fb * fb;
  97. m_m = m_nin + 2 * m_n2;
  98. m_em = 2 * (m_m / 2);
  99. double tp = 2 * doublePi;
  100. calcfz();
  101. calcqz();
  102. if (m_m > m_em) { m_c1[2 * m_m] = 0; }
  103. for (i = 0; i <= 2 * m_m; i += 2) { m_a1[m_m - i / 2] = m_c1[i] + m_d1[i]; }
  104. double a0 = findfact(m_m);
  105. int r = 0;
  106. while (r < m_em / 2)
  107. {
  108. r++;
  109. m_p[r] /= 10;
  110. m_q1[r] /= 100;
  111. double d = 1 + m_p[r] + m_q1[r];
  112. m_b1[r] = (1 + m_p[r] / 2) * fbb / d;
  113. m_zf1[r] = fb / pow(d, .25);
  114. m_zq1[r] = 1 / sqrt(fabs(2 * (1 - m_b1[r] / (m_zf1[r] * m_zf1[r]))));
  115. m_zw1[r] = tp * m_zf1[r];
  116. m_rootR[r] = -.5 * m_zw1[r] / m_zq1[r];
  117. m_rootR[r + m_em / 2] = m_rootR[r];
  118. m_rootI[r] = .5 * sqrt(fabs(m_zw1[r] * m_zw1[r] / (m_zq1[r] * m_zq1[r]) - 4 * m_zw1[r] * m_zw1[r]));
  119. m_rootI[r + m_em / 2] = -m_rootI[r];
  120. complex_t pole(-.5 * m_zw1[r] / m_zq1[r], .5 * sqrt(fabs(m_zw1[r] * m_zw1[r] / (m_zq1[r] * m_zq1[r]) - 4 * m_zw1[r] * m_zw1[r])));
  121. complex_t zero(0, m_zeros[r - 1]);
  122. addPoleZeroConjugatePairs(pole, zero);
  123. }
  124. if (a0 != 0)
  125. {
  126. m_rootR[r + 1 + m_em / 2] = -sqrt(fbb / (.1 * a0 - 1)) * tp;
  127. m_rootI[r + 1 + m_em / 2] = 0;
  128. add(-sqrt(fbb / (.1 * a0 - 1)) * tp, infinity());
  129. }
  130. setNormal(0, (numPoles & 1) ? 1. : pow(10., -rippleDb / 20.0));
  131. }
  132. }
  133. // generate the product of (z+s1[i]) for i = 1 .. sn and store it in b1[]
  134. // (i.e. f[z] = b1[0] + b1[1] z + b1[2] z^2 + ... b1[sn] z^sn)
  135. void AnalogLowPass::prodpoly(int sn)
  136. {
  137. m_b1[0] = m_s1[1];
  138. m_b1[1] = 1;
  139. int i;
  140. for (int j = 2; j <= sn; ++j)
  141. {
  142. m_a1[0] = m_s1[j] * m_b1[0];
  143. for (i = 1; i <= j - 1; ++i) { m_a1[i] = m_b1[i - 1] + m_s1[j] * m_b1[i]; }
  144. for (i = 0; i != j; ++i) { m_b1[i] = m_a1[i]; }
  145. m_b1[j] = 1;
  146. }
  147. }
  148. // determine f(z)^2
  149. void AnalogLowPass::calcfz2(int i)
  150. {
  151. int ji = 0;
  152. int jf = 0;
  153. if (i < m_em + 2)
  154. {
  155. ji = 0;
  156. jf = i;
  157. }
  158. if (i > m_em)
  159. {
  160. ji = i - m_em;
  161. jf = m_em;
  162. }
  163. m_c1[i] = 0;
  164. for (int j = ji; j <= jf; j += 2) { m_c1[i] += m_a1[j] * (m_a1[i - j] * pow(10., m_m - i / 2)); }
  165. }
  166. // calculate f(z)
  167. void AnalogLowPass::calcfz()
  168. {
  169. int i = 1;
  170. if (m_nin == 1) { m_s1[i++] = 1; }
  171. for (; i <= m_nin + m_n2; ++i) { m_s1[i] = m_s1[i + m_n2] = m_z1[i - m_nin]; }
  172. prodpoly(m_nin + 2 * m_n2);
  173. for (i = 0; i <= m_em; i += 2) { m_a1[i] = m_e * m_b1[i]; }
  174. for (i = 0; i <= 2 * m_em; i += 2) { calcfz2(i); }
  175. }
  176. // determine q(z)
  177. void AnalogLowPass::calcqz()
  178. {
  179. int i;
  180. for (i = 1; i <= m_nin; ++i) { m_s1[i] = -10; }
  181. for (; i <= m_nin + m_n2; ++i) { m_s1[i] = -10 * m_z1[i - m_nin] * m_z1[i - m_nin]; }
  182. for (; i <= m_nin + 2 * m_n2; ++i) { m_s1[i] = m_s1[i - m_n2]; }
  183. prodpoly(m_m);
  184. int dd = ((m_nin & 1) == 1) ? -1 : 1;
  185. for (i = 0; i <= 2 * m_m; i += 2) { m_d1[i] = dd * m_b1[i / 2]; }
  186. }
  187. // compute factors
  188. double AnalogLowPass::findfact(int t)
  189. {
  190. int i;
  191. double a = 0;
  192. for (i = 1; i <= t; ++i) { m_a1[i] /= m_a1[0]; }
  193. m_a1[0] = m_b1[0] = m_c1[0] = 1;
  194. int i1 = 0;
  195. for (;;)
  196. {
  197. if (t <= 2) { break; }
  198. double p0 = 0, q0 = 0;
  199. i1++;
  200. for (;;)
  201. {
  202. m_b1[1] = m_a1[1] - p0;
  203. m_c1[1] = m_b1[1] - p0;
  204. for (i = 2; i <= t; ++i) { m_b1[i] = m_a1[i] - p0 * m_b1[i - 1] - q0 * m_b1[i - 2]; }
  205. for (i = 2; i < t; ++i) { m_c1[i] = m_b1[i] - p0 * m_c1[i - 1] - q0 * m_c1[i - 2]; }
  206. int x1 = t - 1;
  207. int x2 = t - 2;
  208. int x3 = t - 3;
  209. double x4 = m_c1[x2] * m_c1[x2] + m_c1[x3] * (m_b1[x1] - m_c1[x1]);
  210. if (x4 == 0) { x4 = 1e-3; }
  211. double ddp = (m_b1[x1] * m_c1[x2] - m_b1[t] * m_c1[x3]) / x4;
  212. p0 += ddp;
  213. double dq = (m_b1[t] * m_c1[x2] - m_b1[x1] * (m_c1[x1] - m_b1[x1])) / x4;
  214. q0 += dq;
  215. if (fabs(ddp + dq) < 1e-6) { break; }
  216. }
  217. m_p[i1] = p0;
  218. m_q1[i1] = q0;
  219. m_a1[1] = m_a1[1] - p0;
  220. t -= 2;
  221. for (i = 2; i <= t; ++i) { m_a1[i] -= p0 * m_a1[i - 1] + q0 * m_a1[i - 2]; }
  222. if (t <= 2) { break; }
  223. }
  224. if (t == 2)
  225. {
  226. i1++;
  227. m_p[i1] = m_a1[1];
  228. m_q1[i1] = m_a1[2];
  229. }
  230. if (t == 1) { a = -m_a1[1]; }
  231. return a;
  232. }
  233. double AnalogLowPass::calcsn(double u)
  234. {
  235. double sn = 0;
  236. // q = modular constant
  237. double q = exp(-doublePi * m_Kprime / m_K);
  238. double v = doublePi * .5 * u / m_K;
  239. for (int j = 0; ; ++j)
  240. {
  241. double w = pow(q, j + .5);
  242. sn += w * sin((2 * j + 1) * v) / (1 - w * w);
  243. if (w < 1e-7) { break; }
  244. }
  245. return sn;
  246. }
  247. //------------------------------------------------------------------------------
  248. void LowPassBase::setup(int order,
  249. double sampleRate,
  250. double cutoffFrequency,
  251. double rippleDb,
  252. double rolloff)
  253. {
  254. m_analogProto.design(order, rippleDb, rolloff);
  255. LowPassTransform(cutoffFrequency / sampleRate,
  256. m_digitalProto,
  257. m_analogProto);
  258. setLayout(m_digitalProto);
  259. }
  260. void HighPassBase::setup(int order,
  261. double sampleRate,
  262. double cutoffFrequency,
  263. double rippleDb,
  264. double rolloff)
  265. {
  266. m_analogProto.design(order, rippleDb, rolloff);
  267. HighPassTransform(cutoffFrequency / sampleRate,
  268. m_digitalProto,
  269. m_analogProto);
  270. setLayout(m_digitalProto);
  271. }
  272. void BandPassBase::setup(int order,
  273. double sampleRate,
  274. double centerFrequency,
  275. double widthFrequency,
  276. double rippleDb,
  277. double rolloff)
  278. {
  279. m_analogProto.design(order, rippleDb, rolloff);
  280. BandPassTransform(centerFrequency / sampleRate,
  281. widthFrequency / sampleRate,
  282. m_digitalProto,
  283. m_analogProto);
  284. setLayout(m_digitalProto);
  285. }
  286. void BandStopBase::setup(int order,
  287. double sampleRate,
  288. double centerFrequency,
  289. double widthFrequency,
  290. double rippleDb,
  291. double rolloff)
  292. {
  293. m_analogProto.design(order, rippleDb, rolloff);
  294. BandStopTransform(centerFrequency / sampleRate,
  295. widthFrequency / sampleRate,
  296. m_digitalProto,
  297. m_analogProto);
  298. setLayout(m_digitalProto);
  299. }
  300. } // namespace Elliptic
  301. } // namespace Dsp