Programming for Robotics with Arduino C++ 4, Challenge 1 solution (how big a median?) Idea: the median template's window is a compile-time size, so a small function template runs one cell (window N, share of wrong echoes p) over 100 seeds of the falling-range signal of the chapter and returns the rms and worst error; main() calls it for windows 3, 5, 9 and 15. A second function adds up the binomial chance that more than half of N independent readings are wrong, which is the only case in which a median can be fooled. CODE: // x1.cpp -- Challenge 1: how big a median window does a given rate of wrong readings need? (host build, seeded; the signal is a MODEL) // Same signal as e_filters.cpp: the range falls from 2000 mm to 300 mm at 200 mm/s, one reading per 20 ms, 5 mm of Gaussian noise, 5 % no echo (held). // New: the rate of WRONG echoes (random values 0..4000 mm) is a parameter, and so is the median window. 100 seeds per cell. #include #include #include #include "filters.h" #include "sim.h" template static void cell(double p_wrong, double& rms, double& worst) { double sq = 0; long n = 0; worst = 0; for (uint32_t seed = 1; seed <= 100; ++seed) { Rng rng(seed); Median f; int16_t out = 2000; for (int i = 0; i < 425; ++i) { double truth = 2000.0 - 200.0 * (i * 0.02); double u = rng.uniform(); int16_t x; if (u < 0.05) x = -1; else if (u < 0.05 + p_wrong) x = (int16_t)(rng.uniform() * 4000.0); else x = (int16_t)std::lround(truth + 5.0 * rng.normal()); if (x >= 0) out = f.update(x); if (i >= 20) { double e = out - truth; sq += e * e; if (std::fabs(e) > worst) worst = std::fabs(e); ++n; } } } rms = std::sqrt(sq / n); } // the chance that more than half of a window of N readings are wrong, when each is wrong with probability p: the median can fail only then static double p_majority(int N, double p) { double total = 0; for (int k = N / 2 + 1; k <= N; ++k) { double c = 1; for (int j = 0; j < k; ++j) c = c * (N - j) / (j + 1); total += c * std::pow(p, k) * std::pow(1 - p, N - k); } return total; } int main() { std::printf("rms error in mm (worst error in mm) of a median filter on the falling range; rows: share of readings that are wrong echoes, columns: window\n"); std::printf(" wrong echoes window 3 window 5 window 9 window 15\n"); for (double p : {0.0, 0.03, 0.10, 0.20, 0.30, 0.40}) { double r3, w3, r5, w5, r9, w9, r15, w15; cell<3>(p, r3, w3); cell<5>(p, r5, w5); cell<9>(p, r9, w9); cell<15>(p, r15, w15); std::printf(" %9.0f %% %6.1f (%6.0f) %6.1f (%6.0f) %6.1f (%6.0f) %6.1f (%6.0f)\n", p * 100, r3, w3, r5, w5, r9, w9, r15, w15); } std::printf("\nchance that more than half of a window is wrong (the median can only be fooled then):\n"); std::printf(" wrong echoes window 3 window 5 window 9 window 15\n"); for (double p : {0.03, 0.10, 0.20, 0.30, 0.40}) std::printf(" %9.0f %% %14.5f %14.5f %14.5f %14.5f\n", p * 100, p_majority(3, p), p_majority(5, p), p_majority(9, p), p_majority(15, p)); return 0; } END CODE OUTPUT: rms error in mm (worst error in mm) of a median filter on the falling range; rows: share of readings that are wrong echoes, columns: window wrong echoes window 3 window 5 window 9 window 15 0 % 5.9 ( 24) 9.4 ( 30) 17.5 ( 40) 30.0 ( 56) 3 % 48.1 ( 3277) 9.8 ( 294) 17.9 ( 48) 30.5 ( 64) 10 % 164.2 ( 3317) 63.2 ( 3325) 23.4 ( 1213) 31.9 ( 67) 20 % 317.7 ( 3526) 172.2 ( 3094) 65.9 ( 2333) 37.5 ( 817) 30 % 484.6 ( 3582) 309.8 ( 3282) 152.7 ( 2683) 82.5 ( 2343) 40 % 634.0 ( 3420) 458.5 ( 3333) 286.6 ( 2946) 178.8 ( 2408) chance that more than half of a window is wrong (the median can only be fooled then): wrong echoes window 3 window 5 window 9 window 15 3 % 0.00265 0.00026 0.00000 0.00000 10 % 0.02800 0.00856 0.00089 0.00003 20 % 0.10400 0.05792 0.01958 0.00424 30 % 0.21600 0.16308 0.09881 0.05001 40 % 0.35200 0.31744 0.26657 0.21310 END OUTPUT Reading the output. With no wrong echoes the rms grows with the window (5.9, 9.4, 17.5 and 30.0 mm) and that is pure lag: the range falls 4 mm per reading, and a median of N is about (N - 1) / 2 readings behind, which is 4, 8, 16 and 28 mm. So the cleanest signal prefers the smallest window. At 3 % wrong echoes a window of 3 is already fooled (rms 48.1 mm, worst 3277) because the chance that 2 of 3 are wrong is 0.00265, about once in 380 readings; a window of 5 (chance 0.00026) is the right choice there, at 9.8 mm. At 20 % the windows of 3, 5 and 9 give 317.7, 172.2 and 65.9 mm, and 15 gives 37.5 mm, so a window of 15 is the one to choose, though its chance of being fooled is still 0.00424 (worst error 817 mm), and its lag at 200 mm/s is 28 mm. At 40 % wrong echoes nothing works (best 178.8 mm): when most of the readings are wrong the sensor needs fixing, not filtering. Check on method: the binomial chance assumes each reading is wrong independently and the window is full. The first readings of each run fill a partly empty window, and real wrong echoes come in bursts (a person walking across the beam gives several in a row), which a binomial understates. These numbers say which window sizes are worth trying, not which one a real sensor needs; the shape would hold for a real sensor, the figures would not. This challenge used 100 seeds per cell, against 200 in the chapter's table, so its 3 % row is not the same run as the chapter's.