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.

noise: Sample class version 1.0

#include stdlib
#include vector
#include iostream

//---------------------------------------------------------------------------
// Sample: Fills an array of doubles with random integers. This is an example of digital noise
// One obvious use of a non recursive digital filter would be to scale
// the random ints into a specified range of doubles.
// e.g. a filter with a single coefficient of 0.001
class Sample
{
std::vector Samples;
int SampleSize;
public:
void Fill(int sampleSize)
{

Samples.resize(sampleSize); // resize STL "Array"
randomize(); // initialize random number generator.
SampleSize = sampleSize; // keep track in the class
for( int n = 0; n < sampleSize; n ++) // fill
{
Samples[n]=rand();
}
}

void PrintSamples()
{
for( int n = 0 ; n < SampleSize; n++)
{
std::cout << n << ": " << Samples[n] << std::endl;
}
}


};


int main(int argc, char* argv[])
{

Sample test;
test.Fill(3);

test.PrintSamples();
int wait;

std::cin >> wait;

return 0;
}

Thursday, January 25, 2007

This is my blog for my independant study class.

I'm a student at NEIU, and my theory of computation teacher agreed to do an independant study based on R.W. Hamming's book "Digital Filters."

So this is where I'll be keeping track of all the stuff I do for that class.