Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Understanding C Code Generation in DSP System Toolbox

Generate C and C++ code from signal processing algorithms in DSP System Toolbox™ using the MATLAB® Coder™ and Simulink® Coder products. You can integrate the generated code into your projects as source code, static libraries, dynamic libraries, or even as standalone executables. You can also generate code optimized for ARM® Cortex®-M and ARM Cortex-A processors using the Embedded Coder® product.

Generate C and C++ code from MATLAB code

Using the MATLAB Coder, you can generate highly optimized ANSI C and C++ code from functions and System objects in DSP System Toolbox. For a list of functions and System objects that support code generation, see Functions and System Objects Supported for C Code Generation. You can use either the MATLAB Coder app or the codegen function to generate code according to the build type you choose. When the build type is one of the following:

  • Source Code –– Generate C source code to integrate with an external project.

  • MEX Code –– Generate a MEX function to run inside MATLAB using the default configuration parameters.

  • Static library (.lib) –– Generate a binary library for static linking with another project.

  • Dynamic library (.dll) –– Generate a binary library for dynamic linking with an external project.

  • Executable –– Generate a standalone program (requires a separate main file written in C or C++).

If you use build scripts to specify input parameter types and code generation options, use the codegen function.

For an example that illustrates the code generation workflow using the codegen function, see Generate C Code from MATLAB Code. For detailed information on each of the code generation steps, see C Code Generation Using the MATLAB Coder App (MATLAB Coder) and C Code Generation at the Command Line (MATLAB Coder).

In order to improve the execution speed and memory usage of generated code, MATLAB Coder has several optimization options. For more details, see MATLAB Coder Optimizations in Generated Code (MATLAB Coder).

Generate C and C++ Code from a Simulink Model

Using the Simulink Coder, you can generate highly optimized ANSI C and C++ code from Simulink blocks in DSP System Toolbox. For a list of blocks that support code generation, open the Simulink block data type support table for DSP System Toolbox. To access this table, type the following command in the MATLAB command prompt:

showsignalblockdatatypetable 

The blocks with 'X' under 'Code Generation Support' column support code generation.

You can generate code from your Simulink model, build an executable, and even run the executable within MATLAB. For an example, see Generate C Code from Simulink Model.

For detailed information on each of the code generation steps, see Generate C Code for a Model (Simulink Coder).

Generated ANSI C Code Optimizations

The generated C code is often suitable for embedded applications and includes the following optimizations:

  • Function reuse (run-time libraries) — Reuse of common algorithmic functions via calls to shared utility functions. Shared utility functions are highly optimized ANSI/ISO C functions that implement core algorithms such as FFT and convolution.

  • Parameter reuse (Simulink Coder run-time parameters) — Multiple instances of a block that have the same value for a specific parameter point to the same variable in the generated code. This process reduces memory requirements.

  • Blocks have parameters that affect code optimization — Some blocks, such as the Sine Wave block, have parameters that enable you to optimize the simulation for memory or for speed. These optimizations also apply to code generation.

  • Other optimizations — Use of contiguous input and output arrays, reusable inputs, overwritable arrays, and inlined algorithms provide smaller generated C code that is more efficient at run time.

Shared Library Dependencies

In most cases, the C/C++ code you generate from DSP System Toolbox objects and blocks is portable. After you generate the code, using the pack-and-go utility, you can package and relocate the code to another development environment that does not have MATLAB and Simulink installed. For examples, see Relocate Code Generated from MATLAB Code to Another Development Environment and Relocate Code Generated from a Simulink Model to Another Development Environment.

There are a few DSP System Toolbox features that generate code with limited portability. The executables generated from these features rely on prebuilt dynamic library files (.dll files) included with MATLAB. You must include these .dll files when you run the corresponding executables on the external environment. For a list of such objects and blocks and for information on how to run those executables outside MATLAB, see How To Run a Generated Executable Outside MATLAB.

Both Simulink Coder and MATLAB Coder provide functions to help you set up and manage the build information for your models. For example, one of the functions that Simulink Coder provides, getNonBuildFiles, allows you to identify the shared libraries required by the blocks in your model. If your model contains any blocks that use precompiled shared libraries, you can install those libraries on the target system. The folder that you install the shared libraries in must be on the system path. The target system does not need to have MATLAB installed, but it does need to be supported by MATLAB. For additional information, see Build Process Customization (Simulink Coder). The function getNonBuildFiles can also apply to MATLAB algorithms. For more information, see Customize the Post-Code-Generation Build Process (MATLAB Coder).

Generate C Code for ARM Cortex-A DST and ARM Cortex-M DST Processors

The DSP System Toolbox supports optimized C code generation for popular algorithms like FIR filtering and FFT on ARM Cortex-M and ARM Cortex-A Processors.

For more information on the support packages and instructions for downloading them, see ARM Cortex-M and ARM Cortex-A Optimization.

Generate Code for Mobile Devices

Using Simulink Support Package for Apple iOS Devices, you can create and run Simulink models on the iPhone, iPod Touch, and iPad. You can also monitor and tune the algorithms running on the Apple devices. For an example, see Array Plot with Apple iOS Devices.

Using Simulink Support Package for Android™ Devices, you can create and run Simulink models on supported Android devices. For an example, see Array Plot with Android Devices.

See Also

Functions

Related Topics

External Websites

Was this topic helpful?