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;

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;
oldest = 0;

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


Leave a Reply

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

You are commenting using your 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