DSP·GUIDEOUT LOUD
Book episodes

Episode 12 · Digital Filters · Book chapters 15, 16 · 35m59s

Moving Average and Windowed-Sinc Filters

The provably optimal smoother that is also the worst frequency-domain filter, and how to get a genuinely sharp cutoff.

The original recording, including numerical examples and figure cues. Switching versions starts at the beginning.

0:0035:59

Choose a playback speed that feels comfortable. Open in Google Drive to download or play on a phone.

Save for offline listening:Detailed MP3Driving MP3
Figures

The optimal smoother that is also the worst filter

The moving average is provably the best linear filter for reducing random noise while keeping edges sharp. The argument is clean: noise affects every sample equally, so no sample deserves more weight, so equal weights are optimal. Its noise reduction is the square root of the window length, and it can be computed recursively — add the entering sample, subtract the leaving one — costing two operations regardless of length.

The moving average is a poor frequency-domain filter

Figure
0.221.00.5 · fs
The kernel is a rectangle, so the response is a sinc. It sags immediately rather than staying flat, rolls off gradually rather than sharply, and — the real problem — the side lobes never go away. The first one sits at about 22% of full amplitude, or −13 dB, so nearly a quarter of the signal you meant to reject comes straight through. This is the same filter that is provably optimal for smoothing while preserving edges. Change the question and it goes from best possible to worst available, which is precisely why “what is the best filter” is not a well-formed question.

Apply it twice and the kernel becomes a triangle; four times and it approaches a Gaussian with essentially no ringing, by the central limit theorem. At eight operations per sample regardless of length, that is the standard fast approximation to Gaussian blurring.

Getting a genuinely sharp cutoff

An ideal low-pass filter is a rectangle in frequency, so its impulse response must be a sinc. That is forced by the mathematics, not chosen — and since a sinc extends infinitely, the ideal filter cannot be built. All filter design is about how you cut it off.

Windowed-sinc designer

Interactive
kernel0-20-40-60-800.5 · fs
0.120 · fs
41 taps
Transition ≈ 4/M = 0.1000 · fsWorst stopband -61 dBLatency 20 samples
The kernel above, its actual response below, both computed live. Set the window to “None” and drag the length: at 40 taps the stopband sits near −37 dB, and at 150 taps it is still near −35 dB. Nearly four times the computation buys essentially nothing, because that floor is Gibbs ringing from abrupt truncation and length cannot fix it. Switch to Hamming and it drops to about −59 dB; Blackman reaches roughly −78 dB. Then watch the real trade — halving the transition width doubles the taps, and with them the computation and the latency.

The design is four steps: generate a sinc at your cutoff, truncate and centre it, multiply by a window, normalise so the coefficients sum to one. The governing relationship is that transition bandwidth is about 4/M — so sharpness costs kernel length linearly, and with it computation and latency.

Key points

  1. The moving average filter averages M consecutive samples. Its kernel is M copies of one over M — a rectangle, all positive, summing to one.
  2. It is the provably optimal linear filter for reducing random noise while retaining edge sharpness, because noise affects every sample equally so no unequal weighting can do better for the same window width.
  3. Its noise reduction is the square root of the number of points averaged. A hundred point average improves noise by a factor of ten, and the returns diminish quadratically.
  4. It can be computed recursively — add the entering sample, subtract the leaving sample — costing two operations per output regardless of M. A ten thousand point moving average costs the same per sample as a three point one. No other filter in this series has that property.
  5. Prefer integer accumulation for the recursive form, since floating point round-off never leaves the accumulator.
  6. Its frequency response is a sinc, because its kernel is a rectangle. The response sags immediately, rolls off slowly, and has a first side lobe at about minus thirteen decibels — twenty two percent leakage. It is essentially the worst choice for frequency domain work.
  7. The same filter is provably optimal and essentially worthless depending only on which question you are asking. That is why there is no best filter.
  8. Applying it twice gives a triangular kernel with lower side lobes; applying it four times approaches a Gaussian with essentially no ringing, by the central limit theorem. Four passes costs eight operations per sample regardless of length, and is the standard fast approximation to Gaussian blurring.
  9. The ideal low pass filter is a rectangle in frequency, so its impulse response must be a sinc. That is forced by the mathematics, and since a sinc is infinite, the ideal filter cannot be built.
  10. The windowed-sinc design is four steps: generate a sinc at your cutoff, truncate to M points and shift to center it, multiply by a Hamming or Blackman window, then normalize so the coefficients sum to one.
  11. Truncating without windowing multiplies by a rectangle and produces about nine percent Gibbs overshoot that does not diminish with kernel length. Windowing is what removes it.
  12. Special-case the center sample, where the sinc is zero divided by zero and the true value is one.
  13. Transition bandwidth is approximately four divided by M, with both as fractions of the sampling rate. So M is about four over the bandwidth you want. Sharpness costs kernel length linearly, and with it computation and latency.
  14. The cutoff is specified at the one-half amplitude point, not the minus three decibel point used for analog filters.
  15. A three point moving average turns a step from zero to nine into the ramp zero, three, six, nine — rising in exactly the kernel length, with no overshoot and perfect symmetry. All three time domain goals at once, from the simplest filter there is.
  16. A windowed-sinc kernel is a large positive peak at the center flanked by alternating smaller lobes fading toward the ends, symmetric about the middle. The zero crossing spacing is set by the cutoff. If your computed kernel is not symmetric, you have an indexing error.
  17. Hamming gives about minus fifty three decibels of stopband attenuation with faster roll-off. Blackman gives about minus seventy four decibels but needs a kernel roughly twenty percent longer. Judge the requirement against the actual interfering signal, not against a general sense of what sounds small.
  18. A worked example: sampling at ten thousand hertz, cutoff one thousand hertz, transition two hundred hertz wide, gives a transition bandwidth of zero point zero two and therefore a kernel of about two hundred points, with a hundred samples of delay.
  19. The Kaiser window is parameterized, sliding continuously between rectangular and Blackman behaviour, so you can specify the stopband attenuation you actually need and get the shortest kernel that meets it. Standard formulas give the parameter and length directly from a specification, and it is what professional design tools default to.
  20. Use odd kernel lengths. An odd length has a real center sample and therefore a whole number delay; an even length shifts the output by half a sample. Spectral inversion also requires an odd length.
  21. On the same noisy step with an interfering tone, the moving average gives a clean ramp but leaks the tone through its side lobes, while the windowed-sinc removes the tone completely but rings around the edge. There is no filter that does both, so choose which failure you can tolerate.
  22. Very sharp FIR filters need enormous kernels, which forces FFT convolution, imposes latency that no amount of computing power can remove, and eventually requires double precision. FIR filters scale gracefully in quality and badly in cost.