EmbeddedRelated.com
The 2024 Embedded Online Conference

Olympic Averaging

Douglas Butler March 29, 20132 comments Coded in C

A method of filtering noisy data that operates on the fly and does not require storing the data to be processed as a whole.  It removes up to two outliers, minimun and maximun, and averages the rest.  Up to sixteen 12 bit samples can be processed using only four 16 bit RAM locations plus the loop index.  With 10 samples the only division is a 3 bit shift.

I created this to add filtering to a product that had almost no extra RAM available, while I was watching Olympic figure skating competitions.

int16 adcval,sum,max,min;
int8 i;
#define samples 10

max=0;
min=0xFFFF;    //Set max and min to the limits of the range.

for (i=0;i<samples;i++){
    adcval=read_adc();
    if (adcval>max) max=adcval;    //Update max and min on the fly.
    if (adcval<min) min=adcval;
    sum+=adcval;    //Generate sum of all samples.
    }
sum-=max;    //Remove the maximum, and minimum values
sum-=min;    //as likely outliers.

return(sum/(samples-2)); //Average the remaining total.
     //If samples = 10 then the division becomes sum<<3

The 2024 Embedded Online Conference