averaging filter program


i have arduino "averaging filter" looks this:

code: [select]

const int numreadings = 35;

int readings[numreadings];      // readings analog input
int index = 0;                  // index of current reading
int total = 0;                  // running total
int average = 0;                // average

const int numreadings_ = 35;

int readings_[numreadings_];      // readings analog input
int index_ = 0;                  // index of current reading
int total_ = 0;                  // running total
int average_ = 0;  

const int numreadingsa = 100;

int readingsa[numreadingsa];      // readings analog input
int indexa = 0;                  // index of current reading
int totala = 0;                  // running total
int averagea = 0;                // average

const int numreadings_a = 100;

int readings_a[numreadings_a];      // readings analog input
int index_a = 0;                  // index of current reading
int total_a = 0;                  // running total
int average_a = 0;

//============================================================================================



int filter_x(int num) {
 // subtract last reading:
 total= total - readings[index];        
 // read sensor:  
 readings[index] = num;
 // add reading total:
 total= total + readings[index];      
 // advance next position in array:  
 index = index + 1;                    

 // if we're @ end of array...
 if (index >= numreadings)              
   // ...wrap around beginning:
   index = 0;                          

 // calculate average:
 average = total / numreadings;

 return average;
}

int filter_y(int num) {
 // subtract last reading:
 total_= total_ - readings_[index_];        
 // read sensor:  
 readings_[index_] = num;
 // add reading total:
 total_= total_ + readings_[index_];      
 // advance next position in array:  
 index_ = index_ + 1;                    

 // if we're @ end of array...
 if (index_ >= numreadings_)              
   // ...wrap around beginning:
   index_ = 0;                          

 // calculate average:
 average_ = total_ / numreadings_;

 return average_;
}

int filter_xa(int num) {
 // subtract last reading:
 total= total - readings[index];        
 // read sensor:  
 readings[index] = num;
 // add reading total:
 total= total + readings[index];      
 // advance next position in array:  
 index = index + 1;                    

 // if we're @ end of array...
 if (index >= numreadings)              
   // ...wrap around beginning:
   index = 0;                          

 // calculate average:
 average = total / numreadings;

 return average;
}

int filter_ya(int num) {
 // subtract last reading:
 total_= total_ - readings_[index_];        
 // read sensor:  
 readings_[index_] = num;
 // add reading total:
 total_= total_ + readings_[index_];      
 // advance next position in array:  
 index_ = index_ + 1;                    

 // if we're @ end of array...
 if (index_ >= numreadings_)              
   // ...wrap around beginning:
   index_ = 0;                          

 // calculate average:
 average_ = total_ / numreadings_;

 return average_;
}



i don't understand after studying 1 entire day, can kindly explain how works?

thanks

it works average (arithmetic mean);
1) add samples, ,
2) divide number of samples.
however, because new samples coming in each cycle, keeps circular buffer of samples, puts in recent , removes oldest.
you need add in new value , subtract oldest value running total, have store samples know oldest.

the example isn't (imho) written - virtually identical code repeated 4 times - addition of simple structure reduce amount of code third of what's there, , make easier maintain , debug.


Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > averaging filter program


arduino

Comments

Popular posts from this blog

CAN'T INSTALL MAMBELFISH 1.5 FROM DIRECTORY - Joomla! Forum - community, help and support

error: expected initializer before 'void'

CPU load monitoring using GPIO and leds - Raspberry Pi Forums