Go Down

Topic: Smoothing Filter (Read 1 time) previous topic - next topic


May 05, 2007, 02:14 pm Last Edit: May 05, 2007, 02:18 pm by paulb Reason: 1
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


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;


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.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131