Main Content

Generate C Code from金宝appModel

金宝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_dspmodel 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.

Open the Model

Theex_codegen_dspmodel 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')

Snapshot of model diagram. There are two input blocks, Sine Wave block and the Random Source block. The output of the Sine Wave block is fed as the first input to the Time Scope block. The output of the Random Source block is fed to the Input signal port of the Normalized LMS Filter block. The same output is also lowpass filtered using a Lowpass Filter block. This lowpass filtered output is fed to the desired signal port of the LMS Filter block. The noisy lowpass filtered signal is added to the output of the Sine Wave and is fed to the second input port of the Time Scope block. The output of the Normalized LMS block subtracts from the noisy Sine Wave Signal and feeds the third input of the Time Scope block. The Error signal of the Normalized LMS block feeds the fourth input of the Time Scope block. The Weights computed by the LMS Filter block feed to the Array Plot block and stored to the workspace as the

You can alternatively create the model using theDSP Systemtemplate. For more information, seeConfigure the Simulink Environment for Signal Processing Models.

Configure Model for Code Generation

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

Simulate the model. TheTime Scopeshows the input and filtered signal characteristics.

Output of the Time Scope block. The first signal is the original sinusoidal signal. The second signal is the sinusoidal signal with low frequency noise. The third signal is the approximation of the input signal. The fourth signal is the error signal generated by the Normalized LMS filter block.

TheArray Plotshows the last 32 filter weights for which the LMS filter has effectively adapted and filtered out the noise from the signal.

The y-axis is labelled as Filter Weights and ranges from -0.2 to 0.6, x-axis is labelled as Number of Weights and ranges from 0 to 32.

These coefficients can also be accessed using the following command:

filter_wts(:,:,1201)

Generate Code from the Model

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:

  1. In theModelingtab of the model toolstrip, clickModel Settings. TheConfiguration Parametersdialog opens. Navigate to theCode Generationtab, select theGenerate code onlyparameter, and clickApply.

  2. 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_rtwin 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).

Build and Run the Generated Code

Set Up the C/C++ Compiler

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

Build the Generated Code

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:

  1. In theModelingtab of the model toolstrip, clickModel Settings. TheConfiguration Parametersdialog opens. Navigate to theCode Generationtab, clear theGenerate code onlyparameter, and clickApply.

  2. 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.

Run the Generated Code

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).

Relocate Code to Another Development Environment

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.

Related Topics

External Websites