This example shows how to use the Least Mean Square (LMS) algorithm to subtract noise from an input signal. This example uses fixed-point data types.
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 example uses the adaptive filter to remove the noise from the signal output. When you run this example, 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.
Seed the random number generator for the randn function which acts as a noise source.
Create a fi object to convert the noise source to a fixed-point data type.
noise = fi(ones(40,1),true,16,13);
Create and configure a fixed-point FIR digital filter System object to filter the random white noise to create colored noise.
hfilt = dsp.DigitalFilter('TransferFunction', 'FIR (all zeros)', ... 'Numerator', fir1(39, .25)); % Specify fixed-point attributes of the filter. hfilt.RoundingMethod = 'Floor'; hfilt.OverflowAction = 'Wrap';
You can specify a System object's fixed-point property data type. For example, you can set the NumeratorCoefficientsDataType property to 'Custom' and then set the CustomNumeratorCoefficientsDataType property to numerictype(,16,17). You use the numerictype object to indicate that you want the numerator coefficients to be fixed-point numbers with 16-bit word lengths and 17-bit fraction lengths.
hfilt.NumeratorCoefficientsDataType = 'Custom'; hfilt.CustomNumeratorCoefficientsDataType = numerictype(,16,17);
Specify more fixed-point attributes of the FIR filter.
hfilt.ProductDataType = 'Custom'; hfilt.CustomProductDataType = numerictype(,32,30); hfilt.AccumulatorDataType = 'Custom'; hfilt.CustomAccumulatorDataType = numerictype(,32,30); hfilt.OutputDataType = 'Same as input';
Create a fi object to act as a data type conversion for the sum of the filtered noise and the original signal
desired = fi(zeros(size(noise)),noise.numerictype);
Create and configure an audio file reader System object to read the input signal from the specified audio file, dspafxf_8000.wav.
hsigsource = dsp.AudioFileReader('dspafxf_8000.wav', ... 'SamplesPerFrame', 40, ... 'PlayCount', Inf,... 'OutputDataType', 'int16');
Create and configure an LMS adaptive filter System object.
hlms = dsp.LMSFilter(40,'Method', 'Normalized LMS', ... 'StepSize', .002);
Specify fixed-point attributes of the adaptive filter.
hlms.RoundingMethod = 'Nearest'; hlms.OverflowAction = 'Saturate'; hlms.StepSizeDataType = 'Custom'; hlms.CustomStepSizeDataType = numerictype(,16,15); hlms.LeakageFactorDataType = 'Custom'; hlms.CustomLeakageFactorDataType = numerictype(,16,14); hlms.WeightsDataType = 'Custom'; hlms.CustomWeightsDataType = numerictype(,16,17); hlms.EnergyProductDataType = 'Custom'; hlms.CustomEnergyProductDataType = numerictype(,32,20); hlms.EnergyAccumulatorDataType = 'Custom'; hlms.CustomEnergyAccumulatorDataType = numerictype(,32,20); hlms.ConvolutionProductDataType = 'Custom'; hlms.CustomConvolutionProductDataType = numerictype(,32,20); hlms.ConvolutionAccumulatorDataType = 'Custom'; hlms.CustomConvolutionAccumulatorDataType = numerictype(,32,20); hlms.StepSizeErrorProductDataType = 'Custom'; hlms.CustomStepSizeErrorProductDataType = numerictype(,32,20); hlms.WeightsUpdateProductDataType = 'Custom'; hlms.CustomWeightsUpdateProductDataType = numerictype(,32,20); hlms.QuotientDataType = 'Custom'; hlms.CustomQuotientDataType = numerictype(,32,20);
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_FIXPT: Acoustic Noise Cancellation', ... 5, 40);
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 example plays the audio signal three times.
numplays = 0; T = numerictype(true,16,15); while numplays < 3 [y, eof] = step(hsigsource); % Read from audio file y = reinterpretcast(y, T); noise(:) = randn(40,1); % Produce random data and convert noisefilt = step(hfilt, noise); % Filter the random data desired = accumpos(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 end
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 example.