Blog The Filter Wizard Remastered

An Excelent fit, Sir!

At the time of writing, some 9 years ago, the cat was just out of the bag. PSoC3 , Cypress’s then-new family of paradigm-busting programmable mixed-signal systems-on-chip, was announced. Phew, it’s hot in here, off with that marketing hat (it’s been on and off periodically since then…).

This is the first of two columns showing a simple technique for designing, optimizing and implementing a response-equalizing filter. The second part will look at implementing it on a device such as PSoC3; this part is about the design process. Figure 1 is the first piece of our story. It’s the curve of the sensitivity of a customer’s sensor versus frequency (what kind of sensor, do you think?):

Figure 1

Frequency response of the sensor.


Frequency response of the sensor.


The customer said they wanted to equalize this so that the final sensitivity would be “independent of frequency”. Now, an elementary rule of Filter Wizardry is: The frequency response of a filter doesn’t stop at the edges of the graph . It’s important to understand what the response should do outside this range. “Don’t Care” doesn’t mean “Don’t Need To Care”.

Now, I’ll wager that the response of that sensor continues to fall off at 20 dB/decade outside the graph limits. But it would be a terrible idea to boost the response with gain that continues to rise without limits. We’ll want the response to flatten off or even fall back down, to avoid noise problems. For a first try, let’s hold constant the response outside Figure 1’s frequency range, for one extra decade in each direction; Figure 2:

Figure 2

Extend the response on either side.


Extend the response on either side.


Next, we’ll try to design a realizable filter with a frequency response approximating this curve to an acceptable error. The low frequencies involved indicate using a digital filter to avoid bulky external passive components. That’s handy, because PSoC3 has a built-in 24-bit digital filtering engine, its performance so fast that it’ll hardly have to wake up to crunch out anything I’ll ask it to do here. The digital stuff is for the next column; first, let’s design it in the analogue domain.

Figure 2’s response is flat at one end, wavy in the middle and flat at the other end (apologies to Monty Python); that’s a second order ‘shelf’ equalizer. The amplitude response of the general second order transfer function or ‘biquad’ section:

is easily computed in a spreadsheet. With ω equal to the input frequency divided by the frequency to which the transfer function is normalized, a bit of manipulation gives:

Figure 3 is a clippet of a working spreadsheet, using one column for each biquad section, with the cell formula shown in glorious ‘Excelcolor’. The gains of all the sections used are multiplied up (linear gains, not dB) and deposited in column G:

Figure 3

Click here for larger image Working out the filter response in Excel.


Working out the filter response in Excel.


Now we need an initial coefficient guess; here’s how I’d do it:

  1. The middle of the frequency range is around 0.3 Hz so that’s where we’ll normalize everything to.
  2. At high frequencies we need a gain of 10, so that’s the ratio of the s2 terms.
  3. At low frequencies we need a gain of 1, so that’s the ratio of the constant terms.
  4. We need a guess at the dissipation, which is the reciprocal of the ‘Q’ of the denominator, and sets the denominator s1 term. The filter covers a frequency range of about 1000:1. My guess is the square root of this , say 30.
  5. At mid frequencies we need a gain of 0.4, and very roughly indeed we can set the ratio of the s1 terms to this. In other words, I guess:

Check out the response – not bad, Figure 4:

Figure 4

My initial guess.


My initial guess.


It’s not perfect; let’s get the spreadsheet to find us a better fit. We need response limits, and a measure for how far out the result is. We’ll use the “method of least squares”. For any point whose gain goes outside set limits, take the square of the relative gain error (1 generated_gain/required_gain) at each frequency, and add ‘em all up.

For the response limits shown in Figure 5, I allowed 1% gain variation in the passband, and between (2x required) and 0.1 in the bands outside. If a response point lies between these limits, it doesn’t contribute to the error function.

Figure 5

You've got to set limits


You’ve got to set limits


Now we use Excel’s Solver to ‘improve’ our response. Configure it to minimize cell A2’s value by adjusting the cells holding the filter coefficients (allowing only positive solutions), and click ‘solve’ to get Figure 6:

Figure 6

As if by magic, a better fit.


As if by magic, a better fit.


Actually, I’m making it look a little easy. Excel’s solver is prone to settling on a poor local minimum if your initial guess is too far out, so you sometimes need to intervene to help it.

This seems good in the desired frequency range, but that gain is still high outside the passband, especially at the higher frequencies. How about adding another filter section to chop off the low and high frequencies? Separate single pole lowpass and highpass filters would do. But did you realize that you can combine those single pole filters into one two-pole bandpass? It means we can reuse the formula we already created. I’ll try a section with the same denominator as my first guess, but with only the s1 term in the numerator non-zero, set to match the denominator:

Let’s also be more aggressive with those upper gain limits, forcing them to fall outside the band, Figure 7:

Figure 7

Adding a bandpass to reduce the out-of-band stuff.


Adding a bandpass to reduce the out-of-band stuff.


We invoke the solver again, and get Figure 8:

Figure 8

Final design with bandlimiting.


Final design with bandlimiting.


which is falls away nicely on both sides. It’s still an analogue filter; in the next column we’ll look at issues involved with ‘turning it digital’ – in the same spreadsheet – then implementing it and analyzing it. Let me know if this workflow could prove useful for your filter curve-fitting – Kendall.

More articles on PSoC3

Cypress expands PSoC’s programmable and precision analog capabilities

Demonstration kit shows precision analog capabilities of the Cypress PSoC 3 architecture

Just Add a Transistor

Temperature Measurement, Part 1

Controlling Brushless DC Motors to Tackle New Challenges

0 comments on “An Excelent fit, Sir!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.