|
Part 2 discusses the process of integrating Embedded MATLAB code into Simulink models as part of a Model-Based Design workflow.
Many embedded developers are familiar with the idea-to-implementation gap. Algorithm developers often create algorithm concepts in MATLAB due to its language features, extensive function libraries, and flexibility.
As the design evolves toward embedded implementation, real-world constraints must be incorporated, which typically requires the user to manually translate MATLAB algorithms into C code. Manual translation involves rewriting the convenient MATLAB syntax for matrices into C implementations. The end result is multiple copies of the same algorithm written in different languages. At this stage, the user faces the burden of verifying that these copies remain equivalent throughout multiple design iterations. In this workflow the cost of verifying revisions quickly becomes prohibitive, resulting in a design that either solidifies too quickly or simply diverges from the original specification.
Recently The MathWorks, creator of MATLAB, introduced new tools that directly address this issue. These tools can automatically convert a well-defined subset of MATLAB language, called Embedded MATLAB, into embeddable C code. This technology can reduce the development and verification cost of manual translation from MATLAB to C. The Embedded MATLAB language subset supports more than 270 MATLAB operators and functions and 90 Fixed-Point Toolbox functions.
Working within the Embedded MATLAB subset, you can maintain one copy of the design—one "truth"—and elaborate it directly within MATLAB to incorporate embedded implementation requirements. Design iterations become easier because the Embedded MATLAB algorithm is still MATLAB code, and you retain the interactive debugging and visualization capabilities in MATLAB. This approach provides the algorithm domain expert and the embedded software engineer a common language and shared understanding of the design intent. You can automatically generate C code from the Embedded MATLAB code, eliminating the cost of producing and verifying hand-written C code.
In this article, we first review features of MATLAB that are useful at the early stages of the design process. We then examine the inefficiencies associated with manually translating MATLAB "concept" code into C code for implementation. We then present an alternative workflow, where all the embedded elaborations to the algorithm are done in MATLAB instead of C. Using an example, we highlight the steps necessary to make an algorithm compliant with the Embedded MATLAB subset and show the use of new tools that automatically translate an Embedded MATLAB code into embeddable C code.
The table below compares MATLAB to C-code with respect to features relevant in the early phases of design.

(Click to enlarge)
Table 1.
Converting a typical MATLAB algorithm into embeddable C code involves accommodating several implementation requirements:
- Data Type Management—Data types must be determined before implementation. For example, the pixel values in image processing are often represented as 8-bit unsigned integers, and samples in speech processing are represented as 16-bit signed integers. The use of default 64-bit double precision variables in MATLAB is not memory efficient.
- Static Memory Allocation—MATLAB seamlessly handles dynamically changing variable sizes at run time. In embedded applications, however, we usually avoid dynamic memory allocation and statically define memory for a given size and data type before using it.
- Reduction of Computational Complexity and Memory Footprint—Embedded software designers spend a lot of time mapping high-level algorithms to operate efficiently within the limited memory and computational resources of the target hardware. This effort results in tuning the design to the instruction set and data representation of the target processor.
- Fixed-Point Support—Implementation in embedded software or hardware may require that the algorithm be completely specified with fixed-point data types.
Engineers typically perform these modifications by first translating the MATLAB algorithm into C code, which creates the design gap mentioned above. During this translation, the software engineer may introduce errors or numerical changes into the C code. If those changes are the intentional result of code optimizations, the algorithm designer may need to reproduce them in the MATLAB algorithm to maintain equivalence. This process adds unnecessary work and potential for errors. A workflow for embedded implementation based on Embedded MATLAB addresses these issues.
Unlike many MATLAB algorithms, Embedded MATLAB code is not an abstract mathematical representation of the design. It contains all the details needed for an efficient, embeddable C implementation. Any MATLAB code that complies with the Embedded MATLAB subset can generate embeddable C code. The process of ensuring compliance with the Embedded MATLAB subset involves the same four implementation requirements discussed previously. With Embedded MATLAB, the implementation constraints are specified directly in the MATLAB code.
Applying the implementation requirements lets you:
- Maintain one copy of your design in MATLAB
- Elaborate the design from a concept form to an implementation-ready form by incorporating embedded design constraints directly in MATLAB
- Iterate, test, and debug the code using the visualization and analysis capabilities available in the MATLAB environment
- Verify the functional correctness of the elaborated design
- Automatically generate embeddable C code using Real-Time Workshop
|