



Firstly, I have taken readings from an LDR sensor (used for testing of filters). Generally we want to filter some very valuable data like the data from an IMU distance sensor. Here is the plot of unfiltered reading:





Now, I have added a low pass RC filter of cut-off frequency around 4.8Hz made up of a 150 ohm resistor and a 220uF of capacitance. The results are as follows:





The quality or SNR (Signal to Noise Ratio) of the signals also depend on the noise level in reference voltage input. An analog reference voltage pin in any microcontroller sets the 1024th or the maximum sensor value which should be very stable. The reference input must be provided by a voltage regulator or a low pass LC filter can be used. LC filter made by a 10uH of inductor and 100nF capacitor should do the trick!



Note that sometimes high frequency signals are to be included to improve the resolution of sensor data.



Sometimes, the sensor must be powered as mentioned in the datasheet like some sensors provide a stable reading when connected to ground with a 100 ohm pull down resistor. So datasheet must be referred for maximum data quality.



Now, let's try to filter our waveforms further and try to play with some coding. The filter I personally like the most is a moving average filter which is easy to implement and also very effective. Before discussing moving average filter, we should see an averaging filter in which an array of sensor values taken is averaged (the measurements are added together and divided by the number of measurements taken). This filter gives a nice result when we average as many as 10 values and as we average more measurements, we will get less noisy data, but as we go up and increase the array size to let's say 120. The averaged data from 110 values is not going to be less noisy than the averaged data from 120 values.



Further, the response time of this filter is not very good. However, it is a very efficient filter for most applications.



In the moving average filter, recent measurements are stored and averaged. It will occupy some memory depending on the size of your array. If you have an array of 5 values, then the most recent sensor value is stored in the array and the older value is removed. Let's write the code based on this.



#define IN_PIN A0

#define ARRAY_SIZE 16



int INDEX = 0;

int VALUE = 0;

int SUM = 0;

int DATABASE[ARRAY_SIZE];

int AVERAGED = 0;



void setup() {

pinMode(IN_PIN, INPUT);

Serial.begin(9600);

}



void loop() {



SUM = SUM - DATABASE[INDEX];

VALUE = analogRead(IN_PIN);

DATABASE[INDEX] = VALUE;

SUM = SUM + VALUE;

INDEX = (INDEX+1) % ARRAY_SIZE;

AVERAGED = SUM / ARRAY_SIZE;



Serial.print(VALUE);

Serial.print(",");

Serial.println(AVERAGED);



delay(25);

}



In the code, first of all we define the size of array and our values are stored in that array. Next, we remove the oldest entry from the array, read the sensor value and add the recent measurement into the array. Then we simply find the average of the values recorded.



Let's look at the waveforms now:









When we increase the array size to 60, we find some very interesting properties of the filter. Note that we sample 60 measurements with a time delay of 25ms in between and so the response time will be very slow.



Observe the startup transient above, the filtered waveform lags and tries to follow the sensor values. The waveform we get is very stable and nice but there is a trade-off with the response time. Another thing I noticed is that the filtered value clips when we increase the the analog value. This happens in the array of large size because the value exceeds the array size.





If we modify the following line or the complete code for simple average averaging implementation using for loop:

INDEX = (INDEX+1) % ARRAY_SIZE; Further, the response time of this filter is not very good. However, it is a very efficient filter for most applications.In the moving average filter, recent measurements are stored and averaged. It will occupy some memory depending on the size of your array. If you have an array of 5 values, then the most recent sensor value is stored in the array and the older value is removed. Let's write the code based on this.In the code, first of all we define the size of array and our values are stored in that array. Next, we remove the oldest entry from the array, read the sensor value and add the recent measurement into the array. Then we simply find the average of the values recorded.Let's look at the waveforms now:Examine the waveforms, the red is the filtered waveform. Look at the lag in the filtered waveform which depends on the array size we choose. If we choose a smaller value like 5, we get a very fast response.But notice that the waveforms still looks somewhat like the raw data with an array size of 5.When we increase the array size to 60, we find some very interesting properties of the filter. Note that we sample 60 measurements with a time delay of 25ms in between and so the response time will be very slow.Observe the startup transient above, the filtered waveform lags and tries to follow the sensor values. The waveform we get is very stable and nice but there is a trade-off with the response time. Another thing I noticed is that the filtered value clips when we increase the the analog value. This happens in the array of large size because the value exceeds the array size.If we modify the following line or the complete code for simple average averaging implementation using for loop:





We get a pretty decent result then.

When we have these large array size, we can also loose much valuable data. The delay value and the array size however mainly depends on your applications. If your values swings and the values are pretty high frequency, you will need a less delay and a proper array size.





I hope you found this post useful and in upcoming posts, we will discuss many other filter algorithms. Implementation and analysis of these algorithms is pretty interesting and we will see more of these.





Thanks for reading!

Most of the times, sensor data is highly unstable and consists of noise. We use sensors in our applications to get a corresponding output of a certain analog value and so filtering the noise and stabilizing the data is necessary before using it in our applications. Well, there are many techniques available to reduce the filter, these techniques are either software based or implementation of a low pass filter at the sensor input. First let's talk about adding a low pass filter.