meteor-engine.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * meteor-engine.h - 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. #if !defined(_METEOR_ENGINE_H_)
  26. #define _METEOR_ENGINE_H_
  27. #define num_specs_MAX 20 /* Max. no. of specifications */
  28. #define MAX_COEFFS 64 /* Max. no. of coefficients */
  29. #define MAX_TAPS 129 /* Max. size of n, where there are n+1 grid-points */
  30. #define NCOL_MAX 6000 /* Max. no. of columns allowed in tableau */
  31. enum meteor_result_e
  32. {
  33. badly_formed_requirements = -1,
  34. optimum_obtained = -2,
  35. too_many_columns = -3,
  36. too_many_pivots = -4,
  37. unbounded_dual = -5,
  38. infeasible_dual = -6,
  39. infeasible_primal = -7,
  40. no_feasible_solution_found = -8,
  41. no_feasible_band_edge_found = -9
  42. };
  43. enum symmetry_types_e
  44. {
  45. symmetry_cosine,
  46. symmetry_sine
  47. };
  48. enum constraint_types_e
  49. {
  50. constraint_type_convexity,
  51. constraint_type_limit
  52. };
  53. enum sense_e
  54. {
  55. sense_lower,
  56. sense_upper,
  57. sense_envelope,
  58. sense_concave,
  59. sense_convex
  60. };
  61. enum interpolation_e
  62. {
  63. interpolation_arithmetic,
  64. interpolation_geometric
  65. };
  66. #if defined(__cplusplus)
  67. extern "C"
  68. {
  69. #endif
  70. struct meteor_constraint_s
  71. {
  72. const char *name; /* A name to use to refer to this definition */
  73. enum constraint_types_e type; /* Type of band */
  74. double left_freq; /* Band edges as read in */
  75. double right_freq;
  76. double left_bound;
  77. double right_bound;
  78. enum sense_e sense; /* Sense of constraint. */
  79. enum interpolation_e interpolation; /* Interpolation method */
  80. int first_col; /* Leftmost column of spec */
  81. int last_col; /* Rightmost column of spec */
  82. bool hug; /* Allow this constraint to be hugged? */
  83. int band_pushed; /* Band edges pushed */
  84. };
  85. struct meteor_spec_s
  86. {
  87. const char *filter_name;
  88. double sample_rate;
  89. enum symmetry_types_e symmetry_type; /* Cosine or sine symmetry */
  90. int grid_points; /* There are n+1 grid-points from 0 to pi */
  91. int shortest; /* Range of L = 2*m-1, 2*m, or 2*m+1 */
  92. int longest; /* Range of L = 2*m-1, 2*m, or 2*m+1 */
  93. int num_specs; /* No. of bands */
  94. struct meteor_constraint_s spec[num_specs_MAX];
  95. };
  96. struct meteor_working_data_s
  97. {
  98. struct meteor_spec_s *spec;
  99. bool unbounded;
  100. bool optimal; /* Flags for simplex */
  101. int iteration; /* Iteration count, index */
  102. int num_pivots; /* Pivot count */
  103. int pivot_col; /* Pivot column */
  104. int pivot_row; /* Pivot row */
  105. double pivot_element; /* Pivot element */
  106. double cbar; /* Price when searching for entering column */
  107. enum meteor_result_e result; /* Result of simplex */
  108. int m; /* No. of coefficients, left and right half m */
  109. int length; /* Filter length = 2*m-1, 2*m, 2*m+1 */
  110. int phase; /* Phase */
  111. double coeff[MAX_COEFFS]; /* Coefficients */
  112. double price[MAX_COEFFS + 1]; /* Shadow prices = row -1 of carry = -dual variables = -coefficients */
  113. int basis[MAX_COEFFS + 1]; /* Basis columns, negative integers artificial */
  114. double carry[MAX_COEFFS + 2][MAX_COEFFS + 2]; /* Inverse-basis matrix of the revised simplex method */
  115. double tab[MAX_COEFFS + 1][NCOL_MAX]; /* Tableau */
  116. double cur_col[MAX_COEFFS + 2]; /* Current column */
  117. double cur_cost; /* Current cost */
  118. double freq[NCOL_MAX]; /* Frequencies at grid points */
  119. double d[NCOL_MAX]; /* Current cost vector */
  120. double c[NCOL_MAX]; /* Cost in original problem */
  121. bool found_feasible_solution; /* Found feasible solution */
  122. int smallest_m; /* Range of m */
  123. int largest_m; /* Range of m */
  124. int best_m; /* Best order */
  125. int num_cols; /* Number of columns */
  126. enum
  127. {
  128. find_len,
  129. max_dist,
  130. push_edge
  131. } what_to_do; /* Type of optimization */
  132. int num_pushed; /* Number of band edges pushed */
  133. enum
  134. {
  135. rr,
  136. ll
  137. } which_way; /* Push which way? */
  138. double low_limit; /* Lower limit for finding if primal is feasible */
  139. bool odd_length; /* Odd-length filters? */
  140. FILE *log_fd;
  141. };
  142. void output_filter_performance_as_csv_file(struct meteor_working_data_s *s, const char *file_name);
  143. int meteor_design_filter(struct meteor_working_data_s *s, struct meteor_spec_s *t, double coeffs[]);
  144. #if defined(__cplusplus)
  145. }
  146. #endif
  147. #endif
  148. /*- End of file ------------------------------------------------------------*/