IIRFilter: can we do a band-stop?

Mar 23, 2012 at 7:28 PM

I notice that you have an IIRFilter in this package. It has BandPass but not BandStop support. How easy would it be to add BandStop support?

Would you mind documenting the parameters on the FilterBuffer method?

Also, I was confused by the history fields in that class. Why does the class track filter history? Do I need a new instance of the filter every time I want to use it?

Mar 24, 2012 at 6:01 PM
Edited Mar 24, 2012 at 6:06 PM

Yes well the IIRFilter class... Really this class has been cobbled together over the years from bits and pieces of info I found around the internet. To be honest, I don't remember what every part of it does and I think at this point it would take me just as long to figure it all out again than it would take you.

To add bandstop as opposed to bandpass, I believe you just need to change the sign from + to - or from - to + somewhere in the algorithm but as I said I don't remember the details of this class. I think some searching around the internet would probably show what the difference in the two algorithms is and would make it possible to add bandstop support by copy/pasting the existing code and flipping the signs.

As for the history fields, well the way IIR filters work is by applying an algorithm to the current sample plus some past sample values. It is those past sample values that need to be stored in the history buffer. Remember that IIR filters simulate RC electronic filters, and think of the histoy as the charge on the capacitor(s). For instance note that there is a IIRFilter.Reset(double startValue) call. The startValue parameter is used to set the history values to this value, as if the capacitor(s) is being "pre-charged" to this value. If this was not done, some ringing would occur when you start filtering a waveform and the waveform doesn't start at zero because the filter first needs to "settle" to the current waveform value.

So yes, for each different waveform you want to filter, you need a separate IIRFilter instance. But note to keep filtering the same waveform, just use the same IIRFilter instance each time you filter the next part of the waveform (which would then already contain the correct history values and you don't need to call Reset between each call).