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.

252 lines
8.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
  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(void *avc,
  45. enum IIRFilterType filt_type,
  46. enum IIRFilterMode filt_mode,
  47. int order, float cutoff_ratio,
  48. float stopband, float ripple)
  49. {
  50. int i, j;
  51. FFIIRFilterCoeffs *c;
  52. double wa;
  53. double p[MAXORDER + 1][2];
  54. if(filt_type != FF_FILTER_TYPE_BUTTERWORTH || filt_mode != FF_FILTER_MODE_LOWPASS)
  55. return NULL;
  56. if(order <= 1 || (order & 1) || order > MAXORDER || cutoff_ratio >= 1.0)
  57. return NULL;
  58. FF_ALLOCZ_OR_GOTO(avc, c, sizeof(FFIIRFilterCoeffs),
  59. init_fail);
  60. FF_ALLOC_OR_GOTO (avc, c->cx, sizeof(c->cx[0]) * ((order >> 1) + 1),
  61. init_fail);
  62. FF_ALLOC_OR_GOTO (avc, c->cy, sizeof(c->cy[0]) * order,
  63. init_fail);
  64. c->order = order;
  65. wa = 2 * tan(M_PI * 0.5 * cutoff_ratio);
  66. c->cx[0] = 1;
  67. for(i = 1; i < (order >> 1) + 1; i++)
  68. c->cx[i] = c->cx[i - 1] * (order - i + 1LL) / i;
  69. p[0][0] = 1.0;
  70. p[0][1] = 0.0;
  71. for(i = 1; i <= order; i++)
  72. p[i][0] = p[i][1] = 0.0;
  73. for(i = 0; i < order; i++){
  74. double zp[2];
  75. double th = (i + (order >> 1) + 0.5) * M_PI / order;
  76. double a_re, a_im, c_re, c_im;
  77. zp[0] = cos(th) * wa;
  78. zp[1] = sin(th) * wa;
  79. a_re = zp[0] + 2.0;
  80. c_re = zp[0] - 2.0;
  81. a_im =
  82. c_im = zp[1];
  83. zp[0] = (a_re * c_re + a_im * c_im) / (c_re * c_re + c_im * c_im);
  84. zp[1] = (a_im * c_re - a_re * c_im) / (c_re * c_re + c_im * c_im);
  85. for(j = order; j >= 1; j--)
  86. {
  87. a_re = p[j][0];
  88. a_im = p[j][1];
  89. p[j][0] = a_re*zp[0] - a_im*zp[1] + p[j-1][0];
  90. p[j][1] = a_re*zp[1] + a_im*zp[0] + p[j-1][1];
  91. }
  92. a_re = p[0][0]*zp[0] - p[0][1]*zp[1];
  93. p[0][1] = p[0][0]*zp[1] + p[0][1]*zp[0];
  94. p[0][0] = a_re;
  95. }
  96. c->gain = p[order][0];
  97. for(i = 0; i < order; i++){
  98. c->gain += p[i][0];
  99. c->cy[i] = (-p[i][0] * p[order][0] + -p[i][1] * p[order][1]) /
  100. (p[order][0] * p[order][0] + p[order][1] * p[order][1]);
  101. }
  102. c->gain /= 1 << order;
  103. return c;
  104. init_fail:
  105. ff_iir_filter_free_coeffs(c);
  106. return NULL;
  107. }
  108. av_cold struct FFIIRFilterState* ff_iir_filter_init_state(int order)
  109. {
  110. FFIIRFilterState* s = av_mallocz(sizeof(FFIIRFilterState) + sizeof(s->x[0]) * (order - 1));
  111. return s;
  112. }
  113. #define CONV_S16(dest, source) dest = av_clip_int16(lrintf(source));
  114. #define CONV_FLT(dest, source) dest = source;
  115. #define FILTER_BW_O4_1(i0, i1, i2, i3, fmt) \
  116. in = *src0 * c->gain \
  117. + c->cy[0]*s->x[i0] + c->cy[1]*s->x[i1] \
  118. + c->cy[2]*s->x[i2] + c->cy[3]*s->x[i3]; \
  119. res = (s->x[i0] + in )*1 \
  120. + (s->x[i1] + s->x[i3])*4 \
  121. + s->x[i2] *6; \
  122. CONV_##fmt(*dst0, res) \
  123. s->x[i0] = in; \
  124. src0 += sstep; \
  125. dst0 += dstep;
  126. #define FILTER_BW_O4(type, fmt) { \
  127. int i; \
  128. const type *src0 = src; \
  129. type *dst0 = dst; \
  130. for (i = 0; i < size; i += 4) { \
  131. float in, res; \
  132. FILTER_BW_O4_1(0, 1, 2, 3, fmt); \
  133. FILTER_BW_O4_1(1, 2, 3, 0, fmt); \
  134. FILTER_BW_O4_1(2, 3, 0, 1, fmt); \
  135. FILTER_BW_O4_1(3, 0, 1, 2, fmt); \
  136. } \
  137. }
  138. #define FILTER_DIRECT_FORM_II(type, fmt) { \
  139. int i; \
  140. const type *src0 = src; \
  141. type *dst0 = dst; \
  142. for (i = 0; i < size; i++) { \
  143. int j; \
  144. float in, res; \
  145. in = *src0 * c->gain; \
  146. for(j = 0; j < c->order; j++) \
  147. in += c->cy[j] * s->x[j]; \
  148. res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1]; \
  149. for(j = 1; j < c->order >> 1; j++) \
  150. res += (s->x[j] + s->x[c->order - j]) * c->cx[j]; \
  151. for(j = 0; j < c->order - 1; j++) \
  152. s->x[j] = s->x[j + 1]; \
  153. CONV_##fmt(*dst0, res) \
  154. s->x[c->order - 1] = in; \
  155. src0 += sstep; \
  156. dst0 += dstep; \
  157. } \
  158. }
  159. void ff_iir_filter(const struct FFIIRFilterCoeffs *c,
  160. struct FFIIRFilterState *s, int size,
  161. const int16_t *src, int sstep, int16_t *dst, int dstep)
  162. {
  163. if (c->order == 4) {
  164. FILTER_BW_O4(int16_t, S16)
  165. } else {
  166. FILTER_DIRECT_FORM_II(int16_t, S16)
  167. }
  168. }
  169. void ff_iir_filter_flt(const struct FFIIRFilterCoeffs *c,
  170. struct FFIIRFilterState *s, int size,
  171. const float *src, int sstep, void *dst, int dstep)
  172. {
  173. if (c->order == 4) {
  174. FILTER_BW_O4(float, FLT)
  175. } else {
  176. FILTER_DIRECT_FORM_II(float, FLT)
  177. }
  178. }
  179. av_cold void ff_iir_filter_free_state(struct FFIIRFilterState *state)
  180. {
  181. av_free(state);
  182. }
  183. av_cold void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs)
  184. {
  185. if(coeffs){
  186. av_free(coeffs->cx);
  187. av_free(coeffs->cy);
  188. }
  189. av_free(coeffs);
  190. }
  191. #ifdef TEST
  192. #define FILT_ORDER 4
  193. #define SIZE 1024
  194. int main(void)
  195. {
  196. struct FFIIRFilterCoeffs *fcoeffs = NULL;
  197. struct FFIIRFilterState *fstate = NULL;
  198. float cutoff_coeff = 0.4;
  199. int16_t x[SIZE], y[SIZE];
  200. int i;
  201. FILE* fd;
  202. fcoeffs = ff_iir_filter_init_coeffs(FF_FILTER_TYPE_BUTTERWORTH,
  203. FF_FILTER_MODE_LOWPASS, FILT_ORDER,
  204. cutoff_coeff, 0.0, 0.0);
  205. fstate = ff_iir_filter_init_state(FILT_ORDER);
  206. for (i = 0; i < SIZE; i++) {
  207. x[i] = lrint(0.75 * INT16_MAX * sin(0.5*M_PI*i*i/SIZE));
  208. }
  209. ff_iir_filter(fcoeffs, fstate, SIZE, x, 1, y, 1);
  210. fd = fopen("in.bin", "w");
  211. fwrite(x, sizeof(x[0]), SIZE, fd);
  212. fclose(fd);
  213. fd = fopen("out.bin", "w");
  214. fwrite(y, sizeof(y[0]), SIZE, fd);
  215. fclose(fd);
  216. ff_iir_filter_free_coeffs(fcoeffs);
  217. ff_iir_filter_free_state(fstate);
  218. return 0;
  219. }
  220. #endif /* TEST */