Earlier we discussed about moving average filter and exponential filter for filtering sensor data, but when there is a sudden impulse noise, the average value will also rise with it and we can’t get a smooth value in response. To do this, we must implement a median filter. A median filter is a non-linear method used to suppress out-of-range noises from the data. Here, the software filter computes the median value from the array values and by this; we can ignore all impulse noises occurring in our data.





For implementing a median filter we have to first collect data of certain quantity which can be the array size. Generally, I prefer the array size of 15. The median filter will compute the median value from these values. But for finding media, we actually have to arrange this data in ascending or descending order. For array size of 5 we have to compare less than 10 values and as the array size increases, we have to compare more and more data values which leads to more and more lines of code.





For populating the array with sensor values, we can use for loop as follows:





for(int i=0; i<(array size); i++0

values[i] = sensor_values;





Then we must add this data in cycles to a cyclic buffer which will help us further sorting the data. There are various ways to implement a cyclic buffer and one of the materials you can refer is: https://medium.com/@charlesdobson/how-to-implement-a-simple-circular-buffer-in-c-34b7e945d30e

For comparison of data values, we can define the function as arrange and write the program as





#define arrange(a,b)

If (a>b){

swap(a,b);}





You have to define swap function before this:





# define swap(a,b)

a^=b;

b^=a;

a^=b;





Then we can perform this operation for b and c, a and c, and so on. And we can get our median value by returning that value. For example for four sensor values a, b, c, d, e, we can arrange the data and get the c value which is the median value in this case.





Besides getting into complexity of programming, you can implement a median filter with the help of NewPing library. It’s a very popular library among makers and goes with ultrasonic sensors like it’s companion. NewPing library works with variety of ultrasonic modules and if your project is based on ultrasonic modules, you are in luck! This library is created by Tom Eckel and you download it from here: https://playground.arduino.cc/Code/NewPing/





For installing, go to Sketch>Include library>Add .zip library and browse for the file location.





In this library, you can use ping_median() function which will implement median filter and greatly improve the the accuracy of ultrasonic sensor readings. If you implement this function, by default the array size will be 5 and if you want to adjust the array size you can do this by following lines:





int iterations = (array size)





Then for implementing filter write





duration = sonar.ping_median(iterations);





This is it for today, I hope you liked this article and found it useful. In next post, I will share some results I got by the implementation of median filter.





Thanks for reading!



