mathematics//signal processing//digital filter//moving average
A moving average is a digital filter whose output is the plain mean of the last \(N\) samples, recomputed as each new sample arrives, and it is the first smoother most engineers reach for: on a slow level transmitter, a battery voltage, a dashboard of plant data. It is the simplest FIR filter, with every weight equal to \(1/N\).
A moving average is a digital filter whose output is the plain mean of the last NNN samples, recomputed as each new sample arrives, and it is the first smoother most engineers reach for: on a slow level transmitter, a battery voltage, a dashboard of plant data. It is the simplest FIR filter, with every weight equal to 1/N1/N1/N.
yk=1N∑i=0N−1xk−iy_k=\frac{1}{N}\sum_{i=0}^{N-1}x_{k-i}yk=N1i=0∑N−1xk−i
Averaging NNN samples of independent noise divides its standard deviation by N\sqrt NN: 16 samples cut the noise by four, 100 samples by ten. The price is delay, (N−1)/2(N-1)/2(N−1)/2 samples for every frequency in band. A 16-sample average at 100 Hz cuts the white noise to a quarter and leaves the reading 75 ms late; at 1 kHz the same average costs 7.5 ms.
Averaging kills only the noise that has no memory.
The N\sqrt NN<path d="M95,702
c-2.7,0,-7.17,-2.7,-13.5,-8c-5.8,-5.3,-9.5,-10,-9.5,-14
c0,-2,0.3,-3.3,1,-4c1.3,-2.7,23.83,-20.7,67.5,-54
c44.2,-33.3,65.8,-50.3,66.5,-51c1.3,-1.3,3,-2,5,-2c4.7,0,8.7,3.3,12,10
s173,378,173,378c0.7,0,35.3,-71,104,-213c68.7,-142,137.5,-285,206.5,-429
c69,-144,104.5,-217.7,106.5,-221
l0 -0
c5.3,-9.3,12,-14,20,-14
H400000v40H845.2724
s-225.272,467,-225.272,467s-235,486,-235,486c-2.7,4.7,-9,7,-19,7
c-6,0,-10,-1,-12,-3s-194,-422,-194,-422s-65,47,-65,47z
M834 80h400000v40h-400000z"/> law holds for white noise, where each sample is independent; a bias is untouched by any amount of averaging and needs calibration, and noise that wanders slowly stops shrinking past a certain window, the point an Allan variance plot locates.
It is cheap to run. Keeping a running sum, adding the new sample and subtracting the one that leaves, costs two operations per sample whatever NNN is, plus a buffer of NNN values.
Its frequency response is poor for its delay. It has nulls at multiples of fs/Nf_s/Nfs/N and leaky lobes between them, so it removes some frequencies completely (an average over exactly one mains period kills 50 Hz hum) and lets others through surprisingly well. For the same delay, a first-order low-pass filter or a designed FIR gives a cleaner band.
It smears what is not noise. An isolated spike from a corrupted byte becomes a bump NNN samples wide, and a true step becomes a ramp; a median filter treats both better.
In statistics and finance the same name covers smoothing a series for display, often centred on each point, which uses future samples and has no delay; that version is offline only, like any zero-phase filtering.