Friday, January 13, 2017

Simple Digital Low Pass Filter

Low pass filters help smooth out signals so they're not as noisy as before they were processed. A simple way to put it is, a low pass filter makes a signal smooth and not so bouncy. If your signal is coming from an analog circuit, you can do this with hardware. All you would need is a capacitor and resistor. Although, for this application, I'm interested in doing this filtering in software.

A "Single Pole Recursive Filter" essentially just looks back at a previous filtered value and takes that into account with the current input. In the equation below

  • y is the current filtered output
  • i is the current input sample
  • o is the previous filtered output
  • a = (1 - c)
  • b = c
  • Where c is a coefficient less than one

y = a*i + b*o

The size of c is like deciding how much influence the previous value has on the filtered output. The larger the value of c, the slower the response in the output to a quick change in the input and the more we're relying on the previous output. With a high value of c, the output ends up being much smoother than the input signal.

Using Google Sheets, I applied the Single Pole Recursive Filter to different sets of data: a constant signal that drops off to zero, a list of random numbers and a short square wave signal. The result is shown with a c value of .75, where the blue line is the original signal and the red line is the filtered signal.





That looks like its doing an ok job of removing the extreme peaks and valleys. The output signal ends up running through about the middle of the input signal. What about a square wave?



Seems like we have a decently working low pass filter. Neat! Let's try to implement some later with some real analog inputs and see how it performs.

Next time, let's go up a few levels and look and graphing libraries. Having some code to visualize our data will help us see the effect our filter has on the input signal. 





1 comment: