Aerospace



Citadel Forums

Home

Company Information

Information Request

Linux How-to Guides

ADSP 21xx
Digital Signal Processing
Tutorials

SW Utilities

On-line Order Form

Server Support


bonk

Have you found this site useful? Did we save you time? Did we cure your head-ache? Is your hair growing back now?

Please make a donation to help with maintenance.


Objective Real-Time Software on the ADSP21XX

The Very Basics on FIR Filters

General

A major reason for the appeal of digital filtering techniques is the fact that one can do things with digital filters that will be well nigh impossible when using analogue techniques. This, coupled with the increasing speed and decreasing cost of Digital Signal Processors, caused a surge in interest in DSP applications. The result is a dramatic increase in consumer communication toys, such as cell phones, which were unthinkable 30 years ago.

It is therefore imperative that digital filtering techniques be demistified. Many DSP courses still concentrate only on the Laplace and Fourier transforms lurking behind digital filters, without teaching students how to create practical filters, quickly and easily. We assume that you are not working on a very specialised system, but only wants to filter audio and that it does not need to be high fidility, but only acceptable. That should cover about 80% of all applications. If you are designing a radar system or a nuclear missile, I'd be very worried if you would need to read this book...

In this chapter, we shall attempt to define a few Rules of Thumb for digital Finite Impulse Response (FIR) filter design and create some examples that you can build upon, without the use of any Greek characters (They are hard to do in HTML anyway!).

While serious academics may squirm at the thought, some real world effects are bound to have such a big impact on a system, that there is usually not much need for a detailed filter design effort. It may be better to perform a quick design and then go and test it properly. The basic rules defined here, will usually suffice to get you going. Optimization should only be performed where needed, thus saving you a lot of design time.

Detailed Design

Our friend H.Acker attended a DSP course at a major institution in preparation for the design of his multi channel data recorder. He figured that to get a maximally flat frequency response, with no funny group delay effects, he needed a 200 tap low pass FIR filter, with a cutoff frequency of 3kHz. After fourteen days of fighting MatLab (please forgive the pun), he finally managed to frequency sweep the filter and draw his first graph. Beautiful! Exactly what he needed - sharp cutoff down to infinity and beyond, with only the tiniest of tiny side lobes...

Hmmm, what is the problem with this? Processing speed requirements for one and memory use for another.

As soon as H.Acker coded the monster filter up, he found that there is no way that it would possibly execute in real time on one channel, never mind four channels simultaneously, plus all the other things the system has to do. H.Acker learned a valuable lesson, somehow he needs to use as few taps as possible, to conserve processing cycles.
 

Rule 1 - Conserve Cycles

Use as few taps as possible to conserve processing cycles, you'll need them later.

That is great, but how many are "as few as possible" anyway? It appears that practical filters can be realised with 10 to 30 taps.
 

Rule 2 - Taps

Practical filters can be realized with 10 to 30 taps.

Hey! Wait a minute, what is this talk about "taps"!!?? Oops, pardon, I thought I'll dive right in first and then surface for breath a while later, just to get your attention.

A Simple FIR Filter

Most text books start with a description of the moving average filter. There is a good reason for it: All the filter coefficients are the same, so even if you get things backwards, it still works. So, who am I to argue?

Great, but what exactly is a "filter coefficient" and where do you get them? You can't get them at a drug store yet, though I fail to see why not - we've had Logarithm Books and Handbooks of Formulas and Tables for centuries already it seems, so what is wrong with publishing filter coefficients? Nothing really, but they tend to be easier to calculate than look up in a book...

Let us calculate the average of 3 and 5:

    (3 + 5) / 2 = 4, right?
Let us now do it another way:
    (3 / 2) + (5 / 2) = 4, OK?
In the second example, the "Filter Coefficients" are 1/2, 1/2. We actually multiply 3 with .5 and multiply 5 with .5 and then finally add the products together.

In simple English, a FIR filter is the sum of the products of the signal samples and the filter coefficients, while the number of samples processed at any one time is the number of filter taps, in this case 2 taps, but please read on.

From this example it is clear that if we wanted to have a 10 tap filter, the coefficients would be 1/10, or .1 each.

Move it along

There are two things wrong still with the above. A filter is only useful if it operates on a continuous stream of signal samples. We do that, by subtracting the oldest sample product in the block and adding in the new sample product, for every sample received from the Analogue to Digital converter. Thereby, we end up with a moving filter.

Another way, is to shift the data in the block up by one, dropping the oldest sample and adding a new one, then performing the whole calculation all over again. This may appear to be inefficient at first glance, but it really depends upon the architecture of the processor. On a Texas Instruments processor and with a small number of taps (10 to 20), this may work very well.
 

A FIR Filter 

A FIR filter is the sum of the products of the signal samples and the (time reversed) filter coefficients.

What is the other thing that is wrong? The coefficients should be taken in reverse order. With an averaging filter, all the coefficients are the same, so it does not matter, but the order of the coefficients is mighty important with any other filter!

The Black Art of digital filter design lies in the choice of the filter coefficients.

Tap Dancing

The number of taps, determines the smoothness of the filter and it also determines the delay, or the length of time it takes for the first result to pop out the other side of the filter block.
 

The Black Art

The Black Art of digital filter design lies in the choice of the filter coefficients.

The number of taps, determines the smoothness and the delay.

We can see this with our moving average, pencil and paper, but a spread sheet works a whole lot better. (In fact, a spread sheet is an extremely valuable tool in DSP design, since it can readily create graphs from number sequences. As one can copy blocks of text from a Windows 95 DOS box and paste it into a modern spread sheet, it is also a valuable debugging tool. If the graph looks funny, you can be sure that your design won't work...).

 

The example spread sheet is in the Microsoft Excel file realtime\avg.xls and shows two moving average examples, in which I simply fed a somewhat random sequence of numbers. The example above is a 2 tap filter. Note that the 5 tap filter below, has a much more smooth output, as intuitively expected. This very same effect holds true for all proper FIR filters. The more taps, the more delay and the smoother the output.


 

So, if the operation of a FIR filter is this simple, then what is the problem with designing digital filters? Just a lack of simple tools, that's all.
 

Wide Spread Use

If you would obtain the Mathematics add on for Microsoft Excel, you could design DSP algorithms in a familiar environment - you do not need expensive and complicated tools to perform DSP design. Lotus 123 was the tool of choice before MathCad and others became available and it is still hard to beat...

To address the design problem, we have included a very easy to use program on the CD-ROM, called DSPlay. Professor Roger Cattin designed this program for a graduate course. It will create the filter coefficients, based upon a simple specification. You can then sweep the filter with a signal, to see the resulting effect on screen. The program will also generate sample filter code in C or assembler, for a number of DSPs.

Let's DSPlay

We can do serious playing with this program. Let us assume that we need a 3300 Hz low pass filter for telephony use. A telephone circuit has a bandwidth of 300 Hz to 3300 Hz and is usually sampled at a rate of 8000 Hz. That allows one to transmit 8 bit compressed PCM voice at a rate of 64000 bits per second.

How did that come about? Somebody liked the 64000 bit rate and divided down from there - honest, no fancy design here!
 

Rule 3 - Sample Rate

Sample at a rate which is at least 3 times that of the highest frequency in the signal.

Why sample at 8000 Hz? Well, according to the Nyquist criterion, one has to sample at, at least double the rate of the highest frequency one wants to represent. In practice, sampling at a rate of 3 times the highest frequency is recommended. Sampling a 3300 Hz signal at 8000 Hz is pushing one's luck a bit and you only need to talk on a phone to realise that it really does not sound very good, but it is understandable.
 

Rule 4 - Anti Alias

Anti Alias Low Pass filter the signal before sampling it

Before one can convert an analogue signal to digital form, one first has to analogue filter it. It is important to remove all high frequency components from the signal, since sampling and filtering is somewhat like a mixer. When one samples a tone with a frequency higher than half the sampling rate, it will cause the high frequency components to fold back into your signal, an effect known as aliasing and it sounds somewhat like ET phoning home - an effect best avoided.
 

Rule 5 - Group Delay

Use a FIR filter when a fixed group delay is required, as for tone detection.

Now, to get DSPlay up and running. Drag and drop the dsplay directory from the CD-ROM onto your hard disk, then go to the dsplay directory and run the program. There is a shortcut in the dsplay directory and the CD comes set up with a work directory and an initialisation file, pointing to this directory, so it should work as is.

Design a FIR filter using DSPlay

OK, we have DSPlay up and running. Click on the File menu, Design FIR, Design FIR using Window

Select the following:

  • Filter Type = Low Pass
  • Lower Cutoff Freq = 3300Hz
  • Filter Order = 20
  • Window Type = Rectangular
  • Sample Rate = 8000
  • OK
The result should look like the screen below:


 

Up pops a filter screen. Now we need a signal to pass through the filter. A frequency chirp would be useful, since it could be used to show the cutoff response of the FIR filter.

Select the File menu, New Sequence:

  • Auto Real
  • Frequency Chirp
  • 256 samples
  • Sample Rate = 8000Hz
  • Frequency = 8000Hz
  • OK
The resulting screen should look like this:


Up pops a time sequence. This sequence will sound like a wheeeooooeeee kind of thing and can be used to test the filter.

Here comes the tricky part. We have to submit the time sequence to the filter. To do that, Right Click on the wheeooooeee graph you just created (Inside the window, on the graph itself). This will cause a new line to appear at the bottom of the screen, which should look something like this:

    OPND: SEQ_17.RSQ
Now Left Click on the FIR filter window, then click on the FIR Menu and select Apply to OPND. Yahooo! Your first Filter design!

Up pops the output from the FIR filter. The high frequency wheee part of the time sequence should be severely attenuated by the low pass filter.

Let's Get Serious with DSPlay

That was fun, you can just as well admit it. This was surely a lot easier than H.Acker's fight with MatLab and even easier than using a spread sheet. Now, would it not be nice if the program could generate us the coefficients needed to create the filter? In fact, it is a lot better than that, DSPlay can generate sample code in C or assembler!
    Select the FIR Menu, Generate C Code or

    FIR Menu, Generate ADSP Code

    then lpf3300, OK OK

    Nothing much will happen...

Ha! Have a look at the directory dsplay\work. There you'll see two new files: lpf3300.c and lpf3300.h. Here is an example of your filter code:
 
/*
    Implementation file for filter 'lpf3300' generated by DSPlay 3.02
    (c) 1995 by Roger Cattin
 
    This code is not reentrant; use this function only once in your program !
*/

double lpf3300( double x )
{
static double xcoeff[20] = {
-1.6768400000e-02,-1.5058100000e-03, 2.4150200000e-02,-4.5549500000e-02,
 5.8865200000e-02,-5.6895100000e-02, 3.2240200000e-02, 2.5441200000e-02,
-1.4753400000e-01, 6.2755600000e-01, 6.2755600000e-01,-1.4753400000e-01,
 2.5441200000e-02, 3.2240200000e-02,-5.6895100000e-02, 5.8865200000e-02,
-4.5549500000e-02, 2.4150200000e-02,-1.5058100000e-03,-1.6768400000e-02
};
static double x_history[20] = { 0.0 };
static int enQueue = 0, serve = 1;
double y;
int i, s;
 
    x_history[enQueue++] = x;
    serve++;
    if( enQueue >= 20 ) enQueue = 0;
    if( serve >= 20 ) serve = 0;
 
    y = 0.0; s = serve;
    for(i=0; i<20; i++)
    {
        y += x_history[s++] * xcoeff[i];
        if( s >= 20 ) s = 0;
    }
 
    return y;
}

and here is your example header file contents:
 
#ifndef _DSPlay_lpf3300
#define _DSPlay_lpf3300
/*
    Definition file for filter 'lpf3300' generated by DSPlay 3.01
    (c) 1995 by Roger Cattin
 
    This code is not reentrant; use this function only once in your program !
*/

double lpf3300( double x );
 
#endif

As easy as that and we didn't even use a single Greek character! Now the fun can start and you can work your way to more complicated designs.

IIR Filters

OK, this chapter is about FIR filters, but what is an IIR filter? At the risk of getting stoned by somebody - An Infinite Impulse Response filter is a recursive version of a FIR filter. This means that the output is fed back to the input.
 

Rule 6 - High Order Filters

Consider using an IIR filter when a high order filter is needed - more than 30 taps.

IIR filters are simpler than FIR filters when high order filters are needed (more than 30 taps), but they are potentially unstable. That is, they can whistle quite nastily in your ears if you are not careful. DSPlay can do those too, so have fun!



Copyright © 1996-2008, Aerospace Software Ltd., GPL.