Example shows random data stream with real-time running median filtering.
Snippet (Full current code on github at https://github.com/davidsmith99/median )
// the function (calls another function 'swap()' not shown here
double median3( double a0, double a1, double a2 ) {
swap(&a1, &a2); // swaps values if arg1 > arg2
swap(&a0, &a2);
swap(&a0, &a1);
return a1; // median value
}
//using in a loop
void loop() {
// generate random data with outliers to observe median filtering
// use 3 most recent values
// drop val_prev2 value and update the latest value
fval_prev2 = fval_prev1;
fval_prev1 = fval_new;
fval_new = 0.99 * (10 + random(-2, 8) + (random(0, 4) == 0) * 4 * (random(0, 5))); // small variation plus occasional spikes
double fmedian = median3(fval_prev2, fval_prev1, fval_new);
Serial.print(fval_new); Serial.print("\t");
Serial.print(fmedian); Serial.print("\n");
delay(200);
}
double median3( double a0, double a1, double a2 ) {
swap(&a1, &a2); // swaps values if arg1 > arg2
swap(&a0, &a2);
swap(&a0, &a1);
return a1; // median value
}
//using in a loop
void loop() {
// generate random data with outliers to observe median filtering
// use 3 most recent values
// drop val_prev2 value and update the latest value
fval_prev2 = fval_prev1;
fval_prev1 = fval_new;
fval_new = 0.99 * (10 + random(-2, 8) + (random(0, 4) == 0) * 4 * (random(0, 5))); // small variation plus occasional spikes
double fmedian = median3(fval_prev2, fval_prev1, fval_new);
Serial.print(fval_new); Serial.print("\t");
Serial.print(fmedian); Serial.print("\n");
delay(200);
}
Other related links:
http://playground.arduino.cc/Main/RunningMedianreference on function overloading c++
http://www.programmingsimplified.com/cpp/source-code/cpp-function-overloading-example-program
No comments:
Post a Comment