| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Embedded MATLAB |
| Contents | Index |
| On this page… |
|---|
Why Add Adapt and Reset Controls |
In this exercise, you add Adapt and Reset controls to your filter. Using these controls, you can turn the filtering on and off. When Adapt is enabled, the filter continuously updates the filter weights. When Adapt is disabled, the filter weights remain at their current values. If Reset is set, the filter resets the filter weights.
The modified filter code is in emldemo_lms_05.m. The changes to the code are in bold:
function [ signal_out, err, weights_out ] = ...
emldemo_lms_05(distorted, desired, reset, adapt) %#eml
persistent weights;
persistent fifo;
% Filter length:
L = uint32(32);
% Adaptation step size:
mu = 4/(32*1024);
% Signal length:
N = length(distorted);
if length(desired) == N
if ( reset || isempty(weights) )
% Filter coefficients:
weights = zeros(L,1);
% FIFO Shift Register:
fifo = zeros(L,1);
end
% Pre-allocate output and error signals:
signal_out = zeros(N,1);
err = zeros(N,1);
% Filter Update Loop:
for n = 1:N
fifo(1:L-1) = fifo(2:L);
fifo(L) = distorted(n);
signal_out(n) = weights' * fifo;
err(n) = desired(n) - signal_out(n) ;
if adapt
weights = weights + mu*err(n)*fifo;
end
end
% Output the filter weights:
weights_out = weights;
else
error('Lengths of input signals are not equal');
end
end
Note the following important changes to the filter algorithm:
The new input parameter reset is used to determine if it is necessary to reset the filter coefficients:
if ( reset || isempty(weights) ) % Filter coefficients: weights = zeros(L,1); % FIFO Shift Register: fifo = zeros(L,1); end
The new parameter adapt is used to control whether the filter coefficients are updated or not:
if adapt weights = weights + mu*err(n)*fifo; end
Follow the same procedure you used in Generating C Code to generate a MEX function and a C library file. To run the build scripts, enter emldemo_lms_build_05_mex and then emldemo_lms_build_05_lib at the MATLAB prompt.
Follow the same procedure you used in Using the Test Script with the file emldemo_lms_test_05. The test outputs show that the modified filter is performing in exactly the same way as the original filter.
Note the following important changes to the test script:
You must now set up the values of the Reset and Adapt controls before using the filter. In this test, the controls are set so that the filter is constantly adapting:
reset = false; adapt = true;
You pass in the values of Reset and Adapt when you call the filter:
[ yt et test.impulse ] = ...
emldemo_lms_05_mex(whiteNoise(indexRange), ...
distorted(indexRange), reset, adapt);If you want to integrate your M-code with Simulink, perform the second procedure in this tutorial, Integrating Your Embedded MATLAB Compliant Code with Simulink.
![]() | Modifying the Filter to Use Streaming | Where to Learn More | ![]() |

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 |