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.

244 lines
8.0KB

  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(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 CONV_S16(dest, source) dest = av_clip_int16(lrintf(source));
  107. #define CONV_FLT(dest, source) dest = source;
  108. #define FILTER_BW_O4_1(i0, i1, i2, i3, fmt) \
  109. in = *src0 * c->gain \
  110. + c->cy[0]*s->x[i0] + c->cy[1]*s->x[i1] \
  111. + c->cy[2]*s->x[i2] + c->cy[3]*s->x[i3]; \
  112. res = (s->x[i0] + in )*1 \
  113. + (s->x[i1] + s->x[i3])*4 \
  114. + s->x[i2] *6; \
  115. CONV_##fmt(*dst0, res) \
  116. s->x[i0] = in; \
  117. src0 += sstep; \
  118. dst0 += dstep;
  119. #define FILTER_BW_O4(type, fmt) { \
  120. int i; \
  121. const type *src0 = src; \
  122. type *dst0 = dst; \
  123. for (i = 0; i < size; i += 4) { \
  124. float in, res; \
  125. FILTER_BW_O4_1(0, 1, 2, 3, fmt); \
  126. FILTER_BW_O4_1(1, 2, 3, 0, fmt); \
  127. FILTER_BW_O4_1(2, 3, 0, 1, fmt); \
  128. FILTER_BW_O4_1(3, 0, 1, 2, fmt); \
  129. } \
  130. }
  131. #define FILTER_DIRECT_FORM_II(type, fmt) { \
  132. int i; \
  133. const type *src0 = src; \
  134. type *dst0 = dst; \
  135. for (i = 0; i < size; i++) { \
  136. int j; \
  137. float in, res; \
  138. in = *src0 * c->gain; \
  139. for(j = 0; j < c->order; j++) \
  140. in += c->cy[j] * s->x[j]; \
  141. res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1]; \
  142. for(j = 1; j < c->order >> 1; j++) \
  143. res += (s->x[j] + s->x[c->order - j]) * c->cx[j]; \
  144. for(j = 0; j < c->order - 1; j++) \
  145. s->x[j] = s->x[j + 1]; \
  146. CONV_##fmt(*dst0, res) \
  147. s->x[c->order - 1] = in; \
  148. src0 += sstep; \
  149. dst0 += dstep; \
  150. } \
  151. }
  152. void ff_iir_filter(const struct FFIIRFilterCoeffs *c,
  153. struct FFIIRFilterState *s, int size,
  154. const int16_t *src, int sstep, int16_t *dst, int dstep)
  155. {
  156. if (c->order == 4) {
  157. FILTER_BW_O4(int16_t, S16)
  158. } else {
  159. FILTER_DIRECT_FORM_II(int16_t, S16)
  160. }
  161. }
  162. void ff_iir_filter_flt(const struct FFIIRFilterCoeffs *c,
  163. struct FFIIRFilterState *s, int size,
  164. const float *src, int sstep, void *dst, int dstep)
  165. {
  166. if (c->order == 4) {
  167. FILTER_BW_O4(float, FLT)
  168. } else {
  169. FILTER_DIRECT_FORM_II(float, FLT)
  170. }
  171. }
  172. av_cold void ff_iir_filter_free_state(struct FFIIRFilterState *state)
  173. {
  174. av_free(state);
  175. }
  176. av_cold void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs)
  177. {
  178. if(coeffs){
  179. av_free(coeffs->cx);
  180. av_free(coeffs->cy);
  181. }
  182. av_free(coeffs);
  183. }
  184. #ifdef TEST
  185. #define FILT_ORDER 4
  186. #define SIZE 1024
  187. int main(void)
  188. {
  189. struct FFIIRFilterCoeffs *fcoeffs = NULL;
  190. struct FFIIRFilterState *fstate = NULL;
  191. float cutoff_coeff = 0.4;
  192. int16_t x[SIZE], y[SIZE];
  193. int i;
  194. FILE* fd;
  195. fcoeffs = ff_iir_filter_init_coeffs(FF_FILTER_TYPE_BUTTERWORTH,
  196. FF_FILTER_MODE_LOWPASS, FILT_ORDER,
  197. cutoff_coeff, 0.0, 0.0);
  198. fstate = ff_iir_filter_init_state(FILT_ORDER);
  199. for (i = 0; i < SIZE; i++) {
  200. x[i] = lrint(0.75 * INT16_MAX * sin(0.5*M_PI*i*i/SIZE));
  201. }
  202. ff_iir_filter(fcoeffs, fstate, SIZE, x, 1, y, 1);
  203. fd = fopen("in.bin", "w");
  204. fwrite(x, sizeof(x[0]), SIZE, fd);
  205. fclose(fd);
  206. fd = fopen("out.bin", "w");
  207. fwrite(y, sizeof(y[0]), SIZE, fd);
  208. fclose(fd);
  209. ff_iir_filter_free_coeffs(fcoeffs);
  210. ff_iir_filter_free_state(fstate);
  211. return 0;
  212. }
  213. #endif /* TEST */