fir.h 3.6 KB

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