"Do not average your Max-Sonar results..ever!" who knew?
December 19, 2013
I've apparently been mistreating my MaxSonar EZ-1. I can't find where i stole.. er... borrowed my idea from for averaging a series of responses to reduce the inconsistencies, but I was looking up some information on the Arduino pulseIN() timeout (Thanks Oddbot!) and came across http://playground.arduino.cc/Main/MaxSonar ... Yeah... I know it should have been one of my first places to look, but...
Then I came across this article in the forum... http://forum.arduino.cc/index.php/topic,20920.0.html
"Filtering in General
For filtering, do not average. Do not average. Do not average. (Did I say, do not average?) We have a lot of internal filtering in our products and if you receive readings that you feel must be filtered, do not use averaging. Instead, throwing out the incorrect range values will produce the best results. Averaging valid and invalid readings together will provide incorrect data.
The filtering that works best, is either a Mode filter or a Median filter. (Did I forget to say, "Do not average the readings"?)
The Median Filter
The median filter would take the last group of readings and first sort them and then pull out the center reading. Here one might take three or more readings (up to say about 11 for people sensing) and after sorting the readings in order of range, pull out and use only the middle (median) reading. Fairly good filtering.
The Mode Filter
The mode filter would take the largest group of the same readings out of a set of even larger readings. Very robust filtering.
Filtering for most applications, the Very Simple Mode Filter
The simplest mode filter, is simple as this question, "Do the last two readings agree?" (with in a certain distance, and the certain distance depends upon how much variation one expects in the actual use. Even so most use the logic of "are these readings exactly the same")? If so, use the readings, otherwise throw away to oldest reading, and compare the next reading with the most current reading and do this again. Obviously with very noisy data, one might have to sample a larger group of readings, and pull out the most common reading, but for most applications, this simple two reading filter works very well.
Even so, for specific cases, one has to try and find out what works best.
Please let us know if you have any further questions. I request that you email me any questions at firstname.lastname@example.org.
http://howdweknows.blogspot.ca/2011/03/maxsonar-rangefinder.html <--- This one does averaging...
http://www.danielandrade.net/2009/07/26/ultrasonic-range-finder-with-arduino/ <---- Also uses averaging...