How FIR filters work | Designing the filter

In the last post, we created a crude low-pass filter by performing a moving average on a signal. We saw how a moving average could be thought of as a convolution between the signal, f(t), and a rectangular function, g(t), as shown below.

When we performed a Fourier Transform on g(t), to find out what it looked like in the frequency domain, we discovered that, as a low-pass filter, it wasn’t very good.

It does work, but the frequency response is very bumpy in both the pass band and the stop band.

We’re now going to find out what we can do to g(t) to improve the filter’s frequency response and make it more like the rectangular shape we set out to design.

A multiplication in the frequency domain is a convolution in the time domain – and vice versa

In order to find the frequency response of g(t), we used the Fourier Transform to convert the time domain function, g(t), into the frequency domain function, G(f).

This process is reversible.

If we define the frequency response, G(f), we want for our filter in the frequency domain, we can perform an inverse Fourier Transform, to get the impulse response, g(t), we need to perform the convolution in the time-domain.

The impulse response of a filter is its output when presented with a brief input signal, called an impulse. An impulse (also known mathematically as a delta, \( \delta \), function) is an infinitely tall, infinitely thin vertical line.

The impulse response of the moving average filter is a rectangular function. What sort of impulse response do we want to filter the noisy signal we saw in the last post? Let’s find out.

First, we need to define our filter. Press the play button below to hear the audio signal we want to filter. Its frequency response will be shown on the graph below the signal.

In the audio signal above, there is an annoying high pitched squeak. We can see from the blue line on the graph that it occurs between 7 and 8kHz. Most of the energy of our signal is below 6kHz, so let’s try setting our filter’s cutoff to 6kHz.

The filter we are trying to design is shown by the red line on the graph. This is known as a box-car filter on account of its shape. Notice how the frequency response of the signal is actually symmetrical about 0Hz, and we have to define two cutoff frequencies for this filter: a positive cutoff and a negative one!! This is because the time domain signal is real; it has no imaginary component.

To find out why a signal must be symmetrical about 0Hz in the frequency domain, in order to make it real in the time domain, check out the video below.

Now we know what we want our filter to look like in the frequency domain, let’s use the inverse Fourier Transform to convert it into the time domain, and find out what our convolving signal, g(t), looks like.

Deriving the \( g(t) \) for a Low-Pass filter

There are two ways we can express the Inverse Fourier Transform (IFT) equation:

Exponential form:

$$ g(t) = \frac{1}{2\pi} \cdot \int_{-\infty}^{+\infty} G(f) \cdot e^{i\omega t} \cdot d\omega $$

Polar form:

$$ g(t) = \frac{1}{2\pi} \cdot \int_{-\infty}^{+\infty} G(f) \cdot \left [ cos(\omega t) + i \cdot sin(\omega t) \right ] \cdot d\omega $$

We’ll be using the exponential form rather than the polar form, as this will make the integration easier to perform. However, the polar form will come in useful later on.

G(f) is the desired response of our filter:

The cutoff frequency of the filter is defined as \( \omega_c \). I am purposely defining the cutoff frequency in radians per second, as it makes the calculation easier. We’ll convert everything into Hz towards the end.

The frequency response is zero for most frequencies. Only between \( -\omega_c \) and \( +\omega_c \) is the frequency response 1. Therefore, \( G(f) = 1 \) and we can change the limits of the integral from \( -\infty \) and \( +\infty \) to \( -\omega_c \) and \( +\omega_c \) because the result of the integral is zero for all other values of \( \omega \).

$$ g(t) = \frac{1}{2\pi} \cdot \int_{-\omega_c}^{+\omega_c} 1 \cdot e^{i\omega t} \cdot d\omega $$

Now here’s why I left the IFT equation in its exponential form. It is very easy to integrate an exponential function, because:

$$ \int e^{ax} \cdot dx = \frac{e^{ax}}{a} $$

Between the limits, \( -\omega_c \) and \( +\omega_c \), \( G(f) = 1 \), \( a = it \), and \( x=\omega \), so:

$$ g(t) = \frac{1}{2\pi} \cdot \int_{-\omega_c}^{+\omega_c} 1 \cdot e^{i\omega t} \cdot d\omega = \frac{1}{2\pi} \cdot \left [ \frac{e^{i\omega t}}{it} \right ]_{-\omega_c}^{+\omega_c} $$

Substituting the limits, \( -\omega_c \) and \( +\omega_c \), into the equation gives:

$$ g(t) = \frac{1}{2\pi} \cdot \left [ \frac{e^{i\omega_c t}}{it} \right ] – \frac{1}{2\pi} \cdot \left [ \frac{e^{-i\omega_c t}}{it} \right ]$$

Let’s rearrange the equation slightly.

$$ g(t) = \frac{1}{2\pi} \cdot \frac{1}{it} \cdot \left ( e^{i \omega_c t } – e^{-i \omega_c t } \right ) $$

Now here is where the polar form of a complex number comes in useful. According to Euler’s formula:

$$ e^{ix} = cos(x) + i \cdot sin(x) $$

and:

$$ e^{-ix} = cos(x) – i \cdot sin(x) $$

You can learn more about Euler’s formula in the video below.

In our case, \( x = \omega_c t \), so:

Grouping the real and imaginary terms:

The cosine terms cancel out and the sine terms add together.

$$ e^{i\omega_c t} – e^{-i\omega_c t} = 2i \cdot sin(\omega_c t) $$

Substituting this back into the equation for \( g(t) \):

$$ g(t) = \frac{1}{2\pi} \cdot \frac{1}{it} \cdot 2i \cdot sin(\omega_c t) $$

The 2 and the i cancel out, leaving:

$$ g(t) = \frac{sin(\omega_c t)}{\pi t} $$

Notice there are no i’s in this equation, meaning it has no imaginary part. This is exactly what we wanted, and why we had to define a negative cutoff frequency for our filter as well as a positive one. Now we can substitute our cutoff frequency, \( f_c \), in Hz, instead of \( \omega_c \).

$$ \omega_c = 2\pi f_c $$

So the \( g(t) \) for a Low-Pass filter can be given by:

Applying the filter

So now we have know what \( g(t) \) should look like. Let’s try it out.

!!! HANG ON !!!

There is a problem. This filter starts responding to the impulse before that impulse has been received at time=0s. How can that be?

No filter can begin reacting to something before that something has happened. However, this is precisely what g(t) appears to be doing. At time=-10s the impulse response is already oscillating as if it knows that an impulse is about to happen. Can digital filters really predict the future? Of course they can’t. Therefore, this filter will not work.

How can we make this filter into something that will work in the real world?

Clue: Remember the phase delay of a moving average filter we spoke about in the last post?

We’ll find out in the next post.