Speech & Audio AI Lab

Interactive labs for speech recognition, speech synthesis, and audio in general: in-browser DSP experiments, algorithms, datasets, and a learning path, without storing any recording.

43experiments

Spectral Subtraction

Spectral Subtraction

IntermediateSignal Processing

Educational content in the form of a scientific draft that needs expert review before being adopted as an academic reference. No experimental result that was not actually run is shown here.

Definition

A method for removing relatively stationary additive noise by estimating the noise spectrum from non-speech segments and subtracting it from the magnitude spectrum of the noisy signal while keeping the phase.

Mathematical Idea

If $y=s+n$, then $|Y|\approx|S|+|N|$ approximately on average, so we estimate $|\hat N|$ from noise frames and compute $|\hat S|=\max(|Y|-\alpha|\hat N|,\ \beta|Y|)$. The floor $\beta$ prevents negative values, and with it "musical noise" appears. We reconstruct with the phase of the noisy signal and then apply the ISTFT.

How It Works

  1. We compute the STFT of the noisy signal.
  2. We estimate the mean noise magnitude from non-speech frames.
  3. We subtract $\alpha$ times the estimate from the magnitude.
  4. We apply a floor of $\beta|Y|$ to avoid negative values.
  5. We reconstruct with the original phase using the ISTFT.

Stages Diagram

  1. Noisy signal
  2. STFT
  3. Noise estimate
  4. Subtraction + floor
  5. ISTFT

Equations

$$|\hat S[m,k]|=\max\big(|Y[m,k]|-\alpha|\hat N[k]|,\ \beta|Y[m,k]|\big)$$

$\alpha$ is the subtraction factor and $\beta$ is the spectral floor.

$$\hat s=\text{ISTFT}\big(|\hat S|\,e^{j\angle Y}\big)$$

Reconstruction with the phase of the noisy signal.

Advantages

  • Simple, fast, and requires no training.
  • Suitable for quasi-stationary noise.
  • Illustrates spectral analysis in practice.

Limitations

  • Produces musical noise.
  • Fails with rapidly varying noise.
  • A wrong noise estimate distorts speech.

Use Cases

  • Speech enhancement in stationary-noise environments.
  • A preprocessing stage before speech recognition.
  • Teaching the fundamentals of audio enhancement.

Applied Example

If $|Y|=5$, $|\hat N|=2$, $\alpha=1$, and $\beta=0.05$, then $|\hat S|=\max(3,0.25)=3$; and if $|Y|=2$, the result is $\max(0,0.1)=0.1$.

Code Example

PythonImplemented and Tested
import numpy as np, librosa
rng = np.random.default_rng(0)
sr = 16000
t = np.arange(2*sr)/sr
clean = np.sin(2*np.pi*500*t) * (t > 0.5)
noisy = clean + 0.2*rng.standard_normal(len(t))
S = librosa.stft(noisy, n_fft=512, hop_length=128)
mag, ph = np.abs(S), np.angle(S)
noise = mag[:, :20].mean(axis=1, keepdims=True)    # noise estimate from the first frames
alpha, beta = 1.0, 0.05
clean_mag = np.maximum(mag - alpha*noise, beta*mag)  # spectral floor
out = librosa.istft(clean_mag*np.exp(1j*ph), hop_length=128, length=len(noisy))
snr = lambda s, e: 10*np.log10(np.sum(s**2)/np.sum((s-e)**2))
print("SNR before:", round(snr(clean, noisy), 2), "dB")
print("SNR after :", round(snr(clean, out), 2), "dB")

References

  1. Boll. Suppression of acoustic noise in speech using spectral subtraction. IEEE Trans. ASSP 27(2):113-120 (1979). DOI
  2. McFee, Raffel, Liang, Ellis, McVicar, Battenberg, et al.. librosa: Audio and Music Signal Analysis in Python. Proceedings of the 14th Python in Science Conference (2015). DOI

Audio processing in the experiments takes place inside your browser. Audio, texts, and conversations are not saved on the server. Privacy Policy