Quote:
Originally Posted by Andrew Blair
As well, if anyone can quickly through up a workable averaging function I'd be much obliged.
|
Team 95 has a huge number of IR sensors (BigHAIR: Big Honkin Array of IR sensors), and we keep time history and a running average on them. Here's a snippet
Code:
long IR_range[6]; // current ranges, generated by another function
long IR_range_history[6][4]; // Last 4 readings, not necessarily in order (see below)
long IR_range_average[6]; // running average of last 4 readings
void Average_IR(void) {
static counter=0;
index=counter++%4; // Keeps from having to copy old data, just
// overwrite one column at a time
for (i=0;i<6;i++) {
IR_range_history[i][index]=IR_range[i];
IR_range_average[i]=0;
for (j=0;j<4;j++) {
IR_range_average[i]+=IR_range_history[i][j];
}
IR_range_average[i]/=4;
}
}