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;
}