2
0

meteor.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * meteor.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 <libxml/xmlmemory.h>
  38. #include <libxml/parser.h>
  39. #include <libxml/xinclude.h>
  40. #include "meteor-engine.h"
  41. #include "meteor-xml-reader.h"
  42. #include "ae.h"
  43. void meteor_output_coefficients_as_h_file(struct meteor_working_data_s *s, const char *file_name)
  44. {
  45. int i;
  46. int k;
  47. FILE *coeffs_file; /* Files for output */
  48. char uc_filter_name[strlen(s->spec->filter_name) + 2];
  49. /* Print resulting coeffs as a C .h file */
  50. coeffs_file = fopen(file_name, "wb");
  51. if (coeffs_file == NULL)
  52. {
  53. fprintf(stderr, "Cannot open file '%s'\n", file_name);
  54. exit(2);
  55. }
  56. /*endif*/
  57. uc_filter_name[0] = '\0';
  58. k = strlen(s->spec->filter_name);
  59. if (k > 0)
  60. {
  61. uc_filter_name[0] = '_';
  62. for (i = 0; i < k; i++)
  63. uc_filter_name[i + 1] = toupper(s->spec->filter_name[i]);
  64. /*endfor*/
  65. uc_filter_name[k + 1] = '\0';
  66. }
  67. if (s->odd_length && s->spec->symmetry_type == symmetry_cosine)
  68. {
  69. fprintf(coeffs_file, "#define NUM_COEFFS%s %4d /* cosine symmetry */\n", uc_filter_name, s->m*2 - 1);
  70. fprintf(coeffs_file, "float %s[NUM_COEFFS%s] =\n", s->spec->filter_name, uc_filter_name);
  71. fprintf(coeffs_file, "{\n");
  72. for (i = s->m - 1; i >= 1; i--)
  73. fprintf(coeffs_file, " % .5E,\n", s->coeff[i]/2);
  74. /*endfor*/
  75. fprintf(coeffs_file, " % .5E,\n", s->coeff[0]);
  76. for (i = 1; i < s->m; i++)
  77. fprintf(coeffs_file, " % .5E,\n", s->coeff[i]/2);
  78. /*endfor*/
  79. }
  80. else if (!s->odd_length && s->spec->symmetry_type == symmetry_cosine)
  81. {
  82. fprintf(coeffs_file, "#define NUM_COEFFS%s %4d /*cosine symmetry */\n", uc_filter_name, s->m*2);
  83. fprintf(coeffs_file, "float %s[NUM_COEFFS%s] =\n", s->spec->filter_name, uc_filter_name);
  84. fprintf(coeffs_file, "{\n");
  85. for (i = s->m - 1; i >= 0; i--)
  86. fprintf(coeffs_file, " % .5E,\n", s->coeff[i]/2);
  87. /*endfor*/
  88. for (i = 0; i < s->m; i++)
  89. fprintf(coeffs_file, " % .5E,\n", s->coeff[i]/2);
  90. /*endfor*/
  91. }
  92. else if (s->odd_length && s->spec->symmetry_type == symmetry_sine)
  93. {
  94. fprintf(coeffs_file, "#define NUM_COEFFS %4d /* sine symmetry */\n", uc_filter_name, s->m*2 + 1);
  95. fprintf(coeffs_file, "float %s[NUM_COEFFS] =\n", s->spec->filter_name, uc_filter_name);
  96. fprintf(coeffs_file, "{\n");
  97. /* L = length, odd */
  98. /* Negative of the first m coefs. */
  99. for (i = s->m - 1; i >= 0; i--)
  100. fprintf(coeffs_file, " % .5E,\n", s->coeff[i]/-2);
  101. /*endfor*/
  102. /* Middle coefficient is always 0 */
  103. fprintf(coeffs_file, " 0.0,\n");
  104. for (i = 0; i < s->m; i++)
  105. fprintf(coeffs_file, " % .5E,\n", s->coeff[i]/2);
  106. /*endfor*/
  107. }
  108. else if (!s->odd_length && s->spec->symmetry_type == symmetry_sine)
  109. {
  110. fprintf(coeffs_file, "#define NUM_COEFFS %4d /* sine symmetry */\n", uc_filter_name, s->m*2);
  111. fprintf(coeffs_file, "float %s[NUM_COEFFS] =\n", s->spec->filter_name, uc_filter_name);
  112. fprintf(coeffs_file, "{\n");
  113. /* Negative of the first m coefs. */
  114. for (i = s->m - 1; i >= 0; i--)
  115. fprintf(coeffs_file, " % .5E,\n", s->coeff[i]/-2);
  116. /*endfor*/
  117. for (i = 0; i < s->m; i++)
  118. fprintf(coeffs_file, " % .5E,\n", s->coeff[i]/2);
  119. /*endfor*/
  120. }
  121. /*endif*/
  122. fprintf(coeffs_file, "};\n");
  123. fclose(coeffs_file);
  124. }
  125. /*- End of function --------------------------------------------------------*/
  126. int main(int argc, char *argv[])
  127. {
  128. struct meteor_working_data_s state;
  129. struct meteor_spec_s spec;
  130. double coeffs[1024];
  131. int num_coeffs;
  132. printf("Welcome to Meteor:\n");
  133. printf("Constraint-based, linear-phase FIR filter design\n");
  134. ae_open();
  135. get_xml_filter_spec(&spec, argv[1]);
  136. if ((num_coeffs = meteor_design_filter(&state, &spec, coeffs)) < 0)
  137. {
  138. fprintf(stderr, "Error %d\n", num_coeffs);
  139. exit(2);
  140. }
  141. meteor_output_coefficients_as_h_file(&state, "coeffs.h");
  142. output_filter_performance_as_csv_file(&state, "performance.csv");
  143. ae_close();
  144. return 0;
  145. }
  146. /*- End of function --------------------------------------------------------*/
  147. /*- End of file ------------------------------------------------------------*/