Implementing FIR filters in C is much easier if we make use of the static variables declared in our functions. In this blog post we will create some simple example FIR filters, and get their impulse response. To understand this post you will have to have the basic theory of FIR filtering down.
FIR Filter Implementation
There are a lot of ways to implement a FIR filter in C. The method provided makes the implementation simple because we can simply put the last time sample into the filter and the filtered result will come out. The filter function itself will handle the delay line of the time samples. It’s a bit abstract so let’s take an example of just creating a delay line of samples. Then later we’ll add in the FIR filter coefficients.
An Example Delay Line
Before we go into the FIR filter implementation, let’s first look at how we’ll be implementing the FIR filters in general. We are going to statically initialize a delay line and each time a sample comes in we’ll shift samples down the line.
What this function does is hold 5 samples in the
xv array. We don’t exactly need to store the 5th sample, but do since it makes it easier later on for loops. The element
xv will be the latest sample and
xv will be the oldest sample. In discrete mathematics this is referred to as
x[n-4]. Since we initialized
xv as static, the variables will be saved for us on each function call.
Let’s look at how the
example_delay_line function can work.
Running this program yeilds the output of:
From this output we can see the array of
x is delayed by 4 samples, or shifted to the right by 4 samples. The zeros in the first 4 samples are the statically initialized elements of the
xv array in the
Implementing a Basic Filter
Now that we have the delay line example behind us it’s time to implement a basic filter. We will apply this basic filter to our delay line. More properly we will convolve our filter with the delay line. We will define our filter taps, or filter coefficients in an array called
h. Our delay line will be the same but we will multiply corresponding filter taps by corresponding delay line elements. What I’m describing here is a convolution between the filter and the time samples in the delay line example.
Getting the FIR Filter Impulse Response
Now that our basic FIR Filter is implemented we’ll get the impulse response of this filter by doing a 1D convolution on the impulse response.
Here we define our impulse function as
imp. We shift over the impulse function by one time sample and we put it through our filter. As you can see our filter coefficients are the result of our output.
Where to Go from Here?
Now that we’ve implemented a FIR Filter and confirmed it’s impulse response the next step would be to create a FIR Filter with the properties we desire. This would be a low pass, band pass or high pass filter. Once we know what we should filter we would design it with say a Butterworth, Chebyshev, Bessel, Gaussian, Elliptic digital filter type.
We have only 5 filter taps above which is really not enough to effectively filter. Any type of real-world FIR Filter would have a larger number coefficients. We also need not define the filter coefficients in the filter function itself and can be externally defined since they normally would not change.