# Digital Filtering

Digital filters are fundamental to digital audio processing. While we only have time here for a cursory overview of the essential features of filters, students are encouraged to pursue more advanced courses and references in filter analysis and design.

• The processing of signals is called filtering. When applied to discrete-time signals, this processing is called digital filtering.

• Digital filters are defined by their impulse response, h[n], or the filter output given a unit sample impulse input signal. A discrete-time unit impulse signal is defined by:

• Digital filters are often best described in terms of their frequency response. That is, how is a sinusoidal signal of a given frequency affected by the filter.

• The frequency response of a digital filter can be found by taking the DFT (or FFT) of the filter impulse response.

• The frequency response of a filter consists of its magnitude and phase responses. The magnitude response indicates the ratio of a filtered sine wave's output amplitude to its input amplitude. The phase response describes the phase offset'' or time delay experienced by a sine wave passing through a filter.

## A Basic Digital Filter:

• What happens when we add a signal to a one-sample delayed version of itself?

y[n] = x[n] + x[n - 1]

• Consider the following input signals:

• The filter's frequency response magnitude is shown in Fig. 2.

• A partial C / C++ implementation:
double lastInput = 0;

double Filter::tick( double input )
{
double output = input + lastInput;   // do filter calculation
lastInput = input;                   // save the filter state

return output;                       // output the value
}

• The filter can be implemented in MSP as shown below:

• The delay~ MSP object implements a delay-line whose length is specified in terms of samples. This object cannot be used in a feedback loop.

• The filter can also be implemented with the gen~ object as shown below:

## Finite Impulse Response (FIR) Filters:

• Finite Impulse Response (FIR) filters are defined by scaled and time-delayed versions of the filter input signal only, as given by the following difference equation:

where the input and output for n < 0.

• An FIR filter can be represented by a block diagram as shown in Fig. 3 below.

• The z-1 terms represent unit delays. Note that while this representation for a delay element is common and widely accepted in the signal processing community, the specification of delay in terms of powers of z is a z-domain characterization (to be described below) while the block diagram itself is a time-domain representation.

• With respect to the filter block diagram, FIR filters make use of feed-forward terms only.

• The impulse response of an FIR filter is only as long as the maximum delayed input term in its difference equation.

• The summation of feedforward input terms can result in destructive signal interference, or cancellations, at certain frequency values.

• The FIR filter is said to have an order equivalent to the number of unit delays in its difference equation.

## A Basic Digital Filter with Feedback:

• What happens if we use a previous filter output value to produce the filter's current output?

y[n] = x[n] + y[n - 1]

• Consider the following input signals:

• This system is a digital integrator.

• The filter's frequency response magnitude is shown in Fig. 5.

• This filter is unstable because the gain for a DC signal is infinite. When working with feedback, we must be careful to guarantee system stability.

• Let's add a scalar coefficient to the system, as follows:

y[n] = x[n] + a1 * y[n - 1]

• The new filter's frequency response magnitude, with a1 = 0.9, is shown in Fig. 7.

• This system, referred to as a leaky integrator'', is stable at all frequencies as long as the feedback coefficient |a1| < 1.

• A partial C / C++ implementation:
double lastOutput = 0;

double Filter::tick( double input )
{
return lastOutput = input + a1 * lastOutput;   // do filter calculation
}

• The delay~ MSP object cannot be used in a feedback loop. But this filter can be implemented with the gen~ object as shown below:

## Infinite Impulse Response (IIR) Filters:

• Infinite Impulse Response (IIR) filters include delayed and scaled versions of the output signal which are fed back into the current output.

• IIR filters are described by the following difference equation:

where the input and output for n < 0.

• An IIR filter can be represented by a block diagram as shown in Fig. 8 below.

• With respect to the filter block diagram, IIR filters make use of both feed-forward and feedback terms.

• Given these feedback terms, IIR filters can become unstable based on the values of the feedback coefficients (ak terms).

• Because of this constructive feedback, an IIR filter can produce strong peaks, or resonances, in its frequency magnitude response.

• The feedback terms also result in a long filter impulse response. Though a stable filter's impulse response will decay toward zero over time, it technically never exactly reaches zero (ignoring issues of finite precision arithmetic).

• The order of an IIR filter is equivalent to the greater of the number of its delayed input or output terms.

## Steady State and Transient Response:

• Before a signal is applied to the input of a digital filter, the filter's internal state'' is assumed equal to zero.

• Digital filters are linear systems. One property of linear systems is that a sinusoidal input will produce a sinusoidal output of the same frequency.

• However, when a sinusoidal signal is first applied to the input of a digital filter, the output initially exhibits a region of transition (referred to as its transient response).

• For FIR filters, this transition region (or warm-up'' period) has a duration in samples equal to the filter order.

• For IIR filters, the length of the transition region is dependent on the filter order and the feedback coefficient values.

• Assuming a continued application of the sinusoidal input, the filter will eventually settle into its steady-state region.

• If the input changes frequency or displays a discontinuity of any sort, another transient region will occur in the filter output.

• The frequency response of a digital filter is understood to represent its steady-state behavior.

## Filter Types and Descriptions

• Lowpass filters do not allow sinusoids above a certain cutoff frequency'' (fc) to pass through them.

• Highpass filters only allow sinusoids above a certain cutoff frequency'' to pass through them.

• The cutoff frequency of a filter is defined as the frequency at which the power transmitted is 1/2 the maximum power transmitted in the passband. This corresponds to a -3dB drop on a decibel scale. As the power of a signal is related to its amplitude squared, the cutoff frequency corresponds to an amplitude reduction of .

• Bandpass filters only allow sinusoids above and below a certain frequency region to pass through them.

• Bandstop filters are designed to pass all sinusoids except those within a certain frequency region.

• Resonance filters accentuate sinusoids within a certain frequency region. They may or may not pass sinusoidal frequencies outside that region.

• Resonance filters are typically described in terms of their center frequency and quality factor (Q), which is given by the center frequency divided by the -3dB bandwidth. A higher Q indicates a sharper'' resonance.

• Allpass filters do not affect the magnitude characteristics of a signal (their magnitude response is equal to 1 for all frequencies) but have frequency-dependent phase characteristics.

## Filter Combinations

• What happens when we cascade digital filters in series or parallel?

• The time-domain operation of a filter is referred to as convolution (and typically denoted with the symbol ). From the properties of linear systems, convolution in the time domain corresponds to multiplication in the frequency domain.

• Thus, the series combination of filter responses H1(f) and H2(f) in Fig. 9 above would produce a new response given in the frequency domain by H1 H2 or in the time domain by .

• The response of filters in parallel is given by simple addition (in both the time- and frequency-domains).

• In this way, the filter combination shown in Fig. 9 could be replaced by a single filter with frequency-domain response (H1 H2) + H3 or time-domain response .

## Resonance Filters

• A second-order IIR filter ( y[n] = x[n] - a1 y[n-1] - a2 y[n-2]) can be designed with a single peak in its frequency magnitude response using coefficients of the form:
 a1 = (1) a2 = r2, (2)

where fc is the center frequency of the resonance peak, Ts = 1 / fs is the sample period, and 0 < r < 1.0. The closer r is to 1.0, the narrower the bandwidth of the resonance peak.

• As a further refinement, we can design the resonance filter such that the resonance peak always has a gain of 1.0 by specifing numerator (or feedforward) coefficients as ( y[n] = b0 x[n] + b1 x[n-1] + b2 x[n-2] - a1 y[n-1] - a2 y[n-2]):

• An example digital resonance filter magnitude response is shown in Fig. 10. The coefficients were determined as indicated above with a 5000 Hz center frequency.

• The reson~ MSP object implements a second-order digital resonance filter with parameters for gain, center frequency, and Q.

• The quality factor, Q = center frequency / 3dB bandwidth, will typically range between 1-100.

## General Second-Order Filtering in MSP

• The biquad~ MSP object implements a general second-order IIR digital filter.

• The filtergraph~ object can be used to graphically design a filter response and output the resulting filter coefficients as a list to the biquad~ object.

• The filtergraph~ object has a variety of operating modes and inlet parameters ... see the help window for more details.

• The spectroscope~ object provides a frequency-domain spectrogram or sonogram display.

• The zplane~ object provides a pole-zero display for filter design.

## Filters in Matlab

• Filter coefficients in Matlab are specified in terms of the following general filter difference equation:

• In this way, a filter with difference equation y[n] = x[n] + x[n-1] would be specified with feedforward coefficients b = [1 1] and a feedback coefficient a = 1.

• Likewise, a filter with difference equation y[n] = x[n] + 0.9 y[n-1] would be specified with a feedforward coefficient b = 1 and feedback coefficients a = [1 -0.9].

• In Matlab, we can filter an input signal x with a filter specified by coefficients b and a as y = filter(b, a, x).

• In Matlab, we can plot a filter's frequency response using the freqz function: freqz(b, a).