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 combinationof 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: Signal flow graph for the general one-zero filter
$ y(n) = b_0x(n) + b_1 x(n - 1)$.
\begin{figure}\input fig/kfig2p17.pstex_t
\end{figure}

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:

\fbox{
\begin{tabular}{rl}
Difference equation: & $y(n) = b_0x(n) + b_1x(n - 1)$...
...requency response: & $H(e^{j\omega T}) = b_0 + b_1e^{-j\omega T}$
\end{tabular}}

By factoring out $ e^{-j\omega T/2}$ from the frequency response, to balance the exponents of $ e$, we can get this closer to polar form as follows:

\begin{eqnarray*}
H(e^{j\omega T}) &=& b_0 + b_1 e^{-j\omega T}\\
&=& (b_0 - ...
...omega T/2)\\
&=& (b_0 - b_1) + e^{-j\pi f T} 2b_1\cos(\pi f T)
\end{eqnarray*}

Figure B.2: Family of frequency responses of the one-zero filter
$ y(n) = x(n) + b_1 x(n - 1)$
for various values of $ b_1$. (a) Amplitude response. (b) Phase response.
\begin{figure}\input fig/kfig2p19.pstex_t
\end{figure}

We now apply the general equations given in Chapter 7 for filter gain $ G(\omega)$ and filter phase $ \Theta(\omega)$ as a function of frequency:

\begin{eqnarray*}
H(e^{j\omega T}) &=& b_0 + b_1e^{-j\omega T}\\
&=& b_0 + b_1...
...left[\frac{-b_1 \sin(\omega T)}{b_0 + b_1 \cos(\omega T)}\right]
\end{eqnarray*}

A plot of $ G(\omega)$ and $ \Theta(\omega)$ for $ b_0 = 1$ and various real values of $ b_1$, is given in Fig.B.2. The filter has a zero at $ z = -b_1/b_0 = -b_1$ in the $ z$ plane, which is always on the real axis. When a point on the unit circle comes close to the zero of the transfer function the filter gain at that frequency is low. Notice that one real zero can basically make either a highpass ( $ b_1/b_0 < 0$) or a lowpass filter ( $ b_1/b_0 > 0$). For the phase response calculation using the graphical method, it is necessary to include the pole at $ z=0$.


One-Pole

Figure B.3: Signal flow graph for the general one-pole filter
$ y(n) = b_0 x(n) - a_1 y(n - 1).$
\begin{figure}\input fig/kfig2p20.pstex_t
\end{figure}

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

Figure B.4: Family of frequency responses of the one-pole filter
$ y(n) = x(n) - a_1 y(n - 1)$
for various real values of $ a_1$. (a) Amplitude response. (b) Phase response.
\begin{figure}\input fig/kfig2p21.pstex_t
\end{figure}

\fbox{
\begin{tabular}{rl}
Difference equation: & $y(n) = b_0 x(n) - a_1 y(n-1)...
...$H(e^{j\omega T}) = \displaystyle\frac{b_0}{1+a_1e^{-j\omega T}}$
\end{tabular}}

The one-pole filter has a transfer function (hence frequency response) which is the reciprocal of that of a one-zero. The analysis is thus quite analogous. The frequency response in polar form is given by

\begin{eqnarray*}
G(\omega) &=& \frac{\vert b_0\vert}{\sqrt{[1 + a_1 \cos(\omega...
... + a_1 \cos(\omega T)}\right], & b_0<0 \\
\end{array} \right..
\end{eqnarray*}

A plot of the frequency response in polar form for $ b_0 = 1$ and various values of $ a_1$ is given in Fig.B.4.

The filter has a pole at $ z = -a_1$, in the $ z$ plane (and a zero at $ z$ = 0). Notice that the one-pole exhibits either a lowpass or a highpass frequency response, like the one-zero. The lowpass character occurs when the pole is near the point $ z = 1$ (dc), which happens when $ a_1$ approaches $ -1$. Conversely, the highpass nature occurs when $ a_1$ is positive.

The one-pole filter section can achieve much more drastic differences between the gain at high frequencies and the gain at low frequencies than can the one-zero filter. This difference is achieved in the one-pole by gain boost in the passband rather than attenuation in the stopband; thus it is usually desirable when using a one-pole filter to set $ b_0$ to a small value, such as $ 1 -
\left\vert a_1\right\vert$, 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 $ \left\vert a_1\right\vert < 1$.


Two-Pole

Figure B.5: Signal flow graph for the general two-pole filter
$ y(n) = b_0 x(n) - a_1 y(n - 1) - a_2 y(n - 2)$.
\begin{figure}\input fig/twopole.pstex_t
\end{figure}

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:

\fbox{
\begin{tabular}{rl}
Difference equation: & $y(n) = b_0 x(n) - a_1 y(n-1)...
... \displaystyle\frac{b_0}{1+a_1e^{-j\omega T}+a_2e^{-j2\omega T}}$
\end{tabular}}

The numerator of $ H(z)$ is a constant, so there are no zeros other than two at the origin of the $ z$ plane.

The coefficients $ a_1$ and $ a_2$ are called the denominator coefficients, and they determine the two poles of $ H(z)$. Using the quadratic formula, the poles are found to be located at

$\displaystyle z = -\frac{a_1}{2} \pm \sqrt{\left(\frac{a_1}{2}\right)^2 -a_2}.
$

When the coefficients $ a_1$ and $ a_2$ are real (as we typically assume), the poles must be either real (when $ (a_1/2)^2\geq a_2$) or form a complex conjugate pair (when $ (a_1/2)^2<a_2$).

When both poles are real, the two-pole can be analyzed simply as a cascade of two one-pole sections, as in the previous section. That is, one can 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

\begin{eqnarray*}
p_1 &=& x_p + j y_p\\
p_2 &=& x_p - j y_p = \overline{p}_1
\end{eqnarray*}

since they must form a complex-conjugate pair when $ a_1$ and $ a_2$ are real. We may express them in polar form as

\begin{eqnarray*}
p_1&=&Re^{j\theta_c}\\
p_2&=&Re^{-j\theta_c}
\end{eqnarray*}

where

\begin{eqnarray*}
R&=&\sqrt{x_p^2 + y_p^2}\,>\,0\\
\theta_c&=&\tan^{-1}\left(\frac{y_p}{x_p}\right).
\end{eqnarray*}

$ R$ is the pole radius, or distance from the origin in the $ z$-plane. As discussed in Chapter 8, we must have $ R<1$ for stability of the two-pole filter. The angles $ \pm\theta_c$ are the poles' respective angles in the $ z$ plane. The pole angle $ \theta _c$ corresponds to the pole frequency $ \omega_c$ via the relation

$\displaystyle \theta_c = \omega_c T = 2\pi f_c T
$

where $ T$ denotes the sampling interval. See Chapter 8 for a discussion and examples of pole-zero plots in the complex $ z$-plane.

If $ R$ is sufficiently large (but less than 1 for stability), the filter exhibits a resonanceB.2 at radian frequency $ \omega_c = 2\pi f_c = \theta_c/T$. We may call $ \omega_c$ or $ f_c$ 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

$\displaystyle H(z)$ $\displaystyle =$ $\displaystyle \frac{b_0}{(1-Re^{j\theta_c}z^{-1})(1-Re^{-j\theta_c}z^{-1})}$  
  $\displaystyle =$ $\displaystyle \frac{b_0}{1 - 2 R \cos(\theta_c)z^{-1}+ R^2 z^{-2}}
\protect$ (B.1)

Comparing this to the transfer function derived from the difference equation, we may identify

\begin{eqnarray*}
a_1 &=& -2R \cos(\theta_c)\\
a_2 &=& R^2.
\protect
\end{eqnarray*}

The difference equation can thus be rewritten as

$\displaystyle y(n) = b_0 x(n) + [2 R \cos(\theta_c)] y(n - 1) - R^2 y(n - 2). \protect$ (B.2)

Note that coefficient $ a_2$ depends only on the pole radius R (which determines damping) and is independent of the resonance frequency, while $ a_1$ is a function of both. As a result, we may retune the resonance frequency of the two-pole filter section by modifying $ a_1$ only.

The gain at the resonant frequency $ \omega=\omega_c$, is found by substituting $ z = e^{j\theta_c}=e^{j\omega_c T}$ into Eq.$ \,$(B.1) to get

$\displaystyle G(\omega_c) \isdef \left\vert H(e^{j\theta_c})\right\vert$ $\displaystyle =$ $\displaystyle \left\vert\frac{b_0}{(1-R)(1-Re^{-j2\theta_c})}\right\vert$  
  $\displaystyle =$ $\displaystyle \frac{\vert b_0\vert}{(1-R)\sqrt{1-2R\cos(2\theta_c)+R^2}}
\protect$ (B.3)

See §B.6 for details on how the resonance gain (and peak gain) can be normalized as the tuning of $ \omega_c$ is varied in real time.

Since the radius of both poles is $ R$, we must have $ R<1$ for filter stability8.4). The closer $ R$ is to 1, the higher the gain at the resonant frequency $ \omega_c = 2\pi f_c$. If $ R=0$, the filter degenerates to the form $ H(z) = b_0$, which is a nothing but a scale factor. We can say that when the two poles move to the origin of the $ z$ plane, they are canceled by the two zeros there.

Resonator Bandwidth in Terms of Pole Radius

The magnitude $ R$ 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 $ R$ is close to 1, a reasonable definition of 3dB-bandwidth $ B$ is provided by

$\displaystyle B$ $\displaystyle \isdef$ $\displaystyle - \frac{\ln(R)}{\pi T}$ (B.4)
$\displaystyle R$ $\displaystyle =$ $\displaystyle e^{- \pi B T}
\protect$ (B.5)

where $ R$ is the pole radius, $ B$ is the bandwidth in Hertz (cycles per second), and $ T$ is the sampling interval in seconds.

Figure B.6 shows a family of frequency responses for the two-pole resonator obtained by setting $ b_0 = 1$ and varying $ R$. The value of $ \theta _c$ in all cases is $ \pi /4$, corresponding to $ f_c =
f_s/8$. The analytic expressions for amplitude and phase response are

\begin{eqnarray*}
G(\omega)\! &=&
\!\frac{b_0}{\sqrt{[1 + a_1 \cos(\omega T) + a...
... + a_1 \cos(\omega T) + a_2 \cos(2\omega T)}\right]\qquad(b_0>0)
\end{eqnarray*}

where $ a_1 = - 2R \cos(\theta_c)$ and $ a_2 = R^2$.

Figure B.6: Frequency response of the two-pole filter
$ y(n) = x(n) + 2R \cos (\theta _c) y(n - 1) - R^2 y(n - 2)$
with $ \theta _c$ fixed at $ \pi /4$ and for various values of $ R$. (a) Amplitude response. (b) Phase response.
\begin{figure}\input fig/kfig2p23.pstex_t
\end{figure}


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:

\fbox{
\begin{tabular}{rl}
Difference equation: & $y(n) = b_0 x(n) + b_1 x(n-1) ...
...+ b_1 \cos(\omega T) + b_2 \cos(2\omega T)}\right]$
\end{tabular}\vspace{10pt}
}

Figure B.7: Signal flow graph for the general two-zero filter
$ y(n) = b_0x(n) + b_1x(n - 1) + b_2x(n - 2)$.
\begin{figure}\input fig/twozero.pstex_t
\end{figure}

As discussed in §5.1, the parameters $ b_1$ and $ b_2$ are called the 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

$\displaystyle z = \frac{-b_1 \pm \sqrt{b_1^2 - 4 b_0 b_2}}{2b_0}
$

If the zeros are real [ $ (b_1/2)^2 \geq b_2$], then the two-zero case reduces to two instances of our earlier analysis for the one-zero. Assuming the zeros to be complex, we may express the zeros in polar form as $ Re^{j\theta_c}$ and $ Re^{-j\theta_c}$, where $ \theta_c = \omega_c T = 2\pi f_c T$.

Forming a general two-zero transfer function in factored form gives

\begin{eqnarray*}
H(z) &=& b_0 (1 - Re^{j\theta_c} z^{-1}) (1 - Re^{-j\theta_c} z^{-1})\\
&=& b_0 [1 - 2R\cos(\theta_c) z^{-1}+ R^2 z^{-2}]
\end{eqnarray*}

from which we identify $ b_1/b_0 = - 2R \cos(\theta_c)$ and $ b_2/b_0 =
R^2$, so that

$\displaystyle y(n) = b_0\{ x(n) - [2R \cos(\theta_c)]x(n - 1) + R^2 x(n - 2)\}
$

is again the difference equation of the general two-zero filter with complex zeros. The frequency $ \omega$, is now viewed as a notch frequency, or antiresonance frequency. The closer R is to 1, the narrower the notch centered at $ \omega_c$.

The approximate relation between bandwidth and $ R$ 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 $ b_0$ to 1 and varying $ R$. The value of $ \theta _c$, is again $ \pi /4$. 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.

Figure B.8: Frequency response of the two-zero filter
$ y(n) = x(n) - 2R\cos (\theta _c) x(n - 1) + R^2 x(n - 2)$
with $ \theta _c$ fixed at $ \pi /4$ and for various values of $ R$. (a) Amplitude response. (b) Phase response.
\begin{figure}\input fig/kfig2p25.pstex_t
\end{figure}


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

$\displaystyle H(z) = \frac{g}{1-pz^{-1}} \protect$ (B.6)

where $ p=Re^{j\theta_c}$ is the single complex pole, and $ g$ is a scale factor. In the time domain, the complex one-pole resonator is implemented as

$\displaystyle y(n) = g x(n) + p y(n-1).
$

Since $ p$ is complex, the output $ y(n)$ is generally complex even when the input $ x(n)$ is real.

Since the impulse response is the inverse 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

$\displaystyle h(n) = u(n) g p^n,
$

where, as always, $ u(n)$ denotes the unit step function:

$\displaystyle u(n) \isdef \left\{\begin{array}{ll}
1, & n\geq 0 \\ [5pt]
0, & n<0 \\
\end{array}\right.
$

Thus, the impulse response is simply a scale factor $ g$ times the geometric sequence $ p^n$ with the pole $ p$ as its ``term ratio''. In general, $ p^n = R^n e^{j\omega_c nT}$ is a sampled, exponentially decaying sinusoid at radian frequency $ \omega_c=\theta_c/T$. By setting $ p$ somewhere on the unit circle to get

$\displaystyle p \isdef e^{j\omega_c T},
$

we obtain a complex sinusoidal oscillator at radian frequency $ \omega_c$ 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:

\begin{eqnarray*}
\mbox{re}\left\{h(n)\right\} &=& u(n) g \cos(\omega_c n T)\\
\mbox{im}\left\{h(n)\right\} &=& u(n) g \sin(\omega_c n T)
\end{eqnarray*}

These may be called 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 $ g$ to be complex,

$\displaystyle g \isdef A e^{j\phi}
$

we can arbitrarily set both the amplitude and phase of this phase-quadrature oscillator:

\begin{eqnarray*}
\mbox{re}\left\{h(n)\right\} &=& u(n) A \cos(\omega_c n T + \p...
...mbox{im}\left\{h(n)\right\} &=& u(n) A \sin(\omega_c n T + \phi)
\end{eqnarray*}

The frequency response of the complex one-pole resonator differs from that of the two-pole real resonator in that the resonance occurs only for one positive or negative frequency $ \omega_c$, but not both. As a result, the resonance frequency $ \omega_c$ 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 $ \theta_c \isdef \omega_c T = 0$, $ \pi/2$, or $ \pi $. 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:

$\displaystyle H(z) = \frac{g}{(1 - p z^{-1}) (1 - \overline{p} z^{-1})} = \frac{g_1}{1-pz^{-1}} + \frac{g_2}{1-\overline{p}z^{-1}} \protect$ (B.7)

where $ g_1$ and $ g_2$ 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 ( $ \left\vert p\right\vert\to1$), and by separating the pole frequencies $ 0\ll\angle p \ll \pi$. The greatest separation occurs when the resonance frequency is at one-fourth the sampling rate ( $ \angle p =\pi/2$). 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.

Figure B.9: Frequency response (solid lines) of the two-pole resonator
$ H(z)=1/(1-2R\cos (\theta _c)z^{-1}+ R^2z^{-2})$,
for $ R=0.8$ and $ \theta _c = \pi /8$, overlaid with the frequency responses (dashed lines) of its positive- and negative-frequency complex one-pole components. Also marked (dashed lines) are the two resonance frequencies; the peak frequencies can be seen to lie slightly outside the resonance frequencies.
\includegraphics[width=\twidth ]{eps/tppfe}

To show Eq.$ \,$(B.7) is always true, let's solve in general for $ g_1$ and $ g_2$ given $ g$ and $ p$. Recombining the right-hand side over a common denominator and equating numerators gives

$\displaystyle g = g_1 - g_1 \overline{p}z^{-1}+ g_2 - g_2 pz^{-1}
$

which implies

\begin{eqnarray*}
g_1+g_2 &=& g\\
g_1 \overline{p} + g_2 p &=& 0.
\end{eqnarray*}

The solution is easily found to be

\begin{eqnarray*}
g_1 &=& g \frac{p}{2\mbox{im}\left\{p\right\}}\\
g_2 &=& -g \frac{\overline{p}}{2\mbox{im}\left\{p\right\}}
\end{eqnarray*}

where we have assumed im$ \left\{p\right\}\neq 0$, as necessary to have a resonator in the first place.

Breaking up the two-pole real resonator into a parallel sum of two complex one-pole resonators is a simple example of a 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

$\displaystyle h(n) = g_1 p^n + g_2 \overline{p}^n,\qquad n=0,1,2,\ldots
$

Since $ h(n)$ is real, we must have $ g_2=\overline{g}_1$, as we found above without assuming it. If $ \left\vert p\right\vert=1$, then $ h(n)$ is a real sinusoid created by the sum of two complex sinusoids spinning in opposite directions on the unit circle.

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

$\displaystyle H(z) = g \frac{1 + \beta_1 z^{-1}+ \beta_2 z^{-2}}{1 + a_1 z^{-1}+ a_2 z^{-2}} \protect$ (B.8)

where $ g$ can be called the overall gain of the biquad. Since both the numerator and denominator of this transfer function are quadratic polynomials in $ z^{-1}$ (or $ z$), the transfer function is said to be ``bi-quadratic'' in $ z^{-1}$ (or $ z$).

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 $ R$ and angle $ \theta$ of the positive-frequency pole. For example, denoting the denominator polynomial by $ A(z)=1 + a_1 z^{-1}+ a_2 z^{-2}$, we have

$\displaystyle A(z) = \left(1 - Re^{j\theta}z^{-1}\right)\left(1 - Re^{-j\theta}z^{-1}\right)
= 1 - 2R\cos(\theta)z^{-1}+ R^2z^{-2}.
$

This representation is most often used for the denominator of the biquad, and we think of $ \theta$ as the resonance frequency (in radians per sample-- $ \theta=2\pi f_c T$, where $ f_c$ is the resonance frequency in Hz), and $ R$ 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 $ z = 1$ (dc) and the other at $ z = -1$ (half the sampling rate), i.e., $ \beta_1=0$ and $ \beta_2=-1$ in Eq.$ \,$(B.8) above $ \Rightarrow B(z) = 1-z^{-2}= (1-z^{-1})(1+z^{-1})$. This zero placement normalizes the peak gain of the resonator if it is swept using the $ a_1$ parameter.

Using the shift theorem for z transforms, the difference equation for the biquad can be written by inspection of the transfer function as

\begin{eqnarray*}
v(n) &=& g\, x(n) \\
y(n) &=& v(n) + \beta_1 v(n-1) + \beta_2 v(n-2) \\
& & \qquad - a_1 y(n-1) - a_2 y(n-2) .
\end{eqnarray*}

where $ x(n)$ denotes the input signal sample at time $ n$, and $ y(n)$ is the output signal. This is the form that is typically implemented in software. It is essentially the direct-form I implementation. (To obtain the official direct-form I structure, the overall gain $ g$ must be not be pulled out separately, resulting in feedforward coefficients $ [g,g\beta_1,g\beta_2]$ instead. See Chapter 9 for more about filter implementation forms.)


Biquad Software Implementations

In matlab, an efficient biquad section is implemented by calling

        outputsignal = filter(B,A,inputsignal);
where

\begin{eqnarray*}
\texttt{B} &=& [g, g\beta_1, g\beta_2],\\
\texttt{A} &=& [1, a_1, a_2].
\end{eqnarray*}

A complete C++ class implementing a biquad filter section is included in the free, open-source Synthesis Tool Kit (STK) [15]. (See the BiQuad STK class.)

Figure B.10 lists an example biquad implementation in the C programming language.

Figure B.10: C code implementing a biquad filter section.

 
  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;
      }
  }


Next Section:
Allpass Filter Sections
Previous Section:
A Sum of Sinusoids at the Same Frequency is Another Sinusoid at that Frequency