/* IIR filter design using bilinear transform and prewarp. Transforms 2nd order s domain analog filter into a digital IIR biquad link. To create a filter fill in a, b, Q and fs and make space for coef and k. Example Butterworth design: Below are Butterworth polynomials, arranged as a series of 2nd order sections: Note: n is filter order. n Polynomials ------------------------------------------------------------------- 2 s^2 + 1.4142s + 1 4 (s^2 + 0.765367s + 1) * (s^2 + 1.847759s + 1) 6 (s^2 + 0.5176387s + 1) * (s^2 + 1.414214 + 1) * (s^2 + 1.931852s + 1) For n=4 we have following equation for the filter transfer function: 1 1 T(s) = --------------------------- * ---------------------------- s^2 + (1/Q) * 0.765367s + 1 s^2 + (1/Q) * 1.847759s + 1 The filter consists of two 2nd order sections since highest s power is 2. Now we can take the coefficients, or the numbers by which s is multiplied and plug them into a standard formula to be used by bilinear transform. Our standard form for each 2nd order section is: a2 * s^2 + a1 * s + a0 H(s) = ---------------------- b2 * s^2 + b1 * s + b0 Note that Butterworth numerator is 1 for all filter sections, which means s^2 = 0 and s^1 = 0 Let's convert standard Butterworth polynomials into this form: 0 + 0 + 1 0 + 0 + 1 --------------------------- * -------------------------- 1 + ((1/Q) * 0.765367) + 1 1 + ((1/Q) * 1.847759) + 1 Section 1: a2 = 0; a1 = 0; a0 = 1; b2 = 1; b1 = 0.765367; b0 = 1; Section 2: a2 = 0; a1 = 0; a0 = 1; b2 = 1; b1 = 1.847759; b0 = 1; Q is filter quality factor or resonance, in the range of 1 to 1000. The overall filter Q is a product of all 2nd order stages. For example, the 6th order filter (3 stages, or biquads) with individual Q of 2 will have filter Q = 2 * 2 * 2 = 8. Arguments: a - s-domain numerator coefficients, a[1] is always assumed to be 1.0 b - s-domain denominator coefficients Q - Q value for the filter k - filter gain factor. Initially set to 1 and modified by each biquad section in such a way, as to make it the coefficient by which to multiply the overall filter gain in order to achieve a desired overall filter gain, specified in initial value of k. fs - sampling rate (Hz) coef - array of z-domain coefficients to be filled in. Note: Upon return from each call, the k argument will be set to a value, by which to multiply our actual signal in order for the gain to be one. On second call to szxform() we provide k that was changed by the previous section. During actual audio filtering k can be used for gain compensation. return -1 if fail 0 if success. */ int af_filter_szxform(const FLOAT_TYPE* a, const FLOAT_TYPE* b, FLOAT_TYPE Q, FLOAT_TYPE fc, FLOAT_TYPE fs, FLOAT_TYPE *k, FLOAT_TYPE *coef) { FLOAT_TYPE at[3]; FLOAT_TYPE bt[3]; if(!a || !b || !k || !coef || (Q>1000.0 || Q< 1.0)) return -1; memcpy(at,a,3*sizeof(FLOAT_TYPE)); memcpy(bt,b,3*sizeof(FLOAT_TYPE)); bt[1]/=Q; /* Calculate a and b and overwrite the original values */ af_filter_prewarp(at, fc, fs); af_filter_prewarp(bt, fc, fs); /* Execute bilinear transform */ af_filter_bilinear(at, bt, k, fs, coef); return 0; }
/* IIR filter design using bilinear transform and prewarp. Transforms 2nd order s domain analog filter into a digital IIR biquad link. To create a filter fill in a, b, Q and fs and make space for coef and k. Example Butterworth design: Below are Butterworth polynomials, arranged as a series of 2nd order sections: Note: n is filter order. n Polynomials ------------------------------------------------------------------- 2 s^2 + 1.4142s + 1 4 (s^2 + 0.765367s + 1) * (s^2 + 1.847759s + 1) 6 (s^2 + 0.5176387s + 1) * (s^2 + 1.414214 + 1) * (s^2 + 1.931852s + 1) For n=4 we have following equation for the filter transfer function: 1 1 T(s) = --------------------------- * ---------------------------- s^2 + (1/Q) * 0.765367s + 1 s^2 + (1/Q) * 1.847759s + 1 The filter consists of two 2nd order sections since highest s power is 2. Now we can take the coefficients, or the numbers by which s is multiplied and plug them into a standard formula to be used by bilinear transform. Our standard form for each 2nd order section is: a2 * s^2 + a1 * s + a0 H(s) = ---------------------- b2 * s^2 + b1 * s + b0 Note that Butterworth numerator is 1 for all filter sections, which means s^2 = 0 and s^1 = 0 Let's convert standard Butterworth polynomials into this form: 0 + 0 + 1 0 + 0 + 1 --------------------------- * -------------------------- 1 + ((1/Q) * 0.765367) + 1 1 + ((1/Q) * 1.847759) + 1 Section 1: a2 = 0; a1 = 0; a0 = 1; b2 = 1; b1 = 0.765367; b0 = 1; Section 2: a2 = 0; a1 = 0; a0 = 1; b2 = 1; b1 = 1.847759; b0 = 1; Q is filter quality factor or resonance, in the range of 1 to 1000. The overall filter Q is a product of all 2nd order stages. For example, the 6th order filter (3 stages, or biquads) with individual Q of 2 will have filter Q = 2 * 2 * 2 = 8. Arguments: a - s-domain numerator coefficients, a[1] is always assumed to be 1.0 b - s-domain denominator coefficients Q - Q value for the filter k - filter gain factor. Initially set to 1 and modified by each biquad section in such a way, as to make it the coefficient by which to multiply the overall filter gain in order to achieve a desired overall filter gain, specified in initial value of k. fs - sampling rate (Hz) coef - array of z-domain coefficients to be filled in. Note: Upon return from each call, the k argument will be set to a value, by which to multiply our actual signal in order for the gain to be one. On second call to szxform() we provide k that was changed by the previous section. During actual audio filtering k can be used for gain compensation. return -1 if fail 0 if success. */ int af_filter_szxform(_ftype_t* a, _ftype_t* b, _ftype_t Q, _ftype_t fc, _ftype_t fs, _ftype_t *k, _ftype_t *coef) { _ftype_t at[3]; _ftype_t bt[3]; if(!a || !b || !k || !coef || (Q>1000.0 || Q< 1.0)) return -1; memcpy(at,a,3*sizeof(_ftype_t)); memcpy(bt,b,3*sizeof(_ftype_t)); bt[1]/=Q; /* Calculate a and b and overwrite the original values */ af_filter_prewarp(at, fc, fs); af_filter_prewarp(bt, fc, fs); /* Execute bilinear transform */ af_filter_bilinear(at, bt, k, fs, coef); return 0; }