The story so far
In Part 1 we took a regular FIR filter design and wrote down the filter coefficients in polynomial form to get equation :
Then we found all the roots of this polynomial, and used them to write down the
factorized form of the polynomial:
As a parting shot, I pointed out that there are three quadratic terms there with unity coefficients of z^0 – and there are three deep nulls in the gain response of the filter, as was shown in the figures from Part 1. Let's take a deep breath and examine the responses of all these individual linear and quadratic factors, to see if there are some clues there.
Figure 1 shows the individual responses, treated as two- or three-tap FIR filters, of each of the factors in parentheses in equation ; the five quadratic factors marked as q1 to q5 and the four linear factors and L1 to L4. It's quite a jumble of a graph, but you don't have to be very awake to see the major salient detail: three of those quadratic factors have deep notches in the frequency response. These are indeed the three factors whose constant (z^0) coefficient is unity!
So, here's the first takeaway. In an FIR filter whose stopband contains a number of sharp nulls, each one of them comes into being because the response of one of the polynomial's quadratic factors falls to zero at one frequency. Just so that we don't jump to conclusions about the particular form the factor needs to have, let's do some more algebra to make sure. Are we having fun yet?
Figure 1: The frequency response of all the quadratic and linear factors of equation 
The frequency response of a quadratic factor used as a filter
Here's where we make an important substitution. Until now, our z has been a mystery variable with no obvious relationship to the behaviour of a sinewave. Let's introduce the expression that actually defines the z-transform. The key relationships between z , z^-1 and the frequency f of a sinewave input can be expressed in either an exponential or a trig format:
where fs is the sample rate. You can make a mental connection to the effect of a small time delay (equal to the sampling interval) on the phase of a sinewave of frequency f. z is a complex variable that 'rotates round' the complex plane between purely real and purely imaginary, as the frequency f affects the argument of the trig functions.
You'll often encounter the exponential form  in filter books, but the complex trig form of  somehow seems more relevant to the engineer's habit of stuffing a sinewave into something and seeing what happens. So, let's substitute  into Part 1's equation  and see if we can develop an expression for the frequency response.
The first line of the boxed portion of  shows the real part, and the second line shows the imaginary part. Now, for a quadratic section to give a null in the frequency response, there must be a frequency fn for which Q(fn)=0. This means that both the real and the imaginary parts of equation  must be identically zero at that frequency. It should be easy to see that for the imaginary term in  to vanish for a frequency fn, we simply need to have
and if this is the case, we can substitute it back into the real part of  and set that to equal zero, from which we get
Bingo! This corroborates our earlier observation, and what's more, it gives us a direct expression for the quadratic factor that's needed in order to locate a frequency response null at any fn that we want. Figure 2 shows the frequency response of quadratic factors (three-tap filters) built using equation , for several notch frequencies.
Equations  and  give the real and imaginary parts, and we can see that there are some special cases. For zero frequency (z term = -2) and half the sample rate (z term = +2), the factor Q(z) is the product of two equal real factors, either (z-1)*(z-1) or (z+1)*(z+1). For a frequency of Fs/4, the roots are purely imaginary, and Q(z)=(z+j)*(z-j).
Figure 2: Factors of the form of equation  for various values of null frequency
Now for a real example. Let's say that I want very high attenuation between around 49 Hz and 51 Hz, again between 59 Hz and 61 Hz (i.e. the system must be able to reject both commonly-used AC line frequencies without change of coefficients or sample rate), and finally also between 98 Hz and 102 Hz. I'll specify 220 Hz as the sample rate to ensure that the 2nd harmonic of 60 Hz aliases round and falls right into that same hole that the 50 Hz second harmonic does (homework, if you can't see why that happens).
So, what happens if I just build a filter that has nulls at these six frequencies? It's simple to make up a spreadsheet that calculates the z coefficient shown in equation  for a set of frequencies, and multiplies them up into a single polynomial. The six frequencies create six quadratic factors, shown in Table 1.
Table 1: Six quadratic factors, calculated for specific null frequencies
When multiplied together (equation ), these six quadratic factors produce a twelfthorder polynomial (equation ).
The frequency response of this filter is shown in Figure 3, straight out of Excel. For the intended application, it's actually already a pretty good filter, “straight out of the box”, and it has no wasted coefficients at all!
Figure 3: The frequency response of the filter defined by equation 
Let's take a closer look at the region around 50 Hz and 60 Hz in LTSpice. Yep, those nulls are indeed where we wanted them to be – see Figure 4. Now, it's easy to play around with this approach using the spreadsheet, adding and moving nulls until you get exactly what you want.
Not all combinations of null frequencies will necessarily produce a response that you're happy with first time round. You'll find that as you try to push the stopband of a lowpass filter well below fs/4, for example, you'll need to add extra nulls above this frequency to 'pin the stopband down'. It's an empirical way to proceed, for sure – but you certainly get a feeling for what's going on with these nulls.
Figure 4: Up close and polynomial; nice notches just where we asked for them
If you want a filter with an even number of taps, you can multiply in an extra linear term of (z+1), which has the effect of putting a null at fs/2 (which gives us some extra stopband rejection). That's something we can deduce from one of those earlier special cases; if a function Q(z)=(z+1)*(z+1) has a null at fs/2, so must its square root.
Flattening the passband
Our filter has got useful stopband behaviour, but the passband response isn't flat anywhere, it rolls off in the passband right from low frequencies. The response is actually close to Gaussian; you can see from the coefficients that the filter isn't going to overshoot at all on a step input signal. How to tell? Just look at the sum of progressively more terms in the impulse response; it's monotonically increasing because all the filter coefficients are positive, so the time response to a step never falls as each extra coefficient is 'uncovered'. That behaviour can be useful, but what do you do if you'd rather have a flatter frequency response over at least part of the passband?
We can add more factors to our polynomial to flatten out that droop, choosing those that contribute a gently rising response as frequency increases from DC. Look back at Figure 1; some of those polynomial terms didn't give us a null, but just a gentle slope up or down in response. These acted together to give a flatter overall frequency response to our original example, plotted in Part 1. We can construct our own terms to flatten out that gentle low frequency droop. Each quadratic factor you add will increase the coefficient count by two; multiplying in a linear term just adds one to the tap count.
The general analytic expression for the frequency response of an arbitrary root value (or pair of values) is quite clunky and I won't write it down here. It's quite possible to build an optimization script (think Million Monkeys) that can tweak the coefficients of extra linear and quadratic terms to flatten out the passband to the desired level.
Here's an example done entirely empirically; I've taken our synthesized filter and multiplied the polynomial by an additional (z-0.65). The rising response of this extra term has the effect of flattening out the droopy passband at lower frequencies – and also degrading the stopband rejection a little. Figure 5 shows what those coefficients look like when entered back into PSoC Creator's filter tool. The impulse response has become non-symmetrical, meaning it's no longer a linear phase filter. We can rectify that with yet more terms – but not today, I'm out of space and time! “Swings and roundabouts” works for filter design too.
Figure 5: Throw in another root (z-0.65) and we can flatten that passband a bit
Let's be honest; this isn't intended as a replacement for more conventional filter design techniques, especially if you're in a hurry! But it's a really good way of understanding the effect that those roots – filter designers call them 'zeroes' – of the filter polynomial are getting up to under the hood. For our example hum rejection filter, we got a useful result (we'll be using it commercially), with the added satisfaction that we built the coefficient set up right from first principles, and we understand exactly why it has its nulls where they are.
I hope this shows you that it's sometimes useful to break down the apparently monolithic, single-stage nature of an FIR filter into simpler pieces that can be separately manipulated. Try dialing some of these nulls into your next FIR filter design. It's the 'notchural' way! best / Kendall
View the Filter Wizard's archive of spells.
P.S. Check out my Cypress blog at www.cypress.com/go/thefilterwizard - you can also email me from there.
About the author:
For more articles like this and others related to audio design, visit Audio Designline and/or subscribe to the monthly Audio newsletter (free registration).
Kendall Castor-Perry is a Principal Architect at Cypress Semiconductor,doing mixed-signal system analysis and design for the new PSoC platform. Kendall uses decades of experience in analog engineering, filtering and signal processing to capture signals across many domains, extract the information from them and do something useful with it.
This article originally appeared on EE Times Europe.