Friday, January 26, 2007

Problems with implementing a generic digital filter class.

Hamming mentions near the end of section 1.1 the "initial data" problem.

The output of a non-recursive filter is the weighted sum of the current input sample and some

number of samples before and after the current position.

This begs the question, "What values are used for the samples 'before' the first sample and 'after'

the last sample?"

Assuming zero values is one possibility. This is relatively easy for both the writer of the

library and the libraries users. However, this potentially introduces a non-linearity in the

filter output. (Which is especially problematic in a recursive filter, where such non-linearities

can continue with every subsequent value of the filter.)

Another possibility is to pass enough "before" and "after" values in the function call to fill in

these gaps. This is vaguely "unclean" as the number of values that are needed to be passed change

when the number of filter coefficients change, so the function must be written to accept a generic

number of variables. This also puts a fairly large burden on the user of the library to initialize

and maintain these values as necessary.

Yet another possibility would be to assume that the initial and final values in the given Sample

are ment to be used as the starting coefficients, and that only the "interior" values of the sample

are ment to be actually filtered. This seemes elegant at first, however, if the Sample is passed

through several filters, the output will get progressively shorter, losing information, unless the

user adds the necessary samples to the beginning and end of each output step before inputting it to

a new filter.

The answer, in my current opinion, is to provide all three methods in the Filter class. When

nature presents a fork in the road, take it.

No comments: