DSP Filters
DSP Filters
The authors thank Judy Lane and Dan Hoory for reviewing selected chapters
and providing invaluable comments and suggestions.
They also thank Kim Heusel for his hard work and long hours
preparing the final manuscript.
Electronics Cookbook Series
DSP
Filters
by
John Lane
Jayant Datta
Brent Karley
Jay Norwood
©2001 by Sams Technical Publishing
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or
transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise,
without written permission from the publisher. No patent liability is assumed with respect to
the use of the information contained herein. While every precaution has been taken in the
preparation of this book, the author, the publisher or seller assumes no responsibility for
errors or omissions. Neither is any liability assumed for damages resulting from the use of
information contained herein.
Trademark Acknowledgments:
All product illustrations, product names and logos are trademarks of their respective manu-
facturers. All terms in this book that are known or suspected to be trademarks or services
have been appropriately capitalized. PROMPT® Publications and Sams Technical Publishing
cannot attest to the accuracy of this information. Use of an illustration, term or logo in this
book should not be regarded as affecting the validity of any trademark or service mark.
987654321
Table of Contents
Table of Contents
v
DSP Filters
vi
Table of Contents
vii
DSP Filters
viii
1
Introduction
1
DSP Filters
2
Introduction
3
[This is a blank page.]
Section I
Filter Design
Formulas
[This is a blank page.]
2
Filter Design
Basics
Low-Pass Filter
A low-pass filter may, in
general, be defined as a filter 10
0 stopband
passed with little or no modi- ripple
transition
fication, from f = 0 Hz up to a - 10 band corner
frequency stopband
cutoff frequency f c , above corner
frequency
which the signal is rejected. - 20
passband
transition stopband
Figure 2-1 shows a gen- - 30 band attenuation
7
DSP Filters
where Vi is the time average of the input, and Vo is the time average of
the output signal. When the output level is greater than the input level,
there is a gain associated with the filter, and the corresponding response
in dB is positive, i.e., G(f) >1. If the output is less than the input level,
there is an attenuation associated with the filter processing so that the
corresponding response in dB is negative, in which case G(f) < 1.
The cutoff frequency fc, is normally defined as the frequency where
the gain is reduced to 1 / 2 = 0.707 times the passband level (-3dB down
from the passband level). As a side note: most of the filters we will be
considering in this text have a flat passband response.
8
Filter Design Basics
10 10
Response [dB]
Response [dB]
0 0 stopband
-3 dB (or 0.707)
- 10 - 10
cutoff frequency fc
roll
o
- 20 - 20
ff [
passband
dB
transition stopband
/oc
- 30 - 30 band attenuation
tav
e]
10 100 1000 10000 10 100 1000 10000
Frequency [Hz] Frequency [Hz]
Figure 2-2. Gain response plot of a second- Figure 2-3. Comparing the second-order
order low-pass filter with cutoff frequency filter response from Figure 2-1 to the low-
fc=100Hz pass filter specification of Figure 2-2
9
DSP Filters
approach in this text. Our target application is audio. It has been ac-
cepted that most audio applications require specific filter design charac-
teristics for specific applications (more on this later in the project chap-
ters). Therefore, our filter design strategy in this text is reduced to choosing
and customizing a filter whose behavior can be described by a small
number of characteristic parameters: filter type, cutoff frequency, filter
bandwidth or Q, and filter order.
High-Pass Filter
The generalized high-pass filter is shown in Figure 2-4. The high-pass
filter’s operation is exactly the opposite of the low-pass filter. This filter
rejects a signal below the cutoff frequency and allows the signal to be
passed with little or no attenuation above the cutoff frequency. The filter
concepts described for the low-pass filter, including rolloff, transition
band, and cutoff frequency also apply here for the high-pass filter. Math-
ematically one can think of this as reversing the direction of frequency.
Bandpass Filter
The bandpass filter processes a signal as shown in Figure 2-5. This
filter type permits a band of frequencies to pass with little or no attenu-
ation and rejects the signal outside the passband. The middle of the
band is called the center frequency f0. The band-edge or cutoff defines
both the lower f1 and upper f2 portions of the band. The cutoff is typically
defined to be –3 dB below the passband maximum level. The bandwidth
∆f is defined as the difference between the upper and lower frequencies:
∆f ≡ f2 – f1.
The quality factor Q is inversely proportional to the frequency
bandwidth ∆f of the gain response curve, and proportional to the
center frequency:
10
Filter Design Basics
10 10
f
0 0
Response [dB]
Response [dB]
-3 dB (or 0.707)
-3 dB (or 0.707)
- 10 f0
- 10 f1 f2
cutoff frequency fc ]
ve
ve]
a
ct
ro
ta
/o
llo
/oc
- 20 - 20 B
ff
[d
dB
[d
ff
B/
llo
ff [
oc
ro
ta
lo
ve
rol
- 30 - 30
]
10 100 1000 10000 10 100 1000 10000
Frequency [Hz] Frequency [Hz]
Figure 2-4. Gain response plot of a second- Figure 2-5. Gain response plot of a second-
order high-pass filter with cutoff frequency order bandpass filter with center frequency
fc = 1000 Hz fo = 200 Hz and Q = 1
f 0 (2-2a)
Q≡
∆f
f0 = f1 f 2 (2-2b)
Using Equations (2-3a) and (2-3b), the edge (or cutoff) frequencies in
Figure 2-5, with f0 = 200 Hz and Q = 1, are: f1 = 123.607 Hz and f2 =
323.607 Hz.
11
DSP Filters
10
f
0
Response [dB]
- 30 f0
Band-stop Filter
The band-stop filter is shown in Figure 2-6. This filter is also known as
the band-reject filter or the notch filter. The band-stop filter’s operation is
exactly opposite of the bandpass filter: it rejects a band of frequencies
while allowing frequencies outside of the band to pass with little or no
attenuation. As with the bandpass filter, the middle of the band is char-
acterized by the center frequency f0. The cutoff defines both the lower f1
and upper f2 portions of the band. The cutoff is typically defined to be
–3 dB below the level of the passband. The mathematical relationships
for the bandstop filter between center frequency f0 , edge frequencies f1
and f2 , and bandwidth (or Q), are also precisely described by Equations
(2-2a) through (2-3b).
Peaking Filter
The peaking filter has a characteristic bell shape and is therefore
also known as a bell filter. Example responses are shown in Figures 2-7a
12
Filter Design Basics
20 20
15 g = +15 dB 15
10 f0 10
Response [dB]
Response [dB]
50% of Peak dB
5 5
f
0 0 f
-5 -5
f0 -50% of Peak dB
- 10 - 10
- 15 - 15 g = -15 dB
Figure 2-7a. Response plot of second- Figure 2-7b. Response plot of second-
order peaking filter with f 0 = 200 Hz, order peaking filter f0 = 200 Hz, Q = 1,
Q = 1, and g = +15 dB and g = -15 dB
and 2-7b. The peaking filter’s shape is qualitatively similar to the bandpass
filter with the exception that the out-of-band frequencies are left unal-
tered (0 dB gain) rather than attenuated, as in the bandpass filter case.
The peaking filter can either boost or attenuate a band of frequencies
while having no effect on the signal frequencies outside the band. As
with the bandpass filter, the center of the band is characterized by its
center frequency f0.
The most important characteristic of a peaking filter is its boost/
cut symmetry. For example, Figure 2-7a shows a peaking filter re-
sponse where the boost gain at the band center is g = +15 dB. The
equivalent cut response with a gain value of g = –15 dB is shown in
Figure 2-7b. Note that these response shapes are mirror images of
one an another.
The definition of bandwidth is somewhat subjective in the case of
peaking filters. All of the filters discussed up to this point consistently
use a –3 dB gain value (0.707) to mark the cutoff or edge frequencies.
Since there is no guarantee that a peaking filter is boosted or cut beyond
+/– 3 dB, a better convention for bandwidth is the width where the peak
13
DSP Filters
is at 50% of the peak value. For example, the peaking filter in Figure 2-7a
has a peak gain of +15 dB. Therefore, the bandwidth can be measured at
the +7.5 dB level.
Peaking filters usually have a bandpass filter at their core (which is
discussed in more detail in Chapter 10). The core bandpass filter has a
definite Q value associated with it, as described in a previous section.
The relationship between this Q value and the bandwidth of the peaking
filter is somewhat complicated. Therefore, we will adopt the convention
that the peaking filters we will discuss are described by intrinsic Q value,
which is qualitatively related to bandwidth. But we will not attempt to
define, quantitatively, the peaking filter bandwidth, using this Q value
(refer to Chapter 10 for more on the peaking filter).
Shelving Filter
The low-pass shelving filter response, shown in Figures 2-8a and 2-
8b, is similar to the low-pass filter discussed previously with the excep-
tion that the frequencies above cutoff remain unaltered. As in the case
Figure 2-8a. Response plot of a first-order Figure 2-8b. Response plot of a first-order
low-pass shelving filter with fc = 30 Hz and low-pass shelving filter with fc = 30 Hz and
g = +15 dB g = –15 dB
14
Filter Design Basics
Figure 2-8c. Response plot of a first-order Figure 2-8d. Response plot of a first-order
high-pass shelving filter with fc = 2000 Hz high-pass shelving filter with fc = 2000 Hz
and g = +15 dB and g = -15dB
15
[This is a blank page.]
17
Tone Control
Introduction
This chapter will be a guide, providing step-by-step instructions
for implementing a tone control using the shelving filters presented in
Chapter 11.
Tone controls are equalizers that operate on two bands. Figure 17-1
displays a typical tone control and its corresponding frequency response.
Tone controls provide a quick and easy way to adjust the sound repro-
duced by an audio system to suit the listener’s tastes. The individual
bass and treble controls apply a gain or attenuation to the respective
frequency bands. The low frequencies are affected only by bass adjust-
ments and high frequencies are affected only by treble adjustments.
Increasing the gain of the bass control increases the relative level of
the lower frequencies. This often creates a richer and fuller sound al-
though it can also cause an overly boomy bass when used excessively.
Increasing the relative level of the treble control has the effect of bring-
ing out vocals or instruments in the higher frequency ranges. This can
allow vocals to be better understood or particular instruments to be heard
more clearly. Individuals with excessive hearing loss in the upper
179
DSP Filters
Figure 17-1. Bass and treble tone controls with corresponding frequency response
180
Tone Control
Design Requirements
The goal of this design is to develop a digital tone control meeting
the following specifications:
Specifications:
Channels: 2 (stereo)
Bands: Bass with a cutoff frequency at 350 Hz
Treble with cutoff frequency at 4 kHz
Gain: +/- 20 dB
Sample Rate: Variable
This design supports stereo (two channels) audio signals with a
bass and treble control applied to both channels. The boost/cut range is
+/- 20 dB with controllable step size.
Filter Overview
The tone control is implemented using shelving filters described in
Chapter 11. A quick review of the low-pass and high-pass shelving fil-
ters is helpful at this point. Equations for implementing a low-pass and
high-pass shelving filter as well as the filter realizations are provided
here for convenience along with the corresponding frequency and phase
response curves.
181
DSP Filters
Figure 17-3. Gain and phase of low-pass shelving filter with fc = 30 and fs = 44100 Hz:
dotted line, g = -15 dB; thin dotted line, g = -7.5 dB; solid line, g = 0 dB; thin dashed line, g
= +7.5 dB; and dashed line, g = +15 dB
182
Tone Control
Difference equation of digital first-order low-pass shelving filter with coefficient formulas
183
DSP Filters
Figure 17-5. Gain and phase of high-pass shelving filter with fc = 1000 and fs = 44100 Hz:
dotted line, g = -15 dB; thin dotted line, g = -7.5 dB; solid line, g = 0 dB; thin dashed line,
g = +7.5 dB; dashed line, g = +15 dB
Difference equation of digital first-order high-pass shelving filter with coefficient formulas
184
Tone Control
Control section
The control section of the tone control can be implemented in ei-
ther hardware or in software. Regardless of the type of design required
(hardware or software), the control section must provide a numerical
representation of the bass and treble control positions.
185
DSP Filters
Filter Section
The filter blocks f1 and f2 in the filter section apply the coefficients a
and g to the respective low-pass and high-pass shelving filters. The filters
are cascaded as shown in Figure 17-6 and applied to the input digital
signal. The filter section effectively modifies the signal frequency response
based on the bass and treble control settings.
186
Tone Control
cutoff frequency (f0), and gain factor (g) each affect the resulting coeffi-
cients used to implement the tone control. The coefficients change dy-
namically with changes to any of these variables.
187
DSP Filters
188
Tone Control
Filtering section
The filter section flow dia-
grams are shown in Figures
17-11 through 17-13. The up-
dating of the coefficients in the
coefficient calculation blocks
can occur asynchronously with
the processing of the samples
in the filter section. Figure 17- Figure 17-10. Flow diagram for updating gain
11 displays the flow diagram, Routine:
which involves reading the calc_coeff_for_new_gain(double dv, int i)
samples from the input stream
189
DSP Filters
where the low-pass shelving filter operates on the sample. The output
from the low-pass shelving filter is input to the high-pass shelving filter
in cascade. The high-pass shelving filter processes the sample and out-
puts the result to the rest of the system.
The low-pass shelving filter processes the input sample as shown in
the flow diagram of Figure 17-12. The low-pass section of the filter is
190
Tone Control
Software Description
All filter-related information is stored in a matrix as shown in
Table 17-1. This matrix stores the variables required for processing each
of the shelving filters. Column 0 contains variables for the low-pass
shelving filter while column 1 contains variables for the high-pass shelv-
ing filter. Each column is referenced in the software by the index i as a
part of an array stages[i]. Table 17-1 displays the tone control vari-
ables in their initialized states.
See also Figure 17-2, which displays the low-pass shelving filter
191
DSP Filters
// Initialization of LP filter
9 {
10 stages[0]->min_f0 = 0.1;
11 stages[0]->min_gain = -20.0; //db
12 stages[0]->max_gain = +20.0;
13 stages[0]->min_Q = 0.1;
14 stages[0]->max_Q = 20.0;
15 stages[0]->gain = 0.0;
16 stages[0]->Q =1.0;
192
Tone Control
17 stages[0]->f0 = 300.0;
18 stages[0]->max_f0 = 22049.0;
// Initialization of HP filter
19 {
20 stages[1]->min_f0 = 0.1;
21 stages[1]->min_gain = -20.0; //db
22 stages[1]->max_gain = +20.0;
23 stages[1]->min_Q = 0.1;
24 stages[1]->max_Q = 20.0;
25 stages[1]->gain = 0.0;
26 stages[1]->Q =1.0;
27 stages[1]->f0 = 4000.0;
28 stages[1]->max_f0 = 22049.0;
29 }
30 num_filter_stages = NUM_BANDS;
31 enabled = 1;
32 }
• Lines 1, 2, and 3 define constants “2pi” (2*π) and “pi” (π) as well as the
number of filter bands (2 – bass and treble).
• Lines 4, 5, and 32 define the boundaries of this routine. Everything
between these lines constitutes the main initialization routine.
• Line 6 calls a routine that clears the memory used for the variable matrix.
• Lines 7 and 8 invoke constructors that establish memory in the system
for the matrix variables and associates the variables with the rows of
the matrix. These routines are shown below.
• Lines 10 through 19 initialize the low-pass shelving filter variables in
the matrix as shown.
Line 10 initializes the minimum cutoff frequency for the low-pass shelving filter.
Line 11 initializes the minimum gain for the low-pass shelving filter.
Line 12 initializes the maximum gain for the low-pass shelving filter.
Line 13 initializes the minimum Q for the low-pass shelving filter. Although Q is
not a controlled low-pass filter variable, it is required overhead in our DirectX
control interface.
193
DSP Filters
Line 14 initializes the maximum Q for the low-pass shelving filter. Although Q is
not a controlled low-pass filter variable, it is required overhead in our DirectX
control interface.
Line 15 initializes the gain for the low-pass shelving filter.
Line 16 initializes the Q for the low-pass shelving filter. Although Q is not a
controlled low-pass filter variable, it is required overhead in our DirectX
control interface.
Line 17 initializes the cutoff frequency for the low-pass shelving filter.
Line 18 initializes the maximum cutoff frequency for the low-pass shelving filter.
194
Tone Control
195
DSP Filters
196
Tone Control
197
DSP Filters
• Line 4 tests the cutoff frequency dv to verify that it does not exceed the
maximum cutoff frequency (max_f0) established in the column pointed
to by st. If the cutoff frequency exceeds the maximum value then it is
set equal to the maximum cutoff frequency.
• Line 5 tests the cutoff frequency dv to verify that it is not less than the
minimum cutoff frequency boundaries (min_f0) established in the col-
umn pointed to by st. If the cutoff frequency is less than the minimum
value then it is set equal to the minimum cutoff frequency.
• Line 6 sets the variable f0 in the column pointed to by st of the matrix
equal to the cutoff frequency value passed to the routine dv.
• Line 7 calculates the normalized cutoff frequency θ in the column
pointed to by st of the matrix given both the cutoff frequency f0 in the
column pointed to by st and the sample rate fs.
• Line 8 calls the routine calc_coeff_for_new_gain (st->gain, i ). The origi-
nal gain value pointed to by st is passed along with the index i indicat-
ing which filter is to be processed.
198
Tone Control
6 st->gain = dv;
7 st->mu = pow(10.0,st->gain/20.0);
8 if (i == 1 ) // HP shelving filter
9 {
10 st->gamma = (1.0-(((1.0+st->mu)/4.0) * tan(st->theta0/2.0)))/
(1.0+(((1.0+st->mu)/4.0) * tan(st->theta0/2.0)));
11 st->alpha = (1.0 + st->gamma)/2.0;
12 }
13 else // LP shelving
14 {
15 st->gamma = (1.0-((4.0/(1.0+st->mu)) * tan(st->theta0/2.0)))/
(1.0+((4.0/(1.0+st->mu)) * tan(st->theta0/2.0)));
16 st->alpha = (1.0 - st->gamma)/2.0;
17 }
18 }
199
DSP Filters
Filter Section
The filter section operates asynchronously with the coefficient cal-
culation section. Each sample is processed as it is streamed into the
filter section. The filter section is implemented in the routine
execute_filter_block_in_place(double *in). This routine is called for each
sample processed in the filter section. A description of this routine is
provided on the following page.
1 void CShelving_Tone::execute_filter_block_in_place(double *in)
2 {
3 int i;
4 double input = *in;
5 stages[0]->x = input;
6 stages[0]->execute_filter_stage();
7 stages[1]->x = stages[0]->y;
8 stages[1]->execute_filter_stage();
9 *in = stages[1]->y;
10 }
200
Tone Control
The pointer *in is then used by an output routine to pass the sample
on for further processing or to an output D/A converter.
201
DSP Filters
1 void CLP_ShelvingFilterStage::execute_filter_stage()
2 {
3 double u;
202
Tone Control
1 void CHP_ShelvingFilterStage::execute_filter_stage()
2 {
3 double u;
4 u = (alpha * (x - x1) + (gamma * u1) );
5 x1 = x;
6 u1 = u;
7 y = (u * (mu - 1.0)) + x;
8 }
203
DSP Filters
5 {
6 for (i = 0; i< NUM_BANDS; i++)
7 {
8 delete stages[i];
9 }
10 delete stages;
11 stages = 0;
12 }
13 }
204