Noise is a common issue in embedded systems. Software engineers are often tempted to use simple averaging to perform a filtering task in software, but this can consume valuable RAM space and real-time in a small processor. It is essentially an FIR filter with coefficients of 1/N. Simple averaging may not provide as aggressive a stop band as a designed FIR or IIR filter. Adding a digital IIR filter is a quick and easy alternative.
Digital filters can be implemented with either floating point or fixed point math. This example uses floating point. While floating point is simpler, it is not always possible due to code size or processor speed requirements. In addition, a fixed-point solution can be used in an FPGA, which can be advantageous. Fixed point solutions will be explored in our next newsletter. Stay tuned!
The first-order IIR filter form can be expressed as follows:
The implementation of the actual floating point filter in C is comprised of just three lines of code:
w0 = (input - (a1 * w1)); /* Calculate w0 term */
output = w0 * b0; /* Calculate output value (b1=0) */
w1 = w0; /* Save w1 term for next calculation */
Note that, as with any A/D converter, an anti-aliasing filter is still recommended at the A/D input even if a digital filter is being used.
For the full code example, please follow this code example link. Follow the filter design calculations and frequency response link to determine coefficient values.