FIR Digital Filter demo applet: guidance notes


Introduction: using the applet

The applet allows one of a choice of three FIR (Finite Impulse Response, i.e. non-recursive) digital filters to be applied to a sampled input signal waveform.

As an introductory demonstration of the applet, simply press the "Plot signal" button with the waveform settings at their default values (sine wave, 1000 Hz frequency, 256 samples, no added DC level or added random noise). The sampled sine wave input signal should be plotted in the upper graph.

Now press the "LP filter" button to apply the low pass filter. The filter output, displayed in the lower graph, should look essentially the same as the input: a sinusoidal signal of the same frequency as the input. In fact, in response to a sinusoidal input, any linear filter - such as those demonstrated by the applet - always gives a sinusoidal output having the same frequency as the input.

The filter used here is a 34th-order low pass filter, with a cutoff frequency of 1 kHz. The 1 kHz frequency of the input signal lies (just) within the passband of the filter, and is therefore "passed" by the filter with very little alteration. Notice, however, that there is a phase difference between the output and input waveforms. Both analog and digital filters typically introduce a phase shift which varies with the input frequency. In most filter applications, this phase shift is relatively unimportant.

Another difference between the output and input signals is that the output signal takes a while to get properly started. This is due to the fact that a digital filter needs previously-sampled values of the input signal to calculate the next output value. When the filter starts up, no previous samples are available, and it takes a while for the filter output to settle down. The length of the "start-up transient" of the filter depends on its order: the higher the order, the longer the duration of the initial transient response. For the 34th order filters used in this demonstration, 34 previous input samples are required for the filter to work properly, and it therefore takes 34 sampling intervals for the output to settle down. The 34-sample start-up phase amounts to about one-eighth of the total duration of the sampled signal (256 samples).

Let's try another filter. With the 1000 Hz sine wave signal still displayed in the upper graph, press the "HP filter" button to apply the 34th-order high pass filter. This filter has a passband between about 2 kHz and the 4 kHz upper frequency limit set by the sampling process. You should find that, apart from a spurious feature in the start-up transient response, the filter output is negligibly small: since the signal frequency lies within the filter's stopband, the filter effectively blocks the input sine wave. (Actually, there is still a 1 kHz sine wave output, but its amplitude is so small that it does not show up on the plot).


Frequency response

Definition

The frequency response of a system refers to its response (output) to a sinusoidal input, over a range of input frequencies.

When a sinusoidal input is applied to a linear system, the output is always sinusoidal, with the same frequency as the input, but in general with different amplitude and phase. The gain of the system relates the output amplitude to the input amplitude, while the phase shift introduced is the phase difference between the two sine waves. The gain and phase shift produced by the system both depend on the input frequency.

The frequency response of a system can therefore be described in terms of

Measuring frequency response

The most direct method of determining the frequency response of a system is to apply a sinusoidal input and measuring the resulting output for different input frequencies . The frequency response of a car suspension system, for example, can be determined by driving the car over a "washboard" surface at different speeds, or by using a test jig in which electrical actuators are used to apply a sinusoidally-varying force to the suspension; the output from the system is measured in terms of the amplitude of vibration of the vehicle. Measurement of the frequency response of an electrical circuit such as an amplifier involves applying a sinusoidal input signal and measuring the output as the input frequency is swept over an appropriate range of input frequencies.

The frequency response of a digital filter is one of its most important attributes, and a specified frequency response - particularly the gain response - is often used as the starting point in designing a filter.

It is possible to determine the frequency response of a digital filter theoretically from its transfer function. However, we can investigate the gain frequency responses of the filters available in the applet simply by applying sinusoidal inputs of different frequencies and comparing the input and output amplitudes.

Low pass filter

We have already seen that the low pass filter passes a 1 kHz sine wave virtually unaltered (apart from a phase shift). To illustrate further the behaviour of the filter, try generating a series of sine waves of different frequencies and apply the LP filter to each. You should find that, for frequencies between 0 and about 1000 Hz, the output and input amplitudes are essentially the same: the gain of the filter in the passband is therefore approximately 1. As the sine wave frequency is increased from 1000 Hz to 1700 Hz, the output amplitude progressively decreases virtually to zero. This range of frequencies, called the "transition band", marks the transition between the filter passband and the stopband, which extends from about 1700 Hz to the limit frequency of 4000 Hz. For input frequencies within the stopband, you should find that the filter effectively blocks the input signal.

The filter gain at the chosen input frequency could in principle be calculated by dividing the estimated amplitude of the output signal by that of the input. Plotting a graph of the filter gain for each frequency value used would give the gain frequency response curve for the filter.

High pass filter

To illustrate the frequency response of the high pass filter, follow a similar procedure to that just described: generate a sine wave signal of a chosen frequency, then apply the HP filter to see its effect on the signal. You should find that for input sine wave frequencies from 0 to about 1300 Hz , the signal is effectively stopped by the filter, while frequencies above about 2000 Hz are passed (with a gain of approximately 1). The transition band extends from around 1300 to 2000 Hz.

Band pass filter

The same procedure can be followed to show broadly the frequency response of the band pass filter. You should find that input sine wave signals in a narrow range of frequencies around 1000 Hz are passed by the filter with approximately unit gain, while sine waves below 300 Hz and above 1600 Hz are effectively blocked. There are two transition bands, extending from about 300 to 900 Hz and 1050 to 1600 Hz.


Non-sinusoidal waveforms

The preceding demonstrations have shown the effect of the three types of filter on simple sine wave signals, and in particular the ability of each filter to pass selectively sine waves within frequency bands. Let's now try applying the filters to more complex waveforms which consist of many different frequency components, rather than just a single component of a specific frequency.

Sine wave with DC offset

The first demonstration will show how digital filters can be used to separate out different components of a signal. Generate a sine wave signal of frequency 2000 Hz (recall that the amplitude is fixed at 1 V) and add a DC level of 1 V. Plot the signal. It should be a 1 V sine wave shifted upwards by 1 V.

Apply the LP filter. Since the DC level is effectively of zero frequency, lying within the passband of the low pass filter, while the sine wave component frequency of 2000 Hz lies in the stopband, the sine wave component should be filtered out, leaving only the constant DC level (apart from the usual start-up transient). This illustrates the ability of a low pass filter to smooth out ripples or other high-frequency components superimposed on a low frequency signal.

Now try applying the HP filter. This has the reverse effect: it blocks the DC component, while passing the 2000 Hz sinusoidal component, thus removing the DC level from the signal. High pass filters can be used to remove constant levels or slowly-varying "wobbles" from a signal.

Square wave harmonics

Generate a square wave with a frequency of 600 Hz. (Make sure you uncheck the "Add DC level" checkbox). Remember that a square wave consists of a fundamental sine wave component of the same frequency, together with odd harmonics. A 600 Hz square wave thus consists of 600 Hz, 1800Hz, 3000 Hz, ... etc. components.

Apply the LP filter. The only frequency component of the square wave signal lying within the filter passband is the 600 Hz fundamental, and you should therefore find that the filter output is sinusoidal, with a frequency (600 Hz) matching that of the square wave itself.

Now generate a 330 Hz square wave signal, and apply the LP filter as before. The third harmonic component of the square wave, at a frequency of 990 Hz, now lies within the filter passband as well as the 330 Hz fundamental. You should find that the filter output resembles a sine wave at the same frequency (330 Hz) as the square wave, but now with an added third harmonic (with an amplitude only one-third that of the fundamental) which produces distinctive dips in the peaks of the waveform, and also makes the zero-crossings of the waveform slightly steeper than for a pure sine wave.

Now, without altering the input signal (the 330 Hz square wave should be displayed in the upper graph), apply the BP (band pass) filter. This time, only the third harmonic component at 990 Hz lies within the filter passband (around 1000 Hz), and the filter should therefore select out the 990 Hz sine wave component of the square wave. This can be verified by confirming that there are three cycles of the sine wave shown in the lower (output signal) graph for each cycle of the square wave plotted input in the upper graph.

Harmonics of triangular and sawtooth waves

If you have tried the FFT spectrum analyser applet demo, you may recall that a triangular waveform has frequency components which are odd harmonics of the fundamental frequency, while a sawtooth waveform consists of both even and odd harmonics.

This property can be demonstrated by applying the band pass filter to select harmonic components around 1000 Hz.

Start by generating a 500 Hz triangular wave, and applying the BP filter. You should find that the filter output is virtually zero, confirming the absence of a component of the triangular wave at the second harmonic frequency of 1000 Hz.

Repeat this with a sawtooth wave instead of the triangular wave. This time, the second harmonic of the 500 Hz sawtooth should show up.

Noise reduction

One common use of filters (both analog and digital) is in reducing the amount of random noise which often crops up in signals due to a variety of causes.

To demonstrate this, select a sine wave signal with a frequency of 1000 Hz; check the "Add random noise" checkbox, and enter a value of 0.5 (volts) for the noise level (remember the amplitude of the sine wave is fixed at 1 volt). Apply the LP filter. You should find that the filtered signal now looks much less "noisy", because the low pass filter has blocked or at least reduced the higher-frequency components of the noise. (The noise components within the filter passband are unaffected, however, so lower-frequency noise is still present).

You could also try applying the BP filter. Since this rejects lower as well as higher frequency noise components, the filter output is a purer sine wave. However, the band pass filter would be suitable if the signal consisted only of components within the passband of the filter, otherwise the filter would remove signal components as well as noise.

Another useful type of filter, not included in this demonstration, is a band stop (or band-reject) filter. As the name suggests, this has exactly the opposite effect to a band pass filter: it selectively removes frequency components within a certain range, while passing all other frequencies. Band stop filters are used, for example, to remove mains noise from signals due to pickup from power cables.


FIR Digital Filter demo applet | DSP home page