How to Implement FIR Filter in C: Simple Guide and Example
To implement a
FIR filter in C, create an array for filter coefficients and an input buffer, then compute the output by summing the products of coefficients and input samples. Use a loop to slide over input data and apply the filter formula y[n] = Σ (b[k] * x[n-k]) where b[k] are coefficients and x[n-k] are input samples.Syntax
The basic syntax for a FIR filter in C involves defining an array of filter coefficients and an input buffer. The output at each step is calculated by multiplying each coefficient with the corresponding input sample and summing all these products.
Key parts:
- coefficients[]: The filter weights.
- input[]: The input signal samples.
- output[]: The filtered output values.
- filter_order: Number of coefficients.
c
for (int n = filter_order - 1; n < input_length; n++) { output[n] = 0; for (int k = 0; k < filter_order; k++) { output[n] += coefficients[k] * input[n - k]; } }
Example
This example shows a simple FIR filter with 5 coefficients applied to an input signal array. It prints the filtered output values.
c
#include <stdio.h> int main() { // Define filter coefficients (example low-pass filter) double coefficients[5] = {0.2, 0.2, 0.2, 0.2, 0.2}; int filter_order = 5; // Input signal samples double input[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int input_length = 10; // Output array double output[10] = {0}; // Apply FIR filter for (int n = filter_order - 1; n < input_length; n++) { output[n] = 0; for (int k = 0; k < filter_order; k++) { output[n] += coefficients[k] * input[n - k]; } } // Print filtered output for (int i = filter_order - 1; i < input_length; i++) { printf("output[%d] = %.2f\n", i, output[i]); } return 0; }
Output
output[4] = 3.00
output[5] = 4.00
output[6] = 5.00
output[7] = 6.00
output[8] = 7.00
output[9] = 8.00
Common Pitfalls
Common mistakes when implementing FIR filters in C include:
- Not handling the start of the input array properly, causing out-of-bound errors.
- Forgetting to initialize the output array to zero before accumulation.
- Using incorrect indexing which reverses the order of coefficients or samples.
- Ignoring filter order and input length mismatch.
Always ensure loops start at filter_order - 1 to avoid accessing negative indices.
c
/* Wrong: starts loop at 0, causes invalid memory access */ for (int n = 0; n < input_length; n++) { output[n] = 0; for (int k = 0; k < filter_order; k++) { output[n] += coefficients[k] * input[n - k]; // input[n-k] invalid when n<k } } /* Right: start from filter_order - 1 */ for (int n = filter_order - 1; n < input_length; n++) { output[n] = 0; for (int k = 0; k < filter_order; k++) { output[n] += coefficients[k] * input[n - k]; } }
Quick Reference
- Filter coefficients: Define the filter shape.
- Input buffer: Store signal samples.
- Output calculation: Sum of products of coefficients and input samples.
- Loop start: Begin at
filter_order - 1to avoid invalid memory access. - Initialization: Set output to zero before accumulation.
Key Takeaways
Use an array of coefficients and input samples to compute the FIR filter output by summing their products.
Start the main loop at filter_order - 1 to avoid accessing invalid input indices.
Initialize output values to zero before accumulating sums.
Check that filter order and input length are correctly handled to prevent errors.
Test your filter with known inputs to verify correct implementation.