Products & Services Solutions Academia Support User Community Company

Integrating with Simulink

Introduction

This part of the tutorial demonstrates how to integrate M-code into Simulink using the Embedded MATLAB Function block. The Embedded MATLAB Function block allows you to add MATLAB functions to Simulink models for simulation, testing, code generation, and/or deployment to embedded processors. It supports a subset of MATLAB commands that generate efficient C code.

Files Supplied with This Tutorial

To begin the process of integrating your M-code with Simulink, you work with the model emldemo_noise_cancel_01.mdl and the file emldemo_lms_05.m. Alternatively, you can view the changes described in this part of the tutorial in the model emldemo_noise_cancel_02.mdl.

The file emldemo_lms_05.m implements a least mean squares (LMS) filter. The models emldemo_noise_cancel_01.mdl and emldemo_noise_cancel_02.mdl use this filter to remove noise from an input signal. For more information, see Example: The LMS Filter.

Adding an Embedded MATLAB Function Block to Your Model

In practice, you would add an Embedded MATLAB Function Block to an empty model, and then add input and output blocks as required. To simplify this process for the purposes of this tutorial, you add the Embedded MATLAB Function Block to the emldemo_noise_cancel_01 model supplied with the tutorial.

To add an Embedded MATLAB Function block to the emldemo_noise_cancel_01 model:

  1. Open emldemo_noise_cancel_01.mdl in Simulink:

    1. Set your MATLAB current folder to the folder that contains your files for this tutorial. At the MATLAB command line, enter:

      cd folder

      where folder is the full path name of the folder containing your files. See Viewing and Changing the Current Folder Using the Current Folder Browser in the MATLAB Desktop Tools and Development Environment documentation for more information.

    2. At the MATLAB command line, type:

      emldemo_noise_cancel_01

      The model opens:

  2. Add an Embedded MATLAB Function block to the model:

    1. At the MATLAB command line, type simulink to open the Simulink library browser.

    2. From the list of Simulink libraries, select the User-Defined Functions library.

    3. Click the Embedded MATLAB Function block and drag it into the emldemo_noise_cancel_01 model. Place the block just above the red text annotation Place Embedded MATLAB Function Block here.

      Your model should now look like this:

    4. Delete the red text annotations from the model.

    5. Save the model in the current folder as my_emldemo_noise_cancel_01.mdl.

      1. In the Simulink Model Editor, select Save As from the File menu.

      2. Enter my_emldemo_noise_cancel_01.mdl as the new model name.

      3. Click Save.

       Best Practice — Saving Incremental Code Updates

Calling Your M-Code from the Embedded MATLAB Function Block

You now use the Embedded MATLAB Function block to call your existing function emldemo_lms_05. To add your M-code to the Embedded MATLAB Function block:

  1. Double-click the Embedded MATLAB Function block to open the Embedded MATLAB editor.

  2. Delete the default code displayed in the Embedded MATLAB editor.

  3. Copy the following code to the Embedded MATLAB Function block.

    function [ Signal_Out, Weights ] = ...
      LMS(Adapt, Reset, Noise_In, Signal_In)  %#eml
    
      % Compute LMS:
      [ ~, Signal_Out, Weights ] = ... 
        emldemo_lms_05(Noise_In, Signal_In, Reset, Adapt);
    
    end
    

    Because the LMS function does not use the first output from emldemo_lms_05, replace this output with the MATLAB ~ operator. Embedded MATLAB ignores inputs and outputs specified by ~. This syntax helps avoid confusion in your program code and unnecessary clutter in your workspace.

     Using eml.extrinsic to Debug Your M-code

  4. Resize the Embedded MATLAB Function block. The emldemo_lms_05 function inputs Noise_In, Signal_In, Reset and Adapt now appear as input ports to the block and the function outputs Signal_Out and Weights appear as output ports.

  5. Save the model by selecting File > Save from the Embedded MATLAB editor menu.

Connecting the Embedded MATLAB Function Block Inputs and Outputs

The next step is to connect the Embedded MATLAB function block inputs and outputs.

Make the following connections:

From...To...
Settings > AdaptEmbedded MATLAB Function Block > Adapt
Settings > ResetEmbedded MATLAB Function Block > Reset
White NoiseEmbedded MATLAB Function Block > Noise_In
Acoustic_Environment > Signal_OutEmbedded MATLAB Function Block > Signal_In
Embedded MATLAB Function Block > Signal_OutTo Audio Device
Embedded MATLAB Function Block > Signal_OutAnalysis and Visualization > Signal
Embedded MATLAB Function Block > WeightsAnalysis and Visualization > Weights

See Connecting Blocks in the Simulink documentation for more information.

Your model should now look like this:

Configuring Embedded MATLAB Outputs

Because the Analysis and Visualization block expects to receive a frame-based signal from the LMS filter, you must configure the Embedded MATLAB Function block Signal_Out output parameter to be frame-based rather than sample-based.

  1. Double-click the Embedded MATLAB Function block to open the Embedded MATLAB editor.

  2. Select Tools > Edit Data/Ports to open the Ports and Data Manager.

  3. Select the signal called Signal_Out from the list in the left pane.

  4. On the General tab, change the Sampling mode from Sample based to Frame based.

    Click the Apply button.

  5. Close the Ports and Data Manager and the Embedded MATLAB editor.

  6. Save the model.

You are now ready to run the model.

Running the emldemo_noise_cancel_01 Model

To run the model:

  1. Ensure that you can see the Time Domain plots.

    To view the plots, in the emldemo_noise_cancel_01 model, open the Analysis and Visualization block and then open the Time Domain block.

  2. Select Simulation > Start in the Simulink model window.

    While the model is running, you can double-click the Reset and Adapt settings to control the operation of the filter. 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.

    For standard operation, you should enable the Adapt signal and disable the Reset signal.

    As Simulink runs the model, you see and hear outputs. Initially, you hear the audio signal distorted by noise. Then, during the first few seconds, the filter attenuates the noise gradually, until you hear only the music playing with very little noise remaining. MATLAB displays the following plot showing filter convergence after only a few seconds.

Next Steps

To...See...

Learn more about using the Embedded MATLAB Function block

Using the Embedded MATLAB Function Block in the Simulink User's Guide

Learn more about the best practices used in this tutorial

Best Practices for Working with the Embedded MATLAB Subset

  


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