fir.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * fir.h - General telephony FIR routines
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2002 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(_FIR_H_)
  26. #define _FIR_H_
  27. /*
  28. Ideas for improvement:
  29. 1/ Rewrite filter for dual MAC inner loop. The issue here is handling
  30. history sample offsets that are 16 bit aligned - the dual MAC needs
  31. 32 bit aligmnent. There are some good examples in libbfdsp.
  32. 2/ Use the hardware circular buffer facility tohalve memory usage.
  33. 3/ Consider using internal memory.
  34. Using less memory might also improve speed as cache misses will be
  35. reduced. A drop in MIPs and memory approaching 50% should be
  36. possible.
  37. The foreground and background filters currenlty use a total of
  38. about 10 MIPs/ch as measured with speedtest.c on a 256 TAP echo
  39. can.
  40. */
  41. /*
  42. * 16 bit integer FIR descriptor. This defines the working state for a single
  43. * instance of an FIR filter using 16 bit integer coefficients.
  44. */
  45. struct fir16_state_t {
  46. int taps;
  47. int curr_pos;
  48. const int16_t *coeffs;
  49. int16_t *history;
  50. };
  51. /*
  52. * 32 bit integer FIR descriptor. This defines the working state for a single
  53. * instance of an FIR filter using 32 bit integer coefficients, and filtering
  54. * 16 bit integer data.
  55. */
  56. struct fir32_state_t {
  57. int taps;
  58. int curr_pos;
  59. const int32_t *coeffs;
  60. int16_t *history;
  61. };
  62. /*
  63. * Floating point FIR descriptor. This defines the working state for a single
  64. * instance of an FIR filter using floating point coefficients and data.
  65. */
  66. struct fir_float_state_t {
  67. int taps;
  68. int curr_pos;
  69. const float *coeffs;
  70. float *history;
  71. };
  72. static inline const int16_t *fir16_create(struct fir16_state_t *fir,
  73. const int16_t *coeffs, int taps)
  74. {
  75. fir->taps = taps;
  76. fir->curr_pos = taps - 1;
  77. fir->coeffs = coeffs;
  78. fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL);
  79. return fir->history;
  80. }
  81. static inline void fir16_flush(struct fir16_state_t *fir)
  82. {
  83. memset(fir->history, 0, fir->taps * sizeof(int16_t));
  84. }
  85. static inline void fir16_free(struct fir16_state_t *fir)
  86. {
  87. kfree(fir->history);
  88. }
  89. static inline int16_t fir16(struct fir16_state_t *fir, int16_t sample)
  90. {
  91. int32_t y;
  92. int i;
  93. int offset1;
  94. int offset2;
  95. fir->history[fir->curr_pos] = sample;
  96. offset2 = fir->curr_pos;
  97. offset1 = fir->taps - offset2;
  98. y = 0;
  99. for (i = fir->taps - 1; i >= offset1; i--)
  100. y += fir->coeffs[i] * fir->history[i - offset1];
  101. for (; i >= 0; i--)
  102. y += fir->coeffs[i] * fir->history[i + offset2];
  103. if (fir->curr_pos <= 0)
  104. fir->curr_pos = fir->taps;
  105. fir->curr_pos--;
  106. return (int16_t) (y >> 15);
  107. }
  108. static inline const int16_t *fir32_create(struct fir32_state_t *fir,
  109. const int32_t *coeffs, int taps)
  110. {
  111. fir->taps = taps;
  112. fir->curr_pos = taps - 1;
  113. fir->coeffs = coeffs;
  114. fir->history = kcalloc(taps, sizeof(int16_t), GFP_KERNEL);
  115. return fir->history;
  116. }
  117. static inline void fir32_flush(struct fir32_state_t *fir)
  118. {
  119. memset(fir->history, 0, fir->taps * sizeof(int16_t));
  120. }
  121. static inline void fir32_free(struct fir32_state_t *fir)
  122. {
  123. kfree(fir->history);
  124. }
  125. static inline int16_t fir32(struct fir32_state_t *fir, int16_t sample)
  126. {
  127. int i;
  128. int32_t y;
  129. int offset1;
  130. int offset2;
  131. fir->history[fir->curr_pos] = sample;
  132. offset2 = fir->curr_pos;
  133. offset1 = fir->taps - offset2;
  134. y = 0;
  135. for (i = fir->taps - 1; i >= offset1; i--)
  136. y += fir->coeffs[i] * fir->history[i - offset1];
  137. for (; i >= 0; i--)
  138. y += fir->coeffs[i] * fir->history[i + offset2];
  139. if (fir->curr_pos <= 0)
  140. fir->curr_pos = fir->taps;
  141. fir->curr_pos--;
  142. return (int16_t) (y >> 15);
  143. }
  144. #endif