Products & Services Solutions Academia Support User Community Company

Adding Adapt and Reset Controls

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.

Modifying Your M-Code

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

Summary of Changes to the Filter Algorithm

Note the following important changes to the filter algorithm:

How to Generate C Code for Your Modified 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_05_mex and then emldemo_lms_build_05_lib at the MATLAB prompt.

How to Test Your MEX Function

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.

Summary of Changes to the Test Script

Note the following important changes to the test script:

Next Task

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.

  


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