Filtering

June 2, 2021

A filter is an operation that discriminates components of an object over some domain.

In the domain of 3-dimensional space, filtering may involve spatial discrimination by rejecting dirt and other large particles while admitting clean water to pass through.

Or, in the domain of time, Olympic qualifying races involve temporal discrimination by rejecting slower runners while admitting faster ones.

Filtering

In statistics, filtering involves statistical discrimination.

For example, a median-5 filter admits the median value of every set of five samples and rejects the other four. These filters apply to image processing and financial analysis.

median-3 filter input median-3 filter output

Input: 222322122200

Output: 2222222220

In the frequency domain, filtering involves frequency discrimination. The filters reject or accept data components based on how frequently they change.

frequency discrimination example - input frequency discrimination example - output

Input: Sinusoid + DC

Output: Sinusoid only

Frequency Discriminating Filters

Apply a filter when you want to discriminate data based on frequency.

General examples include:

  • Rejecting (eliminate or filter out) frequency components.
  • Accepting (admit or pass) frequency components.
  • Boosting the energy level (magnitude) of some components.

Specific examples include:

  • Filtering out a DC offset (a constant offset).
  • Filtering (notching) out 60Hz noise from data.
  • Filtering out components outside of a microphone, accelerometer, or other transducers’ specifications.
  • Filtering out components outside of an audio speaker or other shakers’ specifications.
  • Equalizing an audio recording by attenuating some frequency bands and boosting others.
  • Smoothing a noisy data sequence by attenuating rapidly changing (high frequency) components.

Types of Filter Operations

A filter is an operation on a data sequence that yields a new sequence. The operation could be anything (or nothing in the case of an identity operation). However, there are some types of filter operations that engineers use so frequently that they have a name.

Low-Pass Filter

A low-pass filter attenuates high-frequency components above a specified corner frequency fc and allows low-frequency components below fc to pass through.

low-pass filter graph

For example, a low pass filter can clean up (or smooth) data contaminated with noise so that patterns are more readily apparent.

High-Pass Filter

A high pass filter attenuates low-frequency components below a specified corner frequency and allows the high-frequency components above the corner frequency to pass through.

high-pass filter

For example, if data have a large DC offset, a high pass filter with a small corner frequency can remove the offset so that patterns of interest are more readily apparent.

Bandpass Filter

A bandpass filter attenuates low and high frequencies and allows the middle to pass. The middle portion is the band passed through.

bandpass filter graph

Notch Filter

A notch filter is the reverse of a bandpass filter. A notch filter rejects a band of frequencies and accepts everything outside the rejected band.

notch filter graph

Filter Characteristics

Corner Frequency

Typically, the corner frequency of a filter response H(ω) is the frequency ωc, where the spectral power |H(ω)|2 has dropped from |H(ω)|2 = 1 to |H(ω)|2 = ½ or |H(ω)|=1/√2.

corner frequency graph

On a dB scale, this would mean dropping from 10log10(1) = 0dB to:

(1)   \begin{equation*} 10\log_{10}[|H(\omega c)|^2]=10\log_{10}(1/2) =-10\log_{10}(2) =-10\ln(2)/\ln(10) \approx-3.01\text{dB} \end{equation*}

Roll-Off

There are no perfect filters in the real world—that is, none drop straight down after reaching their corner frequency. Instead, they roll off at some slope. Real-world filters in the frequency domain are not like cliffs but rather like hills. This hill-like roll-off at the end leaves the upper 5% frequencies of the data invalid.

Filter Architectures

Filtering is often synthesized using polynomials for mathematics and engineering.

Mathematics: By the Weierstrass approximation theorem, any continuous function on a closed interval (0 ≤ time ≤ 10) can be approximated to arbitrary precision using polynomials.

Engineering: A polynomial can be implemented with addition and multiplication only. It is easy to implement both operations using digital logic gates in CPUs, DSPs, FPGAs, ASICs, etc.

For example, y = 5x2 + 3x + 2 means:

  • Multiply 5 and x and x to get 5x2
  • Multiply 3 and x to get 3x
  • Add 5x2 and 3x and 2 to get y

Filters are often designed as polynomials over z, where z-n represents a delay by n samples. Therefore, the z-n factors can be implemented into hardware with a simple memory buffer.

Example

If a filter in the z-domain has the transfer function:

(2)   \begin{equation*} \text{H(z)}=\frac{\text{Y(z)}}{\text{X(z)}} \end{equation*}

(3)   \begin{equation*} =\frac{\frac{3}{2}\text{z}^2+\frac{5}{2}\text{z}+\frac{7}{2}}{\text{z}^2+\text{5z}+6}\times\frac{\text{z}^{-2}}{\text{z}^{-2}} \end{equation*}

(4)   \begin{equation*} =\frac{\frac{3}{2}+\frac{5}{2}\text{z}^{-1}+\frac{7}{2}\text{z}^{-2}}{1+5\text{z}^{-1}+6\text{z}^{-2}} \end{equation*}

z domain transfer function

Then, in the z-domain:

(5)   \begin{equation*} \text{Y(z)}[1+5\text{z}^{-1}+6\text{z}^{-2}]=\text{X(z)}\left[\frac{3}{2}+\frac{5}{2}\text{z}^{-1}+\frac{7}{2}\text{z}^{-2}\right] \end{equation*}

In the time domain:

(6)   \begin{equation*} \text{y(n) + 5y(n-1) + 6y(n-2)}=\frac{3}{2}\text{x(n)}+\frac{5}{2}\text{x(n-1)} + \frac{7}{2}\text{x(n-2)} \end{equation*}

Therefore, at time n, the filter output y(n) equals:

(7)   \begin{equation*} \text{y(n)}=\frac{3}{2}\text{x(n)}+\frac{5}{2}\text{x(n-1)}+\frac{7}{2}\text{x(n-2) - 5y(n-1) - 6y(n-2)} \end{equation*}

FIR-IIR Comparison

Under the constraint of a polynomial-based filter structure, there are two common filter architectures: FIR filters and IIR filters.

FIR Filters

The finite impulse response (FIR) filter is a single polynomial. It is the weighted sum of the current input and a finite number of past inputs.

Its advantages include:

  • Extremely stable.
  • Can be designed to have a linear phase.
  • Has a finite impulse response: the output will go to exactly zero within a fixed time of the input going to zero.

Its disadvantages include:

  • Tends to introduce long latency into the signal processing chain.
  • Commonly requires a large number of mathematical operations per input data sample.

IIR Filters

The infinite impulse response (IIR) filter is a polynomial divided by a polynomial (a rational expression). It is the weighted sum of the current input, a finite number of past inputs, and a finite number of past outputs.

Its advantages include:

  • Very stable if properly designed but possibly unstable if poorly designed.
  • Typically requires a small number of mathematical operations per input data sample.
  • Can be designed to have a linear phase.

Its disadvantages include:

  • Generally, has a non-linear phase.
  • Has an infinite impulse response: generally and theoretically, the output will never reach exactly zero once excited with an input due to the feedback from the output.

Example

The example below compares an IIR Butterworth 6th order to FIR filters with 17, 33, and 257 coefficients. All have a target corner frequency of 100Hz. Observe the following:

  • The 17 coefficient FIR under-performs the 13–15 coefficient Butterworth.
  • The 33 coefficient FIR is more-or-less comparable to the Butterworth.
  • The 257 coefficient FIR outperforms the Butterworth in terms of roll-off.

graph comparing an IIR Butterworth 6th order to FIR filters with 17, 33, and 257 coefficients

An FIR filter with many coefficients introduces a significant delay into the signal processing path as illustrated in the 257-coefficient FIR below.

FIR filter with many coefficients introduces a significant delay into the signal processing path

FIR filters can easily be designed to have a linear phase. For example, all FIR filters with symmetric coefficients (as illustrated above) have a linear phase.

This is not the case with IIR filters, as illustrated with the IIR phase responses illustrated below. However, the Butterworth filter phase responses are approximately linear.

IIR filter types

How Filters Work

Filtering in Time with Convolution

Often, a computer uses simple arithmetic to perform frequency-domain filtering. Specifically, each output y(n) is often a linear combination of inputs x(n). This type of filtering is called convolution.

For example, suppose we want to filter out DC. A simple filter with input x(n) and output y(n) will set y(n) = x(n) – x(n-1). Then, any DC signal at the input (x(n) = constant) will be filtered out because y(n) = constant – constant = 0.

Note: this filter is a digital differentiation.

Filtering in Frequency with Multiplication

Engineers may also filter a data sequence by first projecting it onto a basis. Often, this basis is a sequence of sines and cosines, and the tool for the projection is the fast Fourier transform (FFT).

According to the convolution theorem, filtering data in the time domain can be performed in the frequency domain by simple point-by-point multiplication. Before the multiplication in frequency can be carried out, we must first transform the data from time to frequency with the FFT.

X(f) is the Fourier transform of x(n)

X(f) is the Fourier transform of x(n).

H(f) is the Fourier transform of h(n)

H(f) is the Fourier transform of h(n).

y(n) is the inverse Fourier transform of Y(f)

y(n) is the inverse Fourier transform of Y(f).