|
If you can't view the Datasheet, Please click here to try to view without PDF Reader . |
|
Datasheet File OCR Text: |
september 2011 doc id 022240 rev 1 1/46 AN3984 application note iir filter design equati ons for sound terminal ? devices introduction the purpose of this document is to provide a tool to calculate the iir filter coefficients to program the sound terminal ? devices from stmicroelectronics. for each filter the procedure and the formulas to calculate the coefficient will be described; the matlab code is given in appendix a: matlab code (functions) on page 24 . a generalized set of equations can be formulated for the design of first-order low-pass and high-pass filters and of second-order filters. a specialized set of equations is devised for designing parametric biquad eq filters. as with any other filter design procedure, the desired characteristics of the filter are to be made available. the parameters governing the characteristics of each filter are: fc: filter cutoff frequency which is the -3db corner frequency or the midpoint frequency in a peak or notch filter fs: sampling frequency q: quality factor (not applicable for low and high-shelf filters) slope: applicable only for low and high-shelf filters gain: the boost or the attenuation at f = fc these parameters can be used to determine the coefficients of the digital filter transfer function. www.st.com
contents AN3984 2/46 doc id 022240 rev 1 contents 1 overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2 filter stability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2.1 definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2.2 first-order filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2.3 second-order filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 3 first-order filter design (lpf and hpf) . . . . . . . . . . . . . . . . . . . . . . . . . . 7 4 second-order filter design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 4.1 low-pass and high-pass filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 4.1.1 low-pass filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 4.1.2 high-pass filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 4.2 peak filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 4.2.1 peak filter - negative gain (cut) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 4.2.2 peak filter - positive gain (boost) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 4.3 shelf filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 4.3.1 low-shelf filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 4.3.2 high-shelf filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 4.4 notch filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 4.5 all-pass filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 4.6 band-pass filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 5 examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 5.1 1 st -order low-pass filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 5.2 1 st -order high-pass filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 5.3 2 nd -order low-pass filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 5.4 2 nd -order high-pass filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 5.5 low-shelf filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 5.6 high-shelf filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 5.7 notch filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 5.8 all-pass filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 5.9 band-pass filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 AN3984 contents doc id 022240 rev 1 3/46 appendix a matlab code (functions). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 a.1 code structure. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 a.2 peak filter (peakfilterapw.m) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 a.3 low-pass and high-pass filter (lhpassfilterapw.m) . . . . . . . . . . . . . . . . 27 a.4 low and high-shelf filter (shelffilterapw.m) . . . . . . . . . . . . . . . . . . . . . . . 30 a.5 notch filter (notchfilterapw.m) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 a.6 all-pass filter (allpassfilterapw.m). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 a.7 band-pass filter (bandpassfilterapw.m) . . . . . . . . . . . . . . . . . . . . . . . . . 37 a.8 float to hex conversion (myfloat2hex.m) . . . . . . . . . . . . . . . . . . . . . . . . . 39 a.9 max coefficient limit value calculator (lim itval.m) . . . . . . . . . . . . . . . . . . . 40 a.10 display coefficient and error messages (display_coeff.m). . . . . . . . . . . . 41 appendix b abbreviations and acronyms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 6 revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 list of figures AN3984 4/46 doc id 022240 rev 1 list of figures figure 1. 1 st -order low-pass filter - magnitude response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 figure 2. 1 st -order high-pass filter - magnitude response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 figure 3. 2 nd -order low-pass filter - magnitude response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 figure 4. 2 nd -order high-pass filter - magnitude response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 figure 5. low-shelf filter - magnitude response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 figure 6. high-shelf filter - magnitude response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 figure 7. notch filter - magnitude response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 figure 8. all-pass filter - phase response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 figure 9. band-pass filter - magnitude response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 3 figure 10. code structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 AN3984 overview doc id 022240 rev 1 5/46 1 overview the transfer function for a first-order filter in the digital z-domain is: equation 1 for a second-order filter (a biquad) the transfer function is: equation 2 this equation can be modified normalizing the a 0 coefficient; the new equation is: equation 3 the most straightforward implementation form using equation 3 is: equation 4 1 1 0 1 1 0 z a a z b b ) z ( h ? ? ? + ? + = 2 2 1 1 0 2 2 1 1 0 ) ( ? ? ? ? ? + ? + ? + ? + = z a z a a z b z b b z h () () () () () 2 0 2 1 0 1 2 0 2 1 0 1 0 0 1 ) ( ? ? ? ? ? + ? + ? + ? + = z a a z a a z a b z a b a b z h () () () () () ] 2 n [ y a a ] 1 n [ y a a ] 2 n [ x a b ] 1 n [ x a b ] n [ x a b ] n [ y 0 2 0 1 0 2 0 1 0 0 ? ? ? ? ? ? ? ? + ? ? + ? = filter stability AN3984 6/46 doc id 022240 rev 1 2 filter stability 2.1 definition a filter is said to be stable in the z-domain if the roots (or poles) of the filter lie inside the unit circle. this definition of stability can be translat ed in terms of the filter coefficients. 2.2 first-order filter for a first-order filter, the stabilit y condition that needs to be satisfied is that the pole of the filter lies within the unit circle. in terms of the coefficients, the condition can be given as: equation 5 2.3 second-order filter for a 2 nd -order filter, two cond itions must be satisfied to ensu re filter stabilit y and translated in terms of the filter coefficients they are: equation 6 1 1 < a ) 1 ( 1 2 1 2 a a a + < < AN3984 first-order filter design (lpf and hpf) doc id 022240 rev 1 7/46 3 first-order filter design (lpf and hpf) the preliminary step to obtain the coefficients for the first-order low-pass filter or high-pass filter is to define three constants obtained from the filter parameters: equation 7 in a first-order filter both the coefficients a 2 and b 2 are null. the denominator coefficients are identical for both an lpf and an hpf designed for the same cutoff frequency and they are computed as follows: equation 8 the numerator for an lpf can be calculated as follows: equation 9 the numerator for an hpf can be calculated as follows: equation 10 the coefficient used in apworkbench can be calculated by applying these formulas: equation 11 () k k f f c s c c + = = ? ? = 1 2 tan 2 () k a a ? ? = = 1 1 1 0 k b k b = = 1 0 1 1 1 0 ? = = b b () () () () 0 0 0 0 2 2 0 1 1 0 2 2 0 1 1 2 2 2 2 2 2 a b b t coefficien a a t a coefficien a a a t coefficien a b t b coefficien a b b t coefficien = ? = ? = = = second-order filter design AN3984 8/46 doc id 022240 rev 1 4 second-order filter design 4.1 low-pass and high-pass filters the preliminary step to obtain the coefficients for a second-order filter is the calculation of these coefficients obtained from the filter parameters: equation 12 the denominator coefficients are the same for both an lpf and an hpf if designed for the same cutoff frequency. they are computed as follows: equation 13 4.1.1 low-pass filter the numerator coefficient for a second-o rder lpf can be calculated as follows: equation 14 () w q k de k k w k f f c s c c + + = + = = = ? ? = 1 1 2 tan 2 2 ? () de w q k a de w a a + ? = ? ? = = 1 1 2 1 2 1 0 de w b de w b de w b = ? = = 2 1 0 2 AN3984 second-order filter design doc id 022240 rev 1 9/46 for a second-order lpf, the coefficients given in apworkbench can be calculated as follows: equation 15 4.1.2 high-pass filter the numerator coefficient for a second-order hpf can be calculated as follows: equation 16 for a second-order hpf, the coefficients given in apworkbench can be calculated as follows: equation 17 de w b t coefficien de w q k t a coefficien de w a t coefficien de w t b coefficien de w b t coefficien ? = + ? ? = ? ? ? = = = 2 1 2 1 1 1 2 2 0 2 1 2 1 de b de w b de b 1 2 1 2 1 0 = ? ? = = de b t coefficien de w q k t a coefficien de w a t coefficien de t b coefficien de b t coefficien 1 2 1 2 1 1 1 2 1 1 2 0 2 1 2 1 ? = + ? ? = ? ? ? = = ? = second-order filter design AN3984 10/46 doc id 022240 rev 1 4.2 peak filters the first step is the calculation of the constant gain obtained from the gain filter parameter (gdb is expressed in db). equation 18 the filter coefficients are different if the gain is positive or negative. 4.2.1 peak filter - negative gain (cut) the cut value is calculated with the following equation: equation 19 the filter coefficient can be calculated as follows: equation 20 ) 115129254 . 0 exp( ? = db gain gain w gain q k cutvalue + ? ? ? ? ? ? ? + = 1 () () cutvalue w q k b cutvalue w b cutvalue w q k b cutvalue w gain q a cutvalue w a a ) 1 ( 1 2 1 1 1 4 1 2 1 0 2 1 0 + ? = ? ? = ? ? ? ? ? ? ? ? + + = ? ? ? ? ? ? ? ? ? ? ? ? + ? = ? ? = = AN3984 second-order filter design doc id 022240 rev 1 11/46 the coefficients in the apworkbench are consequently calculated as follows: equation 21 4.2.2 peak filter - positive gain (boost) the boost value is calculated with the following equation: equation 22 the filter coefficient can be calculated as follows: equation 23 cutvalue w q k b t coefficien cutvalue w k gain q t a coefficien cutvalue w a t coefficien cutvalue w q k t b coefficien cutvalue w b t coefficien + + ? = + ? ? ? = ? = + ? = ? = 1 2 1 2 1 1 2 ) 1 ( 1 2 0 2 1 2 1 w q k boostvalue + + = 1 boostvalue w q gain k b boostvalue w b boostvalue w q gain k b boostvalue w q k a boostvalue w q gain k a a ) 1 ( 1 2 ) 1 ( 2 1 ) 1 ( 1 2 1 0 2 1 0 + ? ? = ? ? = + ? + ? = ? ? ? ? ? ? ? ? + ? = + ? + = = second-order filter design AN3984 12/46 doc id 022240 rev 1 the coefficients in the apworkbench are consequently calculated as follows: equation 24 4.3 shelf filters the coefficient gain is defined in equation 25 . equation 25 the coefficients and are calculated as follows: equation 26 4.3.1 low-shelf filter the coefficients for an lsf can be calculated as follows: equation 27 the coefficient to load in apworkbench can be calculated by applying the calculation already shown in equation 11 . boostvalue w k q gain b t coefficien boostvalue w q k t a coefficien boostvalue w a t coefficien boostvalue w k q gain t b coefficien boostvalue w b t coefficien + ? + ? = + ? ? = ? = + ? ? = ? = 1 2 1 2 1 1 2 ) 1 ( 1 2 0 2 1 2 1 ) 40 ( 10 db ga in gain = () gain s gain gain c ? ? = ? ? ? ? ? ? ? ? + ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? + ? = ? 2 2 1 1 1 2 sin ()() ()() ()() ()() () ()() () ()() () ? ? ? ? ? ? ? ? ? ? + ? = ? + ? ? ? ? = + ? ? ? + ? = ? ? ? + + = ? + + ? ? ? = + ? ? + + = c c c c c c gain gain gai n b gai n gain gain b gain gain gai n b gain gain a ga in gain a gain gain a co s 1 1 cos 1 1 2 cos 1 1 cos 1 1 cos 1 1 2 cos 1 1 2 1 0 2 1 0 AN3984 second-order filter design doc id 022240 rev 1 13/46 4.3.2 high-shelf filter the coefficients for an hsf can be calculated as follows: equation 28 the coefficient to load in apworkbench to program a hsf can be computed by applying the formulas shown in equation 11 . 4.4 notch filter the first step is to define the constant (a) : equation 29 the coefficients for a notch filter can be calculated as follows: equation 30 the coefficients to load in apworkbench can be calculated using equation 11 . ()() ()() ()() ()() () ()() () ()() () ? ? ? ? ? ? ? ? ? + + ? = ? + ? ? ? ? ? = + ? ? + + ? = ? ? ? ? + = ? + ? ? ? = + ? ? ? + = c c c c c c gain gain gai n b gai n gain gain b gain gain gai n b gain gain a gai n gain a gain gain a cos 1 1 cos 1 1 2 co s 1 1 co s 1 1 cos 1 1 2 cos 1 1 2 1 0 2 1 0 a. ? c is defined in equation 12 () q c ? = 2 sin ? 1 cos 2 1 1 cos 2 1 2 1 0 2 1 0 = ? ? = = ? = ? ? = + = b b b a a a c ? ? second-order filter design AN3984 14/46 doc id 022240 rev 1 4.5 all-pass filter equation 29 allows calculating the constant . the coefficients for an apf can be calculated as follows: equation 31 the coefficients to load in apworkbench can be calculated using equation 11 . 4.6 band-pass filter equation 29 allows calculating the constant while equation 32 is used to calculate the normalized gain. equation 32 the coefficients for a bpf can be calculated as follows (b) : equation 33 the coefficients to load in apworkbench can be calculated using equation 11 . ? ? + = = ? ? = ? = ? = ? ? = + = 1 cos 2 1 1 cos 2 1 2 1 1 0 2 1 0 b a b b a a a c c b. is defined in equation 29 , ? c is defined in equation 12 . ? ? ? ? ? ? = 20 10 db ga in normgain normgain b b b normgain b a a a c ? ? = ? = = ? = ? = ? ? = + = ? 0 2 1 0 2 1 0 0 1 cos 2 1 AN3984 examples doc id 022240 rev 1 15/46 5 examples 5.1 1 st -order low-pass filter input data: cutoff freq: 1 khz coefficient range: 4 processing frequency: 96 khz output data: figure 1. 1 st -order low-pass filter - magnitude response filter coefficients 'coeff 1: b1/2' 'coeff 2: b2' 'coeff 3: -a1/2' 'coeff 4: -a2' 'coeff 5: b0/2' '0081d6' '000000' '0efc52' '000000' '0081d6' 10 -1 10 0 10 1 -20 -15 -10 -5 0 frequency (khz) magnitude (db) magnitude response (db) examples AN3984 16/46 doc id 022240 rev 1 5.2 1 st -order high-pass filter input data: cutoff freq: 1 khz coefficient range: 4 processing frequency: 96 khz output data: figure 2. 1 st -order high-pass filter - magnitude response filter coefficients 'coeff 1: b1/2' 'coeff 2: b2' 'coeff 3: -a1/2' 'coeff 4: -a2' 'coeff 5: b0/2' 'f081d6' '000000' '0efc52' '000000' '0f7e29' 10 -1 10 0 10 1 -20 -15 -10 -5 0 frequency (khz) magnitude (db) magnitude response (db) AN3984 examples doc id 022240 rev 1 17/46 5.3 2 nd -order low-pass filter input data: cutoff freq: 1 khz coefficient range: 4 quality factor (q): 2 processing frequency: 96 khz output data: figure 3. 2 nd -order low-pass filter - magnitude response filter coefficients 'coeff 1: b1/2' 'coeff 2: b2' 'coeff 3: -a1/2' 'coeff 4: -a2' 'coeff 5: b0/2' '0008a0' '0008a0' '1f6af3' 'e10794' '000450' 10 -1 10 0 10 1 -40 -35 -30 -25 -20 -15 -10 -5 0 5 10 15 frequency (khz) magnitude (db) magnitude response (db) examples AN3984 18/46 doc id 022240 rev 1 5.4 2 nd -order high-pass filter input data: cutoff freq: 1 khz coefficient range: 4 quality factor (q): 2 processing frequency: 96 khz output data: figure 4. 2 nd -order high-pass filter - magnitude response filter coefficients 'coeff 1: b1/2' 'coeff 2: b2' 'coeff 3: -a1/2' 'coeff 4: -a2' 'coeff 5: b0/2' 'e08c6b' '1f7394' '1f6af3' 'e10794' '0fb9ca' 10 -1 10 0 10 1 -40 -30 -20 -10 0 10 20 frequency (khz) magnitude (db) magnitude response (db) AN3984 examples doc id 022240 rev 1 19/46 5.5 low-shelf filter input data: cutoff freq: 1 khz gain: -10 db coefficient range: 4 slope: 2 processing frequency: 96 khz output data: figure 5. low-shelf filter - magnitude response filter coefficients 'coeff 1: b1/2' 'coeff 2: b2' 'coeff 3: -a1/2' 'coeff 4: -a2' 'coeff 5: b0/2' 'e0f9f2' '1e8e49' '1efbb2' 'e1cc06' '0fc87d' 10 -1 10 0 10 1 -20 -15 -10 -5 0 5 frequency (khz) magnitude (db) magnitude response (db) examples AN3984 20/46 doc id 022240 rev 1 5.6 high-shelf filter input data: cutoff freq: 1 khz gain: -10 db coefficient range: 4 slope: 2 processing frequency: 96 khz output data: figure 6. high-shelf filter - magnitude response filter coefficients 'coeff 1: b1/2' 'coeff 2: b2' 'coeff 3: -a1/2' 'coeff 4: -a2' 'coeff 5: b0/2' 'f61151' '09aea8' '1f732a' 'e1063e' '052110' 10 -1 10 0 10 1 -15 -10 -5 0 5 frequency (khz) magnitude (db) magnitude response (db) AN3984 examples doc id 022240 rev 1 21/46 5.7 notch filter input data: cutoff freq: 1 khz quality factor: 0.5 coefficient range: 4 processing frequency: 96 khz output data: figure 7. notch filter - magnitude response filter coefficients 'coeff 1: b1/2' 'coeff 2: b2' 'coeff 3: -a1/2' 'coeff 4: -a2' 'coeff 5: b0/2' 'e2075a' '1e091b' '1df8a5' 'e3edc8' '0f048d' 10 -1 10 0 10 1 -60 -50 -40 -30 -20 -10 0 frequency (khz) magnitude (db) magnitude response (db) examples AN3984 22/46 doc id 022240 rev 1 5.8 all-pass filter input data: cutoff freq: 1 khz quality factor: 5 coefficient range: 4 processing frequency: 96 khz output data: figure 8. all-pass filter - phase response filter coefficients 'coeff 1: b1/2' 'coeff 2: b2' 'coeff 3: -a1/2' 'coeff 4: -a2' 'coeff 5: b0/2' 'e046a7' '200000' '1fb958' 'e06a75' '0fcac5' 10 -1 10 0 10 1 -6 -5 -4 -3 -2 -1 0 frequency (khz) phase (radians) phase response AN3984 examples doc id 022240 rev 1 23/46 5.9 band-pass filter input data: cutoff freq: 1 khz gain: +6 db quality factor: 3 coefficient range: 4 processing frequency: 96 khz output data: figure 9. band-pass filter - magnitude response filter coefficients 'coeff 1: b1/2' 'coeff 2: b2' 'coeff 3: -a1/2' 'coeff 4: -a2' 'coeff 5: b0/2' '000000' 'ff4fc0' '1f9650' 'e0b0ab' '00581f' 10 -1 10 0 10 1 -40 -35 -30 -25 -20 -15 -10 -5 0 5 10 frequency (khz) magnitude (db) magnitude response (db) matlab code (functions) AN3984 24/46 doc id 022240 rev 1 appendix a matlab code (functions) a.1 code structure figure 10. code structure start filter data (cutoff freq, gain,?) coefficient calculation coefficient limit calculation float to hex conversion coefficients and filter stability check display coefficient end AN3984 matlab code (functions) doc id 022240 rev 1 25/46 a.2 peak filter (peakfilterapw.m) %-------------------------------------------------------------------------% % function [coeff_hex, coeffapw, limitval] = peakfilterapw(fc, gain, q, % coeffrange, fs) % % args:fc -> cutoff frequency % gain -> gain % q -> quality factor % coeffrange -> coefficient range (1, 2 or 4) % fs -> sample frequency % return: coeff_hex -> apw filter coeff - hex % coeffapw -> apw filter coeff - floating point % limitval -> limit coeff value % description: generates the apworkbench coeff for a peak filter % % stmicroelectronics - agrate (italy) % msh - audio & sound bu % revision: 1.1 % date: 23 june 2011 %-------------------------------------------------------------------------% %% function code function [coeff_hex, coeffapw, limitvalue] = peakfilterapw(fc, gain, q, ... coeffrange, fs) format long if (nargin <5) fs = 96000; end teta = (2*pi*fc)/fs; %angle from frequency k = tan(teta/2); w = k*k; %% process gain gain = gain* 0.115129254; normgain = exp(gain); matlab code (functions) AN3984 26/46 doc id 022240 rev 1 %% coefficint calculation if normgain<1 % negative normgain - cut fcutvalue = 1+(1/normgain/q)*k+w; % boost/normgain coeff_4 = ((1+(1/q)*k+w)/fcutvalue)/2.0; % b0/2 coeff_0 = (w-1)/fcutvalue; % b1/2 coeff_1 = (1-(1/q)*k+w)/fcutvalue; % b2 coeff_3 = ((1-(1/normgain/q)*k+w)/fcutvalue)*-1.0; % -a2 coeff_2 = (coeff_0)*-1.0; % -a1/2 else % positive normgain - boost fboostvalue = 1+(1/q)*k+w; % boost/normgain coeff_4 = ((1+(normgain/q)*k+w)/fboostvalue)/2.0; % b0/2 coeff_0 = (w-1)/fboostvalue; % b1/2 coeff_1 = (1-(normgain/q)*k+w)/fboostvalue; % b2 coeff_3 = ((1-(1/q)*k+w)/fboostvalue)*-1.0; % -a2 coeff_2 =(coeff_0)*-1.0; % -a1/2 end %% coefficient matrix coeffapw = [coeff_0 coeff_1 coeff_2 coeff_3 coeff_4]; %% coefficient limit value limitvalue = limitval(coeffrange); %% coefficient matrix - hex format coeff_hex = myfloat2hex(coeffapw, coeffrange); AN3984 matlab code (functions) doc id 022240 rev 1 27/46 a.3 low-pass and high-pass filter (lhpassfilterapw.m) %-------------------------------------------------------------------------% % function [coeff_hex, coeffapw] = lhpassfilterapw(cutoff_freq, q, % ftype, order, coeffrange, fc) % args:fc -> cutoff frequency % q -> quality factor % ftype -> 0->lowpassfilter; 1->highpass filter % order -> 1=1st order; 2=2nd order % coeffrange -> coefficient range (1, 2 or 4) % fc -> sample frequency % return: coeff_hex -> apw filter coeff - hex % coeffapw -> apw filter coeff - floating point % limitval -> limit coeff value % description: generates the apworkbench coeff for a lhpassfilter % % stmicroelectronics - agrate (italy) % msh - audio & sound bu % revision: 1.1 % date: 23 june 2011 %-------------------------------------------------------------------------% %% function code function [coeff_hex, coeffapw, limitvalue] = lhpassfilterapw(fc, q, ... ftype, order, coeffrange, fs) format long if (nargin <6) fs = 96000; end teta = (2*pi*fc)/fs; %angle from frequency k = tan(teta/2); alpha = 1+k; a2 = 0; b2 = 0; a0 = 1.0; a1 = -(1-k)/alpha; matlab code (functions) AN3984 28/46 doc id 022240 rev 1 if order == 1 %1st order filter if ftype ==0 % low pass filter b0 = k/alpha; b1 = b0; coeff_0 = (b1/2.0)/a0; coeff_1 = (b2)/a0; % always =0!!! coeff_2 = (-a1/2.0)/a0; coeff_3 = (-a2)/a0; % always =0!!! coeff_4 = (b0/2.0)/a0; else % high pass filter b0 = 1/alpha; b1 = -b0; coeff_0 = (b1/2.0)/a0; coeff_1 = (b2)/a0; % always =0!!! coeff_2 = (-a1/2.0)/a0; coeff_3 = (-a2)/a0; % always =0!!! coeff_4 = (b0/2.0)/a0; end else % 2nd order filter teta = (2*pi*fc)/fs; %angle from frequency k = tan(teta/2); w = k*k; de = 1+(1/q)*k+w; coeff_3 = ((1-(1/q)*k+w)/de)*-1.0; % -a2 coeff_2 = ((w-1)/de)*-1.0; % -a1/2 if ftype ==0 % low pass filter 2nd order coeff_4 = (w/de)/2.0; % b0/2 coeff_0 = w/de; % b1/2 coeff_1 = w/de; % b2 else % high pass filter 2nd order coeff_4 = (1/de)/2.0; % b0/2 coeff_0 = -1/de; % b1/2 AN3984 matlab code (functions) doc id 022240 rev 1 29/46 coeff_1 = 1/de; % b2 end end %% coefficient matrix coeffapw = [coeff_0 coeff_1 coeff_2 coeff_3 coeff_4]; %% coefficient limit value limitvalue = limitval(coeffrange); %% coefficient matrix - hex format coeff_hex = myfloat2hex(coeffapw, coeffrange); matlab code (functions) AN3984 30/46 doc id 022240 rev 1 a.4 low and high-shelf filter (shelffilterapw.m) %-------------------------------------------------------------------------% % function [coeffapw] = shelffilterapw(fc, gain, slope, ftype, % coeffrange, fs) % args:fc -> cutoff frequency % gain -> gain % slope -> slope % ftype -> filter type (low or high shelf) % coeffrange -> coefficient range (1, 2 or 4) % fs -> sample frequency % return: coeff_hex -> apw filter coeff - hex % coeffapw -> apw filter coeff - floating point % limitval -> limit coeff value % description: generates apworkbench coeff for a low or a high % shelf filter % % stmicroelectronics - agrate (italy) % msh - audio & sound bu % revision: 1.1 % date: 23 june 2011 %-------------------------------------------------------------------------% %% function code function [coeff_hex, coeffapw, limitvalue] = shelffilterapw(fc, gain, ... slope, ftype,... coeffrange, fs) format long % if ftype=0 =>lowshelf % if ftype=1 =>highshelf if (nargin < 6) fs = 96000; end teta = (2*pi*fc)/fs; %angle from frequency sinteta = sin(teta); AN3984 matlab code (functions) doc id 022240 rev 1 31/46 costeta = cos(teta); % normalized gain normgain = 10^(gain/40); % alpha and beta alpha = (sinteta/2)*sqrt((normgain+(1/normgain))*(1.0/slope-1.0)+2.0); beta = 2*sqrt(normgain)*alpha; %% coefficient calculation if ftype == 0 % ftype = 0 => lowshelf b0 = normgain*((normgain+1)-(normgain-1)*costeta + beta); b1 = 2*normgain*((normgain-1)-(normgain+1)*costeta); b2 = normgain*((normgain+1)-(normgain-1)*costeta - beta); a0 = (normgain+1)+(normgain-1)*costeta + beta; a1 = -2*((normgain-1)+(normgain+1)*costeta); a2 = (normgain+1)+(normgain-1)*costeta-beta; else % ftype = 1 => highshelf b0 = normgain*((normgain+1)+(normgain-1)*costeta + beta); b1 = -2*normgain*((normgain-1)+(normgain+1)*costeta); b2 = normgain*((normgain+1)+(normgain-1)*costeta - beta); a0 = (normgain+1)-(normgain-1)*costeta + beta; a1 = 2*((normgain-1)-(normgain+1)*costeta); a2 = (normgain+1)-(normgain-1)*costeta-beta; end % apw coefficient - reworked coefficient coeff_0 = (b1/2.0)/a0; coeff_1 = (b2)/a0; coeff_2 = (-a1/2.0)/a0; coeff_3 = (-a2)/a0; coeff_4 = (b0/2.0)/a0; matlab code (functions) AN3984 32/46 doc id 022240 rev 1 %% coefficient matrix coeffapw = [coeff_0 coeff_1 coeff_2 coeff_3 coeff_4]; %% coefficient limit value limitvalue = limitval(coeffrange); %% coefficient matrix - hex format coeff_hex = myfloat2hex(coeffapw, coeffrange); AN3984 matlab code (functions) doc id 022240 rev 1 33/46 a.5 notch filter (notchfilterapw.m) %-------------------------------------------------------------------------% % function [filter_coeff, coeffapw] = notchfilterapw(fc, q, coeffrange, % fs) % args:fc -> cutoff frequency % gain -> gain % q -> quality factor % coeffrange -> coefficient range (1, 2 or 4) % fs -> sample frequency % return: coeff_hex -> apw filter coeff - hex % coeffapw -> apw filter coeff - floating point % limitval -> limit coeff value % % description: generates the apworkbench coeff for a notch filter % % stmicroelectronics - agrate (italy) % msh - audio & sound bu % revision: 1.1 % date: 23 june 2011 %-------------------------------------------------------------------------% %% function code function [coeff_hex, coeffapw, limitvalue] = notchfilterapw(fc, q, ... coeffrange, fs) format long if (nargin == 3) fs = 96000; end teta = (2*pi*fc)/fs; %angle from frequency sinteta = sin(teta); costeta = cos(teta); alpha = sinteta/(2*q); %% coefficint calculation b0 = 1; b1 = -2*costeta; matlab code (functions) AN3984 34/46 doc id 022240 rev 1 b2 = 1; a0 = 1+alpha; a1 = -2*costeta; a2 = 1-alpha; % apw coefficient - reworked coefficient coeff_0 = (b1/2.0)/a0; coeff_1 = (b2)/a0; coeff_2 = (-a1/2.0)/a0; coeff_3 = (-a2)/a0; coeff_4 = (b0/2.0)/a0; %% coefficient matrix coeffapw = [coeff_0 coeff_1 coeff_2 coeff_3 coeff_4]; %% coefficient limit value limitvalue = limitval(coeffrange); %% coefficient matrix - hex format coeff_hex = myfloat2hex(coeffapw, coeffrange); AN3984 matlab code (functions) doc id 022240 rev 1 35/46 a.6 all-pass filter (allpassfilterapw.m) %-------------------------------------------------------------------------% % function [coeff_hex, coeffapw] = allpassfilterapw(fc, q, coeffrange, % fs) % args:fc -> cutoff frequency % q -> quality factor % coeffrange -> coefficient range (1, 2 or 4) % fs -> sample frequency % return: coeff_hex -> apw filter coeff - hex % coeffapw -> apw filter coeff - floating point % limitval -> limit coeff value % % description: generates the apworkbench coeff for a all pass filter % % stmicroelectronics - agrate (italy) % msh - audio & sound bu % revision: 1.1 % date: 23 june 2011 %-------------------------------------------------------------------------% %% function code function [coeff_hex, coeffapw, limitvalue] = allpassfilterapw(fc, q, ... coeffrange, fs) format long if (nargin < 4) fs = 96000; end teta = (2*pi*fc)/fs; %angle from frequency sinteta = sin(teta); costeta = cos(teta); alpha = sinteta/(2*q); %% coefficint calculation b0 = 1-alpha; b1 = -2*costeta; b2 = 1+alpha; matlab code (functions) AN3984 36/46 doc id 022240 rev 1 a0 = 1+alpha; a1 = b1; a2 = 1-alpha; % apw coefficient - reworked coefficient coeff_0 = (b1/2.0)/a0; coeff_1 = (b2)/a0; coeff_2 = (-a1/2.0)/a0; coeff_3 = (-a2)/a0; coeff_4 = (b0/2.0)/a0; %% coefficient matrix coeffapw = [coeff_0 coeff_1 coeff_2 coeff_3 coeff_4]; %% coefficient limit value limitvalue = limitval(coeffrange); %% coefficient matrix - hex format coeff_hex = myfloat2hex(coeffapw, coeffrange); AN3984 matlab code (functions) doc id 022240 rev 1 37/46 a.7 band-pass filter (bandpassfilterapw.m) %-------------------------------------------------------------------------% % function [filter_coeff, coeffapw] = bandpassfilterapw(fc, q, coeffrange, % fs) % args:fc -> cutoff frequency % gain -> gain % q -> quality factor % coeffrange -> coefficient range (1, 2 or 4) % fs -> sample frequency % return: coeff_hex -> apw filter coeff - hex % coeffapw -> apw filter coeff - floating point % limitval -> limit coeff value % % description: generates the apworkbench coeff for a band pass filter % % stmicroelectronics - agrate (italy) % msh - audio & sound bu % revision: 1.1 % date: 23 june 2011 %-------------------------------------------------------------------------% %% function code function [coeff_hex, coeffapw, limitvalue] = bandpassfilterapw(fc, gain,... q, coeffrange, fs) format long if (nargin == 3) fs = 96000; end teta = (2*pi*fc)/fs; %angle from frequency sinteta = sin(teta); costeta = cos(teta); alpha = sinteta/(2*q); normgain = 10^(gain/20); %% coefficint calculation matlab code (functions) AN3984 38/46 doc id 022240 rev 1 b0 = alpha*normgain; b1 = 0; b2 = -b0; a0 = 1+alpha; a1 = -2*costeta; a2 = 1-alpha; % apw coefficient - reworked coefficient coeff_0 = (b1/2.0)/a0; coeff_1 = (b2)/a0; coeff_2 = (-a1/2.0)/a0; coeff_3 = (-a2)/a0; coeff_4 = (b0/2.0)/a0; %% coefficient matrix coeffapw = [coeff_0 coeff_1 coeff_2 coeff_3 coeff_4]; %% coefficient limit value limitvalue = limitval(coeffrange); %% coefficient matrix - hex format coeff_hex = myfloat2hex(coeffapw, coeffrange); AN3984 matlab code (functions) doc id 022240 rev 1 39/46 a.8 float to hex conversion (myfloat2hex.m) %-------------------------------------------------------------------------% % function [floatn] = myfloat2hex(hexn, range) % args:hexn -> hexadecimal number to be converted in string format % without the 0x, i.e. 0x123456 => '123456' (24 bits % only) % range -> coefficients range 4, 2, 1 % return: floatn -> floating point notation number % % description: converts a fixed point hexadecimal number into a % floating point one % % stmicroelectronics - agrate (italy) % msh - audio & sound bu % revision: 1.1 % date: 23 june 2011 %-------------------------------------------------------------------------% function [hexn] = myfloat2hex(floatn, range) format long quantizersetup.mode = 'fixed'; % quantizersetup.roundmode = 'nearest'; quantizersetup.roundmode = 'ceil'; quantizersetup.overflowmode = 'saturate'; %quantizer to translate from hex to num if(range == 1) quantizerquantizersetup.format = ([24 23]); elseif(range == 2) quantizerquantizersetup.format = ([24 22]); elseif(range == 4); quantizerquantizersetup.format = ([24 21]); end q = quantizer(quantizerquantizersetup); matlab code (functions) AN3984 40/46 doc id 022240 rev 1 hexn = num2hex(q,floatn); a.9 max coefficient limit value calculator (limitval.m) %-------------------------------------------------------------------------% % function [limitvalue] = limitval(coeffrange) % % args:coeffrange -> apw filter coeff range % % return: limitvalue -> apw filter limit value % % description: from the coeffrange it calculates the limitvalue % % stmicroelectronics - agrate (italy) % msh - audio & sound bu % revision: 1.1 % date: 23 june 2011 %-------------------------------------------------------------------------% %% function code function [limitvalue] = limitval(coeffrange) format long switch coeffrange case 1 % coefficient +/- 1 limitvalue = 0.99999; case 2 % coefficient +/- 2 limitvalue = 1.99999; case 4 % coefficient +/- 4 limitvalue = 3.99999; end AN3984 matlab code (functions) doc id 022240 rev 1 41/46 a.10 display coefficient and er ror messages (display_coeff.m) %-------------------------------------------------------------------------% % function []=display_coeff(filter_coeff, coeffapw, limitvalue) % args:filter_coeff -> apw filter coeff - hex % coeffapw -> apw filter coeff - floating % limitval -> limit coeff value % return: display -> apw filter coeff - hex % % description: display filter coefficient (hex) % % stmicroelectronics - agrate (italy) % msh - audio & sound bu % revision: 1.1 % date: 23 june 2011 %-------------------------------------------------------------------------% function []=display_coeff(filter_coeff, coeffapw, limitvalue) a0 = 1; a1 =-2*coeffapw(3); a2 = -coeffapw(4); b0 =2*coeffapw(5); b1 =2*coeffapw(1); b2 = coeffapw(2); coeff = [b0 b1 b2 a0 a1 a2]; %% check for stability and limit error = 0; if ((abs(b0)>=limitvalue) || (abs(b1)>=limitvalue)|| (abs(b2)>=limitvalue)) error = 1; end if abs(a2)>1 && (abs(a1)>1+a2) error = 2; end matlab code (functions) AN3984 42/46 doc id 022240 rev 1 counter = 1; while counter<=5 if imag(coeffapw(counter))~=0 error = 3; end counter = counter+1; end % filter coefficiners or error message. switch error case 0 % no error h=fvtool(coeff(1:3),coeff(4:6)); disp(' '); disp(' '); disp('****************************************************************************** *****************'); disp(' filter coefficients'); disp('****************************************************************************** *****************'); label = {'coeff 1: b1/2', 'coeff 2: b2', 'coeff 3: -a1/2', 'coeff 4: -a2', 'coeff 5: b0/2'}; table_data = {filter_coeff(1,:) filter_coeff(2,:) filter_coeff(3,:) ... filter_coeff(4,:) filter_coeff(5,:)}; % table_data = num2cell(table_data); table = [label; table_data]; disp (table); case 1 % the coefficient range must be changed disp(' '); disp(' '); disp('****************************************************************************** *****************'); disp(' error!!!'); disp(' the coefficient range must be increased'); disp('****************************************************************************** *****************'); AN3984 matlab code (functions) doc id 022240 rev 1 43/46 case 2 % the filter is not stable disp(' '); disp(' '); disp('****************************************************************************** *****************'); disp(' error!!! the filter is not stable!'); disp(' please check the filter parameters'); disp('****************************************************************************** *****************'); case 3 % a coeffapw coeff is not real disp(' '); disp(' '); disp('****************************************************************************** *****************'); disp(' error!!!'); disp(' please check the filter parameters'); disp('****************************************************************************** *****************'); end abbreviations and acronyms AN3984 44/46 doc id 022240 rev 1 appendix b abbreviations and acronyms the abbreviations and acronyms used throughout this application note are defined as follows: fc: cutoff frequency fs: sampling frequency q: filter quality factor g: gain lpf: low-pass filter hpf: high-pass filter lsf: low-shelf filter hsf: high-shelf filter apf: all-pass filter bpf: band-pass filter AN3984 revision history doc id 022240 rev 1 45/46 6 revision history table 1. document revision history date revision changes 26-sep-2011 1 initial release. AN3984 46/46 doc id 022240 rev 1 please read carefully: information in this document is provided solely in connection with st products. stmicroelectronics nv and its subsidiaries (?st ?) reserve the right to make changes, corrections, modifications or improvements, to this document, and the products and services described he rein at any time, without notice. all st products are sold pursuant to st?s terms and conditions of sale. purchasers are solely responsible for the choice, selection and use of the st products and services described herein, and st as sumes no liability whatsoever relating to the choice, selection or use of the st products and services described herein. no license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. i f any part of this document refers to any third party products or services it shall not be deemed a license grant by st for the use of such third party products or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoev er of such third party products or services or any intellectual property contained therein. unless otherwise set forth in st?s terms and conditions of sale st disclaims any express or implied warranty with respect to the use and/or sale of st products including without limitation implied warranties of merchantability, fitness for a parti cular purpose (and their equivalents under the laws of any jurisdiction), or infringement of any patent, copyright or other intellectual property right. unless expressly approved in writing by two authorized st representatives, st products are not recommended, authorized or warranted for use in milita ry, air craft, space, life saving, or life sustaining applications, nor in products or systems where failure or malfunction may result in personal injury, death, or severe property or environmental damage. st products which are not specified as "automotive grade" may only be used in automotive applications at user?s own risk. resale of st products with provisions different from the statements and/or technical features set forth in this document shall immediately void any warranty granted by st for the st product or service described herein and shall not create or extend in any manner whatsoev er, any liability of st. st and the st logo are trademarks or registered trademarks of st in various countries. information in this document supersedes and replaces all information previously supplied. the st logo is a registered trademark of stmicroelectronics. all other names are the property of their respective owners. ? 2011 stmicroelectronics - all rights reserved stmicroelectronics group of companies australia - belgium - brazil - canada - china - czech republic - finland - france - germany - hong kong - india - israel - ital y - japan - malaysia - malta - morocco - philippines - singapore - spain - sweden - switzerland - united kingdom - united states of america www.st.com |
Price & Availability of AN3984 |
|
|
All Rights Reserved © IC-ON-LINE 2003 - 2022 |
[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy] |
Mirror Sites : [www.datasheet.hk]
[www.maxim4u.com] [www.ic-on-line.cn]
[www.ic-on-line.com] [www.ic-on-line.net]
[www.alldatasheet.com.cn]
[www.gdcy.com]
[www.gdcy.net] |