Smoothing an angle versus delay: 3-point moving average
Practise a 3-point causal moving average, separating the times within the window, the time the result is ready for use, group delay and step response, with a worksheet, answers, images and diagrams
Start the three-point filtering example · Back to the angle-over-time chapter
Read on in Accumulating angle from the gyro with real time to practise unequal dt and the effect of accumulating offset
Objectives and the basics used
Practise reading the result of filtering before using an angle in an MCU: calculate a three-point average, check samples that are not yet complete, and state the reference time of the result. Use a PC calculator or paper; no board is needed. Read Continuous angle and rotation rate from logs before starting.
The realistic image at the start of the chapter was created with AI. The three-slot marbles, ribbon and clock are used as conceptual aids, not as filtering equipment or measured results. All numbers are hypothetical, for one axis, and are already continuous angles; there is no true angle with which to assess accuracy, and no IMU experiment or firmware run.
Define the window and the start-up period clearly
This chapter uses a causal moving average: y[n] = (x[n] + x[n−1] + x[n−2]) / 3 , with each point weighted 1/3, using only the current and past points and no future samples. Samples are defined as equally spaced at 10 ms within a single clock/session, and the result is calculated only when all three points are available.
The first two points therefore do not yet have a full-window average ; this is not a filter result of zero. Zero-padding, repeating the first value, or averaging only the points that exist are different start-up rules; the rule must be stated and the result recalculated. Do not insert values that do not exist just to make the graph continuous.
The DSP Guide by Steven W. Smith explains averaging over several samples and the relative shift that occurs when only one side of points is used. This chapter defines the three-point backward window and the example numbers itself, and does not treat formulas that must wait for the future as online results.
Calculate six points and read the time of the result
Given: t is the sample time in ms, x/y are in degrees; there are no gaps/resets; the value of y can be calculated at the earliest when the latest point arrives, not including processing or data transmission time.
| Sample (ms) | x (°) | y, full 3 points (°) |
|---|---|---|
| 0 | 0 | Not yet complete |
| 10 | 1.2 | Not yet complete |
| 20 | 1.8 | 1 |
| 30 | 3.2 | 31/15 ≈ 2.066667 |
| 40 | 3.8 | 44/15 ≈ 2.933333 |
| 50 | 5.2 | 61/15 ≈ 4.066667 |
At 20 ms: (0 + 1.2 + 1.8) / 3 = 1° ; the window is 0, 10, 20 ms, with its midpoint at 10 ms , but this result can be calculated only once the sample at 20 ms has arrived, so it must not be written that the MCU has the 1° result ready from 10 ms. Shifting the graph to the window midpoint can be done for retrospective comparison, and must be clearly time-labelled.
Find the average rate of the differences using dt = 0.01 s from the actual pairs of points, without rounding y before subtracting:
| Interval (ms) | dt (ms) | x rate (deg/s) | y rate (deg/s) |
|---|---|---|---|
| 0→10 | 10 | 120 | Not yet complete |
| 10→20 | 10 | 60 | Not yet complete |
| 20→30 | 10 | 140 | 320/3 ≈ 106.667 |
| 30→40 | 10 | 60 | 260/3 ≈ 86.667 |
| 40→50 | 10 | 140 | 340/3 ≈ 113.333 |
The first filtered result at 20 ms has no earlier full-window result, so there is no y rate for the 10→20 ms interval. The y rate is the difference of two window averages , not an instantaneous gyro reading or a proven real rotation. Numbers that appear to oscillate less in this set do not prove that they are closer to the true angle.
Matplotlib from hypothetical numbers; the y points are placed at the time of the latest point in the window; the connecting lines show the order and are not a waveform measured between samples. Open the full-size angle filtering and delay chart
Group delay is not the time to wait until the final value
For an equal-weight FIR filter with N uniformly sampled points, the group delay in the range where it is defined is (N−1)/2 samples; for N = 3 it is therefore 1 sample = 10 ms . Read SciPy's definition of group delay , which separates the delay of frequency components and states that the value at a frequency exactly at a zero of the transfer function may be undefined. This chapter did not run SciPy.
Expanding the derivation for this case: H(ω) = (1 + exp(−jω) + exp(−j2ω))/3 = exp(−jω)(1+2cosω)/3 , where ω is in radians/sample. In the range where the real factor is non-zero and does not change sign, the phase has a slope of −1, which gives a group delay of 1 sample. At the zeros of H, this slope must not be claimed across the points where the phase jumps.
Separate step example: x = 0, 0, 0, 3, 3, 3° at t = 0, 10, 20, 30, 40, 50 ms. When it changes to 3° at 30 ms, the full-window results are y at 20, 30, 40, 50 ms = 0, 1, 2, 3° , reaching the final value at 50 ms, separated from the change point by 20 ms . This case therefore separates group delay 10 ms, the replacement of all samples in the window 20 ms , and the actual processing/communication time that the problem does not give. One number is not used as a substitute for another.
Limitations before applying to drone data
Do not average the displayed angles 179° and −179° directly and then claim 0° as a heading close to the original; a method and assumptions suitable for angles must be chosen. Read angle wrap-around and Unwrap before filtering continuous angles. A wrongly chosen number of turns is not corrected by averaging.
If samples are not equally spaced, an average of three numbers can still be calculated, but it is not this equal-interval FIR model and a fixed 10 ms delay should not be declared. When a gap/reset occurs, state which data the window contains and at what times, use a declared restart rule, and do not create points that were not measured. Check the age of the data at the point of use as well.
Averaging does not remove a constant bias: adding an offset b to every x gives the original y plus b, because the weights sum to 1. With no reference, there is no RMSE or evidence of accuracy in this set, and a filter applied after conversion to digital data does not recover information that has already aliased. Read Sampling rate and unmeasured data for the limitations before choosing a filter.
Practice steps and basic guidance
- Read the angle-filtering kit guide , make a copy of the blank angle-filtering planning sheet , and record the axis, units, clock/session and start-up rule.
- Do the angle filtering and delay exercise , keeping the full fractions until the final step; write the window members, midpoint time and calculation-ready time in separate cells.
- Compare the ramp and step examples before opening the angle-filtering answers , which state what can be concluded from the numbers and what still has to be measured for real.
- If you extend this to hardware, keep the input/output/timestamp and the filter configuration so that results can be evaluated against the real task; do not choose drone control tuning values from this set of numbers.
Start with a small window and clear rules. Do not add more points merely to make the graph look nicer without reading the effect on time. There are no new scores in the existing assessment form, and no new measurement results or flight tuning.
Angle-filtering kit numbers and reference sources · Angle-filtering kit hash list · Back to the MCU overview