# Elementary Audio Digital Filters

This appendix is devoted to small but useful digital filters that are commonly used in audio applications. Analytical tools from the main chapters are used to analyze these ``audio gems''.## Elementary Filter Sections

This section gives condensed analysis summaries of the four most elementary digital filters: the one-zero, one-pole, two-pole, and two-zero filters. Despite their relative simplicity, they are quite valuable to master in practice. In particular, recall from Chapter 9 that every causal, finite-order, LTI filter (any difference equation of the form Eq.(5.1)) may be factored into a*series and/or parallel combination*of such sections. Implementing high-order filters as parallel and/or series combinations of low-order sections offers several advantages, such as numerical robustness and easier/safer control in real time.

### One-Zero

Figure B.1 gives the signal flow graph for the general one-zero filter. The frequency response for the one-zero filter may be found by the following steps:### One-Pole

Fig.B.3 gives the signal flow graph for the general one-pole filter. The road to the frequency response goes as follows:*boost*in the passband rather than

*attenuation*in the stopband; thus it is usually desirable when using a one-pole filter to set to a small value, such as , so that the peak gain is 1 or so. When the peak gain is 1, the filter is unlikely to overflow.

^{B.1}Finally, note that the one-pole filter is stable if and only if .

### Two-Pole

The signal flow graph for the general two-pole filter is given in Fig.B.5. We proceed as usual with the general analysis steps to obtain the following:*denominator coefficients*, and they determine the two

*poles*of . Using the quadratic formula, the poles are found to be located at

*multiply pointwise*two magnitude plots such as Fig.B.4a, and

*add pointwise*two phase plots such as Fig.B.4b. When the poles are complex, they can be written as

*polar form*as

*radius*, or distance from the origin in the -plane. As discussed in Chapter 8, we must have for stability of the two-pole filter. The angles are the poles' respective

*angles*in the plane. The pole angle corresponds to the

*pole frequency*via the relation

*resonance*

^{B.2}at radian frequency . We may call or the

*center frequency*of the resonator. Note, however, that the resonance frequency is not usually the precise frequency of

*peak-gain*in a two-pole resonator (see Fig.B.9 on page ). The peak of the amplitude response is usually a little different because each pole sits on the other's ``skirt,'' which is slanted. (See §B.1.5 and §B.6 for an elaboration of this point.) Using polar form for the (complex) poles, the two-pole transfer function can be expressed as

Comparing this to the transfer function derived from the difference equation, we may identify The difference equation can thus be rewritten as

Note that coefficient depends only on the pole radius R (which determines damping) and is independent of the resonance frequency, while is a function of both. As a result, we may

*retune*the resonance frequency of the two-pole filter section by modifying only. The gain at the resonant frequency , is found by substituting into Eq.(B.1) to get

See §B.6 for details on how the resonance gain (and peak gain) can be normalized as the tuning of is varied in real time. Since the radius of both poles is , we must have for filter stability (§8.4). The closer is to 1, the higher the gain at the resonant frequency . If , the filter degenerates to the form , which is a nothing but a scale factor. We can say that when the two poles move to the origin of the plane, they are canceled by the two zeros there.

#### Resonator Bandwidth in Terms of Pole Radius

The*magnitude*of a complex pole determines the

*damping*or

*bandwidth*of the resonator. (Damping may be defined as the reciprocal of the bandwidth.) As derived in §8.5, when is close to 1, a reasonable definition of 3dB-bandwidth is provided by

where is the pole radius, is the bandwidth in Hertz (cycles per second), and is the sampling interval in seconds. Figure B.6 shows a family of frequency responses for the two-pole resonator obtained by setting and varying . The value of in all cases is , corresponding to . The analytic expressions for amplitude and phase response are

### Two-Zero

The signal flow graph for the general two-zero filter is given in Fig.B.7, and the derivation of frequency response is as follows:*numerator coefficients*, and they determine the two

*zeros*. Using the quadratic formula for finding the roots of a second-order polynomial, we find that the zeros are located at

*notch frequency*, or

*antiresonance frequency*. The closer R is to 1, the narrower the notch centered at . The approximate relation between bandwidth and given in Eq.(B.5) for the two-pole resonator now applies to the

*notch width*in the two-zero filter. Figure B.8 gives some two-zero frequency responses obtained by setting to 1 and varying . The value of , is again . Note that the response is exactly analogous to the two-pole resonator with notches replacing the resonant peaks. Since the plots are on a linear magnitude scale, the two-zero amplitude response appears as the reciprocal of a two-pole response. On a dB scale, the two-zero response is an upside-down two-pole response.

### Complex Resonator

Normally when we need a resonator, we think immediately of the two-pole resonator. However, there is also a*complex one-pole resonator*having the transfer function

where is the single complex pole, and is a scale factor. In the time domain, the complex one-pole resonator is implemented as

*z*transform of the transfer function, we can write down the impulse response of the complex one-pole resonator by recognizing Eq.(B.6) as the closed-form sum of an infinite geometric series, yielding

*unit step function*:

*complex sinusoidal oscillator*at radian frequency rad/sec. If we like, we can extract the real and imaginary parts separately to create both a sine-wave and a cosine-wave output:

*phase-quadrature sinusoids*, since their phases differ by 90 degrees. The phase quadrature relationship for two sinusoids means that they can be regarded as the real and imaginary parts of a complex sinusoid. By allowing to be complex,

*real*resonator in that the resonance occurs only for one positive or negative frequency , but not both. As a result, the resonance frequency is also the frequency where the

*peak-gain*occurs; this is only true in general for the complex one-pole resonator. In particular, the peak gain of a real two-pole filter does not occur exactly at resonance, except when , , or . See §B.6 for more on peak-gain versus resonance-gain (and how to normalize them in practice).

#### Two-Pole Partial Fraction Expansion

Note that every real two-pole resonator can be broken up into a sum of two complex one-pole resonators:where and are constants (generally complex). In this ``parallel one-pole'' form, it can be seen that the peak gain is no longer equal to the resonance gain, since each one-pole frequency response is ``tilted'' near resonance by being summed with the ``skirt'' of the other one-pole resonator, as illustrated in Fig.B.9. This interaction between the positive- and negative-frequency poles is minimized by making the resonance sharper ( ), and by separating the pole frequencies . The greatest separation occurs when the resonance frequency is at one-fourth the sampling rate ( ). However, low-frequency resonances, which are by far the most common in audio work, suffer from significant overlapping of the positive- and negative-frequency poles.

*partial fraction expansion*(PFE) (discussed more fully in §6.8). Note that the inverse

*z*transform of a sum of one-pole transfer functions can be easily written down by inspection. In particular, the impulse response of the PFE of the two-pole resonator (see Eq.(B.7)) is clearly

### The BiQuad Section

The term ``biquad'' is short for ``bi-quadratic'', and is a common name for a two-pole, two-zero digital filter. The*transfer function*of the biquad can be defined as

where can be called the

*overall gain*of the biquad. Since both the numerator and denominator of this transfer function are quadratic polynomials in (or ), the transfer function is said to be ``bi-quadratic'' in (or ). As derived in §B.1.3, for real second-order polynomials having complex roots, it is often convenient to express the polynomial coefficients in terms of the radius and angle of the positive-frequency pole. For example, denoting the denominator polynomial by , we have

*resonance frequency*(in radians per sample-- , where is the resonance frequency in Hz), and determines the ``Q'' of the resonance (see §B.1.3). The numerator is less often represented in this way, but when it is, we may think of the zero-angle as the

*antiresonance frequency*, and the zero-radius affects the

*depth*and

*width*of the antiresonance (or

*notch*). As discussed on page , a common setting for the zeros when making a resonator is to place one at (dc) and the other at (half the sampling rate),

*i.e.*, and in Eq.(B.8) above . This zero placement normalizes the peak gain of the resonator if it is swept using the parameter. Using the shift theorem for

*z*transforms, the

*difference equation*for the biquad can be written by inspection of the transfer function as

*direct-form I*implementation. (To obtain the official direct-form I structure, the overall gain must be not be pulled out separately, resulting in feedforward coefficients instead. See Chapter 9 for more about filter implementation forms.)

### Biquad Software Implementations

In matlab, an efficient biquad section is implemented by callingoutputsignal = filter(B,A,inputsignal);where

`BiQuad`STK class.) Figure B.10 lists an example biquad implementation in the

`C`programming language.

typedef double *pp; // pointer to array of length NTICK typedef word double; // signal and coefficient data type typedef struct _biquadVars { pp output; pp input; word s2; word s1; word gain; word a2; word a1; word b2; word b1; } biquadVars; void biquad(biquadVars *a) { int i; dbl A; word s0; for (i=0; i<NTICK; i++) { A = a->gain * a->input[i]; A -= a->a1 * a->s1; A -= a->a2 * a->s2; s0 = A; A += a->b1 * a->s1; a->output[i] = a->b2 * a->s2 + A; a->s2 = a->s1; a->s1 = s0; } } |

## Allpass Filter Sections

The*allpass filter*passes all frequencies with equal gain. This is in contrast with a lowpass filter, which passes only low frequencies, a highpass which passes high-frequencies, and a bandpass filter which passes an interval of frequencies. An allpass filter may have any phase response. The only requirement is that its amplitude response be constant. Normally, this constant is . From a physical modeling point of view, a unity-gain allpass filter models a

*lossless system*in the sense that it

*preserves signal energy*. Specifically, if denotes the input to an allpass filter , and if denotes its output, then we have

This equation says that the total energy out equals the total energy in. No energy was created or destroyed by the filter. All an allpass filter can do is delay the sinusoidal components of a signal by differing amounts. Appendix C proves that Eq.(B.9) holds if and only if

### The Biquad Allpass Section

The general biquad transfer function was given in Eq.(B.8) to be^{B.3}

### Allpass Filter Design

There is a fairly large literature thread on the topic of*allpass filter design*. Generally, they fall into two main categories:

*parametric*and

*nonparametric*methods. Parametric methods can produce allpass filters with optimal group-delay characteristics [42,41]. Nonparametric methods, while suboptimal, can design very large-order allpass filters, and errors can usually be made arbitrarily small by increasing the order [100,70,1], [78, pp. 60,172]. In music applications, it is usually the case that the ``optimality'' criterion is unknown because it depends on aspects of sound perception (see, for example, [35,72]). As a result, perceptually weighted nonparametric methods can often outperform optimal parametric methods in terms of cost/performance. For a nonparametric method that can design very high-order allpass filters according to highly flexible criteria, see [1].

## DC Blocker

The dc blocker is an indispensable tool in digital waveguide modeling [86] and other applications.^{B.4}It is often needed to remove the dc component of the signal circulating in a delay-line loop. It is also often an important tool in multi-track recording, where dc components in the various tracks can add up and overflow the mix. The

*dc blocker*is a small recursive filter specified by the difference equation

Thus, there is a zero at dc () and a pole near dc at . Far away from dc, the pole and zero approximately cancel each other. (Recall the graphical method for determining frequency response magnitude described in Chapter 8.)

### DC Blocker Frequency Response

Figure B.11 shows the frequency response of the dc blocker for several values of . The same plots are given over a log-frequency scale in Fig.B.12. The corresponding pole-zero diagrams are shown in Fig.B.13. As approaches , the notch at dc gets narrower and narrower. While this may seem ideal, there is a drawback, as shown in Fig.B.14 for the case of : The impulse response duration increases as . While the ``tail'' of the impulse response lengthens as approaches 1, its initial magnitude decreases. At the limit, , the pole and zero cancel at all frequencies, the impulse response becomes an impulse, and the notch disappears.### DC Blocker Software Implementations

In plain`C`, the difference equation for the dc blocker could be written as follows:

y = x - xm1 + 0.995 * ym1; xm1 = x; ym1 = y;Here,

`x`denotes the current input sample, and

`y`denotes the current output sample. The variables

`xm1`and

`ym1`hold once-delayed input and output samples, respectively (and are typically initialized to zero). In this implementation, the pole is fixed at , which corresponds to an adaptation time-constant of approximately samples. A smaller value allows faster tracking of ``wandering dc levels'', but at the cost of greater low-frequency attenuation. A complete C++ class implementing a dc blocking filter is included in the free, open-source Synthesis Tool Kit (STK) [15]. (See the

`DCBlock`STK class.) For a discussion of issues and solutions related to fixed-point implementations, see [7].

## Low and High Shelving Filters

The analog transfer function for a*low shelf*is given by [103]

*transition frequency*dividing low and high frequency regions is . See Appendix E for a development of -plane analysis of analog (continuous-time) filters. A

*high shelf*is obtained from a low shelf by the conformal mapping , which interchanges high and low frequencies,

*i.e.*,

^{B.5}Low and high shelf filters are typically implemented in series, and are typically used to give a little boost or cut at the extreme low or high end (of the spectrum), respectively. To provide a boost or cut near other frequencies, it is necessary to go to (at least) a second-order section, often called a ``peaking equalizer,'' as described in §B.5 below.

#### Exercise

Perform the bilinear transform defined above and calculate the coefficients of a first-order*digital*low shelving filter. Find the pole and zero as a function of , , and . Set and verify that you get a gain of . Set and verify that you get a gain of 1 there.

## Peaking Equalizers

A*peaking equalizer*filter section provides a boost or cut in the vicinity of some center frequency. It may also be called a

*parametric equalizer*section. The gain far away from the boost or cut is unity, so it is convenient to combine a number of such sections in series. Additionally, a high and/or low shelf (§B.4 above) are nice to include in series with one's peaking eq sections. The analog transfer function for a

*peak filter*is given by [103,5,6]

*boost*is obtained at frequency . For , a

*cut filter*is obtained at that frequency. In particular, when , there are infinitely deep notches at , and when , the transfer function reduces to (no boost or cut). The parameter controls the

*width*of the boost or cut. It is easy to show that both zeros and both poles are on the unit circle in the left-half plane, and when (a ``cut''), the zeros are closer to the axis than the poles. Again, the bilinear transform can be used to convert the analog peaking equalizer section to digital form. Figure B.15 gives a matlab listing for a peaking EQ section. Figure B.16 shows the resulting plot for an example call:

boost(2,0.25,0.1);The frequency-response utility

`myfreqz`, listed in Fig.7.1, can be substituted for

`freqz`.

function [B,A] = boost(gain,fc,bw,fs); %BOOST - Design a boost filter at given gain, center % frequency fc, bandwidth bw, and sampling rate fs % (default = 1). % % J.O. Smith 11/28/02 % Reference: Zolzer: Digital Audio Signal Processing, p. 124 if nargin<4, fs = 1; end if nargin<3, bw = fs/10; end Q = fs/bw; wcT = 2*pi*fc/fs; K=tan(wcT/2); V=gain; b0 = 1 + V*K/Q + K^2; b1 = 2*(K^2 - 1); b2 = 1 - V*K/Q + K^2; a0 = 1 + K/Q + K^2; a1 = 2*(K^2 - 1); a2 = 1 - K/Q + K^2; A = [a0 a1 a2] / a0; B = [b0 b1 b2] / a0; if nargout==0 figure(1); freqz(B,A); title('Boost Frequency Response') end |

`peak_eq`in

`filter.lib`distributed with Faust (Appendix K) starting with version

`0.9.9.4k-par`.

## Time-Varying Two-Pole Filters

It is quite common to want to*vary*the resonance frequency of a resonator in real time. This is a special case of a

*tunable filter*. In the pre-digital days of analog synthesizers, filter modules were tuned by means of

*control voltages*, and were thus called

*voltage-controlled filters*

*(VCF)*. In the digital domain, control voltages are replaced by

*time-varying filter coefficients*. In the time-varying case, the choice of filter structure has a profound effect on how the filter characteristics vary with respect to coefficient variations. In this section, we will take a look at the time-varying two-pole resonator. Evaluating the transfer function of the two-pole resonator (Eq.(B.1)) at the point on the unit circle (the filter's

*resonance frequency*) yields a

*gain at resonance*equal to

For simplicity, let in what follows. In the special cases (resonance at dc) and (resonance at ), we have

Since is real, we have already found the gain (amplitude response) at a dc or resonance:

*i.e.*, . An important fact we can now see is that

*the gain at resonance depends markedly on the resonance frequency*. In particular, the ratio of the two cases just analyzed is

*unbounded*! The sharper the resonance (the closer is to 1), the greater the disparity in the gain. Figure B.17 illustrates a number of resonator frequency responses for the case . (Resonators in practice may use values of even closer to 1 than this--even the case is used for making recursive digital sinusoidal oscillators [90].) For resonator tunings at dc and , we predict the resonance gain to be dB, and this is what we see in the plot. When the resonance is tuned to , the gain drops well below 40 dB. Clearly, we will need to compensate this gain variation when trying to use the two-pole digital resonator as a tunable filter.

*complex one-pole resonator*, for and 10 values of . In this case, we expect the frequency response evaluated at the center frequency to be . Thus, the gain at resonance for the plotted example is db for all tunings. Furthermore, for the complex resonator, the resonance gain is also exactly equal to the

*peak gain*.

### Normalizing Two-Pole Filter Gain at Resonance

The question we now pose is how to best compensate the*tunable*two-pole resonator of §B.1.3 so that its peak gain is the same for all tunings. Looking at Fig.B.17, and remembering the graphical method for determining the amplitude response,

^{B.6}it is intuitively clear that we can help matters by adding two

*zeros*to the filter, one near dc and the other near . A zero exactly at dc is provided by the term in the transfer function numerator. Similarly, a zero at half the sampling rate is provided by the term in the numerator. The series combination of both zeros gives the numerator . The complete second-order transfer function then becomes

Checking the gain for the case , we have

### Constant Resonance Gain

It turns out it is possible to normalize*exactly*the

*resonance gain*of the second-order resonator tuned by a single coefficient [89]. This is accomplished by placing the two zeros at , where is the radius of the complex-conjugate pole pair . The transfer function numerator becomes , yielding the total transfer function

### Peak Gain Versus Resonance Gain

While the constant resonance-gain filter is very well behaved, it is not ideal, because, while the*resonance gain*is perfectly normalized, the

*peak gain*is not. The amplitude-response peak does not occur exactly at the resonance frequencies except for the special cases , , and . At other resonance frequencies, the peak due to one pole is

*shifted*by the presence of the other pole. When is close to 1, the shifting can be negligible, but in more damped resonators,

*e.g.*, when , there can be a significant difference between the gain at resonance and the true peak gain. Figure B.20 shows a family of amplitude responses for the constant resonance-gain two-pole, for various values of and . We see that while the gain at resonance is exactly the same in all cases, the actual

*peak*gain varies somewhat, especially near dc and when the two poles come closest together. A more pronounced variation in peak gain can be seen in Fig.B.21, for which the pole radii have been reduced to .

### Constant Peak-Gain Resonator

It is surprisingly easy to normalize exactly the*peak gain*in a second-order resonator tuned by a single coefficient [94]. The filter structure that accomplishes this is the one we already considered in §B.6.1:

That is, the two-pole resonator normalized by zeros at has the constant peak-gain property when it has resonant peaks in its response at all. Note, however, that the peak-gain frequency and the pole-resonance frequency (cf. §B.6.3), are generally two different things, as elaborated below. This structure has the added bonus that its difference equation requires only one more addition relative to the unnormalized two-pole resonator, and no new multiply. Real-time audio ``plugins'' based on the constant-peak-gain resonator are developed in Appendix K. The peak gain is , so multiplying the transfer function by normalizes the peak gain to one for all tunings. It can also be shown [94] that the peak gain coincides with the

*variance gain*when the resonator is driven by white noise. That is, if the variance of the driving noise is , the variance of the noise at the resonator output is . Therefore, scaling the resonator input by will normalize the resonator such that the output signal power equals the input signal power when the input signal is white noise. Frequency response overlays for the constant-peak-gain resonator are shown in Fig.B.23 (), Fig.B.20 (), and Fig.B.21 (). While the peak frequency may be far from the resonance tuning in the more heavily damped examples, the peak gain is always normalized to unity. The normalized radian frequency at which the peak gain occurs is related to the pole angle by [94]

When the right-hand side of the above equation exceeds 1 in magnitude, there is no (real) solution for the pole frequency . This happens, for example, when is less than 1 and is too close to 0 or . Conversely, given any pole angle , there always exists a solution for the peak frequency , since when . However, when is small, the peak frequency can be far from the pole resonance frequency, as shown in Fig.B.22. Thus, must be close to 1 to obtain a resonant peak near dc (a case commonly needed in audio work) or half the sampling rate (rarely needed in practice). When is much less than 1, the peak frequency cannot leave a small interval near one-fourth the sampling rate, as can be seen at the far left in Fig.B.22. Figure B.22 predicts that for , the lowest peak-gain frequency should be around radian per sample. Figure B.21 agrees with this prediction. As Figures B.23 through B.25 show, the peak gain remains constant even at very low and very high frequencies, to the extent they are reachable for a given . The zeros at dc and preclude the possibility of peaks at exactly those frequencies, but for near 1, we can get very close to having a peak at dc or , as shown in Figures B.19 and B.20.

### Four-Pole Tunable Lowpass/Bandpass Filters

As a practical note, it is worth mentioning that in popular analog synthesizers (both real and virtual^{B.7}), VCFs are typically

*fourth order*rather than second order as we have studied here. Perhaps the best known VCF is the

*Moog VCF*. The four-pole Moog VCF is configured to be a lowpass filter with an optional resonance near the cut-off frequency. When the resonance is strong, it functions more like a resonator than a lowpass filter. Various methods for digitizing the Moog VCF are described in [95]. It turns out to be nontrivial to preserve all desirable properties of the analog filter (such as frequency response, order, and control structure), when translated to digital form by standard means.

## Elementary Filter Problems

See`http://ccrma.stanford.edu/~jos/filtersp/Elementary_Filter_Problems.html`.

**Next Section:**

Allpass Filters

**Previous Section:**

Background Fundamentals