AN3984
Application note
IIR filter design equations 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.
September 2011 |
Doc ID 022240 Rev 1 |
1/46 |
www.st.com
Contents |
AN3984 |
|
|
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 1st-order low-pass filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 5.2 1st-order high-pass filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 5.3 2nd-order low-pass filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 5.4 2nd-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
2/46 |
Doc ID 022240 Rev 1 |
AN3984 |
Contents |
|
|
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 (LimitVal.m) . . . . . . . . . . . . . . . . . . . 40 A.10 Display coefficient and error messages (Display_Coeff.m). . . . . . . . . . . . 41
Appendix B Abbreviations and acronyms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
44 |
|
6 |
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . |
45 |
Doc ID 022240 Rev 1 |
3/46 |
List of figures |
AN3984 |
|
|
List of figures
Figure 1. 1st-order low-pass filter - magnitude response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Figure 2. 1st-order high-pass filter - magnitude response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 Figure 3. 2nd-order low-pass filter - magnitude response . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Figure 4. 2nd-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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Figure 10. Code structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4/46 |
Doc ID 022240 Rev 1 |
AN3984 |
Overview |
|
|
The transfer function for a first-order filter in the digital z-domain is:
Equation 1
H(z) = b0 + b1 z−1 a0 + a1 z−1
For a second-order filter (a biquad) the transfer function is:
Equation 2
H (z) = b0 + b1 z −1 + b2 z−2 a0 + a1 z −1 + a2 z− 2
This equation can be modified normalizing the a0 coefficient; the new equation is:
Equation 3
H (z) = (b0 a0 )+ (b1 a0 ) z −1 + (b2 a0 ) z −2 1 + (a1 a0 ) z−1 + (a2 a0 ) z −2
The most straightforward implementation form using Equation 3 is:
Equation 4
y[n] = (b0 a0 ) x[n] + (b1a0 ) x[n −1] + (b2 a0 ) x[n − 2] − (a1a0 ) y[n −1] − (a2 a0 ) y[n− 2]
Doc ID 022240 Rev 1 |
5/46 |
Filter stability |
AN3984 |
|
|
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 translated in terms of the filter coefficients.
2.2First-order filter
For a first-order filter, the stability 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
a1 < 1
2.3Second-order filter
For a 2nd-order filter, two conditions must be satisfied to ensure filter stability and translated in terms of the filter coefficients they are:
Equation 6
a 2 < 1
a1 < (1+ a 2 )
6/46 |
Doc ID 022240 Rev 1 |
AN3984 |
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
ωc = 2 π fc fs
K = tan(ω c 2)
α = 1+ K
In a first-order filter both the coefficients a2 and b2 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
a0 |
= 1 |
(1 − K ) |
|
a1 |
= − |
||
α |
|||
|
|
The numerator for an LPF can be calculated as follows:
Equation 9
K b0 = α
b1 = Kα
The numerator for an HPF can be calculated as follows:
Equation 10
1 b0 = α
b1 = − α1
The coefficient used in APWorkbench can be calculated by applying these formulas:
Equation 11
Coefficient b1 2 = (b1 2)
a0
Coefficient b2 = (b2 )
a0
Coefficient a1 2 = (− a1 2)
a0
Coefficient a2 = −aa2
0
Coefficient b0 2 = (b0 2)
a0
Doc ID 022240 Rev 1 |
7/46 |
Second-order filter design |
AN3984 |
|
|
4.1Low-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
ϑ = 2 π |
|
fc |
||
c |
|
fs |
||
K = tan(ωc |
||||
|
2) |
|||
W = K2 |
|
|
|
|
α = 1 + K |
|
|
|
|
DE = 1 + K |
|
+ W |
||
|
Q |
|
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
a0 |
= |
1 |
(W − 1) |
|
|||
a = |
2 |
|
|||||
|
|||||||
1 |
|
|
|
|
DE |
||
|
|
|
|
|
|||
|
|
|
1 − |
K + W |
|||
a2 |
= |
|
|
|
Q |
||
|
|
|
DE |
||||
|
|
|
|
|
4.1.1Low-pass filter
The numerator coefficient for a second-order LPF can be calculated as follows:
Equation 14
W b0 = DE
W b1 = 2 DE
W b2 = DE
8/46 |
Doc ID 022240 Rev 1 |
AN3984 |
Second-order filter design |
|
|
For a second-order LPF, the coefficients given in APWorkbench can be calculated as follows:
Equation 15
Coefficient |
b1 |
2 |
= |
W |
|
|
|
|
|
DE |
|||
Coefficient b = W |
|
|
||||
|
2 |
|
DE |
|
|
|
|
|
|
|
|
||
Coefficient |
a1 |
2 |
= −1 W − 1 |
|||
|
|
|
|
DE |
||
|
|
|
|
1 − |
K |
+ W |
|
|
|
|
|
||
Coefficient a2 = − |
|
Q |
||||
|
DE |
|||||
|
|
|
|
|
||
Coefficient |
b0 |
2 |
= |
1 |
W |
|
|
|
|
2 |
DE |
4.1.2High-pass filter
The numerator coefficient for a second-order HPF can be calculated as follows:
Equation 16
b0 = DE1
b1 = −2 DEW
1 b2 = DE
For a second-order HPF, the coefficients given in APWorkbench can be calculated as follows:
Equation 17
Coefficient |
b1 |
2 |
= − |
|
1 |
|
|
|
1 |
DE |
|||
Coefficient b = |
|
|
|
|||
|
2 |
|
DE |
|
|
|
|
|
|
|
|
|
|
Coefficient |
a1 |
2 |
= −1 W − 1 |
|||
|
|
|
|
DE |
||
|
|
|
1 − |
K |
+ W |
|
|
|
|
|
|||
Coefficient a2 = − |
|
Q |
||||
|
DE |
|||||
|
|
|
|
|
||
Coefficient |
b0 |
2 |
= 1 |
|
1 |
|
|
|
2 |
|
DE |
Doc ID 022240 Rev 1 |
9/46 |
Second-order filter design |
AN3984 |
|
|
The first step is the calculation of the constant gain obtained from the gain filter parameter (GdB is expressed in dB).
Equation 18
Gain = exp(GaindB 0.115129254)
The filter coefficients are different if the gain is positive or negative.
The cut value is calculated with the following equation:
Equation 19
|
|
|
|
|
|
|
|
|
|
|
Q |
|
|
|
||
CutValue = 1 + K |
|
|
|
|
|
|
+ W |
|||||||||
|
|
|
|
|
||||||||||||
|
|
|
|
|
|
|
|
|
|
Gain |
|
|||||
The filter coefficient can be calculated as follows: |
|
|
|
|
|
|
|
|||||||||
Equation 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
a0 |
= 1 |
|
|
|
(W − 1) |
|
|
|
||||||||
a |
= 4 |
|
|
|
|
|||||||||||
|
|
|
|
|
|
|
|
|
|
|
||||||
1 |
|
|
|
|
CutValue |
|
|
|
||||||||
|
|
|
|
|
|
|||||||||||
|
|
|
|
|
|
Q |
|
|
|
|
|
|||||
|
|
|
1 |
− |
|
|
|
+ W |
|
|||||||
|
|
|
Gain |
|
|
|||||||||||
a |
2 |
= |
|
|
|
|
|
|
|
|
|
|
||||
|
|
CutValue |
|
|||||||||||||
|
|
|
|
|
||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|||
|
|
|
|
+ |
|
K |
|
|
|
|
|
|
||||
|
|
|
1 |
|
|
+ W |
|
|
|
|||||||
|
|
|
|
|
|
Q |
|
|
|
|
|
|||||
b0 |
= |
|
|
|
|
|
|
|
|
|
||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
||||
|
|
|
|
CutValue |
|
|
|
|||||||||
b |
= 2 |
|
(W − 1) |
|
|
|
||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|||||
1 |
|
|
|
|
CutValue |
|
|
|
||||||||
|
|
|
|
|
|
|
|
|
||||||||
|
|
|
(1 |
|
− |
K |
+ W ) |
|
|
|
||||||
b2 |
= |
|
Q |
|
|
|
||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|||||
|
CutValue |
|
|
|
||||||||||||
|
|
|
|
|
|
|
10/46 |
Doc ID 022240 Rev 1 |
AN3984 |
Second-order filter design |
|
|
The coefficients in the APWorkbench are consequently calculated as follows:
Equation 21
Coefficient |
b1 |
2 |
= |
|
W −1 |
|
|
|
||||||
|
|
|
CutValue |
|||||||||||
|
|
|
(1− |
K |
+W ) |
|||||||||
Coefficient b2 = |
Q |
|||||||||||||
|
|
|
|
|
|
|
|
|||||||
CutValue |
|
|
|
|||||||||||
|
|
|
|
|
|
|||||||||
Coefficient |
a1 |
2 |
= |
|
1− W |
|
|
|
||||||
|
|
|
CutValue |
|||||||||||
|
|
|
1 − |
|
|
Q |
|
K + W |
||||||
|
|
|
Gain |
|||||||||||
Coefficient a2 = − |
|
|
|
|
|
|||||||||
|
|
|
CutValue |
|||||||||||
|
|
|
|
|
|
|
||||||||
|
|
|
|
|
|
1 + |
K |
|
+ W |
|||||
Coefficient |
b0 |
2 |
= |
1 |
|
|
|
|
Q |
|
||||
2 |
|
CutValue |
||||||||||||
|
|
|
|
|
The boost value is calculated with the following equation:
Equation 22
BoostValue = 1+ KQ + W
The filter coefficient can be calculated as follows:
Equation 23
a0 |
= 1 |
|
|
|
|
|
|
|
|
|
||
|
|
|
(1+ K |
Gain + W ) |
||||||||
a1 |
= |
|
|
|
|
|
|
Q |
|
|
||
|
|
|
|
BoostValue |
|
|
||||||
|
|
|
|
|
|
|
|
|||||
|
|
|
|
|
|
|
K |
|
|
|
||
|
|
|
1 − |
|
+ W |
|
|
|||||
|
|
|
|
|
|
|
Q |
|
|
|
||
a2 |
= |
|
|
|
|
|
|
|
|
|
||
BoostValue |
|
|
||||||||||
|
|
|
|
|
||||||||
|
|
|
|
|
|
(1+ K Gain |
+ W ) |
|||||
b0 |
= 2 |
|
|
|
|
Q |
|
|
||||
|
|
BoostValue |
||||||||||
|
|
|
|
|
|
|
||||||
b1 |
= 2 |
|
|
|
W − 1 |
|
|
|
||||
BoostValue |
|
|
||||||||||
|
|
|
|
|
|
|
||||||
|
|
|
(1 − K |
Gain + W ) |
||||||||
b2 |
= |
|
|
|
|
|
|
Q |
|
|
||
|
|
|
|
BoostValue |
|
|
||||||
|
|
|
|
|
|
|
|
Doc ID 022240 Rev 1 |
11/46 |
Second-order filter design |
AN3984 |
|
|
The coefficients in the APWorkbench are consequently calculated as follows:
Equation 24
Coefficient |
b1 |
2 |
= |
|
W − 1 |
|
|||||
|
|
|
BoostValue |
|
|||||||
|
|
|
(1 − |
Gain |
K + W ) |
||||||
|
|
|
|
|
|
|
|
||||
Coefficient b2 = |
|
|
|
|
Q |
|
|
|
|
||
|
BoostValue |
||||||||||
|
|
|
|
||||||||
Coefficient |
a1 |
2 |
= |
|
1 − W |
|
|||||
|
|
|
BoostValue |
|
|||||||
|
|
|
|
1 − |
K |
+ W |
|
||||
Coefficient a2 = − |
Q |
|
|||||||||
|
|
|
|
|
|
|
|||||
BoostValue |
|
||||||||||
|
|
|
|
|
|||||||
|
|
|
|
|
1 + |
|
Gain |
K + W |
|||
|
b0 |
|
|
1 |
|
|
Q |
||||
Coefficient |
2 |
= |
|
|
|
|
|
||||
2 |
|
BoostValue |
|||||||||
|
|
|
|
|
|
The coefficient gain is defined in Equation 25.
Equation 25
Gain = 10(GaindB 40)
The coefficients α and β are calculated as follows:
Equation 26
|
sin(ϑ ) |
|
|
|
1 |
|
|
|
1 |
α = |
c |
|
|
|
|
||||
|
Gain + |
|
|
|
|
||||
|
2 |
|
|
|
Gain |
|
S |
||
|
|
|
|
||||||
β = 2 α |
Gain |
|
|
|
|
|
|
|
|
− 1 |
+ 2 |
|
|
|
4.3.1Low-shelf filter
The coefficients for an LSF can be calculated as follows:
Equation 27
a0 |
= (Gain + 1)+ (Gain − 1) cos ϑc + β |
a1 = −2 (Gain − 1)+ (Gain + 1) cos ϑc |
|
a2 |
= (Gain + 1)+ (Gain − 1) cos ϑc − β |
b0 |
= Gain ((Gain + 1)− (Gain − 1) cos ϑc + β ) |
b1 = 2 Gain ((Gain − 1)− (Gain + 1) cos ϑc ) |
|
b2 |
= Gain ((Gain + 1)− (Gain − 1) cos ϑc − β ) |
The coefficient to load in APWorkbench can be calculated by applying the calculation already shown in Equation 11.
12/46 |
Doc ID 022240 Rev 1 |
AN3984 |
Second-order filter design |
|
|
4.3.2High-shelf filter
The coefficients for an HSF can be calculated as follows:
Equation 28
a0 |
= (Gain + 1)− (Gain − 1) cos ϑc + β |
a1 = 2 (Gain − 1)− (Gain + 1) cos ϑc |
|
a2 |
= (Gain + 1)− (Gain − 1) cos ϑc − β |
b0 |
= Gain ((Gain + 1)+ (Gain − 1) cos ϑc + β ) |
b1 = −2 Gain ((Gain − 1)− (Gain + 1) cos ϑc ) |
|
b2 |
= Gain ((Gain + 1)+ (Gain − 1) cos ϑc − β ) |
The coefficient to load in APWorkbench to program a HSF can be computed by applying the formulas shown in Equation 11.
The first step is to define the constant α(a):
Equation 29
α = sin(ϑc ) 2 Q
The coefficients for a notch filter can be calculated as follows:
Equation 30
a0 = 1 + α
a1 = −2 cos ϑc a2 = 1− α
b0 = 1
b1 = −2 cos ϑ b2 = 1
The coefficients to load in APWorkbench can be calculated using Equation 11.
a. ϑc is defined in Equation 12
Doc ID 022240 Rev 1 |
13/46 |
Second-order filter design |
AN3984 |
|
|
4.5All-pass filter
Equation 29 allows calculating the constant α.
The coefficients for an APF can be calculated as follows:
Equation 31
a0 |
= 1 |
+ α |
a1 |
= −2 cos ϑ c |
|
a2 |
= 1 |
− α |
b0 |
= 1 |
− α |
b1 |
= −2 cos ϑ c = a1 |
|
b2 |
= 1 |
+ α |
The coefficients to load in APWorkbench can be calculated using Equation 11.
4.6Band-pass filter
Equation 29 allows calculating the constant α while Equation 32 is used to calculate the normalized gain.
Equation 32
GaindB
NormGain =10 20
The coefficients for a BPF can be calculated as follows(b):
Equation 33
a0 = 1 + α
a1 = −2 cos ϑc a2 = 1 − α
b0 = α NormGain b1 = 0
b2 = −b0 = −α NormGain
The coefficients to load in APWorkbench can be calculated using Equation 11.
b. α is defined in Equation 29, ϑc is defined in Equation 12.
14/46 |
Doc ID 022240 Rev 1 |