Products & Services Solutions Academia Support User Community Company

Modifying the Filter to Accept a Variable Length Signal

Why Modify the Filter Algorithm?

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.

Examining the Generated C Code

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.

Prerequisites

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.

What to Look for in the Generated C Code

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.

Examining the C Code for the LMS Filter

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.

Modifying Your M-Code

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']);
end

Summary of Changes to the Filter Algorithm

Note the following important changes to the filter algorithm:

Generating C Code For Your Modified M-Code

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.

How to Generate C 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.

Summary of Changes to the Build Scripts

Note the following changes to the build scripts:

Testing Your MEX Function

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.

How to Test Your MEX Function

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.

Summary of Changes to the Test Script

Note the following changes to the test script:

  


Recommended Products

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