This simulation demonstrates active noise cancellation (ANC) using an adaptive LMS (least-mean-squares) filter, the same algorithm at the heart of noise-cancelling headphones. A reference signal derived from the noise source is passed through an adjustable FIR filter to produce an anti-noise waveform y(n), which is added to the noise d(n) to leave a residual error e(n). The filter weights update every sample in the direction that reduces e(n)², so over time the anti-noise waveform converges to a near-perfect inverted copy of the noise and the residual collapses toward silence.
Three live waveforms: the incoming noise d(n) in red, the filter's anti-noise output y(n) in blue, and the residual error e(n) in green. A fourth plot tracks error power over time, visually showing the LMS algorithm converging as the residual waveform flattens toward zero.
Choose a noise type (tonal 440 Hz, broadband, pink noise, or 50 Hz mains hum), then press Run. Adjust the Step size μ slider to trade convergence speed against stability, the Filter length N slider to change how many past samples the FIR filter uses, and the Reference delay slider to simulate a lagging reference microphone. Reset restarts the filter weights from zero.
Real noise-cancelling headphones must run their LMS-style adaptation in well under a millisecond, because sound above a few hundred Hz changes faster than a human can react — the "anti-noise" has to be computed and emitted before the original wave even reaches your eardrum.
ANC generates a second sound wave, the anti-noise, that is equal in amplitude but inverted in phase relative to the incoming noise. When the two waves add together in the air (or in the electrical signal, as in this simulation), the peaks of one cancel the troughs of the other through destructive interference, leaving only a small residual.
LMS (least-mean-squares) is an adaptive filtering algorithm that updates its filter weights w(n) after every sample using the rule w(n+1) = w(n) − μ·e(n)·x(n), where e(n) is the current error and x(n) is the reference input vector. It is popular in real ANC hardware because it is simple, computationally cheap, and provably converges toward the optimal filter when the step size μ is small enough.
μ controls how large a correction the filter weights receive on each sample. A larger μ makes the filter adapt and converge faster, but if μ exceeds roughly 1/λ_max (where λ_max relates to the power of the reference signal), the weights can overshoot and the filter becomes unstable, causing the error to grow instead of shrink. A smaller μ converges more slowly but more reliably.
The filter length N is the number of taps (past samples) the adaptive FIR filter uses to build its anti-noise output. A longer filter can model more complex noise, including delayed or filtered versions of the original signal, but it also has more weights to adapt, which generally means slower convergence and more computation per sample.
The reference delay simulates a lag between when the noise is sensed and when the reference signal is available to the filter, similar to microphone placement delays in real ANC systems. If the delay is too large relative to the filter length, the correlation between the reference and the actual noise arriving at the error microphone weakens, and the filter struggles to build an accurate anti-noise signal, so the achieved noise reduction in dB drops.