Averaging Filters

Any practical measurement carries some level of noise. Whether from sensor electronics, environmental disturbances, or quantization effects, this noise can obscure the underlying signal of interest. Filters are tools that attenuate noise while preserving the structure of the true signal. This post introduces two foundational averaging-based filters and contrasts their behavior through an interactive demonstration.

Simple Average Filter

The simplest form of filtering is to compute the running mean of all measurements collected so far. Let \(x_k\) denote the measurement at discrete time step \(k\). The output of the average filter is:

\[\bar{x}_k = \frac{1}{k} \sum_{i=1}^{k} x_i\]

A naive implementation requires storing the entire measurement history. This is avoided by recasting the expression recursively. Noting that \(\sum_{i=1}^{k} x_i = \sum_{i=1}^{k-1} x_i + x_k = (k-1)\bar{x}_{k-1} + x_k\), the update equation becomes:

\[\bar{x}_k = \frac{k-1}{k}\,\bar{x}_{k-1} + \frac{1}{k}\,x_k\]

Defining \(\alpha_k = \frac{k-1}{k} \in [0, 1)\), this takes the compact form:

\[\bar{x}_k = \alpha_k\,\bar{x}_{k-1} + (1 - \alpha_k)\,x_k\]

This is the recursive averaging equation. It requires only the previous filtered value \(\bar{x}_{k-1}\) and the current measurement \(x_k\), making it computationally efficient and memory-friendly.

Note that \(\alpha_k \to 1\) as \(k \to \infty\). This means the filter places progressively less weight on new measurements over time — a property that ensures convergence but also limits adaptability.

Example

Consider a voltmeter measuring a constant voltage of 14 V. The measurement noise is modeled as zero-mean white noise with standard deviation \(\sigma = 2\,\text{V}\). As shown in the interactive plot below, the filtered estimate converges steadily toward the true value as more measurements are incorporated. The convergence rate slows over time, consistent with the diminishing weight \((1 - \alpha_k) = 1/k\) assigned to each new observation.

Characteristics

The average filter is unbiased for stationary signals — given sufficient measurements, it converges to the true mean regardless of noise amplitude. It requires minimal computation and no tuning parameters. However, because every past measurement receives nonzero weight, the filter has a memory that extends to the first sample. For a signal whose true value changes over time, this historical weighting introduces systematic lag: the filtered output trails the true value, and the lag grows with the number of samples accumulated. The average filter is therefore best suited to quasi-static or slowly drifting signals.


Moving Average Filter

The moving average filter addresses the lag problem by restricting the average to the \(n\) most recent measurements. This finite, sliding window discards old data, allowing the filter to track changes in the underlying signal. The \(n\)-point moving average is:

\[\bar{x}_k = \frac{1}{n} \sum_{i=k-(n-1)}^{k} x_i\]

An equivalent recursive formulation avoids recomputing the full sum at each step:

\[\bar{x}_k = \bar{x}_{k-1} + \frac{x_k - x_{k-n}}{n}\]

This requires only the current measurement \(x_k\) and the oldest measurement in the window \(x_{k-n}\), so storage is proportional to \(n\) rather than the total sample count.

Example

Consider a sinusoidally varying signal with additive noise. The average filter, anchored to all past data, cannot track the oscillation and produces a flat, lagged output. The moving average filter, by contrast, follows the signal envelope — though with a phase delay that increases with \(n\). Use the interactive demo below to explore this trade-off directly.

The Window Length Trade-off

The parameter \(n\) governs a fundamental trade-off:

  • Small \(n\): The filter responds quickly to changes but provides weaker noise attenuation. In the frequency domain, the passband is wider and high-frequency noise is less suppressed.
  • Large \(n\): Noise is averaged out more aggressively, but the filter introduces greater lag and may blur sharp transitions in the signal.

This trade-off is inherent to any finite-impulse-response (FIR) averaging filter. The choice of \(n\) should be guided by the expected rate of change of the signal relative to the noise bandwidth.


Interactive Comparison

The simulation below generates a noisy sinusoidal signal and applies both the average filter and the moving average filter in real time. Adjust the window length \(n\) to observe how it affects noise suppression and tracking delay.

Noisy measurement
True signal
Average filter
Moving average (n)

Comparison Summary

Property Average Filter Moving Average (window \(n\))
Memory required \(O(1)\) (recursive) \(O(n)\)
Tuning parameter None Window length \(n\)
Lag behavior Grows unboundedly Bounded by \(n/2\) samples
Noise suppression Improves with \(k\) Fixed, governed by \(n\)
Suitable signal Stationary or quasi-static Slowly time-varying

Both filters are linear and shift-invariant (once transients decay), and their frequency responses are well-defined sinc-like functions. More selective attenuation of specific frequency bands requires higher-order filters such as Butterworth or Chebyshev designs — discussed in the companion post on low-pass filters.

See Also

References

  1. Kim, P. (2011). Kalman Filter for Beginners: with MATLAB Examples. CreateSpace Independent Publishing Platform.