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.

346 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. return 0;
  137. }
  138. av_cold struct FFIIRFilterCoeffs* ff_iir_filter_init_coeffs(void *avc,
  139. enum IIRFilterType filt_type,
  140. enum IIRFilterMode filt_mode,
  141. int order, float cutoff_ratio,
  142. float stopband, float ripple)
  143. {
  144. FFIIRFilterCoeffs *c;
  145. int ret = 0;
  146. if (order <= 0 || order > MAXORDER || cutoff_ratio >= 1.0)
  147. return NULL;
  148. FF_ALLOCZ_OR_GOTO(avc, c, sizeof(FFIIRFilterCoeffs),
  149. init_fail);
  150. FF_ALLOC_OR_GOTO (avc, c->cx, sizeof(c->cx[0]) * ((order >> 1) + 1),
  151. init_fail);
  152. FF_ALLOC_OR_GOTO (avc, c->cy, sizeof(c->cy[0]) * order,
  153. init_fail);
  154. c->order = order;
  155. switch (filt_type) {
  156. case FF_FILTER_TYPE_BUTTERWORTH:
  157. ret = butterworth_init_coeffs(avc, c, filt_mode, order, cutoff_ratio,
  158. stopband);
  159. break;
  160. case FF_FILTER_TYPE_BIQUAD:
  161. ret = biquad_init_coeffs(avc, c, filt_mode, order, cutoff_ratio,
  162. stopband);
  163. break;
  164. default:
  165. av_log(avc, AV_LOG_ERROR, "filter type is not currently implemented\n");
  166. goto init_fail;
  167. }
  168. if (!ret)
  169. return c;
  170. init_fail:
  171. ff_iir_filter_free_coeffs(c);
  172. return NULL;
  173. }
  174. av_cold struct FFIIRFilterState* ff_iir_filter_init_state(int order)
  175. {
  176. FFIIRFilterState* s = av_mallocz(sizeof(FFIIRFilterState) + sizeof(s->x[0]) * (order - 1));
  177. return s;
  178. }
  179. #define CONV_S16(dest, source) dest = av_clip_int16(lrintf(source));
  180. #define CONV_FLT(dest, source) dest = source;
  181. #define FILTER_BW_O4_1(i0, i1, i2, i3, fmt) \
  182. in = *src0 * c->gain \
  183. + c->cy[0]*s->x[i0] + c->cy[1]*s->x[i1] \
  184. + c->cy[2]*s->x[i2] + c->cy[3]*s->x[i3]; \
  185. res = (s->x[i0] + in )*1 \
  186. + (s->x[i1] + s->x[i3])*4 \
  187. + s->x[i2] *6; \
  188. CONV_##fmt(*dst0, res) \
  189. s->x[i0] = in; \
  190. src0 += sstep; \
  191. dst0 += dstep;
  192. #define FILTER_BW_O4(type, fmt) { \
  193. int i; \
  194. const type *src0 = src; \
  195. type *dst0 = dst; \
  196. for (i = 0; i < size; i += 4) { \
  197. float in, res; \
  198. FILTER_BW_O4_1(0, 1, 2, 3, fmt); \
  199. FILTER_BW_O4_1(1, 2, 3, 0, fmt); \
  200. FILTER_BW_O4_1(2, 3, 0, 1, fmt); \
  201. FILTER_BW_O4_1(3, 0, 1, 2, fmt); \
  202. } \
  203. }
  204. #define FILTER_DIRECT_FORM_II(type, fmt) { \
  205. int i; \
  206. const type *src0 = src; \
  207. type *dst0 = dst; \
  208. for (i = 0; i < size; i++) { \
  209. int j; \
  210. float in, res; \
  211. in = *src0 * c->gain; \
  212. for(j = 0; j < c->order; j++) \
  213. in += c->cy[j] * s->x[j]; \
  214. res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1]; \
  215. for(j = 1; j < c->order >> 1; j++) \
  216. res += (s->x[j] + s->x[c->order - j]) * c->cx[j]; \
  217. for(j = 0; j < c->order - 1; j++) \
  218. s->x[j] = s->x[j + 1]; \
  219. CONV_##fmt(*dst0, res) \
  220. s->x[c->order - 1] = in; \
  221. src0 += sstep; \
  222. dst0 += dstep; \
  223. } \
  224. }
  225. #define FILTER_O2(type, fmt) { \
  226. int i; \
  227. const type *src0 = src; \
  228. type *dst0 = dst; \
  229. for (i = 0; i < size; i++) { \
  230. float in = *src0 * c->gain + \
  231. s->x[0] * c->cy[0] + \
  232. s->x[1] * c->cy[1]; \
  233. CONV_##fmt(*dst0, s->x[0] + in + s->x[1] * c->cx[1]) \
  234. s->x[0] = s->x[1]; \
  235. s->x[1] = in; \
  236. src0 += sstep; \
  237. dst0 += dstep; \
  238. } \
  239. }
  240. void ff_iir_filter(const struct FFIIRFilterCoeffs *c,
  241. struct FFIIRFilterState *s, int size,
  242. const int16_t *src, int sstep, int16_t *dst, int dstep)
  243. {
  244. if (c->order == 2) {
  245. FILTER_O2(int16_t, S16)
  246. } else if (c->order == 4) {
  247. FILTER_BW_O4(int16_t, S16)
  248. } else {
  249. FILTER_DIRECT_FORM_II(int16_t, S16)
  250. }
  251. }
  252. void ff_iir_filter_flt(const struct FFIIRFilterCoeffs *c,
  253. struct FFIIRFilterState *s, int size,
  254. const float *src, int sstep, float *dst, int dstep)
  255. {
  256. if (c->order == 2) {
  257. FILTER_O2(float, FLT)
  258. } else if (c->order == 4) {
  259. FILTER_BW_O4(float, FLT)
  260. } else {
  261. FILTER_DIRECT_FORM_II(float, FLT)
  262. }
  263. }
  264. av_cold void ff_iir_filter_free_state(struct FFIIRFilterState *state)
  265. {
  266. av_free(state);
  267. }
  268. av_cold void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs)
  269. {
  270. if(coeffs){
  271. av_free(coeffs->cx);
  272. av_free(coeffs->cy);
  273. }
  274. av_free(coeffs);
  275. }
  276. #ifdef TEST
  277. #undef printf
  278. #include <stdio.h>
  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. fcoeffs = ff_iir_filter_init_coeffs(NULL, FF_FILTER_TYPE_BUTTERWORTH,
  289. FF_FILTER_MODE_LOWPASS, FILT_ORDER,
  290. cutoff_coeff, 0.0, 0.0);
  291. fstate = ff_iir_filter_init_state(FILT_ORDER);
  292. for (i = 0; i < SIZE; i++) {
  293. x[i] = lrint(0.75 * INT16_MAX * sin(0.5*M_PI*i*i/SIZE));
  294. }
  295. ff_iir_filter(fcoeffs, fstate, SIZE, x, 1, y, 1);
  296. for (i = 0; i < SIZE; i++)
  297. printf("%6d %6d\n", x[i], y[i]);
  298. ff_iir_filter_free_coeffs(fcoeffs);
  299. ff_iir_filter_free_state(fstate);
  300. return 0;
  301. }
  302. #endif /* TEST */