Products & Services Solutions Academia Support User Community Company

Modifying the Filter to Use Streaming

What Is Streaming?

A streaming filter is called repeatedly to process fixed-size chunks of input data, or frames, until it has processed the entire input signal. The frame size can be as small as a single sample, in which case the filter would be operating in a sample-based mode, or up to a few thousand samples, for frame-based processing.

Why Use Streaming?

The design of the filter algorithm in Modifying the Filter to Accept a Variable Length Signal has the following disadvantages:

In an embedded application, the filter is likely to be processing a continuous input stream. As a result, the input signal may be substantially longer than the maximum length that a filter working in batch mode could possibly handle. To make the filter work for any signal length, it must run in real time. One solution is to convert the filter from batch-based processing to stream-based processing.

Modifying Your M-Code

The conversion to streaming involves:

The modified filter code is in emldemo_lms_04.m. The changes to the code are in bold:

function [ signal_out, err, weights_out ] = ...
  emldemo_lms_04(distorted, desired)            %#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 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) ;
      weights = weights + mu*err(n)*fifo;
    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_04_mex and then emldemo_lms_build_04_lib at the MATLAB prompt.

Summary of Changes to the Build Scripts

The build files contain a new parameter N, which sets up the frame size. In this case, the build script uses a value of N=2048 samples per frame for the frame size.

How to Test Your MEX Function

Follow the same procedure you used in Using the Test Script with the file emldemo_lms_test_04. The test outputs show that the modified filter is performing in exactly the same way as the original filter. The modifications for converting the filter from batch-based processing to stream-based processing generate Embedded MATLAB compliant code that is functionally identical to the original M-code.

Summary of Changes to the Test Script

Note the following changes to the test script:

Testing the Function with Other Audio Signals

Because the filter operates in a stream-based mode, it can process any audio signal regardless of the number of samples the signal contains. You can modify emldemo_lms_test_04.m to load a different audio file such as laughter.mat (on the MATLAB path) instead of handel.mat. MATLAB includes a total of six audio files that you can use as examples to test the LMS function. To see the complete list, use the following command at the MATLAB command line:

ls ([matlabroot '\toolbox\matlab\audiovideo\*.mat'])
  


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