An FIR filter in C

I came across a nice article the other day. The company, Netrino, actually has lots of good articles. This was the code fragment(doesn’t compile without some adding some straighforward declarations and definitions, etc.) in the article(without the comments)
sample = input();
x[oldest] = sample;
y = 0;
for (k = 0; k < N; k++)
y += h[k] * x[(oldest + k) % N];
oldest = (oldest + 1) % N;
output(y);

This is fine for illustration(which was what this article was doing). In a real-time DSP environment though, you would never use the modulus operator(too computationally expensive). Instead:

x[oldest] = sample;
y = 0.0;
for (k = 0; k < N; k++)
{
if( (oldest + k) < N )
y += h[k] * x[(oldest + k)];
else {
y += h[k] * x[(oldest + k)-N];
}
}

if((oldest + 1) < N)
oldest += 1;
else
oldest = 0;
output(y);

In fact, even the built-in sin, cos etc. of the C math library are not used. Instead lookup tables are used.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s