Newsletter

DSP DesignLine  >  Design Center

MATLAB-to-C translation, part 3: Code generation and verification

Part 3 of this 3-part series examines the verification process and makes the case for automatic C generation.

Page 1 of 4

DSP DesignLine

Part 2 looks at complex functions and the role C interface constraints play in the translation process.

In the first two parts of this series, we navigated the waters of MATLAB to C translation and encountered some of the Charybdis and Scylla monsters that await the unsuspecting engineer along the way. By now, it should be plain indeed, that translating MATLAB to C, short of being a Homeric Odyssey, is anything but an easy task.

In this third and last part, we will explore verification and emphasize how automatic C code generation solves most of the issues described so far. We will illustrate this approach with Catalytic MCS (MATLAB to C Synthesis).

Making sure the C is correct
Verifying that the generated C code matches the original MATLAB code is both extremely important and challenging.

Thankfully, various techniques exist for such verification. A popular albeit tedious method consists of using files to pass input and output data, and comparing the results between the MATLAB code and C code. A more systematic method consists of turning the generated C code into a compiled MEX-file that can be called directly from MATLAB like any other function.

The mex command can be used from the MATLAB command line to compile C code into a MEX-file. The mex command uses the MEX API, which provides functions to pass data between your C code and MATLAB. The MEX-file guide provides more information on how to write your own MEX-files.

Turning your C model into a MEX-file allows you to rerun your original MATLAB simulation with no change and ensure the results are identical. However, you need to write a MEX wrapper function, which passes all the data you need to exchange between MATLAB and C. The wrapper uses arcane functions provided by the MEX-API. This task is also error prone—crashes, segmentation violations or incorrect results can easily occur if the MEX wrapper does not allocate and access the data properly.

The case for automation
Automating the generation of C code from the MATLAB file can help overcome these problems. Conceptually, an automated MATLAB-to-C converter offers the following advantages:

  • The generated code is correct by construction
  • The MATLAB and C code are always in-sync
  • You can spend more time in MATLAB developing and tuning your algorithms rather than writing and debugging low-level C-code
  • Updates to the algorithms result in automatic, immediate update of the C code

However, automatic translation of MATLAB code to C code raises a few important questions:

  • How powerful is the set of MATLAB commands that can be translated to C? The solution loses much of its appeal if it requires the algorithm designer to restrict his style drastically. The designer must then heavily rewrite his MATLAB code, a time-consuming and error prone process. Furthermore, the code loses a lot of its flexibility.
  • How efficient is the generated code? Are advanced techniques for memory management and C-code optimization available?
  • Does the generated C-code easily interface with the existing code for your application?
  • How readable is the resulting C code? Is the C-code understandable and maintainable? Does the generator retain the structure of the original MATLAB file?
  • Can the code generator target specific libraries for a given application?

We will review all these pitfalls when looking at Catalytic MCS. But note that code generation from MATLAB isn't a new concept, although it has received limited attention.

The AccelDSP tool from Xilinx targets the FPGA market with Verilog/VHDL code generation from MATLAB. (For a related how-to article, see Generate FPGA designs from M-code.) This tool relies heavily on library modules to obtain the highest quality results out of a quantized design.

More recently, The Mathworks introduced a restricted subset of the MATLAB language called "Embedded MATLAB," for which Real-Time Workshop can generate C code. The subset is aimed at addressing the needs of embedded designers, who do not perform algorithm exploration or mind restricting their MATLAB style for an embedded target. In particular, Embedded MATLAB avoids a lot of the issues described in the previous two parts of this series by requiring rewrite of the MATLAB in Embedded MATLAB.

In the remainder of this article, we'll demonstrate how you can use Catalytic MCS to generate high quality, flexible C code from existing MATLAB code, which was written by algorithm experts without consideration for matrix size or type declaration.

Page 2: Our Initial Example  

Page 1 | 2 | 3 | 4



Rate this article
WORSE | BETTER
1 2 3 4 5




Catalytic
The MathWorks
Xilinx
Related Content

WEBINAR
1. System Level Tools to Accelerate FPGA Design for Signal Processing

COURSE
2. Analog and Mixed Signal Design Using Simulink

TECH PAPER
3. Programmable Solutions for Set-Top Boxes

TECH PAPER
4. Writing RTL Code for Virtex-4 DSP48 Blocks with XST 8.1i

 

 Featured Jobs
T-Mobile seeking Senior Facilities Engineer in Bellevue, WA

Protingent Staffing seeking Quality Engineer in Irvine, CA

Broadcom seeking Principal Packaging Engineer in Irvine, CA

ITT Corporation seeking Systems Engineer in Thousand Oaks, CA

Comverge, Inc. seeking Senior Firmware Engineer in Norcross, GA

More jobs on EETimesCareers
 Sponsor
 CAREER CENTER
Ready to take that job and shove it?
SEARCH JOBS:

 SPONSOR

 RECENT JOB POSTINGS
For more great jobs, career related news, features and services, please visit EETimes' Career Center.