| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Embedded MATLAB |
| Contents | Index |
| On this page… |
|---|
A least mean squares (LMS) filter is an adaptive filter that adjusts its transfer function according to an optimizing algorithm. You provide the filter with an example of the desired output together with the input signal. The filter then calculates the filter weights, or coefficients, that produce the least mean squares of the error between the output signal and the desired signal.
The example for this tutorial uses a LMS filter to remove the noise in a music recording. This example uses two inputs. The first input is the distorted signal: the music recording plus noise. The second input is the noise. The filter works to eliminate the difference between these two signals and outputs the difference, which, in this case, is the clean music recording. When you start the simulation, you hear both noise and the music. Over time, the adaptive filter filters out the noise so you hear only the music.
This tutorial uses the least mean squares (LMS) algorithm to remove noise from an input signal. The LMS algorithm computes the filtered output, filter error, and filter weights given the distorted and desired signals.
The LMS algorithm at the start of the tutorial uses a batch process to filter the audio input. This algorithm is suitable for MATLAB, where you are likely to load in the entire signal and process it all at once. However, a batch process is not suitable for processing a signal in real time. As you work through the tutorial, you refine the design of the filter to convert the algorithm from batch-based to stream-based processing.
Note The LMS filter example in this tutorial uses static array sizing. The Embedded MATLAB subset supports variable-size arrays and matrices with known upper bounds. With this feature, you can define inputs, outputs, and local variables in Embedded MATLAB functions to represent data that varies in size at runtime. For more information, see Generating Code for Variable-Size Data in the Embedded MATLAB User's Guide. |
The function signature for the algorithm is:
function [ signal_out, err, weights ] = lms(distorted, desired )
The filtering is performed in the following loop:
for n = 1:N signal_out(n) = weights' * distorted(n:n+L-1); err(n) = desired(n) - signal_out(n) ; weights = weights + mu*err(n)*distorted(n:n+L-1); end
where N is the length of the input signal, L is the filter length, and mu is the adaptation step size.
What is the adaptation step size?
The filtering process has three phases:
Convolution
The convolution for the filter is performed in:
signal_out(n) = weights' * distorted(n:n+L-1);
Calculation of error
The error is the difference between the desired signal and the output signal:
err(n) = desired(n) - signal_out(n);
Adaptation
The new value of the filter weights is the old value of the filter weights plus a correction factor that is based on the error signal, the distorted signal, and the adaptation step size:
weights = weights + mu*err(n)*distorted(n:n+L-1);
Haykin, Simon, Adaptive Filter Theory, Prentice-Hall, Inc., 1996
![]() | Design Considerations for Code Generation | Files for This Tutorial | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |