00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00027 #include "iirfilter.h"
00028 #include <math.h>
00029
00033 typedef struct FFIIRFilterCoeffs{
00034 int order;
00035 float gain;
00036 int *cx;
00037 float *cy;
00038 }FFIIRFilterCoeffs;
00039
00043 typedef struct FFIIRFilterState{
00044 float x[1];
00045 }FFIIRFilterState;
00046
00048 #define MAXORDER 30
00049
00050 av_cold struct FFIIRFilterCoeffs* ff_iir_filter_init_coeffs(enum IIRFilterType filt_type,
00051 enum IIRFilterMode filt_mode,
00052 int order, float cutoff_ratio,
00053 float stopband, float ripple)
00054 {
00055 int i, j;
00056 FFIIRFilterCoeffs *c;
00057 double wa;
00058 double p[MAXORDER + 1][2];
00059
00060 if(filt_type != FF_FILTER_TYPE_BUTTERWORTH || filt_mode != FF_FILTER_MODE_LOWPASS)
00061 return NULL;
00062 if(order <= 1 || (order & 1) || order > MAXORDER || cutoff_ratio >= 1.0)
00063 return NULL;
00064
00065 c = av_malloc(sizeof(FFIIRFilterCoeffs));
00066 c->cx = av_malloc(sizeof(c->cx[0]) * ((order >> 1) + 1));
00067 c->cy = av_malloc(sizeof(c->cy[0]) * order);
00068 c->order = order;
00069
00070 wa = 2 * tan(M_PI * 0.5 * cutoff_ratio);
00071
00072 c->cx[0] = 1;
00073 for(i = 1; i < (order >> 1) + 1; i++)
00074 c->cx[i] = c->cx[i - 1] * (order - i + 1LL) / i;
00075
00076 p[0][0] = 1.0;
00077 p[0][1] = 0.0;
00078 for(i = 1; i <= order; i++)
00079 p[i][0] = p[i][1] = 0.0;
00080 for(i = 0; i < order; i++){
00081 double zp[2];
00082 double th = (i + (order >> 1) + 0.5) * M_PI / order;
00083 double a_re, a_im, c_re, c_im;
00084 zp[0] = cos(th) * wa;
00085 zp[1] = sin(th) * wa;
00086 a_re = zp[0] + 2.0;
00087 c_re = zp[0] - 2.0;
00088 a_im =
00089 c_im = zp[1];
00090 zp[0] = (a_re * c_re + a_im * c_im) / (c_re * c_re + c_im * c_im);
00091 zp[1] = (a_im * c_re - a_re * c_im) / (c_re * c_re + c_im * c_im);
00092
00093 for(j = order; j >= 1; j--)
00094 {
00095 a_re = p[j][0];
00096 a_im = p[j][1];
00097 p[j][0] = a_re*zp[0] - a_im*zp[1] + p[j-1][0];
00098 p[j][1] = a_re*zp[1] + a_im*zp[0] + p[j-1][1];
00099 }
00100 a_re = p[0][0]*zp[0] - p[0][1]*zp[1];
00101 p[0][1] = p[0][0]*zp[1] + p[0][1]*zp[0];
00102 p[0][0] = a_re;
00103 }
00104 c->gain = p[order][0];
00105 for(i = 0; i < order; i++){
00106 c->gain += p[i][0];
00107 c->cy[i] = (-p[i][0] * p[order][0] + -p[i][1] * p[order][1]) /
00108 (p[order][0] * p[order][0] + p[order][1] * p[order][1]);
00109 }
00110 c->gain /= 1 << order;
00111
00112 return c;
00113 }
00114
00115 av_cold struct FFIIRFilterState* ff_iir_filter_init_state(int order)
00116 {
00117 FFIIRFilterState* s = av_mallocz(sizeof(FFIIRFilterState) + sizeof(s->x[0]) * (order - 1));
00118 return s;
00119 }
00120
00121 #define FILTER(i0, i1, i2, i3) \
00122 in = *src * c->gain \
00123 + c->cy[0]*s->x[i0] + c->cy[1]*s->x[i1] \
00124 + c->cy[2]*s->x[i2] + c->cy[3]*s->x[i3]; \
00125 res = (s->x[i0] + in )*1 \
00126 + (s->x[i1] + s->x[i3])*4 \
00127 + s->x[i2] *6; \
00128 *dst = av_clip_int16(lrintf(res)); \
00129 s->x[i0] = in; \
00130 src += sstep; \
00131 dst += dstep; \
00132
00133 void ff_iir_filter(const struct FFIIRFilterCoeffs *c, struct FFIIRFilterState *s, int size, const int16_t *src, int sstep, int16_t *dst, int dstep)
00134 {
00135 int i;
00136
00137 if(c->order == 4){
00138 for(i = 0; i < size; i += 4){
00139 float in, res;
00140
00141 FILTER(0, 1, 2, 3);
00142 FILTER(1, 2, 3, 0);
00143 FILTER(2, 3, 0, 1);
00144 FILTER(3, 0, 1, 2);
00145 }
00146 }else{
00147 for(i = 0; i < size; i++){
00148 int j;
00149 float in, res;
00150 in = *src * c->gain;
00151 for(j = 0; j < c->order; j++)
00152 in += c->cy[j] * s->x[j];
00153 res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1];
00154 for(j = 1; j < c->order >> 1; j++)
00155 res += (s->x[j] + s->x[c->order - j]) * c->cx[j];
00156 for(j = 0; j < c->order - 1; j++)
00157 s->x[j] = s->x[j + 1];
00158 *dst = av_clip_int16(lrintf(res));
00159 s->x[c->order - 1] = in;
00160 src += sstep;
00161 dst += sstep;
00162 }
00163 }
00164 }
00165
00166 av_cold void ff_iir_filter_free_state(struct FFIIRFilterState *state)
00167 {
00168 av_free(state);
00169 }
00170
00171 av_cold void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs)
00172 {
00173 if(coeffs){
00174 av_free(coeffs->cx);
00175 av_free(coeffs->cy);
00176 }
00177 av_free(coeffs);
00178 }
00179
00180 #ifdef TEST
00181 #define FILT_ORDER 4
00182 #define SIZE 1024
00183 int main(void)
00184 {
00185 struct FFIIRFilterCoeffs *fcoeffs = NULL;
00186 struct FFIIRFilterState *fstate = NULL;
00187 float cutoff_coeff = 0.4;
00188 int16_t x[SIZE], y[SIZE];
00189 int i;
00190 FILE* fd;
00191
00192 fcoeffs = ff_iir_filter_init_coeffs(FF_FILTER_TYPE_BUTTERWORTH,
00193 FF_FILTER_MODE_LOWPASS, FILT_ORDER,
00194 cutoff_coeff, 0.0, 0.0);
00195 fstate = ff_iir_filter_init_state(FILT_ORDER);
00196
00197 for (i = 0; i < SIZE; i++) {
00198 x[i] = lrint(0.75 * INT16_MAX * sin(0.5*M_PI*i*i/SIZE));
00199 }
00200
00201 ff_iir_filter(fcoeffs, fstate, SIZE, x, 1, y, 1);
00202
00203 fd = fopen("in.bin", "w");
00204 fwrite(x, sizeof(x[0]), SIZE, fd);
00205 fclose(fd);
00206
00207 fd = fopen("out.bin", "w");
00208 fwrite(y, sizeof(y[0]), SIZE, fd);
00209 fclose(fd);
00210
00211 ff_iir_filter_free_coeffs(fcoeffs);
00212 ff_iir_filter_free_state(fstate);
00213 return 0;
00214 }
00215 #endif