fir.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. */
  13. /*
  14. * See http://www.asterisk.org for more information about
  15. * the Asterisk project. Please do not directly contact
  16. * any of the maintainers of this project for assistance;
  17. * the project provides a web site, mailing lists and IRC
  18. * channels for your use.
  19. *
  20. * This program is free software, distributed under the terms of
  21. * the GNU General Public License Version 2 as published by the
  22. * Free Software Foundation. See the LICENSE file included with
  23. * this program for more details.
  24. */
  25. #if !defined(_FIR_H_)
  26. #define _FIR_H_
  27. typedef struct
  28. {
  29. int taps;
  30. int curr_pos;
  31. int16_t *coeffs;
  32. int16_t *history;
  33. } fir16_state_t;
  34. typedef struct
  35. {
  36. int taps;
  37. int curr_pos;
  38. int32_t *coeffs;
  39. int16_t *history;
  40. } fir32_state_t;
  41. static inline void fir16_create (fir16_state_t *fir,
  42. int16_t *coeffs,
  43. int taps)
  44. {
  45. fir->taps = taps;
  46. fir->curr_pos = taps - 1;
  47. fir->coeffs = coeffs;
  48. fir->history = kmalloc(taps*sizeof (int16_t), GFP_KERNEL);
  49. if (fir->history)
  50. memset (fir->history, '\0', taps*sizeof (int16_t));
  51. }
  52. /*- End of function --------------------------------------------------------*/
  53. static inline void fir16_free (fir16_state_t *fir)
  54. {
  55. kfree(fir->history);
  56. }
  57. /*- End of function --------------------------------------------------------*/
  58. static inline int16_t fir16 (fir16_state_t *fir, int16_t sample)
  59. {
  60. int i;
  61. int offset1;
  62. int offset2;
  63. int32_t y;
  64. fir->history[fir->curr_pos] = sample;
  65. offset2 = fir->curr_pos + 1;
  66. offset1 = fir->taps - offset2;
  67. y = 0;
  68. for (i = fir->taps - 1; i >= offset1; i--)
  69. y += fir->coeffs[i]*fir->history[i - offset1];
  70. for ( ; i >= 0; i--)
  71. y += fir->coeffs[i]*fir->history[i + offset2];
  72. if (fir->curr_pos <= 0)
  73. fir->curr_pos = fir->taps;
  74. fir->curr_pos--;
  75. return y >> 15;
  76. }
  77. /*- End of function --------------------------------------------------------*/
  78. static inline void fir32_create (fir32_state_t *fir,
  79. int32_t *coeffs,
  80. int taps)
  81. {
  82. fir->taps = taps;
  83. fir->curr_pos = taps - 1;
  84. fir->coeffs = coeffs;
  85. fir->history = kmalloc(taps*sizeof (int16_t), GFP_KERNEL);
  86. if (fir->history)
  87. memset (fir->history, '\0', taps*sizeof (int16_t));
  88. }
  89. /*- End of function --------------------------------------------------------*/
  90. static inline void fir32_free (fir32_state_t *fir)
  91. {
  92. kfree(fir->history);
  93. }
  94. /*- End of function --------------------------------------------------------*/
  95. static inline int16_t fir32 (fir32_state_t *fir, int16_t sample)
  96. {
  97. int i;
  98. int offset1;
  99. int offset2;
  100. int32_t y;
  101. fir->history[fir->curr_pos] = sample;
  102. offset2 = fir->curr_pos + 1;
  103. offset1 = fir->taps - offset2;
  104. y = 0;
  105. for (i = fir->taps - 1; i >= offset1; i--)
  106. y += fir->coeffs[i]*fir->history[i - offset1];
  107. for ( ; i >= 0; i--)
  108. y += fir->coeffs[i]*fir->history[i + offset2];
  109. if (fir->curr_pos <= 0)
  110. fir->curr_pos = fir->taps;
  111. fir->curr_pos--;
  112. return y >> 15;
  113. }
  114. /*- End of function --------------------------------------------------------*/
  115. #endif
  116. /*- End of file ------------------------------------------------------------*/