Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15514

Int vs float discrepancies in running average filter

$
0
0

Good afternoon.

I have this sub-routine which is working fine with int data types but returns large negative numbers when int is changed to float.

data in:
COGPos; ALToutput; ROLLoutput; PITCHoutput; YAWoutput; is a float

//Running Average filter for output smoothing--------------------------------------------------------------
#define iterations 20
#define vars 6

int Buffer[iterations][vars] = { 0 };              //change to float
int var1, var2, var3, var4, var5, var6 = 0;   //change to float

void AVG() {
  //output running average filter block
  int i, j = 0;
  int accum[vars] = { 0 };                                 //change to float

  var1 = COGPos;
  var2 = ALToutput;  
  var3 = ROLLoutput;  
  var4 = PITCHoutput;
  var5 = YAWoutput;    
  var6 = 0;               //6th variable not yet assigned

    for (i = 1; i < iterations; i++) {
    for (j = 0; j < vars; j++) {
      accum[j] += Buffer[i][j];         //add each buffer line to accumulator
      Buffer[i - 1][j] = Buffer[i][j];   //shift buffer data 1 address up
    }
  }

  Buffer[iterations - 1][0] = var1;  //add new data at end of buffer
  Buffer[iterations - 1][1] = var2;
  Buffer[iterations - 1][2] = var3;
  Buffer[iterations - 1][3] = var4;
  Buffer[iterations - 1][4] = var5;
  Buffer[iterations - 1][5] = var6;

  for (j = 0; j < vars; j++) {
    accum[j] += Buffer[iterations - 1][j];  //add new data to accumulator
    accum[j] = accum[j] / iterations;       //divide accumulator by number of data points
  }
  
  //passing parameter out of filter block
  COGPos = accum[0];
  ALToutput = accum[1];
  ROLLoutput = accum[2];
  PITCHoutput = accum[3]);
  YAWoutput = accum[4];
}

Any ideas on changes required to approach or computation?

7 posts - 4 participants

Read full topic


Viewing all articles
Browse latest Browse all 15514

Trending Articles