Hi,
I noticed a number of people talking about averages, and in particular, averaging to overcome spurious signals. I thought it might be worth discussing some theory and providing some code examples to help in this area.
If you have a number of samples and you want to take an average, it is worth thinking about what you are trying to achieve by taking an average. In the case that some of these samples are spurious, taking the mean average will certainly dilute the impact of these values, but won't remove them. More appropriate would be the median average, which will naturally ignore outliers.
For anyone that needs a reminder of the differences:
Arithmetic Mean
The sum of all the samples, divided by the number of samples
Median
The middle sample when the samples are placed in order
So, lets get to some code examples:
To calculate the mean, we can simply add up all the values in an array of samples, and return the average by dividing by the number of samples:
float mean(float *samples, int n) {
float sum = 0.0;
for (int i=0; i<n; i++) {
sum += samples[i];
}
return sum / (float)n;
}
For the median implementation I've used the C qsort function to sort the list in to order, then simply return the middle sample. The compare function is needed to tell qsort how to sort values:
int compare(const void *a, const void *b) {
return (int)(*(float*)a - *(float*)b);
}
float median(float *samples, int n) {
qsort(samples, n, sizeof(float), compare);
return samples[(n - 1)/2];
}
Note that this only deals with an odd number of samples to keep the example simple. The code in the program example below includes logic to handle even numbers of samples too, and a simple test program to sample an AnalogIn and print out the averages.
Hope this is useful!
Hi,
I noticed a number of people talking about averages, and in particular, averaging to overcome spurious signals. I thought it might be worth discussing some theory and providing some code examples to help in this area.
If you have a number of samples and you want to take an average, it is worth thinking about what you are trying to achieve by taking an average. In the case that some of these samples are spurious, taking the mean average will certainly dilute the impact of these values, but won't remove them. More appropriate would be the median average, which will naturally ignore outliers.
For anyone that needs a reminder of the differences:
Arithmetic Mean
The sum of all the samples, divided by the number of samples
Median
The middle sample when the samples are placed in order
So, lets get to some code examples:
To calculate the mean, we can simply add up all the values in an array of samples, and return the average by dividing by the number of samples:
For the median implementation I've used the C qsort function to sort the list in to order, then simply return the middle sample. The compare function is needed to tell qsort how to sort values:
Note that this only deals with an odd number of samples to keep the example simple. The code in the program example below includes logic to handle even numbers of samples too, and a simple test program to sample an AnalogIn and print out the averages.
Hope this is useful!