meteor-engine.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * meteor-engine.c - The meteor FIR design algorithm
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2013 Steve Underwood
  9. *
  10. * All rights reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2, as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. /*! \file */
  26. #include <inttypes.h>
  27. #include <stdbool.h>
  28. #include <time.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31. #include <ctype.h>
  32. #include <math.h>
  33. #include <setjmp.h>
  34. #include <assert.h>
  35. #include <stddef.h>
  36. #include <stdlib.h>
  37. #include "meteor-engine.h"
  38. #include "ae.h"
  39. /* version I: Wed Jun 27 13:36:06 EDT 1990 */
  40. /* copyright Prof. K. Steiglitz
  41. Dept. of Computer Science
  42. Princeton University
  43. Princeton, NJ 08544 */
  44. /* Constraint-based design of linear-phase fir filters with
  45. upper and lower bounds, and convexity constraints.
  46. Finds minimum length, or optimizes fixed length, or pushes band-edges.
  47. If L is the filter length, the models are
  48. odd-length
  49. cosine: sum(i from 0 to (L-1)/2) coeff[i]*cos(i*omega)
  50. sine: sum(i from 0 to (L-3)/2) coeff[i]*sin((i + 1)*omega)
  51. even-length
  52. cosine: sum(i from 0 to L/2 - 1) coeff[i]*cos((i + 0.5)*omega)
  53. sine: sum(i from 0 to L/2 - 1) coeff[i]*sin((i + 0.5)*omega) */
  54. #define MAX_PIVOTS 1000 /* Maximum no. of pivots */
  55. #define SMALL 1.0e-8 /* Small number used in defining band-edges */
  56. #define LARGE 1.0e+31 /* Large number used in search for minimum cost column */
  57. #define EPS 1.0e-8 /* For testing for zero */
  58. static void make_bands(struct meteor_working_data_s *s, int i)
  59. {
  60. int j;
  61. int kmax;
  62. /* Fill in frequencies to make grid - frequencies are kept as reals
  63. in radians, and each band has equally spaced grid points */
  64. if (i == 0)
  65. s->spec->spec[i].first_col = 1;
  66. else
  67. s->spec->spec[i].first_col = s->spec->spec[i - 1].last_col + 1;
  68. /*endif*/
  69. kmax = (int) ((s->spec->spec[i].right_freq - s->spec->spec[i].left_freq)*s->spec->grid_points/0.5 + SMALL);
  70. /* kmax + 1 cols. in this band */
  71. if (kmax == 0)
  72. {
  73. s->freq[s->spec->spec[i].first_col - 1] = 2.0*M_PI*s->spec->spec[i].left_freq;
  74. }
  75. else
  76. {
  77. for (j = 0; j <= kmax; j++)
  78. s->freq[s->spec->spec[i].first_col + j - 1] = 2.0*M_PI*(s->spec->spec[i].left_freq + (s->spec->spec[i].right_freq - s->spec->spec[i].left_freq)*j/kmax);
  79. /*endfor*/
  80. }
  81. /*endif*/
  82. s->spec->spec[i].last_col = s->spec->spec[i].first_col + kmax;
  83. }
  84. /*- End of function --------------------------------------------------------*/
  85. static double trig0(struct meteor_working_data_s *s, int i, double freq)
  86. {
  87. double res;
  88. /* Trig function in filter transfer function */
  89. if (s->odd_length)
  90. {
  91. if (s->spec->symmetry_type == symmetry_cosine)
  92. res = cos(i*freq);
  93. else
  94. res = sin((i + 1.0)*freq);
  95. /*endif*/
  96. }
  97. else
  98. {
  99. if (s->spec->symmetry_type == symmetry_cosine)
  100. res = cos((i + 0.5)*freq);
  101. else
  102. res = sin((i + 0.5)*freq);
  103. /*endif*/
  104. }
  105. /*endif*/
  106. return res;
  107. }
  108. /*- End of function --------------------------------------------------------*/
  109. static double trig2(struct meteor_working_data_s *s, int i, double freq)
  110. {
  111. double res;
  112. /* Second derivative of trig function in filter transfer function */
  113. if (s->odd_length)
  114. {
  115. if (s->spec->symmetry_type == symmetry_cosine)
  116. res = -i*i*cos(i*freq);
  117. else
  118. res = -(i + 1.0)*(i + 1.0)*sin(i*freq);
  119. /*endif*/
  120. }
  121. else
  122. {
  123. if (s->spec->symmetry_type == symmetry_cosine)
  124. res = -(i + 0.5)*(i + 0.5)*cos((i + 0.5)*freq);
  125. else
  126. res = -(i + 0.5)*(i + 0.5)*sin((i + 0.5)*freq);
  127. /*endif*/
  128. }
  129. /*endif*/
  130. return res;
  131. }
  132. /*- End of function --------------------------------------------------------*/
  133. static void convex(struct meteor_working_data_s *s, int i)
  134. {
  135. int row;
  136. int col;
  137. /* Set up tableau columns for convexity constraints on magnitude */
  138. make_bands(s, i);
  139. for (col = s->spec->spec[i].first_col - 1; col < s->spec->spec[i].last_col; col++)
  140. {
  141. /* For all frequencies in band */
  142. s->c[col] = 0.0;
  143. for (row = 0; row < s->m; row++)
  144. {
  145. /* Normal constraint is <= */
  146. if (s->spec->spec[i].sense == sense_convex)
  147. s->tab[row][col] = -s->tab[row][col];
  148. else
  149. s->tab[row][col] = trig2(s, row, s->freq[col]);
  150. /*endif*/
  151. }
  152. /*endfor*/
  153. s->tab[s->m][col] = 0.0;
  154. }
  155. /*endfor*/
  156. }
  157. /*- End of function --------------------------------------------------------*/
  158. static void limit(struct meteor_working_data_s *s, int i)
  159. {
  160. int row;
  161. int col;
  162. /* Sets up tableau columns for upper or lower bounds on transfer
  163. function for specification i; the bound is linearly interpolated
  164. between the start and end of the band */
  165. make_bands(s, i);
  166. for (col = s->spec->spec[i].first_col - 1; col < s->spec->spec[i].last_col; col++)
  167. {
  168. /* For all frequencies in band */
  169. if (s->spec->spec[i].first_col == s->spec->spec[i].last_col)
  170. {
  171. s->c[col] = s->spec->spec[i].left_bound;
  172. }
  173. else
  174. {
  175. switch (s->spec->spec[i].interpolation)
  176. {
  177. case interpolation_geometric:
  178. s->c[col] = s->spec->spec[i].left_bound*exp((double) (col - s->spec->spec[i].first_col + 1) /
  179. (s->spec->spec[i].last_col - s->spec->spec[i].first_col)*log(fabs(s->spec->spec[i].right_bound/s->spec->spec[i].left_bound)));
  180. break;
  181. case interpolation_arithmetic:
  182. s->c[col] = s->spec->spec[i].left_bound + (double) (col - s->spec->spec[i].first_col + 1) /
  183. (s->spec->spec[i].last_col - s->spec->spec[i].first_col)*(s->spec->spec[i].right_bound - s->spec->spec[i].left_bound);
  184. break;
  185. }
  186. /*endswitch*/
  187. }
  188. /*endif*/
  189. if (s->spec->spec[i].sense == sense_lower)
  190. s->c[col] = -s->c[col];
  191. /*endif*/
  192. for (row = 0; row < s->m; row++)
  193. {
  194. s->tab[row][col] = (s->spec->spec[i].sense == sense_lower) ? -trig0(s, row, s->freq[col]) : trig0(s, row, s->freq[col]);
  195. }
  196. /*endfor*/
  197. s->tab[s->m][col] = (s->spec->spec[i].hug) ? 0.0 : 1.0;
  198. }
  199. /*endfor*/
  200. }
  201. /*- End of function --------------------------------------------------------*/
  202. static void setup(struct meteor_working_data_s *s)
  203. {
  204. int i;
  205. /* Initialize constraints */
  206. for (i = 0; i < s->spec->num_specs; i++)
  207. {
  208. switch (s->spec->spec[i].type)
  209. {
  210. case constraint_type_convexity:
  211. convex(s, i);
  212. break;
  213. case constraint_type_limit:
  214. limit(s, i);
  215. break;
  216. }
  217. /*endswitch*/
  218. }
  219. /*endfor*/
  220. s->num_cols = s->spec->spec[s->spec->num_specs - 1].last_col;
  221. }
  222. /*- End of function --------------------------------------------------------*/
  223. static void column_search(struct meteor_working_data_s *s)
  224. {
  225. int i;
  226. int col;
  227. double cost;
  228. /* Look for favorable column to enter basis.
  229. returns lowest cost and its column number, or turns on the flag optimal */
  230. /* Set up price vector */
  231. for (i = 0; i <= s->m; i++)
  232. s->price[i] = -s->carry[0][i + 1];
  233. /*endfor*/
  234. s->optimal = false;
  235. s->cbar = LARGE;
  236. s->pivot_col = 0;
  237. for (col = 0; col < s->num_cols; col++)
  238. {
  239. cost = s->d[col];
  240. for (i = 0; i <= s->m; i++)
  241. cost -= s->price[i]*s->tab[i][col];
  242. /*endfor*/
  243. if (s->cbar > cost)
  244. {
  245. s->cbar = cost;
  246. s->pivot_col = col + 1;
  247. }
  248. /*endif*/
  249. }
  250. /*endfor*/
  251. if (s->cbar > -EPS)
  252. s->optimal = true;
  253. /*endif*/
  254. }
  255. /*- End of function --------------------------------------------------------*/
  256. static void row_search(struct meteor_working_data_s *s)
  257. {
  258. int i;
  259. int j;
  260. double ratio;
  261. double min_ratio;
  262. /* Look for pivot row. returns pivot row number, or turns on the flag unbounded */
  263. /* Generate column */
  264. for (i = 1; i <= (s->m + 1); i++)
  265. {
  266. /* Current column = B inverse * original col. */
  267. s->cur_col[i] = 0.0;
  268. for (j = 0; j <= s->m; j++)
  269. s->cur_col[i] += s->carry[i][j + 1]*s->tab[j][s->pivot_col - 1];
  270. /*endfor*/
  271. }
  272. /*endfor*/
  273. /* First element in current column */
  274. s->cur_col[0] = s->cbar;
  275. s->pivot_row = -1;
  276. min_ratio = LARGE;
  277. /* Ratio test */
  278. for (i = 0; i <= s->m; i++)
  279. {
  280. if (s->cur_col[i + 1] > EPS)
  281. {
  282. ratio = s->carry[i + 1][0]/s->cur_col[i + 1];
  283. if (min_ratio > ratio)
  284. {
  285. /* Favorable row */
  286. min_ratio = ratio;
  287. s->pivot_row = i;
  288. s->pivot_element = s->cur_col[i + 1];
  289. }
  290. else
  291. {
  292. /* Break tie with max pivot */
  293. if (min_ratio == ratio && s->pivot_element < s->cur_col[i + 1])
  294. {
  295. s->pivot_row = i;
  296. s->pivot_element = s->cur_col[i + 1];
  297. }
  298. /*endif*/
  299. }
  300. /*endif*/
  301. }
  302. /*endif*/
  303. }
  304. /*endfor*/
  305. s->unbounded = (s->pivot_row == -1);
  306. }
  307. /*- End of function --------------------------------------------------------*/
  308. static double pivot(struct meteor_working_data_s *s)
  309. {
  310. int i;
  311. int j;
  312. s->basis[s->pivot_row] = s->pivot_col;
  313. for (j = 0; j <= (s->m + 1); j++)
  314. s->carry[s->pivot_row + 1][j] /= s->pivot_element;
  315. /*endfor*/
  316. for (i = 0; i <= (s->m + 1); i++)
  317. {
  318. if ((i - 1) != s->pivot_row)
  319. {
  320. for (j = 0; j <= (s->m + 1); j++)
  321. s->carry[i][j] -= s->carry[s->pivot_row + 1][j]*s->cur_col[i];
  322. /*endfor*/
  323. }
  324. /*endif*/
  325. }
  326. /*endfor*/
  327. return -s->carry[0][0];
  328. }
  329. /*- End of function --------------------------------------------------------*/
  330. static double change_phase(struct meteor_working_data_s *s)
  331. {
  332. int i;
  333. int j;
  334. int b;
  335. /* Change phase from 1 to 2, by switching to the original cost vector */
  336. s->phase = 2;
  337. for (i = 0; i <= s->m; i++)
  338. {
  339. if (s->basis[i] <= 0)
  340. printf("...artificial basis element %5d remains in basis after phase 1\n", s->basis[i]);
  341. /*endif*/
  342. }
  343. /*endfor*/
  344. /* Switch to original cost vector */
  345. for (i = 0; i < s->num_cols; i++)
  346. s->d[i] = s->c[i];
  347. /*endfor*/
  348. for (j = 0; j <= (s->m + 1); j++)
  349. {
  350. s->carry[0][j] = 0.0;
  351. for (i = 0; i <= s->m; i++)
  352. {
  353. /* Ignore artificial basis elements that are still in basis */
  354. b = s->basis[i];
  355. if (b >= 1)
  356. s->carry[0][j] -= s->c[b - 1]*s->carry[i + 1][j];
  357. /*endif*/
  358. }
  359. /*endfor*/
  360. }
  361. /*endfor*/
  362. return -s->carry[0][0];
  363. }
  364. /*- End of function --------------------------------------------------------*/
  365. static double magnitude_response(struct meteor_working_data_s *s, double freq)
  366. {
  367. int i;
  368. double temp;
  369. /* Compute magnitude function, given radian frequency f */
  370. temp = 0.0;
  371. for (i = 0; i < s->m; i++)
  372. temp += s->coeff[i]*trig0(s, i, freq);
  373. /*endfor*/
  374. return temp;
  375. }
  376. /*- End of function --------------------------------------------------------*/
  377. static double half_magnitude_response(struct meteor_working_data_s *s, double freq)
  378. {
  379. int i;
  380. double temp;
  381. /* Compute magnitude function, given radian frequency f */
  382. temp = 0.0;
  383. for (i = 0; i < (s->m + 1)/2; i++)
  384. temp += s->coeff[i]*trig0(s, i, freq);
  385. /*endfor*/
  386. return temp;
  387. }
  388. /*- End of function --------------------------------------------------------*/
  389. static int simplex(struct meteor_working_data_s *s)
  390. {
  391. int i;
  392. int j;
  393. int col;
  394. int row;
  395. bool done;
  396. /* Simplex for linear programming */
  397. done = false;
  398. s->phase = 1;
  399. for (i = 0; i <= (s->m + 1); i++)
  400. {
  401. for (j = 0; j <= (MAX_COEFFS + 1); j++)
  402. s->carry[i][j] = 0.0;
  403. /*endfor*/
  404. }
  405. /*endfor*/
  406. /* Artificial basis */
  407. for (i = 1; i <= (s->m + 1); i++)
  408. s->carry[i][i] = 1.0;
  409. /*endfor*/
  410. /* - initial cost */
  411. s->carry[0][0] = -1.0;
  412. s->cur_cost = -s->carry[0][0];
  413. /* Variable minimized in primal */
  414. s->carry[s->m + 1][0] = 1.0;
  415. /* Initial, artificial basis */
  416. for (i = 0; i <= s->m; i++)
  417. s->basis[i] = -i;
  418. /*endfor*/
  419. /* Check number of columns */
  420. if (s->num_cols <= NCOL_MAX)
  421. {
  422. /* Initialize cost for phase 1 */
  423. for (col = 0; col < s->num_cols; col++)
  424. {
  425. s->d[col] = 0.0;
  426. for (row = 0; row <= s->m; row++)
  427. s->d[col] -= s->tab[row][col];
  428. /*endfor*/
  429. }
  430. /*endfor*/
  431. }
  432. else
  433. {
  434. printf("...termination: too many columns for storage\n");
  435. done = true;
  436. s->result = too_many_columns;
  437. }
  438. /*endif*/
  439. s->num_pivots = 0;
  440. while (s->num_pivots < MAX_PIVOTS && !done && (s->cur_cost > s->low_limit || s->phase == 1))
  441. {
  442. column_search(s);
  443. if (s->optimal)
  444. {
  445. if (s->phase == 1)
  446. {
  447. if (s->cur_cost > EPS)
  448. {
  449. /* Dual of problem is infeasible */
  450. /* This happens if all specs are hugged */
  451. done = true;
  452. s->result = infeasible_dual;
  453. }
  454. else
  455. {
  456. if (s->num_pivots != 1 && s->num_pivots%10 != 0)
  457. printf("Pivot %d cost = %.5f\n", s->num_pivots, s->cur_cost);
  458. /*endif*/
  459. printf("Phase 1 successfully completed\n");
  460. s->cur_cost = change_phase(s);
  461. }
  462. /*endif*/
  463. }
  464. else
  465. {
  466. if (s->num_pivots != 1 && s->num_pivots%10 != 0)
  467. printf("Pivot %d cost = %.5f\n", s->num_pivots, s->cur_cost);
  468. /*endif*/
  469. printf("Phase 2 successfully completed\n");
  470. done = true;
  471. s->result = optimum_obtained;
  472. }
  473. /*endif*/
  474. }
  475. else
  476. {
  477. row_search(s);
  478. if (s->unbounded)
  479. {
  480. /* Dual of problem is unbounded */
  481. done = true;
  482. s->result = unbounded_dual;
  483. }
  484. else
  485. {
  486. s->cur_cost = pivot(s);
  487. s->num_pivots++;
  488. if (s->num_pivots == 1 || s->num_pivots%10 == 0)
  489. printf("Pivot %d cost = %.5f\n", s->num_pivots, s->cur_cost);
  490. /*endif*/
  491. }
  492. /*endif*/
  493. }
  494. /*endif*/
  495. }
  496. /*endwhile*/
  497. if (s->cur_cost <= s->low_limit && s->phase == 2)
  498. {
  499. if (s->num_pivots != 1 && s->num_pivots%10 != 0)
  500. printf("Pivot %d cost = %.5f\n", s->num_pivots, s->cur_cost);
  501. /*endif*/
  502. s->result = infeasible_primal;
  503. }
  504. /*endif*/
  505. if (s->num_pivots >= MAX_PIVOTS)
  506. {
  507. printf("...termination: maximum number of pivots exceeded\n");
  508. s->result = too_many_pivots;
  509. }
  510. /*endif*/
  511. /* Optimal */
  512. return s->result;
  513. }
  514. /*- End of function --------------------------------------------------------*/
  515. static void print_result(int result)
  516. {
  517. /* Print enumerated result type */
  518. switch (result)
  519. {
  520. case badly_formed_requirements:
  521. printf("badly formed requirements\n");
  522. break;
  523. case optimum_obtained:
  524. printf("optimum obtained\n");
  525. break;
  526. case too_many_columns:
  527. printf("too many columns in specifications\n");
  528. break;
  529. case too_many_pivots:
  530. printf("too many pivots\n");
  531. break;
  532. case unbounded_dual:
  533. printf("infeasible (unbounded dual)\n");
  534. break;
  535. case infeasible_dual:
  536. printf("infeasible or unbounded\n");
  537. break;
  538. case infeasible_primal:
  539. printf("infeasible\n");
  540. break;
  541. case no_feasible_solution_found:
  542. printf("no infeasible solution found\n");
  543. break;
  544. case no_feasible_band_edge_found:
  545. printf("no infeasible bend edge found\n");
  546. break;
  547. }
  548. /*endswitch*/
  549. }
  550. /*- End of function --------------------------------------------------------*/
  551. static int get_m(struct meteor_working_data_s *s)
  552. {
  553. int left_m;
  554. int right_m;
  555. bool found_m;
  556. bool checked_left;
  557. bool checked_right;
  558. int result;
  559. int length;
  560. int i;
  561. /* Find best order (and hence length) */
  562. s->found_feasible_solution = false;
  563. left_m = s->smallest_m;
  564. right_m = s->largest_m;
  565. found_m = false;
  566. checked_left = false;
  567. checked_right = false;
  568. for (s->iteration = 0; !found_m; s->iteration++)
  569. {
  570. if (s->iteration == 0)
  571. {
  572. /* First time through */
  573. s->m = left_m + (right_m - left_m)/2;
  574. }
  575. /*endif*/
  576. printf("\nIteration %d\n", s->iteration);
  577. if (s->odd_length)
  578. {
  579. if (s->spec->symmetry_type == symmetry_cosine)
  580. length = s->m*2 - 1;
  581. else
  582. length = s->m*2 + 1;
  583. /*endif*/
  584. }
  585. else
  586. {
  587. length = s->m*2;
  588. }
  589. /*endif*/
  590. printf("L=%d\n", length);
  591. setup(s);
  592. result = simplex(s);
  593. print_result(result);
  594. if (result == optimum_obtained)
  595. {
  596. s->found_feasible_solution = true;
  597. right_m = s->m;
  598. s->best_m = s->m;
  599. /* Right side of bracket has been checked */
  600. checked_right = true;
  601. if (s->odd_length)
  602. {
  603. if (s->spec->symmetry_type == symmetry_cosine)
  604. length = s->best_m*2 - 1;
  605. else
  606. length = s->best_m*2 + 1;
  607. /*endif*/
  608. }
  609. else
  610. {
  611. length = s->best_m*2;
  612. }
  613. /*endif*/
  614. printf("New best length L=%d\n", length);
  615. for (i = 0; i < s->m; i++)
  616. s->coeff[i] = -s->carry[0][i + 1];
  617. /*endfor*/
  618. }
  619. /*endif*/
  620. if (result != optimum_obtained)
  621. {
  622. left_m = s->m;
  623. /* Left side of bracket has been checked */
  624. checked_left = true;
  625. }
  626. /*endif*/
  627. if (right_m > left_m + 1)
  628. s->m = left_m + (right_m - left_m)/2;
  629. /*endif*/
  630. if (right_m == left_m + 1)
  631. {
  632. if (!checked_left)
  633. {
  634. s->m = left_m;
  635. checked_left = true;
  636. }
  637. else if (!checked_right)
  638. {
  639. s->m = right_m;
  640. checked_right = true;
  641. }
  642. else
  643. {
  644. found_m = true;
  645. }
  646. /*endif*/
  647. }
  648. /*endif*/
  649. if (right_m == left_m)
  650. found_m = true;
  651. /*endif*/
  652. }
  653. /*endfor*/
  654. if (!s->found_feasible_solution)
  655. return no_feasible_solution_found;
  656. /*endif*/
  657. s->m = s->best_m;
  658. putchar('\n');
  659. if (s->odd_length)
  660. {
  661. if (s->spec->symmetry_type == symmetry_cosine)
  662. printf("Best length L=%d\n", s->best_m*2 - 1);
  663. else
  664. printf("Best length L=%d\n", s->best_m*2 + 1);
  665. /*endif*/
  666. }
  667. /*endif*/
  668. if (!s->odd_length)
  669. printf("Best length L=%d\n", s->best_m*2);
  670. /*endif*/
  671. return 0;
  672. }
  673. /*- End of function --------------------------------------------------------*/
  674. static int get_edge(struct meteor_working_data_s *s)
  675. {
  676. double left_edge;
  677. double right_edge;
  678. double newe;
  679. double beste;
  680. double one_space;
  681. double stop_space;
  682. int i;
  683. /* Optimize band edge */
  684. one_space = 0.5/s->spec->grid_points; /* Space between grid points */
  685. stop_space = one_space/10.0;
  686. /* Stop criterion is 1/10 nominal grid spacing */
  687. if (s->which_way == rr)
  688. {
  689. /* Start with rightmost left edge */
  690. left_edge = s->spec->spec[s->spec->spec[0].band_pushed - 1].left_freq;
  691. for (i = 1; i < s->num_pushed; i++)
  692. {
  693. if (s->spec->spec[s->spec->spec[i].band_pushed - 1].left_freq > left_edge)
  694. left_edge = s->spec->spec[s->spec->spec[i].band_pushed - 1].left_freq;
  695. /*endif*/
  696. }
  697. /*endfor*/
  698. right_edge = 0.5;
  699. }
  700. else
  701. {
  702. /* Start with leftmost right edge */
  703. left_edge = 0.0;
  704. right_edge = s->spec->spec[s->spec->spec[0].band_pushed - 1].right_freq;
  705. for (i = 1; i < s->num_pushed; i++)
  706. {
  707. if (s->spec->spec[s->spec->spec[i].band_pushed - 1].right_freq < right_edge)
  708. right_edge = s->spec->spec[s->spec->spec[i].band_pushed - 1].right_freq;
  709. /*endif*/
  710. }
  711. /*endfor*/
  712. }
  713. /*endif*/
  714. s->found_feasible_solution = false;
  715. for (s->iteration = 0; (right_edge - left_edge) > stop_space; s->iteration++)
  716. {
  717. newe = (right_edge + left_edge)/2.0;
  718. printf("\nIteration %d\n", s->iteration);
  719. printf("Trying new edge = %10.4f\n", newe);
  720. for (i = 0; i < s->num_pushed; i++)
  721. {
  722. if (s->which_way == rr)
  723. s->spec->spec[s->spec->spec[i].band_pushed - 1].right_freq = newe;
  724. else
  725. s->spec->spec[s->spec->spec[i].band_pushed - 1].left_freq = newe;
  726. /*endif*/
  727. }
  728. /*endif*/
  729. setup(s);
  730. s->result = simplex(s);
  731. print_result(s->result);
  732. if (s->result == optimum_obtained)
  733. {
  734. if (s->which_way == rr)
  735. left_edge = newe;
  736. else
  737. right_edge = newe;
  738. /*endif*/
  739. s->found_feasible_solution = true;
  740. beste = newe;
  741. for (i = 0; i < s->m; i++)
  742. s->coeff[i] = -s->carry[0][i + 1];
  743. /*endfor*/
  744. }
  745. else
  746. {
  747. if (s->which_way == rr)
  748. right_edge = newe;
  749. else
  750. left_edge = newe;
  751. /*endif*/
  752. }
  753. /*endif*/
  754. }
  755. /*endfor*/
  756. putchar('\n');
  757. if (!s->found_feasible_solution)
  758. return no_feasible_band_edge_found;
  759. /*endif*/
  760. printf("Found edge = %10.4f\n", beste);
  761. for (i = 0; i < s->num_pushed; i++)
  762. {
  763. if (s->which_way == rr)
  764. s->spec->spec[s->spec->spec[i].band_pushed - 1].right_freq = beste;
  765. else
  766. s->spec->spec[s->spec->spec[i].band_pushed - 1].left_freq = beste;
  767. /*endif*/
  768. }
  769. /*endfor*/
  770. for (i = 0; i < s->spec->num_specs; i++)
  771. make_bands(s, i);
  772. /*endfor*/
  773. return 0;
  774. }
  775. /*- End of function --------------------------------------------------------*/
  776. static int get_max_dist(struct meteor_working_data_s *s)
  777. {
  778. int i;
  779. /* Maximize distance from constraints */
  780. printf("Optimization: maximize distance from constraints\n");
  781. setup(s);
  782. s->result = simplex(s);
  783. print_result(s->result);
  784. if (s->result != optimum_obtained)
  785. return s->result;
  786. /*endif*/
  787. printf("Final cost = distance from constraints = %.5f\n", s->cur_cost);
  788. /* Record coefficients */
  789. for (i = 0; i < s->m; i++)
  790. s->coeff[i] = -s->carry[0][i + 1];
  791. /*endfor*/
  792. return 0;
  793. }
  794. /*- End of function --------------------------------------------------------*/
  795. static int meteor_get_coefficients(struct meteor_working_data_s *s, double coeffs[])
  796. {
  797. int i;
  798. int j;
  799. j = 0;
  800. if (s->odd_length && s->spec->symmetry_type == symmetry_cosine)
  801. {
  802. for (i = s->m - 1; i >= 1; i--)
  803. coeffs[j++] = s->coeff[i]/2.0;
  804. /*endfor*/
  805. coeffs[j++] = s->coeff[0];
  806. for (i = 1; i < s->m; i++)
  807. coeffs[j++] = s->coeff[i]/2.0;
  808. /*endfor*/
  809. }
  810. else if (!s->odd_length && s->spec->symmetry_type == symmetry_cosine)
  811. {
  812. for (i = s->m - 1; i >= 0; i--)
  813. coeffs[j++] = s->coeff[i]/2.0;
  814. /*endfor*/
  815. for (i = 0; i < s->m; i++)
  816. coeffs[j++] = s->coeff[i]/2.0;
  817. /*endfor*/
  818. }
  819. else if (s->odd_length && s->spec->symmetry_type == symmetry_sine)
  820. {
  821. /* L = length, odd */
  822. /* Negative of the first m coefs. */
  823. for (i = s->m - 1; i >= 0; i--)
  824. coeffs[j++] = -s->coeff[i]/2.0;
  825. /*endfor*/
  826. /* Middle coefficient is always 0 */
  827. coeffs[j++] = 0.0;
  828. for (i = 0; i < s->m; i++)
  829. coeffs[j++] = s->coeff[i]/2.0;
  830. /*endfor*/
  831. }
  832. else if (!s->odd_length && s->spec->symmetry_type == symmetry_sine)
  833. {
  834. /* Negative of the first m coefs. */
  835. for (i = s->m - 1; i >= 0; i--)
  836. coeffs[j++] = -s->coeff[i]/2.0;
  837. /*endfor*/
  838. for (i = 0; i < s->m; i++)
  839. coeffs[j++] = s->coeff[i]/2.0;
  840. /*endfor*/
  841. }
  842. /*endif*/
  843. return j;
  844. }
  845. /*- End of function --------------------------------------------------------*/
  846. static int vet_data(struct meteor_working_data_s *s)
  847. {
  848. int i;
  849. bool all_hugged;
  850. char ch;
  851. printf("Filter name: '%s'\n", s->spec->filter_name);
  852. if (s->spec->shortest < 1 || s->spec->longest > MAX_TAPS)
  853. {
  854. printf("Shortest or longest out of range\n");
  855. return badly_formed_requirements;
  856. }
  857. /*endif*/
  858. if ((s->spec->shortest & 1) != (s->spec->longest & 1))
  859. {
  860. printf("Parity of smallest andlongest unequal\n");
  861. return badly_formed_requirements;
  862. }
  863. /*endif*/
  864. s->odd_length = s->spec->shortest & 1;
  865. if (s->odd_length)
  866. {
  867. if (s->spec->symmetry_type == symmetry_cosine)
  868. {
  869. s->smallest_m = (s->spec->shortest + 1)/2;
  870. s->largest_m = (s->spec->longest + 1)/2;
  871. }
  872. else
  873. {
  874. s->smallest_m = (s->spec->shortest - 1)/2;
  875. s->largest_m = (s->spec->longest - 1)/2;
  876. }
  877. /*endif*/
  878. }
  879. else
  880. {
  881. s->smallest_m = s->spec->shortest/2;
  882. s->largest_m = s->spec->longest/2;
  883. }
  884. /*endif*/
  885. if (s->spec->shortest != s->spec->longest)
  886. {
  887. s->what_to_do = find_len;
  888. printf("Finding minimum length: range %d to %d\n", s->spec->shortest, s->spec->longest);
  889. }
  890. else
  891. {
  892. s->m = s->smallest_m;
  893. s->length = s->spec->shortest;
  894. printf("Fixed length of %4d\n", s->length);
  895. scanf("%c%*[^\n]", &ch);
  896. /* Right, left, or neither: edges to be pushed? */
  897. getchar();
  898. if (ch == 'n')
  899. {
  900. s->what_to_do = max_dist;
  901. }
  902. else
  903. {
  904. s->what_to_do = push_edge;
  905. s->which_way = (ch == 'r') ? rr : ll;
  906. scanf("%d%*[^\n]", &s->num_pushed);
  907. getchar();
  908. for (i = 0; i < s->num_pushed; i++)
  909. scanf("%d", &s->spec->spec[i].band_pushed);
  910. /*endfor*/
  911. scanf("%*[^\n]");
  912. getchar();
  913. printf("Pushing band edges right\n", (s->which_way == rr) ? "right" : "left");
  914. printf("Constraint numbers: ");
  915. for (i = 0; i < s->num_pushed; i++)
  916. printf("%3d ", s->spec->spec[i].band_pushed);
  917. /*endfor*/
  918. putchar('\n');
  919. }
  920. /*endif*/
  921. }
  922. /*endif*/
  923. for (i = 0; i < s->spec->num_specs; i++)
  924. {
  925. printf("Constraint name '%s'\n", s->spec->spec[i].name);
  926. switch (s->spec->spec[i].type)
  927. {
  928. case constraint_type_convexity:
  929. switch (s->spec->spec[i].sense)
  930. {
  931. case sense_convex:
  932. printf("Constraint %2d: convexity, sense convex\n", i);
  933. break;
  934. case sense_concave:
  935. printf("Constraint %2d: convexity, sense concave\n", i);
  936. break;
  937. }
  938. /*endswitch*/
  939. printf(" Band edges: %10.4f %10.4f\n", s->spec->spec[i].left_freq, s->spec->spec[i].right_freq);
  940. break;
  941. case constraint_type_limit:
  942. if (s->spec->spec[i].interpolation == interpolation_geometric && s->spec->spec[i].left_bound*s->spec->spec[i].right_bound == 0.0)
  943. {
  944. printf("Geometrically interpolated band edge in constraint %5d is zero\n", i);
  945. return badly_formed_requirements;
  946. }
  947. /*endif*/
  948. switch (s->spec->spec[i].sense)
  949. {
  950. case sense_lower:
  951. printf(" Constraint %2d: lower limit\n", i);
  952. break;
  953. case sense_upper:
  954. printf(" Constraint %2d: upper limit\n", i);
  955. break;
  956. case sense_envelope:
  957. printf(" Constraint %2d: envelope limit\n", i);
  958. break;
  959. }
  960. /*endswitch*/
  961. switch (s->spec->spec[i].interpolation)
  962. {
  963. case interpolation_geometric:
  964. printf(" Geometric interpolation\n");
  965. break;
  966. case interpolation_arithmetic:
  967. printf(" Arithmetic interpolation\n");
  968. break;
  969. }
  970. /*endswitch*/
  971. if (s->spec->spec[i].hug)
  972. printf(" This constraint will be hugged\n");
  973. else
  974. printf(" This constraint will be optimized\n");
  975. /*endif*/
  976. printf(" Band edges: %10.4f %10.4f\n", s->spec->spec[i].left_freq, s->spec->spec[i].right_freq);
  977. printf(" Bounds: %10.4f %10.4f\n", s->spec->spec[i].left_bound, s->spec->spec[i].right_bound);
  978. break;
  979. }
  980. /*endswitch*/
  981. make_bands(s, i);
  982. printf(" Initial columns: %10d %10d\n", s->spec->spec[i].first_col, s->spec->spec[i].last_col);
  983. }
  984. s->num_cols = s->spec->spec[s->spec->num_specs - 1].last_col;
  985. printf("Number of specs = %5d\n", s->spec->num_specs);
  986. printf("Initial number of columns = %5d\n", s->num_cols);
  987. all_hugged = true;
  988. for (i = 0; i < s->spec->num_specs; i++)
  989. {
  990. if (s->spec->spec[i].type == constraint_type_limit && !s->spec->spec[i].hug)
  991. all_hugged = false;
  992. /*endif*/
  993. }
  994. /*endfor*/
  995. if (all_hugged)
  996. {
  997. printf("All constraints are hugged: ill-posed problem\n");
  998. return badly_formed_requirements;
  999. }
  1000. /*endif*/
  1001. return 0;
  1002. }
  1003. /*- End of function --------------------------------------------------------*/
  1004. void output_filter_performance_as_csv_file(struct meteor_working_data_s *s, const char *file_name)
  1005. {
  1006. int i;
  1007. double mg;
  1008. double mg2;
  1009. FILE *magnitude_file;
  1010. if (s->log_fd)
  1011. {
  1012. magnitude_file = s->log_fd;
  1013. }
  1014. else
  1015. {
  1016. /* Print final frequency response */
  1017. if ((magnitude_file = fopen(file_name, "wb")) == NULL)
  1018. {
  1019. fprintf(stderr, "Cannot open file '%s'\n", file_name);
  1020. exit(2);
  1021. }
  1022. /*endif*/
  1023. }
  1024. /*endif*/
  1025. if (s->spec->filter_name && s->spec->filter_name[0])
  1026. {
  1027. fprintf(magnitude_file, "%s\n", s->spec->filter_name);
  1028. }
  1029. /*endif*/
  1030. fprintf(magnitude_file, "Frequency, Gain (dB), Gain (linear), Half gain (linear)\n");
  1031. /* Magnitude on regular grid */
  1032. for (i = 0; i <= s->spec->grid_points; i++)
  1033. {
  1034. mg = fabs(magnitude_response(s, i*M_PI/s->spec->grid_points));
  1035. if (mg == 0.0)
  1036. mg = SMALL;
  1037. /*endif*/
  1038. mg2 = fabs(half_magnitude_response(s, i*M_PI/s->spec->grid_points));
  1039. if (mg2 == 0.0)
  1040. mg2 = SMALL;
  1041. /*endif*/
  1042. fprintf(magnitude_file, "%10.4lf, %.10lf, %.5lf, %.5lf\n", 0.5*s->spec->sample_rate*i/s->spec->grid_points, 20.0*log10(mg), mg, mg2);
  1043. }
  1044. /*endfor*/
  1045. fprintf(magnitude_file, "\nMagnitude at band edges\n\n");
  1046. for (i = 0; i < s->spec->num_specs; i++)
  1047. {
  1048. if (s->spec->spec[i].type == constraint_type_limit)
  1049. {
  1050. fprintf(magnitude_file, "%10.4f %.5E\n",
  1051. s->freq[s->spec->spec[i].first_col - 1]*0.5/M_PI, magnitude_response(s, s->freq[s->spec->spec[i].first_col - 1]));
  1052. fprintf(magnitude_file, "%10.4f %.5E\n",
  1053. s->freq[s->spec->spec[i].last_col - 1]*0.5/M_PI, magnitude_response(s, s->freq[s->spec->spec[i].last_col - 1]));
  1054. putchar('\n');
  1055. }
  1056. /*endif*/
  1057. }
  1058. /*endfor*/
  1059. if (s->log_fd == NULL)
  1060. fclose(magnitude_file);
  1061. /*endif*/
  1062. }
  1063. /*- End of function --------------------------------------------------------*/
  1064. int meteor_design_filter(struct meteor_working_data_s *s, struct meteor_spec_s *t, double coeffs[])
  1065. {
  1066. int res;
  1067. memset(s, 0, sizeof(*s));
  1068. s->spec = t;
  1069. if ((res = vet_data(s)) < 0)
  1070. return res;
  1071. /*endif*/
  1072. /* Dual cost negative => primal infeasible */
  1073. s->low_limit = -EPS;
  1074. switch (s->what_to_do)
  1075. {
  1076. case find_len:
  1077. res = get_m(s);
  1078. break;
  1079. case push_edge:
  1080. res = get_edge(s);
  1081. break;
  1082. case max_dist:
  1083. res = get_max_dist(s);
  1084. break;
  1085. }
  1086. /*endswitch*/
  1087. if (res < 0)
  1088. return res;
  1089. /*endif*/
  1090. return meteor_get_coefficients(s, coeffs);
  1091. }
  1092. /*- End of function --------------------------------------------------------*/
  1093. /*- End of file ------------------------------------------------------------*/