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.

351 lines
12KB

  1. /*
  2. * IIR filter
  3. * Copyright (c) 2008 Konstantin Shishkov
  4. *
  5. * This file is part of Libav.
  6. *
  7. * Libav 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. * Libav 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 Libav; 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. static int butterworth_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c,
  45. enum IIRFilterMode filt_mode,
  46. int order, float cutoff_ratio,
  47. float stopband)
  48. {
  49. int i, j;
  50. double wa;
  51. double p[MAXORDER + 1][2];
  52. if (filt_mode != FF_FILTER_MODE_LOWPASS) {
  53. av_log(avc, AV_LOG_ERROR, "Butterworth filter currently only supports "
  54. "low-pass filter mode\n");
  55. return -1;
  56. }
  57. if (order & 1) {
  58. av_log(avc, AV_LOG_ERROR, "Butterworth filter currently only supports "
  59. "even filter orders\n");
  60. return -1;
  61. }
  62. wa = 2 * tan(M_PI * 0.5 * cutoff_ratio);
  63. c->cx[0] = 1;
  64. for(i = 1; i < (order >> 1) + 1; i++)
  65. c->cx[i] = c->cx[i - 1] * (order - i + 1LL) / i;
  66. p[0][0] = 1.0;
  67. p[0][1] = 0.0;
  68. for(i = 1; i <= order; i++)
  69. p[i][0] = p[i][1] = 0.0;
  70. for(i = 0; i < order; i++){
  71. double zp[2];
  72. double th = (i + (order >> 1) + 0.5) * M_PI / order;
  73. double a_re, a_im, c_re, c_im;
  74. zp[0] = cos(th) * wa;
  75. zp[1] = sin(th) * wa;
  76. a_re = zp[0] + 2.0;
  77. c_re = zp[0] - 2.0;
  78. a_im =
  79. c_im = zp[1];
  80. zp[0] = (a_re * c_re + a_im * c_im) / (c_re * c_re + c_im * c_im);
  81. zp[1] = (a_im * c_re - a_re * c_im) / (c_re * c_re + c_im * c_im);
  82. for(j = order; j >= 1; j--)
  83. {
  84. a_re = p[j][0];
  85. a_im = p[j][1];
  86. p[j][0] = a_re*zp[0] - a_im*zp[1] + p[j-1][0];
  87. p[j][1] = a_re*zp[1] + a_im*zp[0] + p[j-1][1];
  88. }
  89. a_re = p[0][0]*zp[0] - p[0][1]*zp[1];
  90. p[0][1] = p[0][0]*zp[1] + p[0][1]*zp[0];
  91. p[0][0] = a_re;
  92. }
  93. c->gain = p[order][0];
  94. for(i = 0; i < order; i++){
  95. c->gain += p[i][0];
  96. c->cy[i] = (-p[i][0] * p[order][0] + -p[i][1] * p[order][1]) /
  97. (p[order][0] * p[order][0] + p[order][1] * p[order][1]);
  98. }
  99. c->gain /= 1 << order;
  100. return 0;
  101. }
  102. static int biquad_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c,
  103. enum IIRFilterMode filt_mode, int order,
  104. float cutoff_ratio, float stopband)
  105. {
  106. double cos_w0, sin_w0;
  107. double a0, x0, x1;
  108. if (filt_mode != FF_FILTER_MODE_HIGHPASS &&
  109. filt_mode != FF_FILTER_MODE_LOWPASS) {
  110. av_log(avc, AV_LOG_ERROR, "Biquad filter currently only supports "
  111. "high-pass and low-pass filter modes\n");
  112. return -1;
  113. }
  114. if (order != 2) {
  115. av_log(avc, AV_LOG_ERROR, "Biquad filter must have order of 2\n");
  116. return -1;
  117. }
  118. cos_w0 = cos(M_PI * cutoff_ratio);
  119. sin_w0 = sin(M_PI * cutoff_ratio);
  120. a0 = 1.0 + (sin_w0 / 2.0);
  121. if (filt_mode == FF_FILTER_MODE_HIGHPASS) {
  122. c->gain = ((1.0 + cos_w0) / 2.0) / a0;
  123. x0 = ((1.0 + cos_w0) / 2.0) / a0;
  124. x1 = (-(1.0 + cos_w0)) / a0;
  125. } else { // FF_FILTER_MODE_LOWPASS
  126. c->gain = ((1.0 - cos_w0) / 2.0) / a0;
  127. x0 = ((1.0 - cos_w0) / 2.0) / a0;
  128. x1 = (1.0 - cos_w0) / a0;
  129. }
  130. c->cy[0] = (-1.0 + (sin_w0 / 2.0)) / a0;
  131. c->cy[1] = (2.0 * cos_w0) / a0;
  132. // divide by gain to make the x coeffs integers.
  133. // during filtering, the delay state will include the gain multiplication
  134. c->cx[0] = lrintf(x0 / c->gain);
  135. c->cx[1] = lrintf(x1 / c->gain);
  136. c->cy[0] /= c->gain;
  137. c->cy[1] /= c->gain;
  138. return 0;
  139. }
  140. av_cold struct FFIIRFilterCoeffs* ff_iir_filter_init_coeffs(void *avc,
  141. enum IIRFilterType filt_type,
  142. enum IIRFilterMode filt_mode,
  143. int order, float cutoff_ratio,
  144. float stopband, float ripple)
  145. {
  146. FFIIRFilterCoeffs *c;
  147. int ret = 0;
  148. if (order <= 0 || order > MAXORDER || cutoff_ratio >= 1.0)
  149. return NULL;
  150. FF_ALLOCZ_OR_GOTO(avc, c, sizeof(FFIIRFilterCoeffs),
  151. init_fail);
  152. FF_ALLOC_OR_GOTO (avc, c->cx, sizeof(c->cx[0]) * ((order >> 1) + 1),
  153. init_fail);
  154. FF_ALLOC_OR_GOTO (avc, c->cy, sizeof(c->cy[0]) * order,
  155. init_fail);
  156. c->order = order;
  157. switch (filt_type) {
  158. case FF_FILTER_TYPE_BUTTERWORTH:
  159. ret = butterworth_init_coeffs(avc, c, filt_mode, order, cutoff_ratio,
  160. stopband);
  161. break;
  162. case FF_FILTER_TYPE_BIQUAD:
  163. ret = biquad_init_coeffs(avc, c, filt_mode, order, cutoff_ratio,
  164. stopband);
  165. break;
  166. default:
  167. av_log(avc, AV_LOG_ERROR, "filter type is not currently implemented\n");
  168. goto init_fail;
  169. }
  170. if (!ret)
  171. return c;
  172. init_fail:
  173. ff_iir_filter_free_coeffs(c);
  174. return NULL;
  175. }
  176. av_cold struct FFIIRFilterState* ff_iir_filter_init_state(int order)
  177. {
  178. FFIIRFilterState* s = av_mallocz(sizeof(FFIIRFilterState) + sizeof(s->x[0]) * (order - 1));
  179. return s;
  180. }
  181. #define CONV_S16(dest, source) dest = av_clip_int16(lrintf(source));
  182. #define CONV_FLT(dest, source) dest = source;
  183. #define FILTER_BW_O4_1(i0, i1, i2, i3, fmt) \
  184. in = *src0 * c->gain \
  185. + c->cy[0]*s->x[i0] + c->cy[1]*s->x[i1] \
  186. + c->cy[2]*s->x[i2] + c->cy[3]*s->x[i3]; \
  187. res = (s->x[i0] + in )*1 \
  188. + (s->x[i1] + s->x[i3])*4 \
  189. + s->x[i2] *6; \
  190. CONV_##fmt(*dst0, res) \
  191. s->x[i0] = in; \
  192. src0 += sstep; \
  193. dst0 += dstep;
  194. #define FILTER_BW_O4(type, fmt) { \
  195. int i; \
  196. const type *src0 = src; \
  197. type *dst0 = dst; \
  198. for (i = 0; i < size; i += 4) { \
  199. float in, res; \
  200. FILTER_BW_O4_1(0, 1, 2, 3, fmt); \
  201. FILTER_BW_O4_1(1, 2, 3, 0, fmt); \
  202. FILTER_BW_O4_1(2, 3, 0, 1, fmt); \
  203. FILTER_BW_O4_1(3, 0, 1, 2, fmt); \
  204. } \
  205. }
  206. #define FILTER_DIRECT_FORM_II(type, fmt) { \
  207. int i; \
  208. const type *src0 = src; \
  209. type *dst0 = dst; \
  210. for (i = 0; i < size; i++) { \
  211. int j; \
  212. float in, res; \
  213. in = *src0 * c->gain; \
  214. for(j = 0; j < c->order; j++) \
  215. in += c->cy[j] * s->x[j]; \
  216. res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1]; \
  217. for(j = 1; j < c->order >> 1; j++) \
  218. res += (s->x[j] + s->x[c->order - j]) * c->cx[j]; \
  219. for(j = 0; j < c->order - 1; j++) \
  220. s->x[j] = s->x[j + 1]; \
  221. CONV_##fmt(*dst0, res) \
  222. s->x[c->order - 1] = in; \
  223. src0 += sstep; \
  224. dst0 += dstep; \
  225. } \
  226. }
  227. #define FILTER_O2(type, fmt) { \
  228. int i; \
  229. const type *src0 = src; \
  230. type *dst0 = dst; \
  231. for (i = 0; i < size; i++) { \
  232. float in = *src0 * c->gain + \
  233. s->x[0] * c->cy[0] + \
  234. s->x[1] * c->cy[1]; \
  235. CONV_##fmt(*dst0, s->x[0] + in + s->x[1] * c->cx[1]) \
  236. s->x[0] = s->x[1]; \
  237. s->x[1] = in; \
  238. src0 += sstep; \
  239. dst0 += dstep; \
  240. } \
  241. }
  242. void ff_iir_filter(const struct FFIIRFilterCoeffs *c,
  243. struct FFIIRFilterState *s, int size,
  244. const int16_t *src, int sstep, int16_t *dst, int dstep)
  245. {
  246. if (c->order == 2) {
  247. FILTER_O2(int16_t, S16)
  248. } else if (c->order == 4) {
  249. FILTER_BW_O4(int16_t, S16)
  250. } else {
  251. FILTER_DIRECT_FORM_II(int16_t, S16)
  252. }
  253. }
  254. void ff_iir_filter_flt(const struct FFIIRFilterCoeffs *c,
  255. struct FFIIRFilterState *s, int size,
  256. const float *src, int sstep, float *dst, int dstep)
  257. {
  258. if (c->order == 2) {
  259. FILTER_O2(float, FLT)
  260. } else if (c->order == 4) {
  261. FILTER_BW_O4(float, FLT)
  262. } else {
  263. FILTER_DIRECT_FORM_II(float, FLT)
  264. }
  265. }
  266. av_cold void ff_iir_filter_free_state(struct FFIIRFilterState *state)
  267. {
  268. av_free(state);
  269. }
  270. av_cold void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs)
  271. {
  272. if(coeffs){
  273. av_free(coeffs->cx);
  274. av_free(coeffs->cy);
  275. }
  276. av_free(coeffs);
  277. }
  278. #ifdef TEST
  279. #define FILT_ORDER 4
  280. #define SIZE 1024
  281. int main(void)
  282. {
  283. struct FFIIRFilterCoeffs *fcoeffs = NULL;
  284. struct FFIIRFilterState *fstate = NULL;
  285. float cutoff_coeff = 0.4;
  286. int16_t x[SIZE], y[SIZE];
  287. int i;
  288. FILE* fd;
  289. fcoeffs = ff_iir_filter_init_coeffs(FF_FILTER_TYPE_BUTTERWORTH,
  290. FF_FILTER_MODE_LOWPASS, FILT_ORDER,
  291. cutoff_coeff, 0.0, 0.0);
  292. fstate = ff_iir_filter_init_state(FILT_ORDER);
  293. for (i = 0; i < SIZE; i++) {
  294. x[i] = lrint(0.75 * INT16_MAX * sin(0.5*M_PI*i*i/SIZE));
  295. }
  296. ff_iir_filter(fcoeffs, fstate, SIZE, x, 1, y, 1);
  297. fd = fopen("in.bin", "w");
  298. fwrite(x, sizeof(x[0]), SIZE, fd);
  299. fclose(fd);
  300. fd = fopen("out.bin", "w");
  301. fwrite(y, sizeof(y[0]), SIZE, fd);
  302. fclose(fd);
  303. ff_iir_filter_free_coeffs(fcoeffs);
  304. ff_iir_filter_free_state(fstate);
  305. return 0;
  306. }
  307. #endif /* TEST */