Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // variable numbering and array indexing is in reverse chronological order, i.e.
- // 0 is the current sample, 1 is the previous sample, etc
- //
- // x = filter input samples
- // y = filter output samples
- //================== generic biquad filter =====================================================
- struct BiquadCoeffs {
- float b2, b1, b0;
- float a2, a1;
- };
- float filter_biquad( struct BiquadCoeffs const *coeffs,
- float x2, float x1, float x0, float y2, float y1 )
- {
- float y0 = coeffs->b2 * x2
- + coeffs->b1 * x1
- + coeffs->b0 * x0
- - coeffs->a2 * y2
- - coeffs->a1 * y1;
- return y0;
- }
- //================== biquad lowpass filter =====================================================
- //
- // same as above, but optimized for lowpass filters, for which
- // b2 = b0
- // b1 = b0 * 2
- struct LPBiquadCoeffs {
- float b0;
- float a2, a1;
- };
- void filter_lpbiquad( struct BiquadCoeffs const *coeffs,
- float x2, float x1, float x0, float y2, float y1 )
- {
- float y0 = coeffs->b0 * ( x2
- + 2 * x1
- + x0 )
- - coeffs->a2 * y2
- - coeffs->a1 * y1;
- return y0;
- }
- // resample to half the sample rate, using lowpass biquad filter.
- // returns one output sample per two input samples
- //
- float decimate_biquad( struct LPBiquadCoeffs const *coeffs, float state[2],
- float const in[2], float const in_prev[2] )
- {
- state[1] = coeffs->b0 * ( in_prev[1]
- + 2 * in_prev[0]
- + in[1] )
- - coeffs->a2 * state[1]
- - coeffs->a1 * state[0];
- state[0] = coeffs->b0 * ( in_prev[0]
- + 2 * in[1]
- + in[0] )
- - coeffs->a2 * state[0]
- - coeffs->a1 * state[1];
- return state[0];
- }
- // EM100-G IIR filters:
- // (2nd-order Butterworth filters with various cutoff frequencies, 1200 Hz sample rate)
- static struct LPBiquadCoeffs const
- em100_iir_1 = { .b0 = 2.11929601e-3, .a2 = 0.910120873, .a1 = -1.90164369 }, // 18 Hz
- em100_iir_2 = { .b0 = 4.29590264e-4, .a2 = 0.958983150, .a1 = -1.95726479 }, // 8 Hz
- em100_iir_3 = { .b0 = 1.08521907e-4, .a2 = 0.979274600, .a1 = -1.97884051 }, // 4 Hz
- em100_iir_4 = { .b0 = 6.12030910e-5, .a2 = 0.984415081, .a1 = -1.98417027 }, // 3 Hz
- em100_iir_5 = { .b0 = 2.72725213e-5, .a2 = 0.989582759, .a1 = -1.98947367 }, // 2 Hz
- em100_iir_6 = { .b0 = 6.83597982e-6, .a2 = 0.994777708, .a1 = -1.99475036 }, // 1 Hz
- em100_iir_7 = { .b0 = 1.71123201e-6, .a2 = 0.997385432, .a1 = -1.99737859 }, // 0.5 Hz
- em100_iir_8 = { .b0 = 4.28088003e-7, .a2 = 0.998691860, .a1 = -1.99869015 }; // 0.25 Hz
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement