Quote Originally Posted by aratti View Post
I came to the conclusion that a better method is the MEDIAN and not the MEAN since the average
incorporate into the final result (the mean) also any anomalous reading due to spikes or other
electrical noisy phenomenon could occur during the sampling.

Here the contradiction, we make the average to get rid of anomalous reading but we just incorporate
them, in other words, the mean value is a way to dilute anomalous reading while, the MEDIAN is a mean to
get rid of them.
I have not looked at your implementation, but I've been mulling over your logic, and I don't think I can agree.

First, it sounds like you are really interested in the mode, rather than the median. i.e. take a number of readings and report the one that comes up most often.

But I guess I have a problem with the fundamental assertion that using average is somehow flawed. The value fluctuates because of quantization distortion - basically the real value falls between the ADC steps. You can actually get additional resolution by "oversampling" and averaging the results. There's lots of detail on the issue (or opportunity depending on you point of view), and an excellent solution here http://www.darreltaylor.com/DT_Analog/ It's discussed at length on the MicroChip site too.

The bottom line, though, is that the fluctuation is not noise - it's additional information that you should WANT to incorporate, and hence the almost universal use of average.

Great discussion topic - too often we do things without thinking about why we do them.