Guy and Seth on Simulink

Introducing the System Toolboxes 5

Posted by Guy Rouleau,

This week I am teaming up with my colleague Ken Karnofsky to introduce another big change in MATLAB R2011a.

Guy: So Ken, what is this big change?

Ken: In MATLAB R2011a, not only the code generation products changed. The products for designing signal processing algorithms have also been reorganized. Here is a visual summary what’s happening:

Changes in product names in MATLAB R2010a

Guy: What is a System Toolbox?

Ken: Last year, we introduced a new technology in several blocksets called System objects. System objects are pre-defined algorithms for MATLAB with functionality that had previously been available only to Simulink users, such as fast stream processing, implicit state handling, design options such as fixed-point, and code generation support.

With the System Toolboxes, you can design signal processing algorithms suitable for fast simulation, real-time prototyping, and embedded implementation. Any MATLAB algorithm that you design for code generation, including the use of System objects, is reusable in Simulink using the new MATLAB function block.

Guy: That sounds pretty cool. Let's see how we can identify a system using an LMS adaptive filter, and implement it in 3 different ways:

MATLAB script

In this example, I create a dsp.LMSFilter object and a dsp.DigitalFilter object. Then I use the step method to process data using the algorithm defined by each objects. Executing step 10 times in a loop is similar to running a Simulink model for 10 time steps.

% Create adaptive filter object
hlms = dsp.LMSFilter(11, 'StepSize', 0.01);
% Create system to be identified... an FIR filter.
hfilt = dsp.DigitalFilter('TransferFunction', 'FIR (all zeros)', ...
    'Numerator', fir1(10, .25));

% Run 10 iterations of filter adaptation
for i = 1:10
    % Input to the system
    x = randn(100,1);
    % We measure some output to the system, including noise
    d = step(hfilt, x) + 0.01*randn(100,1);
    % Now let's run our adaptive filter
    [y,~,w] = step(hlms, x, d);
    % compare the measured and identified outputs
    diff(i) = sqrt(sum((y-d).^2));
end


After writing this script, I can see advantages of using System objects. After I initialize the object, all I have to do is call the step method in a loop to process data. The implementation of the step method is very efficient because it does not include the same overhead normally required for MATLAB functional programming.

If your goal is to target an embedded processor, you can use MATLAB Coder and generate C code directly from this algorithm. If you want to integrate this algorithm into a Simulink model...

MATLAB Function Block

With the MATLAB Function block and System objects, it is easy to bring an algorithm created in MATLAB into Simulink. To implement the same adaptive filter example, I made the following model:

Adaptive LMS filter implemented using System objects in MATLAB Function Block

Then I define the FIR Filter and Adaptive Filter in the MATLAB Function block. I initialize the System objects on the first time step of the model and declare them as persistent so they can keep their states between each time step.

Adaptive LMS filter implemented using System objects in MATLAB Function Block

Of course it is possible to combine many System objects inside one MATLAB Function block and create more complex components.

Simulink blocks

For users who prefer just connecting blocks without typing code, don't worry! The System Toolboxes include ready to use Simulink libraries. The blocks in the Simulink libraries use the same System objects under the hood. For this example, here is how my model looks implemented using blocks from the DSP System Toolbox:

Adaptive LMS filter implemented using Simulink blocks

Now it’s your turn

Are you going to try the new System objects in your MATLAB code? Are you using MATLAB algorithms in your Simulink models? Let us know by leaving a comment here.

5 CommentsOldest to Newest

It is great the simulink can use Matlab code by Matlab function block!
Since Matlab can take advantage of GPU directly (I do not think that simulink can at this moment),
Can I build my matlab algrithm with Matlab GUP or Jacket GUP capability, and Matlab function block, so that my Simulink can take advantage of GPU?

Cheers!
Chen

Chen,

GPU support in MATLAB is provided in Parallel Computing Toolbox. MATLAB Coder does not directly support GPU code generation; neither does the MATLAB Function block in Simulink.

One option is to use the coder.extrinsic construct. When you declare a function to be extrinsic, it executes in MATLAB, rather than in the generated code, so it could include GPU statements. However, the overhead may diminish the benefit of using the GPU.

I use FDATool created a 8 order low-pass butterworth filter model, the sampling frequency is 80 kHz, the cut-off frequency is533 Hz, this model meet the need to output, but according to the model to build the same function model with DSP Builder, the result of the DSP Builder model is not consistent with the FDATool model, the parameters and module function in DSP Builder model using are consistent with FDATool, please give the solutions, what is caused the problem? Thanks a lot.

Dear Ken,

Does the Fixed Point Toolbox already supports the backslash operation used for estimation? Cheers.

Gabriel Costa

Add A Comment

What is 5 + 6?

Preview: hide

These postings are the author's and don't necessarily represent the opinions of MathWorks.