Pages: [1]   Go Down
Author Topic: Smoothing Filter  (Read 1146 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 267
dinosaur cork
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's a really simple smoothing filter that I would like to see in core. It's implemented here with a float  and with fixed point math. We use this all the time but some of my students have a hard time remembering the math. Useful for sensor jitter or many operations where one wants to slow down a response to a stimulus.   1 == filter off, small fraction == max filter


Paul


int smooth(int data, float filterVal){
  static float accum;

  if (filterVal > 1){      // defensive programming - check to make param's are within range
    filterVal = 1;
  }
  else if (filterVal <= 0){
    filterVal = .0001;
  }

  accum = (data * filterVal) + (accum * (1 - filterVal));

  return (int)accum;
}


// fixed point version


int smooth(int data, long filterVal){
  static long accum;

  if (filterVal > 1000){      // defensive programming - check to make param's are within range
    filterVal = 1000;
  }
  else if (filterVal <= 0){
    filterVal = 1;
  }

  accum = ((data * filterVal) / 1000) + ((accum * (1000 - filterVal) / 1000));

  return accum;
}





« Last Edit: May 05, 2007, 07:18:53 am by paulb » Logged

Forum Administrator
Cambridge, MA
Offline Offline
Faraday Member
*****
Karma: 12
Posts: 3538
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It's definitely a useful operation.  There are a few issues that would need to be addressed if it were to go in the core.  First, because it's using a static variable as the accumulator, it only works on a single data source.  Second, we haven't put any floats in the core because of the amount of program space they use, although starting with Arduino 0008 that shouldn't be a problem because you'll only get the increased size if you actually use the functions that deal with floats.  The fixed point version of the function arbitrarily picks a multiplier, something we'd probably want to standardize across many functions before including.  Finally, I'm not sure it's that fundamental.  But feel free to try to convince me.  Also, you should post this to the developers list too.
Logged

Pages: [1]   Go Up
Jump to: