I have a arduino "averaging filter" that looks like this:
const int numReadings = 35;
int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
const int numReadings_ = 35;
int readings_[numReadings_]; // the readings from the analog input
int index_ = 0; // the index of the current reading
int total_ = 0; // the running total
int average_ = 0;
const int numReadingsA = 100;
int readingsA[numReadingsA]; // the readings from the analog input
int indexA = 0; // the index of the current reading
int totalA = 0; // the running total
int averageA = 0; // the average
const int numReadings_A = 100;
int readings_A[numReadings_A]; // the readings from the analog input
int index_A = 0; // the index of the current reading
int total_A = 0; // the running total
int average_A = 0;
//============================================================================================
int filter_x(int num) {
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = num;
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
return average;
}
int filter_y(int num) {
// subtract the last reading:
total_= total_ - readings_[index_];
// read from the sensor:
readings_[index_] = num;
// add the reading to the total:
total_= total_ + readings_[index_];
// advance to the next position in the array:
index_ = index_ + 1;
// if we're at the end of the array...
if (index_ >= numReadings_)
// ...wrap around to the beginning:
index_ = 0;
// calculate the average:
average_ = total_ / numReadings_;
return average_;
}
int filter_xA(int num) {
// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = num;
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;
// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;
// calculate the average:
average = total / numReadings;
return average;
}
int filter_yA(int num) {
// subtract the last reading:
total_= total_ - readings_[index_];
// read from the sensor:
readings_[index_] = num;
// add the reading to the total:
total_= total_ + readings_[index_];
// advance to the next position in the array:
index_ = index_ + 1;
// if we're at the end of the array...
if (index_ >= numReadings_)
// ...wrap around to the beginning:
index_ = 0;
// calculate the average:
average_ = total_ / numReadings_;
return average_;
}
I really don't understand after studying it for 1 entire day, can anybody kindly explain how this works?
Thanks