0% found this document useful (1 vote)
363 views50 pages

DSP Filters

DSP FILTERS

Uploaded by

solomon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
363 views50 pages

DSP Filters

DSP FILTERS

Uploaded by

solomon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

free sample chapters - click to visit

Electronics Cookbook Series


DSP
Filters
You can browse Chapters 1, 2 and 17
Acknowledgments

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

PROMPT© Publications is an imprint of Sams Technical Publishing, 5436 W. 78th St.,


Indianapolis, IN 46268.

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.

International Standard Book Number: 0-7906-1204-6


Library of Congress Control Number: 2001088401

Acquisitions Editor: Alice J. Tripp


Editor: Kim Heusel
Assistant Editor: Cricket A. Franklin
Typesetting: Kim Heusel
Indexing: Kim Heusel
Proofreader: Cricket A. Franklin
Cover Design: Christy Pierce
Graphics Conversion: Christy Pierce
Illustrations: Courtesy the authors

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.

PRINTED IN THE UNITED STATES OF AMERICA

987654321
Table of Contents

Table of Contents

Chapter 1 - Introduction ........................................................... 1

Section I - Filter Design Formulas ......................... 5


Chapter 2 - Filter Design Basics .............................................. 7
Low-Pass Filter ........................................................................................... 7
High-Pass Filter ........................................................................................ 10
Bandpass Filter ........................................................................................ 10
Band-stop Filter ........................................................................................ 12
Peaking Filter ............................................................................................ 12
Shelving Filter ........................................................................................... 14

Chapter 3 - Digital Basics ...................................................... 17


Analog versus Digital ................................................................................ 17
Sampling Theorem, Aliasing, and Quantization .......................................... 19
Mathematical Transforms ......................................................................... 24
Digital Filter .............................................................................................. 27
Unit Circle ................................................................................................ 29
Digital Signal Processing Operations ......................................................... 30
Types of Digital Filters .............................................................................. 32
Comparison of IIR and FIR Filters ............................................................. 34
The Biquad Section ................................................................................... 35
Returning Back to the Analog Domain ....................................................... 36

Chapter 4 - First-Order Low-Pass Filter .................................. 39


Analog Filter Network ................................................................................ 39
Digital Filter Network ................................................................................ 42
Difference Equation .................................................................................. 46

v
DSP Filters

Chapter 5 - First-Order High-Pass Filter ................................ 49


Analog Filter Network ................................................................................ 49
Digital Filter Network ................................................................................ 52
Difference Equation .................................................................................. 56

Chapter 6 - Second-Order Low-Pass Filter ........................... 59


Analog Filter Network ................................................................................ 59
Digital Filter Network ................................................................................ 63
Difference Equation .................................................................................. 67

Chapter 7 - Second-Order High-Pass Filter ............................ 69


Analog Filter Network ................................................................................ 69
Digital Filter Network ................................................................................ 73
Difference Equation .................................................................................. 76

Chapter 8 - Second-Order Bandpass Filter ............................. 79


Analog Filter Network ................................................................................ 79
Digital Filter Network ................................................................................ 84
Difference Equation .................................................................................. 90

Chapter 9 - Second-Order Band-Stop Filter ............................ 93


Analog Filter Network ................................................................................ 93
Digital Filter Network ................................................................................ 98
Difference Equation ................................................................................ 105

Chapter 10 - Peaking Filter .................................................. 107


Digital Filter Network .............................................................................. 107
Difference Equation ................................................................................ 113

Chapter 11 - Shelving Filter ................................................. 115


Low-Pass IIR .......................................................................................... 115
Digital Filter Network .............................................................................. 115
Difference Equation ................................................................................ 118
High-Pass IIR ......................................................................................... 120
Digital Filter Network .............................................................................. 120
Difference Equation ................................................................................ 123

vi
Table of Contents

Chapter 12 - Cascaded Low-Pass Filter ................................ 125


Analog Filter Network .............................................................................. 125
Digital Filter Network .............................................................................. 131
Difference Equation ................................................................................ 136

Chapter 13 - Cascaded High-Pass Filter ............................... 139


Analog Filter Network .............................................................................. 139
Digital Filter Network .............................................................................. 145
Difference Equation ................................................................................ 150

Chapter 14 - Cascaded Bandpass Filter ............................... 153


Digital Filter Network .............................................................................. 153
Difference Equation ................................................................................ 161

Chapter 15 - Cascaded Band-Stop Filter .............................. 163


Digital Filter Network .............................................................................. 163
Difference Equation ................................................................................ 171

Section II - Filter Projects ................................. 173


Chapter 16 - Introduction ..................................................... 175
Overview ................................................................................................. 175
Project Outline ........................................................................................ 176

Chapter 17 - Tone Control .................................................... 179


Design Requirements .............................................................................. 181
Filter Overview ........................................................................................ 181
Functional Block Diagram ....................................................................... 185
Flow Diagram Descriptions ...................................................................... 186
Software Description ............................................................................... 191

Chpater 18 - 60 Hz Hum Eliminator ..................................... 205


Design Requirements .............................................................................. 206
Band-Stop Filter Overview ....................................................................... 207
Functional Block Diagram ....................................................................... 208
Implementation ...................................................................................... 211

vii
DSP Filters

Chapter 19 - 31-Band Graphic EQ-I ...................................... 223


Design Requirements .............................................................................. 225
Peaking Filter Overview ........................................................................... 226
Functional Block Diagram ....................................................................... 228
Flow Diagram Descriptions ...................................................................... 230
Software Description ............................................................................... 235

Chapter 20 - 31-Band Graphic EQ-II ..................................... 245


Design Requirements .............................................................................. 245
Bandpass Filter Overview ........................................................................ 246
Functional Block Diagram ....................................................................... 248
Flow Diagram Descriptions ...................................................................... 249
Software Description ............................................................................... 256

Chapter 21 - 4-Band Parametric EQ ..................................... 267


Design Requirements .............................................................................. 270
Filter Overview ........................................................................................ 271
Functional Block Diagram ....................................................................... 276
Flow Diagram Descriptions ...................................................................... 278
Software Description ............................................................................... 284

Chapter 22 - Digital Crossover ............................................. 297


Which Filters Should be Considered in Crossover Designs? ....................... 299
Design Requirements .............................................................................. 308
Filter Overview ........................................................................................ 308
Functional Blocks ................................................................................... 315
Control Flow Descriptions ....................................................................... 317
Software Description ............................................................................... 319

Appendix - Odd-Order Filters ............................................... 335


Digital Low-Pass Filter ............................................................................. 336
Digital High-Pass Filter ............................................................................ 338
Difference Equations ............................................................................... 339

Index .................................................................................... 341

References ........................................................................... 344

viii
1
Introduction

Digital filters and real-time processing of digital signals have tradi-


tionally been beyond the reach of most hobbyists, due partially to hard-
ware cost as well as complexity of design. In recent years, low-cost digi-
tal signal processor (DSP) development boards have been introduced to
the market in a price range within the budget of most home electronics
enthusiasts. However, complexity of design has still been a major hurdle
to the hobbyist, with the result that most DSP development boards have
remained in the hands of the design engineer, as well as students en-
rolled in university engineering programs.
DSP Filters is an attempt to break down this design complexity bar-
rier by means of simplified tutorials and step-by-step instructions along
with a collection of audio projects. This book is written in the spirit of
the Active Filter Cookbook (Don Lancaster, 1982) and Design of Active
Filters with Experiments (Howard Berlin, 1977). These previous books
shattered the complexity barrier of active filter design. In a similar

1
DSP Filters

manner, we have chosen to describe digital filter design by presenting


design formulas needed to build the digital equivalent of standard au-
dio filters: low-pass, high-pass, bandpass, and band-stop, also includ-
ing the more specialized peaking and shelving filters. Section I: Filter
Design Formulas describes the design and analysis formulas of both
analog and the digital equivalents of 14 specific filter types.
The filters described in the active filter design books may be conve-
niently classified as first and second order. Cascading first- and second-
order sections create higher order filters in the appropriate combina-
tions. The particular class of digital filters described in this book in-
cludes those that most closely resemble analog filters — Infinite Impulse
Response (IIR) filters (another name for this type of digital filter is recur-
sive filter). Each class of analog and digital filters is characterized by
poles and zeros in the complex s-plane (for analog) or complex z-plane
(for digital).
The goal of Section I is to present the digital filter design formulas
and compare them to the equivalent analog formulas whenever possible.
No derivations are given with the hope of keeping the technical level
constrained. The only requirement for the reader is the ability to under-
stand algebraic formulas involving trigonometric functions.
Section II: Digital Audio Filter Projects describes implementation
examples of the various filter types using the C++ programming lan-
guage, which will further support the mathematical descriptions. Note
that Section I can be used strictly as a reference section. Therefore, one
strategy for the reader is to skip directly to the Section II projects. The
specific design formulas from Section I needed in each of the projects
are repeated in Section II for convenience.
Five audio projects are described in Section II. These projects have
been chosen for their potential interest to the electronics hobbyist, with
special consideration of the usefulness in illustrating the design and
implementation of concepts presented in the chapters of Section I.

2
Introduction

Unlike active filter design methodologies, DSP filter design is prima-


rily a software task. For that reason, the breadboard with collections of
op amps, resistors, and capacitors, can be substituted for a DSP evalu-
ation module (EVM) development board. Wire-wrap tools, pliers, and a
soldering iron are replaced with DSP tools — the compiler (and/or as-
sembler), simulator, and evaluation module/debugger.
Since most modern DSP development systems begin with a C++
compiler, the authors of this book have chosen to present the projects as
C++ programming examples. This is the most general approach and
does not lock the examples into a single DSP software instruction set.
In summary, DSP Filters has a two-fold purpose:
1. Provide digital filter design formulas, comparing these to the ana-
log filter equivalent whenever possible.
2. Demonstrate these concepts by building, or more specifically, cod-
ing audio projects using the C++ programming language.

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

that permits a signal to be passband


Response [dB]

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

eralized low-pass filter speci-


10 100 1000 10000
fication. This figure repre- Frequency [Hz]
sents the desired output sig- Figure 2-1. Example response
nal level relative to the input specification for a low-pass filter

7
DSP Filters

signal level, over a frequency range (plotted on a logarithmic scale). As


shown in Figure 2-1, the range of frequency covered by the unaltered
signal up to the transition band is known as the passband. The range of
frequency immediately above the point at which the signal is attenuated
below a predefined level is the stopband. The level that defines the point
at which the stopband begins is based on the requirements of the filter
specification, which in turn is dependent on the particular application.
The general design requirements of a low-pass filter can be speci-
fied by the following set of parameters:
• Passband to Transition Band Corner Frequency, f1.
• Passband Ripple: maximum gain variation (in dB) throughout the
passband.
• Transition Band to Stop-band Corner Frequency, f2.
• Stop-band Attenuation: minimum filter attenuation throughout the
stop band.
An example of a physically realizable filter response, the second-
order low-pass filter, is shown in Figure 2-2. The gain, G(f), defined as
output level divided by the input level (usually a function of frequency),
can be conveniently expressed in decibels (dB), as:
Vo (2-1)
dB ≡ 20 log(gain) = 20 log
Vi

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

The transition band response of most Nth-order low-pass filters,


when viewed on a dB versus log frequency plot, often decrease linearly
with frequency. The rate of decrease is dependent upon the order of
the filter and is measured in dB/octave or dB/decade. The rate of de-
crease is often referred to as the filter rolloff. A –6 dB/octave rolloff
occurs for each order of the filter. For example, the rolloff of a first-
order filter is –6 dB/octave, whereas the rolloff of a second-order filter
is –12 dB/octave.
An octave is a doubling or halving of the frequency. For example, the
octaves above 2 kHz are 4 kHz, 8 kHz, 16 kHz, etc., and the octaves
below 2 kHz are 1 kHz, 500 Hz, 250 Hz, etc. A decade is a tenfold in-
crease or decrease in frequency. For example, the decades above 1 kHz
are 10 kHz, 100 kHz, etc., while the decades below 1 kHz are 100 Hz, 10
Hz, and 1 Hz. A –6 dB/octave is equivalent to –20 dB/decade.
Figure 2-3 shows that a solution to the example filter specification
from Figure 2-1 is the second-order low-pass filter of Figure 2-2. The
primary goal of most filter design activities is to determine and verify a
filter design configuration whose characteristics (cutoff frequency and
rolloff) match the filter specifications. Even though this may be the gen-
eral strategy for most filter design problems, we will be taking a different

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

where again, ∆f = f2 – f1.


The center frequency f0 is equal to the geometric mean of f1 and f2 :

f0 = f1 f 2 (2-2b)

The band-edge frequencies f1 and f2 are defined by the following rela-


tionships:
f0  2 
f1 =  1 + 4Q − 1 (2-3a)
2Q  
f0  
 1 + 4Q + 1 (2-3b)
2
f2 =
2Q  

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]

-3 dB (or 0.707) Figure 2-6. Gain


response plot of a
- 10 second-order band-
f1 f2
stop filter with center
frequency fo = 200 Hz
- 20 and Q = 1

- 30 f0

10 100 1000 10000


Frequency [Hz]

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

10 100 1000 10000 10 100 1000 10000


Frequency [Hz] Frequency [Hz]

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

of the low-pass filter, a characteristic frequency, or cutoff frequency fc,


determines the point where the response makes its transition from boost
or cut to the unaltered filter state (with a response of 0 dB) above cutoff.
Like the peaking filter of the previous section, the shelving filter can
either boost or attenuate low frequencies. As with the peaking filter, the
shape of the boost and cut modes are mirror images of each other (see
Figures 2-8a and 2-8b). In addition, the characteristic cutoff frequency
qualitatively controls the point of boost/cut transition. The cutoff fre-
quency fc used to implement the shelving filter is based on the core first-
order filter making up the shelving network. fc determines the –3 dB
point of the core first-order filter.
The shelving filter also works as a high-pass filter, again with sym-
metric boost/cut modes (see Figures 2-8c and 2-8d). Shelving filters are
discussed in greater detail in Chapter 11.

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

frequencies may compensate for some of the lost information by increas-


ing the treble gain control as well. However, excessive treble gain creates
a tinny or thinner sound for those with normal hearing abilities.
Several common applications such as automobile and home stereo
systems, boom boxes, and portable audio players implement tone con-
trols. The design described in this chapter can be used in any of these
applications. First-order low-pass and high-pass shelving filters will be
utilized to implement tone controls. The respective cutoff frequencies
for these filters are fixed depending on the implementation requirements.
100 Hz and 10 kHz are common cutoff frequencies. As shown in Figure
17-1, the frequency at which the first-order shelving filters no longer
affect the signal is called the hinge point. The low-pass and high-pass
hinge points are also fixed depending on the implementation require-
ments and are not necessarily the same. Often the hinge point for the
high-pass shelving filter is lower in frequency than the hinge point for the
low-pass shelving filter. This is not a desirable situation as it allows the

Figure 17-1. Bass and treble tone controls with corresponding frequency response

180
Tone Control

filters to influence the midband frequencies. Ideally, tone controls have a


midband that lies between the bass and treble control bands that is not
affected by either control.
As with most other types of equalizers, the application of tone con-
trols has not drastically changed from its inception. However, the imple-
mentation has evolved from conventional passive and active analog de-
signs to digital implementations. This chapter provides a solution for
implementing a digital 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

Low-pass shelving filter overview


A first-order IIR low-pass shelving filter can be implemented by sum-
ming the input x(n) with the output of a first-order low-pass filter scaled
by µ-1, as shown in Figure 17-2. The low-pass output scale factor is cho-
sen so that when µ = 1, the output is equal to the input, y(n) = x(n). The
coefficient depends on the shelving level g, as µ ≡ 10 g / 20 , where g is the
boost/cut gain in dB.
The frequency and phase response of the low-pass shelving filter is
displayed in Figure 17-3. Note that since the network is first order, the
phase excursion will not exceed ±90°. The maximum and minimum phase
value is controlled by the shelving filter’s boost/cut gain factor g.
The difference equa-
tion describing a low-pass
shelving filter is provided
in Equations (17-1a)
and (17-1b) while the
coefficients α and γ are
determined by Equation
(17-1c). Figure 17-2. IIR low-pass shelving filter

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

High-pass shelving filter overview


A first-order IIR high-pass shelving filter can be implemented by
summing the input x(n) with the output of a first-order high-pass filter
scaled by µ-1, as shown in Figure 17-4. The high-pass output scale factor
is chosen so that when µ = 1, the output is equal to the input, y(n) = x(n).
The coefficient depends on the shelving level g, as µ ≡ 10 g / 20 , where g is
the boost/cut gain in dB.

Figure 17-4. IIR High-pass Shelving Filter

183
DSP Filters

The frequency and phase response of the high-pass shelving filter is


displayed in Figure 17-5. Note that since the network is first order, the
phase excursion will not exceed ±90°. The maximum and minimum phase
value is controlled by the shelving filter boost/cut gain factor g.
The difference equation describing a high-pass shelving filter is pro-
vided in Equations (17-2a) and (17-2b), while the coefficients α and γ are
determined by Equation 17-2c.

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

Functional Block Diagram


The tone control’s functional block diagram is shown in Figure 17-6.
The bass and treble controls provide gain information to the respective
coefficient calculation blocks. These blocks determine the appropriate
coefficients α and γ to be applied to the respective shelving filters
(f1 and f2). f1 corresponds to the low-pass shelving filter, while f2 repre-
sents the high-pass shelving filter. The filters are cascaded together and
operate on the input digital signal. The digital output is the processed
audio signal corresponding to the user’s taste as determined by the bass
and treble control settings.

Figure 17-6. Tone control


functional block diagram

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

Coefficient calculation section


The coefficient calculation blocks receive the bass and treble gain
values from the tone controls. The gain values are provided to the coeffi-
cient calculation blocks in digital form as numbers representing the re-
spective gains. Using these bass and treble gain values, the coefficient
calculation blocks determine the α and γ coefficients required to cor-
rectly implement the corresponding low-pass and high-pass shelving fil-
ters. The coefficients are determined using the difference equations from
Chapter 11 and repeated in this chapter as Equations (17-1a) and (17-2a).

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.

Flow Diagram Descriptions


The following describes the implementation of the coefficient calcu-
lation and filter sections outlined in the block diagram in Figure 17-6.
Both the coefficient calculation and filter sections are described. The
control section is not overviewed as it can be implemented in many ways
in either hardware or software and does not directly effect how the filter
equations are utilized in the implementation of the tone controls. The
coefficient calculation and filter sections operate asynchronously and
are described in more detail in the sections that follow.
Figures 17-7 through 17-10 display flow diagrams utilized for imple-
menting the coefficient calculation section. As shown in Figure 17-7, there
are three system level variables that effect the coefficients α and γ re-
quired to implement the tone controls. The sample rate (fs), shelving filter

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.

Figure 17-7. Coefficient calculation block diagram

Updating the sample rate


Figure 17-8 shows the software
flow diagram implementing the
coefficient changes based on a
change in sample rate (fs). When the
sample rate (fs) changes, the maxi-
mum cutoff frequency (max_f0) is up-
dated. max_f0 must be less than fs/2,
which is the Nyquist frequency. This
is performed in the
calc_coeff_for_new_fs (double dv) rou-
tine. The normalized cutoff
frequency (θ) is dependent upon
the fs and must also be updated
for each high-pass and low-pass Figure 17-8. Flow diagram for updating f
s
shelving filters. This is updated Routine: calc_coeff_for_new_fs(double dv)

187
DSP Filters

by the routine, which is called from calc_coeff_ for_new_f0(double dv, int


i). The coefficients α and γ can then be calculated for each filter from
the updatedθ.
Updating the cutoff frequency
As shown in Figure 17-9, when
the cutoff frequency (f0) is adjusted
there are several parameters to
evaluate. The cutoff frequency (f0)
cannot excede the Nyquist fre-
quency noted here as the maximum
cutoff frequency (max_f0). There is
also a minimum cutoff frequency
(min_f0) that should be evaluated
if, for example, it is desireable to
match a speaker’s frequency range.
Note that this parameter is not nor-
mally adjustable in tone controls.
User controls are not typically pro-
vided to adjust these parameters,
but in this design we have made it
adjustable and the system designer
can choose to extend the control Figure 17-9. Flow diagram for updating f0
to the user or not to give the user Routine:
controls for cutoff frequency. The calc_coeff_for_new_f0(double dv, int i)
normalized cutoff frequency θ is
dependent upon both the cutoff frequency and sample rate and thus
must be recalculated for the corresponding filter whenever either of these
variables changes.

Updating the gain


The third variable, gain (g), also affects the coefficients for the re-
spective shelving filter. This is shown in Figure 17-10. The gain is com-

188
Tone Control

pared to the system designer’s


maximum (max_gain) and mini-
mum gain (min_gain) values.
These may be established based
on available headroom or noise
floor considerations. The gain
factor is used to calculate the
variable µ. α and γ can then be
calculated using µ.

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

Figure 17-11. Flow diagram for Processing Input Sample


Routine: execute_filter_block_in_place(double *in)

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

Figure 17-12. Flow diagram for low-pass shelving filter


Implementation Routine: stage[0]->execute_filter_stage()

Figure 17-13. Flow diagram for high-pass shelving filter


Implementation Routine: stage[1]>execute_filter_stage()

190
Tone Control

calculated resulting in the output scale factor u (not to be confused with


µ—the gain factor). The delayed input sample x1 is updated, the delayed
output sample u1 is updated, and the low-pass shelving filter output is
calculated as y.
The high-pass shelving filter operates similar to the low-pass shelv-
ing filter as shown in Figure 17-13.

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.

Table 17-1. Shelving Filter variable matrix: stages[i]->variable

See also Figure 17-2, which displays the low-pass shelving filter

191
DSP Filters

The software routines described below implement the functions out-


lined by the flow diagrams. There are several routines dedicated to ini-
tialization of the array as well. The following describes the initialization,
coefficient calculation section, and filtering section. Each line of the soft-
ware is numbered to assist in the description of operation.

Initialize the variable matrix


The implementation begins with a description of the initialization
routines required to establish the variable matrix in Table 1 to a known
stable state. Initialization is performed prior to activation of the tone
control. It is performed only once prior to activation or following any
system reset condition. A line-by-line analysis of the initialization routine
is provided here.
// Implementation of Simple Shelving Tone Control Filter Block,

1 const double TWO_PI = 2.0 *3.14159265358979323846;


2 const double PI = 3.14159265358979323846;
3 const int NUM_BANDS = 2;
4 CShelving_Tone::CShelving_Tone()
5 {
6 stages = new (CCookFilterStage * [NUM_BANDS]);
7 stages[0] = new CLP_ShelvingFilterStage;
8 stages[1] = new CHP_ShelvingFilterStage;

// 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.

• Lines 19 through 29 initialize the high-pass filter variables in the


matrix as shown.
Line 20 initializes the minimum cutoff frequency for the high-pass shelving filter.
Line 21 initializes the minimum gain for the high-pass shelving filter.
Line 22 initializes the maximum gain for the high-pass shelving filter.
Line 23 initializes the minimum Q for the high-pass shelving filter. Although Q
is not a controlled high-pass filter variable, it is required overhead in our DirectX
control interface.
Line 24 initializes the maximum Q for the high-pass shelving filter. Although Q
is not a controlled high-pass filter variable, it is required overhead in our DirectX
control interface.
Line 25 initializes the gain for the high-pass shelving filter.
Line 26 initializes the Q for the high-pass shelving filter. Although Q is not a
controlled high-pass filter variable, it is overhead required in our DirectX
control interface.
Line 27 initializes the cutoff frequency for the high-pass shelving filter.
Line 28 initializes the maximum cutoff frequency for the high-pass shelving filter.

• Line 30 establishes num_filter_stages equal to NUM_BANDS, which in


this case is 2.
• Line 31 initializes the variable enabled, which indicates that initializa-
tion has been completed and the tone control can now be enabled.

194
Tone Control

The following routine is the constructor used in the initialization


process and initializes the low-pass shelving filter state variables called
in the initialization routine.
1 CLP_ShelvingFilterStage::CLP_ShelvingFilterStage()
2 {
3 x = 0;
4 y = 0;
5 x1 = 0;
6 u1 = 0;
7 }

The following routine is the constructor used in the initialization


process and initializes the high-pass shelving filter state variables called
in the above initialization routine.
1 CHP_ShelvingFilterStage::CHP_ShelvingFilterStage()
2 {
3 x = 0;
4 y = 0;
5 x1 = 0;
6 u1 = 0;
7 }

Following the completion of the initialization routine, the data con-


tained in the matrix is as shown in Table 1-17.

Coefficient calculation section


Whenever a parameter is adjusted for one or both of the filters such
as sample rate (fs), cutoff frequency (f0), or gain (g) the coefficient calcu-
lation blocks calculate new coefficients. The flow diagrams shown in Fig-
ures 17-8, 17-9, and 17-10 describe the operation of the coefficient cal-
culation blocks. The following routines implement these flow diagrams.
These routines are designed to work together such that they do not du-
plicate operations. For example, when the sample rate changes the rou-

195
DSP Filters

tine calc_coeff_for_new_fs (double dv) is called. It performs functions


unique to sample rate change and then calls the routine
calc_coeff_for_new_f0 (double dv, int i) that will then call the routine
calc_coeff_for_new_gain (double dv, int i) to complete the necessary
changes. calc_coeff_for_new_f0 (double dv, int i) performs operations
related to f0 and fs while calc_coeff_for_new_gain (double dv, int i) )
performs operations related to f0, fs and gain.
1 void CShelving_Tone::calc_coeff_for_new_fs (double dv)
2 {
3 int i;
4 fs = dv;
5 for (i = 0; i< NUM_BANDS; i++)
6 {
7 stages[i]->max_f0 = fs/2.0 -1.0;
8 calc_coeff_for_new_f0 (stages[i]->f0, i);
9 }
10 }

• Lines 1, 2, and 10 define the boundaries of this routine. Everything


between these lines constitutes the calc_coeff_for_new_fs (double dv)
routine.
• Line 3 defines the index i.
• Line 4 establishes the variable fs to be equal to the value of the passed
parameter dv in Hertz.
• Lines 5, 6, and 9 define a loop that recalculates coefficients based on
a sample rate change. This loop operates twice, once for the low-pass
shelving filter (column 0) and once for the high-pass shelving filter
(column 1).
• Line 7 calculates the maximum cutoff frequency value base on Nyquist’s
theorem. The maximum cutoff frequency should not exceed one-half
of the sample rate frequency.

196
Tone Control

• Line 8 calls the routine calc_coeff_for_new_f0 (stages[i]->f0, i) shown


below, which will pass the existing cutoff frequency f0 and the index i
indicating which filter to process. The calc_coeff_for_new_f0(double dv,
int i) routine will calculate θ for each filter based on the updated sample
rate and subsequently update the respective coefficients α and γ.

Typically the cutoff frequency in a tone control is constant, but in


the event the implementation requires a variable cutoff frequency the
following routine can be used to make the appropriate coefficient changes.
This routine is called either when one of the filter’s cutoff frequencies
has been altered or from the calc_coeff_for_new_fs (double dv) routine.
The routine calc_coeff_for_new_f0 (double dv, int i) will operate on either
filter (high-pass shelving or low-pass shelving) as defined by the index i
passed to it. The value dv passed to the routine is the cutoff frequency in
hertz. The following describes the operation of this routine.

1 void CShelving_Tone::calc_coeff_for_new_f0 (double dv, int i)


2 {
3 CCookFilterStage *st = stages[i];
4 if (dv > st->max_f0) dv = st->max_f0;
5 else if (dv < st->min_f0) dv = st->min_f0;
6 st->f0 = dv;
7 st->theta0 = TWO_PI * st->f0/fs;
8 calc_coeff_for_new_gain (st->gain, i);
9 }

• Lines 1, 2, and 9 define the boundaries of this routine. Everything


between these lines constitutes the calc_coeff_for_new_f0 (double dv,
int i) routine.
• Line 3 establishes st as a pointer to the variables in column i of the
variables matrix. If the value 0 is passed to the routine as the index i
then st points to the variables in column 0 of the matrix (low-pass
shelving filter). If the value 1 is passed to the routine as the index i
then st points to the variables in column 1 of the matrix (high-pass
shelving filter).

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.

Coefficients α and γ are modified only by the calc_coeff_for_new_gain


(double dv, int i) routine below. This routine can be called by directly alter-
ing the bass or treble gain or from either of the previous two routines
calc_coeff_for_new_fs (double dv) and calc_coeff_for_new_f0 (double dv, int
i). In the former case, the new gain is passed to the routine as the vari-
able dv along with the filter designation denoted by the index i. This
routine will be called if any updates are to be made to the coefficients
due to changes in sample rate, cutoff frequency, or gain.

1 void CShelving_Tone::calc_coeff_for_new_gain (double dv, int i)


2 {
3 CCookFilterStage *st = stages[i];

4 if (dv > st->max_gain) dv = st->max_gain;


5 else if (dv < st->min_gain) dv = st->min_gain;

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 }

• Lines 1, 2, and 18 define the boundaries of this routine. Everything


between these lines constitutes the calc_coeff_for_new_gain (double dv,
int i) routine.
• Line 3 establishes st as a pointer to the variables in column i of the
variables matrix. If the value 0 is passed to the routine as the index i
then st points to the variables in column 0 (low-pass shelving filter) of
the matrix. If the value 1 is passed to the routine as the index i then st
points to the variables in column 1 (high-pass shelving filter) of
the matrix.
• Line 4 tests the gain dv to verify that it does not exceed the maximum
gain (max_gain) established in the column pointed to by st. If the
gain exceeds the maximum gain value then it is set equal to the
maximum gain.
• Line 5 tests the gain dv to verify that it is not less than the minimum
gain boundaries (min_gain) established in the column pointed to by st.
If the gain is less than the minimum gain value then it is set equal to
the minimum gain.

199
DSP Filters

• Line 6 sets the variable gain in the column pointed to by st of the


matrix equal to the gain value passed to the routine dv.
• Line 7 calculates µ in the column pointed to by st given the gain in the
column pointed to by st as 10(g/20).
• Lines 8 through 12 determine the coefficients α and γ for the high-pass
shelving filter when the index i is 1 indicating operation using variables
in column 1.
Lines 8, 9, and 12 define the boundaries of the if statement given that i equals 1.

• Lines 10 and 11 calculate α and γ respectively based on Equation 17-2c.


• Lines 13 through 17 determine the coefficients α and γ for the low-
pass shelving filter when the index i is 0 indicating operation using
variables in column 0.
Lines 13, 14, and 17 define the boundaries of the if statement given that i equals 0.
Line 15 and 16 calculates a and g respectively based on Equation 17-1c.

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

• Lines 1, 2, and 10 define the boundaries of this routine. Everything


between these lines constitutes the execute_filter_block_in_place (double
*in) routine.
• Line 3 defines the index i.
• Line 4 defines input to be a value equal to the value of the sample
pointed to by *in. *in points to the next sample to be processed by the
filter section.
• Line 5 copies the sample (input) to the matrix variable x location in
column 0 (low-pass shelving filter).
• Line 6 calls the routine execute_filter_stage() which processes the input
sample using the low-pass shelving filter as noted by index i=0. All
processing parameters for execute_filter_stage() will come from col-
umn 0 of the matrix.
• Line 7 copies the low-pass filter’s output sample stages[0]->y (where y
denotes the output sample and 0 refers to the matrix column) to the
high-pass filter’s input sample stages[1]->x (where x denotes the input
sample and 1 refers to the matrix column).
• Line 8 calls the routine execute_filter_stage() which processes the
sample using the high-pass shelving filter as noted by index i=1. All
processing parameters for execute_filter_stage() will come from col-
umn 1 of the matrix.
• Line 9 stores the output sample of the high-pass shelving filter,
stages[1]->y (where y denotes the output sample and 1 refers to the
matrix column) in the memory location pointed to by *in.

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

The execute_filter_stage() implements the second-order IIR shelv-


ing filter as shown in network diagram in Figures 17-2 and 17-4. The
coefficients and state variables are stored in the respective columns
of the matrix for each filter. The variables utilized in this routine are
listed below:
x – Input sample
u – lowpass filter Output sample
x1 – past input sample
u1 – past lowpass filter output sample
alpha, gamma – filter coefficients
mu – gain coefficient

This routine implements the low-pass shelving filter difference equa-


tion, Equation (17-1a), and the flow diagram shown in Figure 17-12. The
following describes this routine.

// Implementation of a simple LP_Shelving Filter Stage,


// class CLP_ShelvingFilterStage

1 void CLP_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 }
• Lines 1, 2, and 8 define the boundaries of this routine.
• Line 3 defines the variable u.
• Line 4 calculates the low-pass filter output value u.
• Lines 5 and 6 update the filter state variables (x1, u1) for the low-pass
shelving filter.
• Line 7 calculates the low-pass shelving filter’s output value and stores
it in the variable y.

202
Tone Control

This routine implements the high-pass shelving filter difference equa-


tion, Equation (17-2a), and the flow diagram shown in Figure 17-12. The
following describes this routine.
// Implementation of a simple HP_Shelving Filter Stage,
// class CHP_ShelvingFilterStage

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 }

• Lines 1, 2, and 8 define the boundaries of this routine.


• Line 3 defines the variable u.
• Line 4 calculates the high-pass filter output value u.
• Lines 5 and 6 update the filter state variables (x1, u1) for the high-pass
shelving filter.
• Line 7 calculates the high-pass shelving filter’s output value and stores
it in the variable y.

Disabling the tone control in a DirectX application requires a de-


structor routine that gives up memory to the rest of the system. Embed-
ded applications on dedicated hardware do not require this routine. It is
provided here for completeness. The ~CShelving_Tone() routine clears
the memory used for the matrix and gives it back to the system.
1 CShelving_Tone::~CShelving_Tone()
2 {
3 int i;
4 if (stages)

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

You might also like