DSP Trick: FIR Filtering in C

Subject: DSP Trick: Filtering
From: rainer.storn@infineon.com
Date: 1999/04/21
Newsgroups: comp.dsp

Name: Filtering in C

Category: Programming Trick

Application: FIR-Filtering

Advantages: Simple and fast


This is more like a trick in C but might also be applicable in DSP environments. It allows to compute FIR-filtering in a fast manner when the filter length contains a factor of, let's say 4 (other factors are also possible).

The Trick:

Let's suppose you have an array for the FIR filter coefficients w[LEN] and an array for the delay line x[LEN]. A straightforward approach to do the filtering operation would be:

   y=0; //will contain your result
   for (i=0; i<LEN; i++)
      y = y + w[i]*x[i];

However, many processors don't like the index calculations, so it is sometimes advantageous to do less index calculations. Here's how it goes (suppose LEN is divisible by four. As stated above, the trick basically also works for other factors):

   w_end_ptr  = &w[LEN]; // load sentinel
   w_ptr      = w;
   x_ptr      = x;
   y0         = 0.; // we assume floating point here, so scaling is
   y1         = 0.; // not an issue
   y2         = 0.;
   y3         = 0.;
//----Do the filtering------------------------------------------
   while(w_ptr != w_end_ptr)
      y0 = y0 + w_ptr[0]*x_ptr[0];
      y1 = y1 + w_ptr[1]*x_ptr[1];
      y2 = y2 + w_ptr[2]*x_ptr[2];
      y3 = y3 + w_ptr[3]*x_ptr[3];
      w_ptr = w_ptr + 4;
      x_ptr = x_ptr + 4;
   y = y0+y1+y2+y3;         // y will contain the filtering result

I had pretty good result with this on a SPARC.

Rainer Storn