Skip to content
Tutorials Center
My learning

Topics

Lesson 37 of 38 ยท Projects and Practice

Project: Spectral Subtraction Noise Reduction

Build a noise reducer with the STFT. Estimate the noise spectrum, subtract it with a spectral floor, resynthesize with overlap-add and measure the SNR improvement.

A fixed filter removes noise only if the noise lives in a different frequency band than the signal. Broadband noise, like the hiss of a microphone or the roar of a ventilation system, overlaps with everything. Spectral subtraction attacks it differently: estimate how strong the noise is at each frequency, then reduce each frequency of the noisy signal by that amount, frame by frame. It is the grandfather of speech enhancement algorithms and a good project to tie together the STFT, windows and overlap-add.

The plan

  1. Compute the STFT of the noisy signal.
  2. Estimate the noise magnitude spectrum from a stretch that contains only noise (the first few frames, or a quiet gap).
  3. For each frame, subtract the noise estimate from the magnitude and keep the noisy phase: |S| = max(|X| - alpha |N|, beta |X|).
  4. Inverse STFT with overlap-add.

The parameter alpha (over-subtraction, 1 to 3) removes more noise; beta (spectral floor, 0.01 to 0.1) keeps some of the original so the result does not become "watery" with isolated spectral peaks called musical noise.

Spectral subtraction

Analyze STFT of the noisy signal
Estimate Average noise magnitude
Subtract Reduce each bin, keep a floor
Resynthesize Inverse STFT, overlap-add

The noisy phase is reused.

1 / 2

A test signal

A signal with changing tones (a rough stand-in for voiced sounds) and 0.5 seconds of noise-only lead-in, plus white noise at about 0 dB SNR.

Python Building the test signal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The denoiser

scipy.signal.stft and istft handle framing, windowing and overlap-add. We use a Hann window of 512 samples with 75% overlap.

Python Spectral subtraction end to end

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The SNR improves by about 9 dB (from roughly 0 to 9.2 dB) and the noise-only part is reduced to about half a percent of its original power. The harmonics survive because they are far above the noise floor in the bins they occupy.

Tuning alpha and beta

Python Over-subtraction versus floor

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In this run alpha = 2 with a small floor gives the best SNR (about 9.3 dB); very high values start to remove the signal itself (4 dB at alpha = 5). A larger floor beta lowers the SNR slightly but usually sounds smoother in audio. SNR is only one measure: listen to the result as well.

Limits and improvements

  • Stationary noise only: the estimate assumes the noise stays constant. For changing noise, update the estimate during pauses (voice activity detection) or track the minimum of the smoothed spectrum.
  • Musical noise: isolated bins that survive randomly. Smooth the gain across time and frequency, or use a Wiener filter gain G = max(1 - alpha |N|^2/|X|^2, g_min).
  • Phase: reusing the noisy phase is fine at moderate SNR but limits quality at low SNR.
  • Window and frame size: longer frames resolve harmonics better; shorter ones follow transients (the STFT trade-off).
  • Alternatives: adaptive cancellation when a reference exists (see adaptive noise cancellation), and neural denoisers for non-stationary noise.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does spectral subtraction estimate from the noise-only segment?

    The average noise magnitude per frequency bin is subtracted from every frame of the noisy signal.
  2. 2. Why keep a spectral floor beta times the original magnitude?

    The floor prevents zeroing bins, which would create isolated spectral islands that sound like warbling tones.
  3. 3. Which phase is used for resynthesis in the basic method?

    Only the magnitude is modified; the original phase is reused, which works well at moderate SNR.
  4. 4. When does spectral subtraction work poorly?

    A fixed noise estimate becomes wrong as the noise changes; adaptive noise tracking or other methods are needed.

Summary

  • Spectral subtraction estimates the noise magnitude spectrum and subtracts it from each STFT frame.
  • Over-subtraction alpha and the spectral floor beta trade noise removal against distortion and musical noise.
  • Overlap-add with a Hann window reconstructs the signal; the noisy phase is reused.
  • It assumes stationary noise; adaptive or Wiener-style methods extend it.

Next: DSP Practice Problems