53 int order,
float cutoff_ratio,
62 "low-pass filter mode\n");
67 "even filter orders\n");
71 wa = 2 * tan(
M_PI * 0.5 * cutoff_ratio);
74 for(i = 1; i < (order >> 1) + 1; i++)
75 c->
cx[i] = c->
cx[i - 1] * (order - i + 1LL) /
i;
79 for(i = 1; i <=
order; i++)
80 p[i][0] = p[i][1] = 0.0;
81 for(i = 0; i <
order; i++){
83 double th = (i + (order >> 1) + 0.5) *
M_PI /
order;
84 double a_re, a_im, c_re, c_im;
91 zp[0] = (a_re * c_re + a_im * c_im) / (c_re * c_re + c_im * c_im);
92 zp[1] = (a_im * c_re - a_re * c_im) / (c_re * c_re + c_im * c_im);
94 for(j = order; j >= 1; j--)
98 p[j][0] = a_re*zp[0] - a_im*zp[1] + p[j-1][0];
99 p[j][1] = a_re*zp[1] + a_im*zp[0] + p[j-1][1];
101 a_re = p[0][0]*zp[0] - p[0][1]*zp[1];
102 p[0][1] = p[0][0]*zp[1] + p[0][1]*zp[0];
106 for(i = 0; i <
order; i++){
109 (p[order][0] * p[order][0] + p[order][1] * p[order][1]);
118 float cutoff_ratio,
float stopband)
120 double cos_w0, sin_w0;
126 "high-pass and low-pass filter modes\n");
134 cos_w0 = cos(
M_PI * cutoff_ratio);
135 sin_w0 = sin(
M_PI * cutoff_ratio);
137 a0 = 1.0 + (sin_w0 / 2.0);
140 c->
gain = ((1.0 + cos_w0) / 2.0) /
a0;
141 x0 = ((1.0 + cos_w0) / 2.0) /
a0;
142 x1 = (-(1.0 + cos_w0)) /
a0;
144 c->
gain = ((1.0 - cos_w0) / 2.0) /
a0;
145 x0 = ((1.0 - cos_w0) / 2.0) /
a0;
146 x1 = (1.0 - cos_w0) / a0;
148 c->
cy[0] = (-1.0 + (sin_w0 / 2.0)) / a0;
149 c->
cy[1] = (2.0 * cos_w0) / a0;
162 int order,
float cutoff_ratio,
163 float stopband,
float ripple)
168 if (order <= 0 || order >
MAXORDER || cutoff_ratio >= 1.0)
207 #define CONV_S16(dest, source) dest = av_clip_int16(lrintf(source)); 209 #define CONV_FLT(dest, source) dest = source; 211 #define FILTER_BW_O4_1(i0, i1, i2, i3, fmt) \ 212 in = *src0 * c->gain \ 213 + c->cy[0]*s->x[i0] + c->cy[1]*s->x[i1] \ 214 + c->cy[2]*s->x[i2] + c->cy[3]*s->x[i3]; \ 215 res = (s->x[i0] + in )*1 \ 216 + (s->x[i1] + s->x[i3])*4 \ 218 CONV_##fmt(*dst0, res) \ 223 #define FILTER_BW_O4(type, fmt) { \ 225 const type *src0 = src; \ 227 for (i = 0; i < size; i += 4) { \ 229 FILTER_BW_O4_1(0, 1, 2, 3, fmt); \ 230 FILTER_BW_O4_1(1, 2, 3, 0, fmt); \ 231 FILTER_BW_O4_1(2, 3, 0, 1, fmt); \ 232 FILTER_BW_O4_1(3, 0, 1, 2, fmt); \ 236 #define FILTER_DIRECT_FORM_II(type, fmt) { \ 238 const type *src0 = src; \ 240 for (i = 0; i < size; i++) { \ 243 in = *src0 * c->gain; \ 244 for(j = 0; j < c->order; j++) \ 245 in += c->cy[j] * s->x[j]; \ 246 res = s->x[0] + in + s->x[c->order >> 1] * c->cx[c->order >> 1]; \ 247 for(j = 1; j < c->order >> 1; j++) \ 248 res += (s->x[j] + s->x[c->order - j]) * c->cx[j]; \ 249 for(j = 0; j < c->order - 1; j++) \ 250 s->x[j] = s->x[j + 1]; \ 251 CONV_##fmt(*dst0, res) \ 252 s->x[c->order - 1] = in; \ 258 #define FILTER_O2(type, fmt) { \ 260 const type *src0 = src; \ 262 for (i = 0; i < size; i++) { \ 263 float in = *src0 * c->gain + \ 264 s->x[0] * c->cy[0] + \ 265 s->x[1] * c->cy[1]; \ 266 CONV_##fmt(*dst0, s->x[0] + in + s->x[1] * c->cx[1]) \ 276 const int16_t *
src,
int sstep, int16_t *
dst,
int dstep)
280 }
else if (c->
order == 4) {
289 const float *
src,
int sstep,
float *
dst,
int dstep)
293 }
else if (c->
order == 4) {
330 float cutoff_coeff = 0.4;
336 cutoff_coeff, 0.0, 0.0);
339 for (i = 0; i <
SIZE; i++) {
340 x[
i] =
lrint(0.75 * INT16_MAX * sin(0.5*
M_PI*i*i/SIZE));
345 for (i = 0; i <
SIZE; i++)
346 printf(
"%6d %6d\n", x[i], y[i]);
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
struct FFIIRFilterState FFIIRFilterState
IIR filter state.
static int butterworth_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c, enum IIRFilterMode filt_mode, int order, float cutoff_ratio, float stopband)
av_cold struct FFIIRFilterState * ff_iir_filter_init_state(int order)
Create new filter state.
av_cold struct FFIIRFilterCoeffs * ff_iir_filter_init_coeffs(void *avc, enum IIRFilterType filt_type, enum IIRFilterMode filt_mode, int order, float cutoff_ratio, float stopband, float ripple)
Initialize filter coefficients.
struct FFIIRFilterCoeffs FFIIRFilterCoeffs
IIR filter global parameters.
#define FILTER_DIRECT_FORM_II(type, fmt)
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
static int biquad_init_coeffs(void *avc, struct FFIIRFilterCoeffs *c, enum IIRFilterMode filt_mode, int order, float cutoff_ratio, float stopband)
void av_log(void *avcl, int level, const char *fmt,...)
void ff_iir_filter_flt(const struct FFIIRFilterCoeffs *c, struct FFIIRFilterState *s, int size, const float *src, int sstep, float *dst, int dstep)
Perform IIR filtering on floating-point input samples.
void(* filter_flt)(const struct FFIIRFilterCoeffs *coeffs, struct FFIIRFilterState *state, int size, const float *src, int sstep, float *dst, int dstep)
Perform IIR filtering on floating-point input samples.
#define FILTER_O2(type, fmt)
#define MAXORDER
maximum supported filter order
#define FILTER_BW_O4(type, fmt)
av_cold void ff_iir_filter_free_coeffs(struct FFIIRFilterCoeffs *coeffs)
Free filter coefficients.
void ff_iir_filter(const struct FFIIRFilterCoeffs *c, struct FFIIRFilterState *s, int size, const int16_t *src, int sstep, int16_t *dst, int dstep)
Perform IIR filtering on signed 16-bit input samples.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
synthesis window for stochastic i
static av_always_inline av_const long int lrint(double x)
IIR filter global parameters.
void ff_iir_filter_init_mips(FFIIRFilterContext *f)
void ff_iir_filter_init(FFIIRFilterContext *f)
Initialize FFIIRFilterContext.
common internal and external API header
#define FF_ALLOC_OR_GOTO(ctx, p, size, label)
printf("static const uint8_t my_array[100] = {\n")
else dst[i][x+y *dst_stride[i]]
int main(int argc, char **argv)
av_cold void ff_iir_filter_free_state(struct FFIIRFilterState *state)
Free filter state.
#define FF_ALLOCZ_OR_GOTO(ctx, p, size, label)