Newsletter

DSP DesignLine  >  Design Center

MATLAB-to-C Translation, part 2: Complex functions and C interfaces

Part 2 of this 3-part series looks at complex functions and the role C interface constraints play in the translation process.

Page 1 of 3

DSP DesignLine

Part 1 looks at the basic problems of translating MATLAB to C. Part 3 examines the verification process and makes the case for automatic C generation. It will be published Thursday, December 13.

In the first part of this series, we showed how basic MATLAB operations can be translated into C code. This section focuses on translating more complex MATLAB functions or toolbox functions into C, and on integrating C algorithms derived from MATLAB into your application.

Implementing complex MATLAB functions
Of the many MATLAB functions used to develop algorithms, most do not have equivalents in the C language. For example, the C language does not have equivalents for basic MATLAB functions such as "find" (which automatically extracts elements from an array) or "sort" (which sorts array elements).

Some of these functions are fairly well known and can be found in C libraries or on the Internet. For example, it is easy to find sorting algorithms. Nevertheless, finding the most efficient implementation for MATLAB's sort function can be quite an adventure, as noted in this article.

In addition, you may not be able to use the functions you find due to license restrictions. These include license restrictions on derivative work from copyrighted material (such as MATLAB files) or open-source (GPL) files. In such case, you may need to carefully and independently recode the algorithms by hand for your commercial application.

When you implement a MATLAB function in a language such as C, you may find it difficult to replicate MATLAB's results. For example, image processing applications may use morphology functions such as "imresize" to resize an image. When implementing such functions, it is easy to end up with results that are a fraction of a pixel off from MATLAB's results. (Even MATLAB 2006b and 2007a produce different results for the "imresize" function because they implement it differently). To replicate MATLAB's results, you may need to recalibrate your algorithm in C. This might add significant delay to your project.

Some functions have implementations in both MATLAB and C. The standard math C library can be used to implement functions such as divide, sqrt, cos, sin, etc. in C when dealing with real scalar data. Things get more complicated once you start adding complex numbers and matrices into the mix.

For example, the operation "a/b" may be look like a simple divide operator in MATLAB.

When the operands are complex scalars, the operation is slightly more complicated in C:


If b is a matrix, "a/b" becomes a "matrix divide," which is a lot more complicated to implement. A "matrix divide" requires solving a linear system of equations consisting of finding 'x' such that "a*x=b". This is typically performed using complex matrix manipulations based on matrix QR decompositions. An example of this approach is shown in Implementing matrix inversions in fixed-point hardware.

Integrating with other functions and interface definition
When translating a MATLAB algorithm to C, you may be able to re-use existing code or leverage functions available in libraries. Your task then is to integrate the C code derived from your MATLAB algorithm with existing source code and libraries. To make this integration successful you need to make sure the interfaces of your different functions are compatible. In particular, you need to pay special attention to conventions on how complex and multi-dimensional variables are laid out in memory, when data are passed by references and by value, and whether indices start from 1 or 0.

Indices start with 1 in MATLAB and 0 in C
One difference between MATLAB and C is that MATLAB uses one-base indexing whereas C uses zero-base indexing. This means that the index of the first element of an array in C is 0, and is 1 in MATLAB.

You need to be aware of this when communicating index values between C and MATLAB functions, otherwise you may end up addressing elements that are off by one in your final C code.




Page 2: next page  

Page 1 | 2 | 3



Rate this article
WORSE | BETTER
1 2 3 4 5




Catalytic
Intel
The MathWorks
Related Content

TECH PAPER
1. Modular Communications Platform

TECH PAPER
2. An Adaptive Polyphase Orthonormal IIR Structure

TECH PAPER
3. Development and Optimization Techniques for Multi-core Processors

TECH PAPER
4. Intel 45nm Penryn Processor (QX9650) Overview

 

 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.