Tuesday, April 2, 2013

Digital Filter University - Course VI: FIR Filters





Now time for the long awaited FIR filters!






The general equation and block diagram for FIR filters are shown below. As we have discussed, there is no feedback, no a coefficients.








FIR filters have only zeros, so it takes more effort to get desired characteristics from them than from IIR filters. But they are also inherently stable. And, while all FIR filters are not linear phase, it is easy to make them have a linear phase characteristic. This requires only that the coefficients be symmetric (conjugate symmetric in the case of complex coefficients) about the center.









Take a look at the following example. The symmetric set of coefficients have a nice linear phase and, hence, constant group delay, while the assymetric case does not.








A very common technique for FIR filter design is the Remez exchange algorithm. You can find the guts of the algorithm in a paper, and you can find software that will perform the optimization for you. I will leave such exercises to you and show you two very simple methods of FIR filter design. You can almost do them on the back of a napkin.









The first method of discussion is the Fourier series method. Note that things change a bit depending upon whether N is even or odd. Note alsos that when N is even, m is not an integer. That's OK; everything works out. Just don't be surprised.








Let's consider an example where fc = 250 Hz and Fs = 10 kHz. Use both N = 150 and N = 175 and see if you can get the results below.






The resulting frequency responses are plotted below. Note that N = 150 corresponds to 149th order, etc.







Here's the equation for a bandstop filter. The book gives equations for HP and BP as well.








This example is for a stop band between 1 and 2 kHz, a 20 kHz sample rate and N = 199. Note that this filter is only defined for N odd.






If we zoom up closely on the magnitude characteristics of any of these filters, we'll find ringing such as seen below for the bandstop filter above.

This ringing is called Gibbs Phenomenon, but, so far as I know, has nothing at all to do with Mark Harmon. Instead, it tends to be what happens when things that ought to be infinite are truncated.








For many applications the ringing might not be an issue, but it's actually also very easy to address using window functions which taper the filter coefficient.

There are many different kinds of window functions; three common ones are defined below.










These functions are plotted below for the case of N = 199.

These are applied just by direct multiplication with the filter coefficients. As you can see, the center coefficients don't change much while those farther out are scaled down significantly.






Note the effects of the various windows on the ringing. Rectangular window is another name for no window. In the right hand plot the original (no window) case is removed so the other cases can be compared.








Now for a different FIR filter design method, frequency sampling.

The double bars in the equation for n denote rounding: 4.5 becomes 5. The topless bracket on the sum in the equation for h(n) is the floor function, which means truncation down to the nearest integer: 4.5 becomes 4.









Let's try a highpass example where N = 17, Fs = 23 kHz and fc = 5 kHz. n from the equation above goes from 0 to 8, so we have frequency points at 0, 11.5 / 8, 23 / 8, ... 11.5 kHz as shown below.

We can then set the gain we'd like to have at each of these frequencies. Since we wanted a 5 kHz cutoff, we can go with 0s up through 4.3 kHz, and 1s thereafter. We did this for the first case (solid line in the plot).

There are three things to consider here:
1) if we made a longer filter we'd have more frequency granularity to work with, as well as a generally nice response, since longer filters do that.

2) Even for a short filter, we don't have to use only 1s and 0s. We can use something in between to transition the filter more gradually, and move its cutoff to points we cannot access directly. Note that the response can even go above 1 if you want to put a gain on part of the signal. Don't take it below 0 though. That's negative infinity dB, which should be enough attentuation for anyone.

3) While it is certainly possible to make coefficients using this method, and then apply windows to them as we did for the Fourier series method. Many times one can achieve much the same effect by making gentle transitions as we did for the second case below. That is, the gentle transisitions serve more than one purpose.









Now, for something even more fun.










Initially let r = 1. Then let's use the equation we derived to place pairs of zeros around the unit circle at various (normalized) frequencies.









Now, those could all be cascaded as 2nd order filters or we can convolve the coefficients together to create one long filter with all these zeros in it, as shown below.






The resulting magnitude response is shown below. Notice that it has nulls at the places we specified out zeros.






Now let's change the value of r and try again.






We still get nulls at the same places, but they are no longer take the magnitude response to 0. Furthermore, we've lost the phase linearity (no longer symmetric).




But, for some applications that might be exactly what we want. At the very least, we learned a thing or two about how zeros behave.




No comments:

Post a Comment