| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Embedded MATLAB |
| Contents | Index |
| On this page… |
|---|
Why Modify the Filter Algorithm? Examining the Generated C Code |
The filter you have worked on so far in this tutorial uses a simple batch process. This design is suitable for MATLAB, where you are likely to load in the entire signal and process it all at once. This design is not suitable for processing a signal in real time.
Now you are ready to modify your design to make it more suitable for embedded implementation.
One method of identifying potential improvements to the design is to examine the generated C code. In this exercise, you examine the C code generated in Generating C Code.
Open the file emldemo_lms_02.c which is in folder\emcprj\rtwlib\emldemo_lms_02, where folder is the folder that contains your tutorial files.
Look for code that won't work efficiently or accurately when operating in real time on an embedded system. For example, the use of hard-coded constants instead of variables that can accept real-time signals.
In emldemo_lms_02.c, examine the function definition:
/* Function Definitions */ void emldemo_lms_02(const real_T eml_distorted[73113], const real_T eml_desired[73113], real_T eml_signal_out[73113], real_T eml_err[73113], real_T eml_weights[32])
The size of the input parameters eml_distorted and eml_desired are hard-coded to a constant (73113 samples), the size of the handel.mat audio signal. Ideally, you want to design a filter that accepts a signal of any length. A simple first step is to modify the batch filter to accept any signal up to a maximum size that you specify at code generation time, and then to specify the actual signal length as an input parameter.
To modify emldemo_lms_02.m to accept any signal up to a maximum size, you preallocate maximum, fixed-size arrays for the desired and distorted input signals at code generation time (see Summary of Changes to the Build Scripts) and then provide a new input parameter, N, to track the actual size of the audio sample. The value of N ensures that the filter loop processes only valid data.
The modified filter code is supplied in emldemo_lms_03.m. The changes to the code are in bold:
function [ signal_out, err, weights ] = ...
emldemo_lms_03(distorted, desired , N) %#eml
% Version Number:
k = 3;
% Filter Length:
L = uint32(32);
% Adaptation step-size:
mu = 4/(32*1024);
% Maximum signal length:
MaxLength = length(distorted);
% Filter coefficients:
weights = zeros(L,1);
% Zero-Pad Input Signal:
distorted_pad = [ zeros(L-1,1); distorted ];
% Pre-allocate output and error signals:
signal_out = zeros(MaxLength,1);
err = zeros(MaxLength,1);
% Filter Update Loop:
for n = 1:N
signal_out(n)=weights' * distorted_pad(n+(0:L-1));
err(n)=desired(n) - signal_out(n) ;
weights=weights + mu*err(n)*distorted_pad(n+(0:L-1));
end
% Plot Impulse Response:
plotimpulse(weights,k);
end
function plotimpulse(weights,k)
% Declare extrinsic functions:
eml.extrinsic('figure','stem','title','num2str');
% Plot the impulse response of the filter weights:
figure;
stem(weights);
title(['LMS Filter -- Version #' num2str(k) ...
' -- Impulse Response']);
endNote the following important changes to the filter algorithm:
The data type of the filter length L has changed from double to uint32. You must declare L an integer because it is used in the matrix index distorted_pad(n+(0:L-1)) where n is an integer. If L is not an integer, a compilation error occurs.
The new input parameter N, which contains the actual size of the audio sample, controls the filter loop to ensure that the loop processes only valid data:
for n = 1:N signal_out(n) = weights' * distorted_pad(n+(0:L-1)); err(n) = desired(n) - signal_out(n) ; weights = weights + mu*err(n)*distorted_pad(n+(0:L-1)); end
The plot functions are now contained in the subfunction plotimpulse(weights,k). It is good practice to separate your pre- and post-processing operations (such as plotting) from your core algorithm.
In Modifying the Filter to Accept a Variable Length Signal, you learned how to modify your M-code to accept any input signal up to a maximum size. You are now ready to generate a MEX function and C code for this new M-code.
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_03_mex and then emldemo_lms_build_03_lib at the MATLAB prompt.
Note the following changes to the build scripts:
MaxLength defines the maximum size of the input signal. Set MaxLength to a value that is greater than the biggest signal size you are likely to encounter. In this case, MaxLength is set to a value of 131072 (128*1024) samples.
X = zeros(MaxLength,1) preallocates a fixed-size array using MaxLength. X defines the properties of the desired and distorted input parameters at code generation time.
the –T mex option instructs emlc to generate a MEX function. See Recommended Compilation Options for emlc for more information.
–T rtw:lib option instructs emlc to generate embeddable C code and compile it to a library (.lib file.)
In this exercise, you test the MEX function that you generated in How to Generate C Code to verify that it provides the same functionality as the original M-Code.
Follow the same procedure you used in Using the Test Script with the test script emldemo_lms_test_03. The test outputs show that the modified filter is performing in the same way as the original filter.
Note the following changes to the test script:
For test purposes, the script provides the emldemo_lms_03_mex function with input signals of length MaxLength. To create these signals, the script zero pads the whiteNoise and distorted input signals to create two new input signals whiteNoise_pad and distorted_pad.
whiteNoise_pad = [ whiteNoise ; zeros(MaxLength-N,1) ]; distorted_pad = [ distorted ; zeros(MaxLength-N,1) ]; [ test.signal test.error test.impulse ] = ... emldemo_lms_03_mex(whiteNoise_pad, distorted_pad, ... uint32(N));
The script compares the first N elements of the output signal, which contain the filtered audio sample, to the golden reference.
%% Find differences: diff.signal = test.signal(1:N) - gold.signal; diff.error = test.error(1:N) - gold.error; diff.impulse = test.impulse - gold.impulse;
![]() | Elaborating Your Algorithm for Embedded Implementation | Modifying the Filter to Use Streaming | ![]() |

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 |