FIR.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. dsp/FIR.h
  3. Copyright 2003-10 Tim Goetze <tim@quitte.de>
  4. http://quitte.de/dsp/
  5. finite impulse response filters, with options for up- and down-sampling.
  6. */
  7. /*
  8. This program is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU General Public License
  10. as published by the Free Software Foundation; either version 2
  11. of the License, or (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  19. 02111-1307, USA or point your web browser to http://www.gnu.org.
  20. */
  21. #ifndef _FIR_H_
  22. #define _FIR_H_
  23. #include "util.h"
  24. namespace DSP {
  25. /* brute-force FIR filter with downsampling method (decimating).
  26. *
  27. * CAVEAT: constructing it from another FIR makes the filter share the other's
  28. * kernel data set. IOW, the other FIR must be valid throughout the lifetime
  29. * of this instance.
  30. */
  31. class FIR
  32. {
  33. public:
  34. /* kernel length, history length - 1 */
  35. int n, m;
  36. /* coefficients, history */
  37. sample_t * c, * x;
  38. bool borrowed_kernel;
  39. /* history index */
  40. int h;
  41. FIR (int N)
  42. {
  43. c = 0;
  44. init (N);
  45. }
  46. FIR (FIR & fir)
  47. {
  48. c = fir.c;
  49. init (fir.n);
  50. }
  51. FIR (int n, sample_t * kernel)
  52. {
  53. c = 0;
  54. init (n);
  55. memcpy (c, kernel, n * sizeof (*c));
  56. }
  57. ~FIR()
  58. {
  59. if (!borrowed_kernel)
  60. free (c);
  61. free (x);
  62. }
  63. void init (int N)
  64. {
  65. n = N;
  66. /* keeping history size a power of 2 makes it possible to wrap the
  67. * history pointer by & instead of %, saving a few cpu cycles. */
  68. m = next_power_of_2 (n);
  69. if (c)
  70. borrowed_kernel = true;
  71. else
  72. borrowed_kernel = false,
  73. c = (sample_t *) malloc (n * sizeof (sample_t));
  74. x = (sample_t *) malloc (m * sizeof (sample_t));
  75. m -= 1;
  76. reset();
  77. }
  78. void reset()
  79. {
  80. h = 0;
  81. memset (x, 0, n * sizeof (sample_t));
  82. }
  83. /* TODO: write an SSE-enabled version */
  84. inline sample_t process (sample_t s)
  85. {
  86. x[h] = s;
  87. s *= c[0];
  88. for (int Z = 1, z = h - 1; Z < n; --z, ++Z)
  89. s += c[Z] * x[z & m];
  90. h = (h + 1) & m;
  91. return s;
  92. }
  93. /* Z is the time, in samples, since the last non-zero sample.
  94. * OVER is the oversampling factor. just here for documentation, use
  95. * a FIRUpsampler instead.
  96. */
  97. template <int Z, int OVER>
  98. inline sample_t upsample (sample_t s)
  99. {
  100. x[h] = s;
  101. s = 0;
  102. /* for the interpolation, iterate over the history in z ^ -OVER
  103. * steps -- all the samples between are 0.
  104. */
  105. for (int j = Z, z = h - Z; j < n; --z, j += OVER)
  106. s += c[j] * x[z & m];
  107. h = (h + 1) & m;
  108. return s;
  109. }
  110. /* used in downsampling */
  111. inline void store (sample_t s)
  112. {
  113. x[h] = s;
  114. h = (h + 1) & m;
  115. }
  116. };
  117. /* close relative of FIR, but distinct enough to not justify inheritance.
  118. *
  119. * the difference to the FIR is the shorter history length. don't need
  120. * to clutter the d-cache with interleaved 0s.
  121. *
  122. * however, an initial test shows this to be a fraction *slower* than a
  123. * complete FIR for N = 32, OVER = 4.
  124. */
  125. class FIRUpsampler
  126. {
  127. public:
  128. /* kernel length, history length - 1 */
  129. int n, m;
  130. /* oversampling ratio */
  131. int over;
  132. /* coefficients, history */
  133. sample_t * c, * x;
  134. /* history index */
  135. int h;
  136. FIRUpsampler (int _n, int _over)
  137. {
  138. c = x = 0;
  139. init (_n, _over);
  140. }
  141. FIRUpsampler (FIR & fir, int _over)
  142. {
  143. c = x = 0;
  144. init (fir.n, _over);
  145. memcpy (c, fir.c, n * sizeof (sample_t));
  146. }
  147. ~FIRUpsampler()
  148. {
  149. if (c) free (c);
  150. if (x) free (x);
  151. }
  152. void init (int _n, int _over)
  153. {
  154. /* oversampling ratio must be multiple of FIR kernel length */
  155. // assert (_n % _over == 0);
  156. n = _n;
  157. over = _over;
  158. /* like FIR, keep the history buffer a power of 2; additionally,
  159. * compress and don't store the 0 samples inbetween.
  160. */
  161. m = next_power_of_2 ((n + over - 1) / over);
  162. c = (sample_t *) malloc (n * sizeof (sample_t));
  163. x = (sample_t *) malloc (m * sizeof (sample_t));
  164. m -= 1;
  165. reset();
  166. }
  167. void reset()
  168. {
  169. h = 0;
  170. memset (x, 0, (m + 1) * sizeof (sample_t));
  171. }
  172. /* upsample the given sample */
  173. inline sample_t upsample (sample_t s)
  174. {
  175. x[h] = s;
  176. s = 0;
  177. for (int Z = 0, z = h; Z < n; --z, Z += over)
  178. s += c[Z] * x[z & m];
  179. h = (h + 1) & m;
  180. return s;
  181. }
  182. /* upsample a zero sample (interleaving), Z being the time, in samples,
  183. * since the last non-0 sample. */
  184. inline sample_t pad (int Z)
  185. {
  186. sample_t s = 0;
  187. for (int z = h - 1; Z < n; --z, Z += over)
  188. s += c[Z] * x[z & m];
  189. return s;
  190. }
  191. };
  192. }; /* namespace DSP */
  193. #endif /* _FIR_H_ */