7
$\begingroup$

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.

$\endgroup$
4
  • 1
    $\begingroup$ 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”. $\endgroup$
    – Kevin Reid
    Commented Jul 22, 2015 at 4:13
  • $\begingroup$ This is not what are you asking, but it shows one possible approach. $\endgroup$
    – Gluttton
    Commented Sep 2, 2015 at 6:44
  • $\begingroup$ 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? $\endgroup$ Commented Sep 30, 2016 at 17:05
  • $\begingroup$ 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. $\endgroup$ Commented Sep 30, 2016 at 18:05

1 Answer 1

4
$\begingroup$

Some thoughts on it at least.

First, if you can shown linearity and time-invariance and you know that the filter has the correct impulse response you are home. Given that the filter is stable. So in this case there is no need to run different other input signals and the frequency response is given based on the impulse response.

Checking impulse response is obviously quite simple. Linearity and time-invariance I guess is actually really complicated (any specific input signal you test may just work out of coincidence, but others may not). However, as the filter code in most cases shouldn't be that complicated, it should be possible to eye-ball the code and see that it does what is expected (no non-linear operators, no if-cases based on signal values etc).

Remains stability. For IIR filters this is a really complicated issue and I will not go into that discussion. For FIR filters, they will always be stable (unless they are implemented using some recursive algorithm). However, you may run into numerical issues relating to overflow (most likely not underflow) and round-off noise. You can find formal proofs that an FIR filter will not overflow (maximum output is the sum of the absolute values of the impulse response coefficients times the maximum input magnitude). For round-off errors you can find statistical expressions, but they are not of much use in formally proving anything (but rather as an argument that the expected output with "infinite precision" and the actual output still conforms).

Also not that all impulse responses etc should be evaluated using the actual (possibly rounded) coefficients not the (possibly higher precision) ones obtained from the filter design tool. This is because the filter will always realize the transfer function given by the coefficients that it use, no matter how they were derived. This may still not be the impulse response you get at the output though, as there may be round-off issues. An easy way to see that is to use a scaled impulse (i.e. not one) and note that typically not only the magnitude of the frequency-response will change, but also the shape. The filter will still implement the same transfer function, but the round-off noise added to the two different input signals are different.

Hope this gives you a bit more insight.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.