You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

216 lines
6.3KB

  1. /*
  2. * IIR filter
  3. * Copyright (c) 2008 Konstantin Shishkov
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg 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 GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file libavcodec/iirfilter.c
  23. * different IIR filters implementation
  24. */
  25. #include "iirfilter.h"
  26. #include <math.h>
  27. /**
  28. * IIR filter global parameters
  29. */
  30. typedef struct FFIIRFilterCoeffs{
  31. int order;
  32. float gain;
  33. int *cx;
  34. float *cy;
  35. }FFIIRFilterCoeffs;
  36. /**
  37. * IIR filter state
  38. */
  39. typedef struct FFIIRFilterState{
  40. float x[1];
  41. }FFIIRFilterState;
  42. /// maximum supported filter order
  43. #define MAXORDER 30
  44. av_cold struct FFIIRFilterCoeffs* ff_iir_filter_init_coeffs(enum IIRFilterType filt_type,
  45. enum IIRFilterMode filt_mode,
  46. int order, float cutoff_ratio,
  47. float stopband, float ripple)
  48. {
  49. int i, j;
  50. FFIIRFilterCoeffs *c;
  51. double wa;
  52. double p[MAXORDER + 1][2];
  53. if(filt_type != FF_FILTER_TYPE_BUTTERWORTH || filt_mode != FF_FILTER_MODE_LOWPASS)
  54. return NULL;
  55. if(order <= 1 || (order & 1) || order > MAXORDER || cutoff_ratio >= 1.0)
  56. return NULL;
  57. c = av_malloc(sizeof(FFIIRFilterCoeffs));
  58. c->cx = av_malloc(sizeof(c->cx[0]) * ((order >> 1) + 1));
  59. c->cy = av_malloc(sizeof(c->cy[0]) * order);
  60. c->order = order;
  61. wa = 2 * tan(M_PI * 0.5 * cutoff_ratio);
  62. c->cx[0] = 1;
  63. for(i = 1; i < (order >> 1) + 1; i++)
  64. c->cx[i] = c->cx[i - 1] * (order - i + 1LL) / i;
  65. p[0][0] = 1.0;
  66. p[0][1] = 0.0;
  67. for(i = 1; i <= order; i++)
  68. p[i][0] = p[i][1] = 0.0;
  69. for(i = 0; i < order; i++){
  70. double zp[2];
  71. double th = (i + (order >> 1) + 0.5) * M_PI / order;
  72. double a_re, a_im, c_re, c_im;
  73. zp[0] = cos(th) * wa;
  74. zp[1] = sin(th) * wa;
  75. a_re = zp[0] + 2.0;
  76. c_re = zp[0] - 2.0;
  77. a_im =
  78. c_im = zp[1];
  79. zp[0] = (a_re * c_re + a_im * c_im) / (c_re * c_re + c_im * c_im);
  80. zp[1] = (a_im * c_re - a_re * c_im) / (c_re * c_re + c_im * c_im);
  81. for(j = order; j >= 1; j--)
  82. {
  83. a_re = p[j][0];
  84. a_im = p[j][1];
  85. p[j][0] = a_re*zp[0] - a_im*zp[1] + p[j-1][0];
  86. p[j][1] = a_re*zp[1] + a_im*zp[0] + p[j-1][1];
  87. }
  88. a_re = p[0][0]*zp[0] - p[0][1]*zp[1];
  89. p[0][1] = p[0][0]*zp[1] + p[0][1]*zp[0];
  90. p[0][0] = a_re;
  91. }
  92. c->gain = p[order][0];
  93. for(i = 0; i < order; i++){
  94. c->gain += p[i][0];
  95. c->cy[i] = (-p[i][0] * p[order][0] + -p[i][1] * p[order][1]) /
  96. (p[order][0] * p[order][0] + p[order][1] * p[order][1]);
  97. }
  98. c->gain /= 1 << order;
  99. return c;
  100. }
  101. av_cold struct FFIIRFilterState* ff_iir_filter_init_state(int order)
  102. {
  103. FFIIRFilterState* s = av_mallocz(sizeof(FFIIRFilterState) + sizeof(s->x[0]) * (order - 1));
  104. return s;
  105. }
  106. #define FILTER(i0, i1, i2, i3) \
  107. in = *src * c->gain \
  108. + c->cy[0]*s->x[i0] + c->cy[1]*s->x[i1] \
  109. + c->cy[2]*s->x[i2] + c->cy[3]*s->x[i3]; \
  110. res = (s->x[i0] + in )*1 \
  111. + (s->x[i1] + s->x[i3])*4 \
  112. + s->x[i2] *6; \
  113. *dst = av_clip_int16(lrintf(res)); \
  114. s->x[i0] = in; \
  115. src += sstep; \
  116. dst += dstep; \
  117. void ff_iir_filter(const struct FFIIRFilterCoeffs *c, struct FFIIRFilterState *s, int size, const int16_t *src, int sstep, int16_t *dst, int dstep)
  118. {
  119. int i;
  120. if(c->order == 4){
  121. for(i = 0; i < size; i += 4){
  122. float in, res;
  123. FILTER(0, 1, 2, 3);
  124. FILTER(1, 2, 3, 0);
  125. FILTER(2, 3, 0, 1);
  126. FILTER(3, 0, 1, 2);
  127. }
  128. }else{
  129. for(i = 0; i < size; i++){
  130. int j;
  131. float in, res;
  132. in = *src * c->gain;
  133. for(j = 0; j < c->order; j++)
  134. in += c->cy[j] * s->x[j];
  135. res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1];
  136. for(j = 1; j < c->order >> 1; j++)
  137. res += (s->x[j] + s->x[c->order - j]) * c->cx[j];
  138. for(j = 0; j < c->order - 1; j++)
  139. s->x[j] = s->x[j + 1];
  140. *dst = av_clip_int16(lrintf(res));
  141. s->x[c->order - 1] = in;
  142. src += sstep;
  143. dst += sstep;
  144. }
  145. }
  146. }
  147. av_cold void ff_iir_filter_free_state(struct FFIIRFilterState *state)
  148. {
  149. av_free(state);
  150. }
  151. av_cold void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs)
  152. {
  153. if(coeffs){
  154. av_free(coeffs->cx);
  155. av_free(coeffs->cy);
  156. }
  157. av_free(coeffs);
  158. }
  159. #ifdef TEST
  160. #define FILT_ORDER 4
  161. #define SIZE 1024
  162. int main(void)
  163. {
  164. struct FFIIRFilterCoeffs *fcoeffs = NULL;
  165. struct FFIIRFilterState *fstate = NULL;
  166. float cutoff_coeff = 0.4;
  167. int16_t x[SIZE], y[SIZE];
  168. int i;
  169. FILE* fd;
  170. fcoeffs = ff_iir_filter_init_coeffs(FF_FILTER_TYPE_BUTTERWORTH,
  171. FF_FILTER_MODE_LOWPASS, FILT_ORDER,
  172. cutoff_coeff, 0.0, 0.0);
  173. fstate = ff_iir_filter_init_state(FILT_ORDER);
  174. for (i = 0; i < SIZE; i++) {
  175. x[i] = lrint(0.75 * INT16_MAX * sin(0.5*M_PI*i*i/SIZE));
  176. }
  177. ff_iir_filter(fcoeffs, fstate, SIZE, x, 1, y, 1);
  178. fd = fopen("in.bin", "w");
  179. fwrite(x, sizeof(x[0]), SIZE, fd);
  180. fclose(fd);
  181. fd = fopen("out.bin", "w");
  182. fwrite(y, sizeof(y[0]), SIZE, fd);
  183. fclose(fd);
  184. ff_iir_filter_free_coeffs(fcoeffs);
  185. ff_iir_filter_free_state(fstate);
  186. return 0;
  187. }
  188. #endif /* TEST */