金宝app®Coder™generates standalone C and C++ code from Simulink models for deployment in a wide variety of applications.For a list of DSP System Toolbox™ features supported bySimulink Coder, seeBlocks Supported for C Code Generation.
This example generates C code from theex_codegen_dsp
model and builds an executable from the generated code. You can run the executable inside the MATLAB®environment. In addition, you can package and relocate the code to another development environment that does not have the MATLAB and Simulink products installed.
Theex_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 the following command in MATLAB command prompt:
open_system('ex_codegen_dsp')
You can alternatively create the model using theDSP Systemtemplate. For more information, seeConfigure the Simulink Environment for Signal Processing Models.
Prepare the model for code generation by specifying code generation settings in theConfiguration Parametersdialog box. Choose the appropriate solver and code generation target, and check the model configuration for execution efficiency. For more details on each of these steps, seeGenerate C Code for a Model(Simulink Coder).
Simulate the model. TheTime Scopeshows the input and filtered signal characteristics.
TheArray Plotshows the last 32 filter weights for which the LMS filter has effectively adapted and filtered out the noise from the signal.
These coefficients can also be accessed using the following command:
filter_wts(:,:,1201)
Before you generate code from the model, you must first ensure that you have write permission in your current folder.
To generate code, you must make the following changes:
In theModelingtab of the model toolstrip, clickModel Settings. TheConfiguration Parametersdialog opens. Navigate to theCode Generationtab, select theGenerate code onlyparameter, and clickApply.
In the Apps gallery, clickSimulink Coder. TheC Codetab appears. Click theGenerate Codeicon ().
After the model finishes generating code, theCode Generation Reportappears, allowing you to inspect the generated code. Note that the build process creates a new subfolder calledex_codegen_dsp_grt_rtw
in your current MATLAB working folder. This subfolder contains all the files created by the code generation process, including those that contain the generated C source code. For more information on viewing the generated code, seeGenerate C Code for a Model(Simulink Coder).
To build an executable, you must set up a supported C compiler. For a list of compilers supported in the current release, seeSupported and Compatible Compilers.
To set up your compiler, run the following command in the MATLAB command prompt:
mex–setup
After your compiler is setup, you can build and run the compiled code. Theex_codegen_dspmodel is currently configured to generate code only. To build the generated code, you must first make the following changes:
In theModelingtab of the model toolstrip, clickModel Settings. TheConfiguration Parametersdialog opens. Navigate to theCode Generationtab, clear theGenerate code onlyparameter, and clickApply.
In theC Codetab of the model toolstrip, click theBuildicon ().
The code generator builds the executable and generates theCode Generation Report. The code generator places the executable in the working folder. On Windows, the executable isex_codegen_dsp.exe
. On Linux, the executable isex_codegen_dsp
.
To run the generated code, enter the following command in the MATLAB command prompt:
!ex_codegen_dsp
运行生成的代码创建了一个MAT-filecontains the same variables as those generated by simulating the model. The variables in the MAT-file are named with a prefix ofrt_
. After you run the generated code, you can load the variables from the MAT-file by typing the following command at the MATLAB prompt:
loadex_codegen_dsp.mat
你现在可以比较变量从背包ed code with the variables from the model simulation. To access the last set of coefficients from the generated code, enter the following in the MATLAB prompt:
rt_filter_wts(:,:,1201)
Note that the coefficients infilter_wts(:,:,1201)
andrt_filter_wts(:,:,1201)
match.
For more details on building and running the executable, seeGenerate C Code for a Model(Simulink Coder).
Once you generate code from your Simulink model, you can relocate the code to another development environment using the pack-and-go utility. Use this utility when the development environment does not have the MATLAB and Simulink products.
The pack-and-go utility uses the tools for customizing the build process after code generation and apackNGo
(Simulink Coder)function to find and package files for building an executable image. The files are packaged in a compressed file that you can relocate and unpack using a standard zip utility.
You can package the code by either using the user interface or by using the command-line interface. The command-line interface provides more control over the details of code packaging. For more information on each of these methods, seeRelocate Code to Another Development Environment(Simulink Coder).
For an example on how to package the C code and executable generated from this example, seeRelocate Code Generated from a Simulink Model to Another Development Environment.