Main Content

从中生成C代码金宝appModel

金宝app®CODER™从Simulink模型中生成独立的C和C ++代码,以在各种应用程序中部署。金宝appFor a list of DSP System Toolbox™ features supported by金宝appSimulink编码器, 看Blocks Supported for C Code Generation

This example generates C code from theex_codegen_dsp模型并从生成的代码中构建可执行文件。您可以在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

ex_codegen_dsp模型实现了一个简单的自适应过滤器,以从信号中删除噪声,同时识别表征噪声频率内容的过滤器。要打开此模型,请在MATLAB命令提示中输入以下命令:

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

您可以使用DSP系统template. For more information, seeConfigure the Simulink Environment for Signal Processing Models

配置代码生成模型

通过在“代码生成”模型中指定代码生成设置配置参数对话框。选择适当的求解器和代码生成目标,并检查模型配置是否有执行效率。有关这些步骤中每个步骤的更多详细信息,请参见Generate C Code for a Model(金宝appSimulink编码器)

Simulate the Model

Simulate the model. The时间范围显示输入和过滤的信号特性。

时间范围块的输出。第一个信号是原始的正弦信号。第二个信号是频率低的正弦信号。第三个信号是输入信号的近似值。第四个信号是归一化LMS滤波器块生成的误差信号。

Array Plot显示了最后32个滤波器的重量,其LMS滤波器已有效地调整并过滤了信号中的噪声。

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

这se 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.

要生成代码,您必须进行以下更改:

  1. 在里面造型tab of the model toolstrip, click模型设置。这配置参数dialog opens. Navigate to the代码生成选项卡,选择仅生成代码参数,然后单击申请

  2. 在里面应用模型工具条的选项卡,单击drop-down arrow. Under代码生成, click金宝appSimulink编码器。这C Code选项卡出现在模型窗口中。在里面C Codetab, click theGenerate Code图标 ()。

After the model finishes generating code, the代码生成Report看起来,允许您检查生成的代码。请注意,构建过程创建了一个新的子文件夹ex_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(金宝appSimulink编码器)

Build and Run the Generated Code

设置C/C ++编译器

要构建可执行文件,您必须设置一个支持的C编译器。金宝app有关当前版本中支持的编译器列表,请参见金宝appSupported and Compatible Compilers

To set up your compiler, run the following command in the MATLAB command prompt:

mex-设置

构建生成的代码

编译器设置后,您可以构建并运行编译的代码。这ex_codegen_dspmodel is currently configured to generate code only. To build the generated code, you must first make the following changes:

  1. 在里面造型tab of the model toolstrip, click模型设置。这配置参数dialog opens. Navigate to the代码生成标签,清除仅生成代码参数,然后单击申请

  2. 在里面C Code模型工具条的选项卡,单击Build图标 ()。

代码生成器构建可执行文件并生成代码生成Report。代码生成器将可执行文件放置在工作文件夹中。在Windows上,可执行文件是ex_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_。运行生成的代码后,您可以通过在MATLAB提示符下键入以下命令来加载来自MAT文件的变量:

加载ex_codegen_dsp.mat

现在,您可以将生成代码中的变量与模型仿真的变量进行比较。要从生成的代码访问最后一组系数,请在MATLAB提示中输入以下内容:

RT_filter_wts(:,:,1201)

Note that the coefficients infilter_wts(:,:,1201)andRT_filter_wts(:,:,1201)匹配。

有关构建和运行可执行文件的更多详细信息,请参见Generate C Code for a Model(金宝appSimulink编码器)

Relocate Code to Another Development Environment

从Simulink模型中生成代码后,您可以使用包装实用程序将代码重金宝app置为另一个开发环境。当开发环境没有MATLAB和SIMULINK产品时,请使用此实用程序。下载188bet金宝搏金宝app

这pack-and-go utility uses the tools for customizing the build process after code generation and aPackngo(金宝appSimulink编码器)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 or Share Generated Code(金宝appSimulink编码器)

有关如何包装C代码和从此示例生成的可执行文件的示例,请参见将生成的代码从simulink模型重新定位到另一个开发环境金宝app

Related Topics

外部网站