Programming for Robotics with Arduino C++ 2, Challenge 2 solution (how much averaging can the loop afford?) Idea: simulate a converter that rounds a true value of 409.6 counts plus Gaussian noise to a whole count, average N readings, repeat 2000 times, and look at the scatter of the averages in millivolts (one count is 4.888 mV). For each noise level take the smallest power-of-two N whose scatter is under 1 mV, and convert N into time at 104 microseconds a reading. The bias column (the mean of the averages minus the truth) is printed too, because a small scatter says nothing about whether the answer is right. CODE: // x2.cpp -- challenge 2: how many ADC readings must be averaged to bring the spread under 1 mV, for three noise levels, and what does it cost in time? (host build; the noise is assumed) #include #include #include #include int main() { const double conv_us = 13.0 / (16000000.0 / 128.0) * 1.0e6; // 104 us per conversion const double true_counts = 409.6; const double mv_per_count = 5000.0 / 1023.0; std::mt19937 rng(5); std::printf("smallest N (a power of two up to 1024) whose averaged reading has a spread under 1 mV (2000 trials each):\n"); std::printf(" noise sigma counts N spread mV bias mV time ms share of a 20 ms period\n"); for (double sigma : {0.3, 1.5, 4.0}) { std::normal_distribution noise(0.0, sigma); bool found = false; for (int N = 1; N <= 1024; N *= 2) { double sum = 0, sum2 = 0; const int trials = 2000; for (int t = 0; t < trials; ++t) { long total = 0; for (int i = 0; i < N; ++i) { long c = std::lround(true_counts + noise(rng)); total += c < 0 ? 0 : (c > 1023 ? 1023 : c); } double avg = (double)total / N; sum += avg; sum2 += avg * avg; } double mean = sum / trials, sd = std::sqrt(sum2 / trials - mean * mean) * mv_per_count; if (sd < 1.0) { std::printf(" %18.1f %-4d %9.2f %+7.2f %7.2f %6.1f %%\n", sigma, N, sd, (mean - true_counts) * mv_per_count, N * conv_us / 1000.0, N * conv_us / 200.0); found = true; break; } } if (!found) std::printf(" %18.1f none up to 1024\n", sigma); } return 0; } END CODE OUTPUT: smallest N (a power of two up to 1024) whose averaged reading has a spread under 1 mV (2000 trials each): noise sigma counts N spread mV bias mV time ms share of a 20 ms period 0.3 8 0.86 +0.16 0.83 4.2 % 1.5 64 0.94 -0.02 6.66 33.3 % 4.0 512 0.87 +0.03 53.25 266.2 % END OUTPUT Reading the output. At 0.3 counts of noise 8 readings suffice (0.86 mV, 0.83 ms, 4.2 % of a 20 ms period); at 1.5 counts it takes 64 (0.94 mV, 6.66 ms, 33.3 %); at 4.0 counts it takes 512 (0.87 mV) and 53.25 ms, which is 266 % of the period: the averaging cannot be done inside a 20 ms loop at all, because the readings alone take longer than the loop. For that input the design has to change, not the loop: reduce the noise at its source (a capacitor across the input, shorter wires, a steadier supply), accept a larger spread, or spread the averaging over several periods with a running filter, at the cost of lag (the filters arrive in Programming for Robotics with Arduino C++ 4). The bias column shows a second effect: with the smallest noise the average is 0.16 mV off the truth, the largest bias of the three, because noise of 0.3 counts does not spread the readings across enough counts to cancel the rounding; at 1.5 and 4.0 counts the bias is within 0.03 mV. So too little noise is a problem too. Check on method: Gaussian noise of a chosen size is an assumption, not a measurement of any input, and real noise has other shapes (a hum at the mains frequency does not average away the same way). The reference voltage is assumed to be steady at 5 V.