DSP System Toolbox

Acoustic Noise Cancellation (LMS)

This demo illustrates using the Least Mean Square (LMS) algorithm to subtract noise from an input signal. This demo is supported only on Windows® platforms.



The LMS adaptive filter uses the reference signal and the desired signal, to automatically match the filter response. As it converges to the correct filter model, the filtered noise is subtracted and the error signal should contain only the original signal.

The desired signal is composed of colored noise and an audio signal from a .wav file. The first input signal to the adaptive filter is white noise. This demo uses the adaptive filter to remove the noise from the signal output. When you run this demo, you hear both noise and a person playing the drums. Over time, the adaptive filter in the model filters out the noise so you only hear the drums.

This demo is supported only on Windows® platforms.

if ~ispc

Create a random number generator stream for the randn function which acts as noise source.

s = RandStream.create('mt19937ar','seed',23341);

Create and configure an FIR digital filter System object to filter our random numbers to create colored noise.

hfilt = dsp.FIRFilter('Numerator', fir1(39, .25));

Create and configure an audio file reader System object to read from an audio file.

hsigsource = dsp.AudioFileReader('dspafxf_8000.wav',  ...
              'SamplesPerFrame', 40, ...
              'PlayCount', Inf, ...
              'OutputDataType', 'double');

Create and configure an LMS adaptive filter System object.

hlms = dsp.LMSFilter(40,'Method', 'Normalized LMS', ...
    'StepSize', .01);

Create and configure an audio player System object with a sample rate of 8000 Hz to play the audio signal.

haudioout = dsp.AudioPlayer('SampleRate', 8000);

Set up a waterfall plot that displays 5 traces of the 40 filter coefficients.

hplot = plotancdata('DSPANC: Acoustic Noise Cancellation', 5, 40);

Stream Processing Loop

In the processing loop, the LMS adaptive filter updates its coefficients to model the digital filter, which enables you to remove the colored noise from the signal. The demo plays the audio signal three times.

numplays = 0;
while numplays < 3
  [y, eof]      = step(hsigsource);        % Read from audio file
  noise         = randn(s,40,1);           % Produce random data
  noisefilt     = step(hfilt, noise);      % Filter the random data
  desired       = noisefilt + y;           % Construct LMS 'desired' input
  [out, err, w] = step(hlms, noise, desired); % Run the LMS filter
  step(haudioout, double(err));            % Play the output signal
  hplot(w);                                % Plot the weights
  numplays = numplays + eof;               % Update number of plays


Here you call the release method on the System objects to close any open files and devices.



You have seen how the signal processing System objects can be used to subtract noise from an input signal.


The following helper functions are used in this demo.

  • plotancdata.m