# How to test digital filters?

First the question(s):

How should I write unit tests for a digital filter (band-pass/band-stop) in software? What should I be testing? Is there any sort of canonical test suite for filtering?

How to select test inputs, generate expected outputs, and define "conformance" in a way that I can say the actual output conforms to expected output?

Now the context:

The application I am developing (electromyographic signal acquisition and analysis) needs to use digital filtering, mostly band-pass and band-stop filtering (C#/.Net in Visual Studio).

The previous version of our application has these filters implemented with some legacy code we could use, but we are not sure how mathematically correct it is, since we don't have unit-tests for them.

Besides that we are also evaluating Mathnet.Filtering, but their unit test suite doesn't include subclasses of OnlineFilter yet.

We are not sure how to evaluate one filtering library over the other, and the closest we got is to filter some sine waves to eyeball the differences between them. That is not a good approach regarding unit tests either, which is something we would like to automate (instead of running scripts and evaluating the results elsewhere, even visually).

I imagine a good test suite should test something like?

• Linearity and Time-Invariance: how should I write an automated test (with a boolean, "pass or fail" assertion) for that?
• Impulse response: feeding an impulse response to the filter, taking its output, and checking if it "conforms to expected", and in that case:
• How would I define expected response?
• How would I define conformance?
• Amplitude response of sinusoidal input;
• Amplitude response of step / constant-offset input;
• Frequency Response (including Half-Power, Cut-off, Slope, etc.)

I could not be considered an expert in programming or DSP (far from it!) and that's exactly why I am cautious about filters that "seem" to work well. It has been common for us to have clients questioning our filtering algorithms (because they need to publish research where data was captured with our systems), and I would like to have formal proof that the filters are working as expected.

• I don't have enough experience in DSP to answer this question, but I'm going to guess the answer is no simpler than “measure the specific properties you care about and check if the results are within acceptable limits”. Jul 22, 2015 at 4:13
• This is not what are you asking, but it shows one possible approach. Sep 2, 2015 at 6:44
• I have a similar desire to unit test some DSP code and I'm curious what solutions you came up with in the end. Could you post an update? Sep 30, 2016 at 17:05
• I ended up testing frequency response, creating a collection of logarithmically increasing frequency senoidal signals, and getting attenuation for that frequency, that is, how much peak-to-peak values decreased after filtering. From that, I created a graph displaying the plateau I expect to see in a band-pass filter. But that was very pragmatic and not scientific at all. Sep 30, 2016 at 18:05