Skip to content
FoundationSource checked

Moving Average

Smooths a sequence by replacing each value with an average over a nearby window.

Quick Facts

Compact metadata for implementation and discovery.

Category
Signal Processing
Difficulty
Beginner
Family
Smoothing Filter
Tier
Foundation
Status
Verified
Review
Source Checked
Sources
1
Formulas
1
Relationships
5
Code example
Not ready
Visualization
Ready
Aliases
Rolling Average

Overview

A Moving Average filter computes local averages across a sliding window, reducing short-term fluctuations while preserving slower trends.

Core Concepts

sliding windowlocal averagesmoothinglag

Quick facts

Complexity

Runtime and memory behavior at a glance.

Best

O(n)

Average

O(n)

Worst

O(n)

Space

O(1)

When to Use

  • Use it for simple smoothing of time series or sampled signals.
  • Use it when a small amount of lag is acceptable.
  • Use cumulative sums for efficient fixed-width window averages.

When Not to Use

  • Avoid it when sharp changes must be preserved.
  • Avoid it when phase lag is unacceptable without compensation.
  • Avoid it when outliers dominate the mean.

What It Does

  • Smooths short-term variation
  • Tracks local trend

Use Cases

  • Time-series smoothing
  • Noise reduction
  • Simple trend indicators

Inputs

  • Sequence of values
  • Window size

Outputs

  • Smoothed sequence

Key Formulas (1)

Moving average

averagei = ∑(x[i − k + 1] … x[i]) / k
average_i = sum(x[i - k + 1] ... x[i]) / k

Smooths a signal by averaging a fixed-size local window.

Pseudocode

  1. 1.Choose a window size
  2. 2.Keep a running sum of values in the window
  3. 3.Divide the sum by the window length
  4. 4.Move the window one position
  5. 5.Update the running sum and repeat

Canonical Example

Input
samples = [1, 2, 3, 2, 1]
Output
transformed or filtered samples
Explanation
Moving Average: The algorithm reads local or frequency structure from the sample sequence.

Implementation Notes

  • Specify centered, trailing, or leading window alignment.
  • Use a running sum to update fixed-size windows in O(1) per sample.
  • Decide how to handle incomplete windows at the edges.

Common Pitfalls

  • Forgetting that larger windows increase smoothing and delay.
  • Handling boundaries inconsistently.
  • Using a mean when a median or robust filter is needed.

Interactive Mechanism Walkthrough

Iterative Convergence & State Trajectory

Show a window moving over a line chart and smoothing the output.

Moving Average mechanism walkthrough step 1 of 5x₀x₁x₂x*
Step 1 of 5: Choose a window size
sliding-average

Notes

Moving averages trade noise reduction for lag and blurred transitions.

Sources (1)

What each reference supports for this entry.

Discrete-Time Signal Processing

Book

Signal-processing reference for digital filters, convolution, spectral analysis, windows, resampling, and transform-based methods.

Open source

Explore Next

Good next jumps from the relationship graph, ranked for useful discovery.

Moving average uses a fixed window, while EMA uses decaying weights.

SpecializesLow-Pass Filter

A moving average is a simple low-pass smoothing filter.

Related Algorithms

Tags

Signal ProcessingFilteringSmoothing