Exponential smoothing technique acts as a low pass filter for high-frequency noise and it smooths the data by the exponential window function. You can read about exponential filter more in this wikipedia page . Now, let's look at the function.

So here, x sub t is the current sensor value, s sub (t-1) is the earlier smoothed value and s sub t is the current smoothed data output. Alpha is the smoothing factor which must be between 0 and 1. As you increase the value of smoothing factor, the filter more closely follow the raw data and for example, with the smoothing factor of 0.9, we get the similar waveform as the raw data. We will see the effect of alpha in filtering waveforms.





Now, let's implement it. Here's the code I tested and again I use an LDR reading and will try to smooth it with this code.





#define IN_PIN A0

int VALUE = 0; int AVERAGED = 0; int OLD_VALUE = 0;

void setup() { pinMode(IN_PIN, INPUT); Serial.begin(9600); }

void loop() { VALUE = analogRead(IN_PIN);

AVERAGED = ((0.25*VALUE) + (1-0.25)*OLD_VALUE); OLD_VALUE = AVERAGED; Serial.print(VALUE); Serial.print(","); Serial.println(AVERAGED); delay(25); }





I have used alpha value to be 0.25 which I usually prefer. Here are the waveforms (red is the smoothed waveform and blue is the raw data):





You can observe the smoothness in the output waveform and also a little lag. Next, I choose the smoothing factor of 0.1 and here, you can see the lag in the output waveform and sometimes with drastically changing raw data, some information might be lost.





Here, you can see the loss of data which might be important. Besides lag and lost information, in the below waveform, you can observe the startup transient and decrease in the output waveform which occurs only by choosing such low smoothing factor.









Next I changed the smoothing factor to 0.9 and you can see how closely the output waveform follows the raw data.





Thus, it is better to choose the smoothing factor according to your application and how rapidly it's value can change. However, I usually recommend the value of 0.25. So this is it for today. I hope you found this post useful.





Thanks for reading!

Noisy sensor data is an important concern and the raw output of sensor is sometimes not sufficient to acquire the desired information. Of course, hardware is susceptible to noise and it is important to implement filters to get a smooth desired waveform. We earlier discussed running average filter . Today we will discuss about exponential filter.