Programming for Robotics with Arduino C++ 4, Challenge 2 solution (how many guard bits?) Idea: EmaG stores the output times 2^G and adds the difference divided by 2^K (a division, so that negative differences behave the same as positive ones). G = 0 is the plain shift, G = K is the guard-bit filter of the chapter's text. For each pair it holds a constant input for a long time (a step from a settled state) and prints where the output ends, then compares the output with the float filter on a noisy ramp. CODE: // x2.cpp -- Challenge 2: how many guard bits does an integer exponential filter need? (host build) // EmaG is y += (x - y) / 2^K with y stored times 2^G. G = 0 is the plain shift of chapter section 4; G = K is the EmaIntGuard of the text. // Tests: three steps up from 0 (to 1000, 37 and 5), a step DOWN from 1000 to 0, and rms difference from the float filter y += (x - y) / 2^K on a noisy ramp. #include #include #include #include "filters.h" #include "sim.h" template class EmaG { public: EmaG() : acc_(0), init_(false) {} int16_t update(int16_t x) { if (!init_) { acc_ = (int32_t)x * (1L << G); init_ = true; } else acc_ += ((int32_t)x * (1L << G) - acc_) / (1L << K); // C++ division rounds toward zero return (int16_t)((acc_ + (1L << G) / 2) / (1L << G)); // round the output to the nearest integer } private: int32_t acc_; bool init_; }; template static int final_value(int from, int to) { F f; for (int i = 0; i < 300; ++i) f.update((int16_t)from); int16_t y = 0; for (int i = 0; i < 2000; ++i) y = f.update((int16_t)to); return y; } template static double rms_vs_float(int K) { // noisy ramp 2000 -> 300, 5 mm noise Rng rng(5); F f; EmaFloat ref(1.0f / (float)(1 << K)); double sq = 0; long n = 0; for (int i = 0; i < 425; ++i) { double truth = 2000.0 - 200.0 * (i * 0.02); int16_t x = (int16_t)std::lround(truth + 5.0 * rng.normal()); double a = f.update(x), b = ref.update((float)x); if (i >= 20) { sq += (a - b) * (a - b); ++n; } } return std::sqrt(sq / n); } template static void row() { std::printf(" %3d %3d %9d %9d %9d %9d %22.3f\n", K, G, final_value >(0, 1000), final_value >(0, 37), final_value >(0, 5), final_value >(1000, 0), rms_vs_float >(K)); } int main() { std::printf("final output after a long wait, for a = 1/2^K and G guard bits (the true answer is the new input: 1000, 37, 5 and 0)\n"); std::printf(" K G 0 -> 1000 0 -> 37 0 -> 5 1000 -> 0 rms vs float, noisy ramp (mm)\n"); row<3, 0>(); row<3, 1>(); row<3, 2>(); row<3, 3>(); row<3, 4>(); row<5, 0>(); row<5, 2>(); row<5, 4>(); row<5, 5>(); row<5, 6>(); return 0; } END CODE OUTPUT: final output after a long wait, for a = 1/2^K and G guard bits (the true answer is the new input: 1000, 37, 5 and 0) K G 0 -> 1000 0 -> 37 0 -> 5 1000 -> 0 rms vs float, noisy ramp (mm) 3 0 993 30 0 7 3.623 3 1 997 34 2 4 2.106 3 2 998 35 3 2 1.026 3 3 999 36 4 1 0.569 3 4 1000 37 5 0 0.382 5 0 969 6 0 31 10.351 5 2 992 29 0 8 3.882 5 4 998 35 3 2 1.030 5 5 999 36 4 1 0.517 5 6 1000 37 5 0 0.357 END OUTPUT Reading the output. The stall is a quantity you can predict: the accumulator stops when the difference is under 2^K units, so the output ends (2^K - 1) / 2^G output counts short of the input. For K = 3 that is 7, 3.5, 1.75, 0.875 and 0.4375 counts for G = 0 to 4, and the measured ends are 993, 997, 998, 999 and 1000 for the step to 1000 (7, 3, 2, 1 and 0 short, after rounding the output to the nearest integer), and the stall is symmetric: stepping down from 1000 to 0 ends at 7, 4, 2, 1 and 0. The output is exact once the remaining stall is under half a count, that is (2^K - 1) / 2^G < 1/2, so the smallest G is K + 1: 4 for K = 3 and 6 for K = 5 (K = 5 with G = 5 still ends at 999, 36, 4 and 1, and the step to 5 shows it as 4). The rms difference from the float filter falls as G rises (3.623, 2.106, 1.026, 0.569 and 0.382 for K = 3), and levels off near the quantisation of rounding the output to whole millimetres. Note the difference from the chapter's EmaIntGuard (G = K), which ends at 1000, 37 and 5 going up but at 1 going down. That class adds x - acc / 2^K, which rounds the increment up when rising, so it is exact going up and leaves a residue of one count going down; the EmaG here rounds toward zero both ways and is symmetric. Rounding direction is a design choice, and it moves the error to one side or the other. Check on method: the inputs are constants, which is the worst case for a stall; on a signal that is never constant the stall shows up as the mean bias of section 5 instead. The cost of G is RAM and range: the accumulator is an int32_t here, and with G = 6 an input of 16-bit size would still fit in it (32767 x 64 = 2.1 million, well under the 2.1 billion limit), but on an 8-bit chip each extra byte of accumulator is more instructions in every update, which was not measured.