Better Analog Signal Debounce

Status
Not open for further replies.

Iwilsonp

Member
I've spent a significant amount of time trying to find the best way to filter the noise out of the analog reads from the Teensy. Of course, one of the first things that comes up is the classic Arduino Smoothing Tutorial, which is a very suboptimal solution (gotta love how it claims "no lag"...).

One way to improve over just a moving average is exponential smoothing: we just keep track of the last smoothed value and, to get our new value, average in our new reading. Implemented:
Code:
smoothed_value = k * new_value + (1-k) * smoothed_value
Where K can be changed to vary the amount of smoothing. This solution is easier on memory (1 value vs an array of values) but it still runs into trouble when we are reading a linear trend: it causes us to 'lag' the trend as our new values try to pull up the average.

A much better solution is provided by the NIST: double exponential smoothing. Basically, it combines exponential smoothing of the values themselves with smoothing of the rate of change of the values, which is then fed back into our original smoothed values to control for the trend. Easier to see implemented (or on the NIST page):
Code:
//diff_value is the smoothed output from the pots
//value is the new reading 
//diff_speed is the rate of change of the pot value
//NEW_WEIGHT and SPD_WEIGHT are constants (range: 0 to 1) used to adjust how much the filter filters out: lower is more filtering
diff_value = NEW_WEIGHT * value + (1-NEW_WEIGHT) * (diff_value + diff_speed);   //updates diff_value, our smoothed pot outputs. Note that diff_speed is used too!
diff_speed = SPD_WEIGHT * (diff_value - diff_old_value) + (1-SPD_WEIGHT) * diff_speed;  //updates diff_speed, or the rate of change of the smoothed output
diff_old_value = diff_value;  //diff_old_value is to let us calculate the speed
Speed is provided in ADC counts per loop.

Hope that this helps someone.
 
THanks. I will have to try the NIST solution. Sampling a DC current shunt, I tried an exponential moving average filter but had better results with this median filter. The histogram was more gaussian-like.
 
This has been a huge help thank you, I was having issues with adjusting an audio delay due to the jotter in the pot, this smoothed it out perfectly.
 
Status
Not open for further replies.
Back
Top