Robotics Fundamentals 1, Chapter 5: Challenge 2 solution (filtering and averaging) Formulas: mean = sum / N moving average of N: mean of the last N median of N: the middle value of the last N sorted exponential filter: y(k) = y(k-1) + alpha ( x(k) - y(k-1) ) sigma(mean) = sigma / sqrt(N) EMA step response after n samples: 1 - (1 - alpha)^n time constant about dt (1 - alpha) / alpha moving average of N lags (N - 1) / 2 samples Readings (cm), true value 150.0: 150.2, 149.6, 150.5, 149.9, 168.0, 150.1, 149.7, 150.4, 150.0, 149.8 (a) Sum = 1518.2, so the mean = 151.82 cm (the outlier pulls it 1.8 cm off). Sorted: 149.6, 149.7, 149.8, 149.9, 150.0, 150.1, 150.2, 150.4, 150.5, 168.0. The middle two are 150.0 and 150.1, so the median = 150.05 cm. Mean without the outlier: (1518.2 - 168.0) / 9 = 1350.2 / 9 = 150.02 cm. (b) k raw MA3 Med3 3 150.5 150.10 150.2 (mean of 150.2, 149.6, 150.5; median of the same three) 4 149.9 150.00 149.9 5 168.0 156.13 150.5 <-- the outlier enters the window 6 150.1 156.00 150.1 7 149.7 155.93 150.1 8 150.4 150.07 150.1 <-- the outlier has left the window 9 150.0 150.03 150.0 10 149.8 150.07 150.0 MA3 is wrong for three readings (k = 5, 6, 7), by up to +6.13 cm (156.13 against about 150.0): the outlier is averaged in until it leaves the window. Med3 is never more than 0.5 cm off, no more than the ordinary reading-to-reading noise: a single wild value is never the middle one of three. (c) EMA, alpha = 0.4, y(1) = 150.2: y(2) = 150.2 + 0.4 (149.6 - 150.2) = 149.96 y(3) = 149.96 + 0.4 (150.5 - 149.96) = 150.18 y(4) = 150.18 + 0.4 (149.9 - 150.18) = 150.07 (150.066) y(5) = 150.066 + 0.4 (168.0 - 150.066) = 157.24 y(6) = 157.24 + 0.4 (150.1 - 157.24) = 154.38 y(7) = 154.38 + 0.4 (149.7 - 154.38) = 152.51 y(8) = 151.67 y(9) = 151.00 y(10) = 150.52 Errors from the outlier onward (true 150.0): k = 5 to 10: +7.24, +4.38, +2.51, +1.67, +1.00, +0.52 cm. Each step keeps 60 % of the previous error (1 - alpha), so it fades but does not vanish at once. (d) sigma(mean) = 0.4 / sqrt(N) is at most 0.1 when sqrt(N) is at least 4, so N = 16 independent readings. At 25 readings a second, 16 readings take 16 / 25 = 0.64 s. A robot at 0.3 m/s moves 0.3 x 0.64 = 0.192 m = 19.2 cm in that time. For 0.05 cm: sqrt(N) = 0.4 / 0.05 = 8, N = 64, taking 64 / 25 = 2.56 s (0.77 m of travel at 0.3 m/s). Halving the noise costs four times as many readings. (e) Time constant about dt (1 - alpha) / alpha = 0.04 x 0.6 / 0.4 = 0.06 s. Fraction of a step covered after n samples = 1 - 0.6^n: n = 1: 0.400 2: 0.640 3: 0.784 4: 0.870 5: 0.922. It reaches 63 % after 1 / (-ln 0.6) = 1.96 samples = 0.078 s (the 0.06 s is the rule-of-thumb value, close for alpha this large). (f) A moving average of 3 lags (3 - 1) / 2 = 1 sample = 0.04 s. At 0.3 m/s that is 1.2 cm of travel: small, which is why a short average or median is fine even in a control loop, but N = 25 would lag 12 samples = 0.48 s, 14 cm of travel, which a fast robot cannot afford.