Back to timeline
school Mar 2024

A tone, removed in software

An analog-electronics assignment asked us to build a band-stop circuit to strip a constant tone out of a music recording. I solved it in software, first with a spectrogram-based band-stop, then with phase cancellation, and compared the two side by side. Phase cancellation won; the writeup is in Norwegian and lives in the repo as a PDF.

The problem

A music file had been corrupted by a single, constant-frequency tone, the kind of thing that shows up as a bright horizontal line near the bottom of a spectrogram. The brief: remove that tone with minimal damage to the music underneath.

Method 1: spectrogram band-stop

It works, and the audio sounds clean, but the spectrogram shows a clearly empty slot where the tone used to be: a thin black stripe in the middle of the music. Some original signal was thrown away with the noise.

Three stacked spectrograms: original with a bright horizontal line, and two processed versions where the line has been cut out.
Top: original with the tone as a bright horizontal stripe. Middle/bottom: band-stop output. Tone gone, but a thin empty band is now visible across the entire clip.

Method 2: phase cancellation

Because the offending tone is just an added sinusoid, you can cancel it by adding back another sinusoid of the same frequency and amplitude, phase-shifted 180°. Estimate amplitude, frequency, and phase from the recording; generate the inverse; sum them. In theory, the tone vanishes and the original music is left completely intact.

Two sine waves of the same frequency, offset 180°. They sum to zero at every point.
The idea: two sinusoids of equal amplitude and 180° apart sum to zero. Estimate the tone, generate its inverse, add.

In practice, that is roughly what happened. The output spectrogram looks identical to the original everywhere except where the tone used to be, and even there, the surrounding music spectrum is preserved. The audio sounds natural; no traces of the processing leaked through.

Two spectrograms compared: original and phase-cancelled output. The bright horizontal tone line is gone in the output, and the rest of the spectrum is unchanged.
Final result. Original (top) vs. phase-cancelled output (bottom). Tone removed, surrounding spectrum intact.

What I learned

That a band-stop filter is the right answer to the question "remove a narrow band of frequencies", and the wrong answer to the question "remove a constant sinusoid". The latter has more structure than the former and a cancellation approach exploits all of it. Whenever the problem hands you that much structure, spend a moment looking before you reach for the generic filter.

Writeup

The full Norwegian course report (figures, derivations, before/after spectrograms) is in the repo:

Back to timeline