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.
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 in DSP System Toolbox that Support C Code Generation. You
can use either the MATLAB
Coder app or the
codegen (MATLAB Coder) 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
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 Generate C Code by Using the MATLAB Coder App (MATLAB Coder) and Generate C Code 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).
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, see Simulink Blocks in DSP System Toolbox that Support C Code Generation. Alternatively, you can find this data in the Simulink block data type support table for DSP System Toolbox. To access this table, type the following command in the MATLAB command prompt:
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).
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.
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.
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
getNonBuildFiles (Simulink Coder), 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 Build Process Customization (MATLAB Coder).
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. To generate this optimized code, you must install the Embedded Coder Support Package for ARM Cortex-M Processors or Embedded Coder Support Package for ARM Cortex-A Processors. In addition, you must have the following products: DSP System Toolbox, MATLAB Coder, Embedded Coder, Simulink and Simulink Coder for Simulink based workflows.
Using these Embedded Coder support packages, you can generate C code that can link with the CMSIS library or calls the Ne10 library functions. This generated code can be compiled to provide optimized executables that run on ARM Cortex-M or ARM Cortex-A processors.
You can also port the generated ARM Cortex-M CRL code from MATLAB to KEIL μVision IDE and IAR Embedded Workbench. For details, see Port the Generated ARM Cortex-M CRL Code from MATLAB to KEIL μVision IDE and Port the Generated ARM Cortex-M CRL Code from MATLAB to IAR Embedded Workbench.
To download the Embedded Coder support packages for the ARM Cortex processors, see https://www.mathworks.com/hardware-support.html.
For more information on the support packages and instructions for downloading them, see Embedded Coder Support Package for ARM Cortex-M Processors and Embedded Coder Support Package for ARM Cortex-A Processors.
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 (Simulink Support Package for 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.