Sunday, September 29, 2013

Errata for Digital Filters for Everyone

Thanks to an attentive reader who found a bug that had been in my First Edition and the 1st Printing of my Second Edition. Note that I missed one spot when correcting, so the bottom of page 19 in the 2nd Printing of the Second Edition still has one mistake. Third Printing of Second Edition, which is what you'll get if you order now (and for the past couple weeks) has all known issues resolved. (Second edition ebook came out only after this was resolved.)

I am less concerned about minor bugs that don't much affect understanding, though I'll continue to try to clean those up too. However, since this particular issue could leave you scratching your head, I'm posting the correction here too. Most of you don't need a new book, you just need to see the correct presentation.

Let me know if you have questions.


(The second half of the slide above is from the bottom of page 19 in the current edition. The 1-alpha part was backwards still in the 2nd Printing of the Second Edition. The other errors, however, were corrected in that printing.)


Saturday, July 20, 2013

Saturday, June 29, 2013

Announcing Second Edition

The second edition of my popular filter book, Digital Filters for Everyone, is now available on Amazon. 

An e-book is coming.

The paper book will also be available from other retailers.

Look Inside and other features of the Amazon page will also be populated. 

All of these things take a little time but they are coming.

When all is said and done I intend to keep the first edition for sale indefinitely, but I do intend to raise the price to $35 since I would like to incentivize buyers to take the newer edition which will continue to be $29.99.

The new edition contains everything that was in the old one but also has a chapter on filter applications, a section on fixed point implementation, and more guidance and use examples for the filter equations. The IIR equations are mathematically equivalent to those of the first edition, but the common denominator is written out only once to simplify the presentation (and programming from the book). Likewise, the simple relationships of numerator coefficients are made explicit: if b3 = b1, this is what is written for b3, rather than the full equation. All in all the book is clearer and easier to follow. Nothing is lost, but many new features will be useful. Enjoy!

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.




Saturday, March 9, 2013

Digital Filter University - Course V: IIR Filters

So, yall ready for IIR filters? I know I am!






While IIR filters can take on any block diagram form that implements the transfer function, they must implement some variant of the transfer function shown in this chart.

Note that various coefficients can be 0. If they are all 0 the transfer function itself is 0. I suppose this could be called a trivial IIR filter!

If all the denominator (a) coefficients are 0, this is an FIR filter. This is why the IIR filter is more general. But we really can't say that FIR filters are specific types of IIR filters, since FIR filters really do have finite impulse responses (that is, the feedback has been turned off since the a coefficients are all 0).

The block diagrams shown are some of the standard forms.







My book gives you a way to design IIR filters without going through all of this, or without using expensive software. But what's really behind the equations in my book, and behind the expensive software, is something like the following approach.

There are those that don't, but a good many digital filters actually begin life in the analog domain. Hopefully that will not shake your new found faith in digital ones.

The typical process is outline below.










Butterworth filters are, well ..., the bread and butter of the filter world (tee-hee). Here is what they look like mathematically when the process begins. These are the definitions of the Butterworth poles which define these filters in the analog domain.

To design a filter, one chooses the appropriate equation depending upon whether an even or odd order is desired.







The Butterworth poles define an analog filter, but it's not a very interesting one for most applications. This is because it is a lowpass filter and has a cut-off frequency of 1 radian per second and. Not very useful if you needed a bandpass filter centered at 83.4 MHz, right?

Fortunately, there are ways to transform this filter into the desired type with the desired critical frequency(ies), as seen below.









For example, the following transforms a 1st order Butterworth lowpass to a highpass with a 10 radian per second cutoff.













Now, to make that into a digital filter with the same characteristics, we do the following. Note that this transform must take into account the sample rate at which the filter will be implemented.





















Notice that there is a very slight deviation at the upper end, but basically we've made a digital filter that, for the specified sample rate, is a very nice replica of the original analog filter.










If you're the kind of person who rebuilds engines, reupholsters furniture, builds houses, and makes cakes from scratch, maybe you'll enjoy going through the above process every time you need a filter.

I do think it's rather fun, but even I just don't do that anymore. Software has made it so that one can just put in parameters and get answers. That's fine. But there are a lot of menus to try to figure out. And that process rather isolates you from what's happening inside.

In my book I took a different tact. I figured you wouldn't mind doing a little math, but that after you'd done it a time or two, you probably also wouldn't mind if I collapsed all those transforms above into a single step for you. So I derived the simplest possible equations for a number of popular filters, from 1st to 4th order.

This way, you can get the digital filter coefficients each from a single equation, just by providing what you know, cutoff frequency and sample rate, for the case of the 2nd order Butterworth lowpass below.

These are the simplest possible equations for designing such filters. Note that the denominators are the same, and that, in this case, b0 and b2 are also the same. So this makes the implementation even simpler. Note also that, besides using these when you want to design a filter, their simplicity makes them attractive for implementing in real-time systems that need to have adjustable filters.









My book constains equations for the following types of IIR filters.









Now, let's think a little bit more about what's happening behind the scenes. Note how the transfer function can be factored into poles and zeros.









Now let's think a bit about what poles and zeros actually do. First, consider one real zero at 0 Hz.









Now, what happens if we put a real zero at Fs/2? (Note that a single zero has to be real. To be complex it has to have a conjugate pair, so there'd be two zeros...)









A pair of zeros could both be real, but could also be a complex conjugate pair as below.






Here are some of the things we can do with complex conjugate pairs of zeros.







Now let's try some of the same things with poles.







And complex conjugate pairs of poles...








And then we can put poles and zeros together to form all sorts of interesting frequency responses. 







And this is really what digital filters are. Combinations of zeros and poles to create a variety of different responses. Of course you're free to create them anyway you like, but the standard forms are useful because, otherwise, it can take some trial and error to get the shapes we need. On the other hand, once we have a standard filter that's almost what we need, we can sometimes add zeros and poles, or jiggle them a bit, to fine tune.


Thursday, February 28, 2013

That was so nice; Thank You!

Many thanks to the reviewers of my book, Digital Filters for Everyone. If I am doing some things right, I appreciate you for letting everyone know. Bottom line: I am striving to make digital filters more accessible. Thanks to all who are helping me do that!

Monday, February 18, 2013

Digital Filter University - Course IV: Filter Theory

You may have been wondering when we'd get around to filter theory. Or maybe you paid attention in the first lecture and knew it would be now.







I couldn't have said it better if I'd ... oh yeah, I did say it myself. But once is enough.












Think back to the seven sample averging filter we discussed previously.














This filter can be drawn in standard filter notation as follows. Recall that the z^-1 element is a unit delay. This filter takes a seven-sample average of the most recent seven samples.











The averaging filter is an FIR or finite impulse response filter. The genera filter of this variety is shown below. Note that, generally speaking, this can have any number of delays and coefficients, and that the coefficients need not all be the same value.










Due to its utility in filter analysis, we define the impulse signal, which is valued 1 at one time (usually n=0), and 0 at all other times.











We use this impulse signal to get the impulse response of a filter. For an FIR filter, such as the average, the impulse response is just the list of coefficients, as you see here.









We are going to use the z-transform a an analysis tool for digital filters. In the sample domain, we have to 'convolve' the impulse response by the input to get the output. This is simple to do in HW but more difficult to do by hand. And, it doesn't teach us as much about the system as the 'frequency response,' which is the z-transform of the impulse response.

But don't worry. You might have run into z-transforms before and hated them. So did I. But I promise this time it will be simple and painless.







Recall that we can take our difference equation description, and change it to a z-transform description by applying a simple relationship as below.

Did you follow all that?











The transfer function is Y(z)/X(z). It tells us what we multiply the input by to get the output (in the z-domain, at least). The general (IIR) transfer function is shown. For FIR filters the denominator coefficients are all 0. (The funny upside down Vs, capital lambdas I guess, should have been ellipses (...). One day I might get around to updating the graphic.)







A step function is one that is zero up until a point, usually time 0, and then one forever after.







Like its very good friend the impulse function, the step function gives us insight into filter behavior.









Notice that it took the filter a while to "charge" up to the steady output of one. We learned something here about the "charge" time of the filter, and about how it will behave initially when hit with a new signal, or a large, long-lasting burst of energy even within a signal that it was already filtering.

We often plot filter responses on a dB scale. Decibels are defined below. This gives us a way to plot data with ranges that are otherwise too large to visualize.









We can get from the z-domain transfer function to the frequency response of a filter by substituting exp(i omega) for z, as shown below. This is a complex function. If we plot the magnitude (in dB, typically) as a function of frequency we have the magnitude response. We can also plot the phase, as shown below.










The phase above is actually linear, but "wrapped." If we were to "unwrap" it - remove the discontinuities at 180 degree intervals and attach the segments end to end - this one would be a nice linear function. Note how linear each segment appears. And there are ways to know, theoretically that it's perfectly linear.

When the phase is perfectly linear, it has a constant group delay. We see below that the group delay is the negative frequency derivative of the phase.










The Matlab code below shows how to create the filter response plots without using any fancy tools such as from the Signal Processing Toolbox. Similar commands could be used in other math tools. In my book I even show how to do it in Excel.










The arithmetic below shows how to use Euler's equations to get the frequency response of the averaging filter in terms of sines and cosines. You won't need to do all that by hand if you have access to a good math tool. And there are free ones out there.











Now let's think about this filter that showed up previously.










The frequency response for this filter, with the coefficients shown, is shown below. Note also how it smoothes the erratic noise signal (blue) to create the red output signal.







The phase response and group delay for this filter are shown. 









Recall the various types of filters. There are types not shown here, but this is most of the common ones.














If you use my book to design your digital filters, you will not need these, though their effects are embedded within my equations.

Many digital filters begin life as analog filters, and typically as a prototype analog lowpass filter. This prototype is then transformed, using one of the equations below, to have the desired critical frequency(ies) and to be of the desired type. After that it is transformed to digital.













Below are some of the standard forms for digital filters. Any of these can implement any filter type, depending upon the coefficients.