C Code Generation from Simulink

    Note:   You must have both the DSP System Toolbox™ and Simulink® Coder™ products installed on your computer to complete the procedures in this section.

Open and Run the Model

The ex_codegen_dsp model implements a simple adaptive filter to remove noise from a signal while simultaneously identifying a filter that characterizes the noise frequency content. To open this model, enter

open_system('ex_codegen_dsp')

Run the model and observe the output in both scopes. This model saves the filter weights each time they adapt. You can plot the last set of coefficients using the following command:

plot(filter_wts(:,:,1201))

Generate Code from the Model

To generate code from the model, you must first ensure that you have write permission in your current folder. The code generation process creates a new subfolder inside the current MATLAB® working folder. MATLAB saves all of the files created by the code generation process in that subfolder, including those which contain the generated C source code.

To start the code generation process, click the Build Model icon ( ) on your model toolbar. After the model finishes generating code, the Code Generation Report appears, allowing you to inspect the generated code. You may also notice that the build process created a new subfolder inside of your current MATLAB working folder. The name of this folder consists of the model name, followed by the suffix _grt_rtw. In the case of this example, the subfolder that contains the generated C source code is named ex_codegen_dsp_grt_rtw.

Build and Run the Generated Code

Setup the C/C++ Compiler

If you want to build and run the generated code, you need to have access to a C compiler. For more information about which compilers are supported in the current release, see http://www.mathworks.com/support/compilers/current_release/.

To setup your compiler, run the following command:

mex -setup

Build the Generated Code

After your compiler is setup, you can build and run the generated code. The ex_codegen_dspex_codegen_dsp model is currently configured to generate code only. To build the generated code, you must first make the following changes:

  1. Open the Model Configuration Parameters dialog, navigate to the Code Generation tab, and clear the Generate Code Only checkbox.

  2. Click OK to apply your changes and close the dialog box.

  3. From the model toolbar, click the Build Model icon ( ).

Because you re-configured the model to generate and build code, the code generation process continues until the code is compiled and linked.

Run the Generated Code

To run the generated code, enter the following command at the MATLAB prompt:

!ex_codegen_dsp

Running the generated code creates a MAT-file which contains the same variables as those generated by simulating the model. The variables in the MAT-file are named with a prefix of rt_. After you run the generated code, you can load the variables from the MAT-file by typing the following command at the MATLAB prompt:

load ex_codegen_dsp.mat

You can now compare the variables from the generated code with the variables from the model simulation. To plot the last set of coefficients from the generated code, enter the following command at the MATLAB prompt:

plot(rt_filter_wts(:,:,1201))

The last set of coefficients from the generated code are shown in the following figure.

For further information on generating code from Simulink, see the Simulink Coder documentation.

Was this topic helpful?