Design Article

IMG1

One-button MATLAB-to-C conversion

Robert Yu & Niraj Shah, Catalytic

1/16/2007 10:00 AM EST

[Editor's note: For a related product story, click here.]

Introduction
For signal processing algorithm developers, MATLAB from The MathWorks is an essential tool. Its vector semantics and powerful visualization capabilities are a necessity for algorithm development. However, since algorithm development is often separate from product development, MATLAB programmers generally need to create a C-code model of their algorithm. This C-code model of the algorithm can have a number uses, including:

  • Algorithm prototyping: The ubiquity of C-code platforms makes it a popular choice for prototyping algorithms before they are implemented. With C-code, algorithms can be easily run on multiple platforms and server farms to ease the testing burden. In addition, it is easier to connect C code to existing input and output sources, such as video streamers.
  • Integration into a system model: DSP systems typically contain many components, and different components are often modeled in different languages. Thus, it is often necessary to create a C-code system model to integrate these disparate models together. Having C-code for an algorithm makes it easy to integrate it into a larger system model.
  • As the core of a software application: If the final target of the algorithm is a software application, then the C-code model of the algorithm can simply be integrated with a small bit of hand-written code and compiled into the application.

Creating the C-code model requires developers to temporarily freeze algorithm changes and manually translate their algorithms into C. Manually translating the algorithmic model to C code can be a laborious task. As a result, developers tend to avoid making this translation until late in the design process. This increases the length of the development process and increases the cost of fixing bugs. Manual translation is also an error-prone process. This makes it difficult to determine if functional bugs are the result of a poor algorithm or a translation error.

These challenges can be addressed by using a tool such as Catalytic MCS that automatically generates C-code from MATLAB. This automation also enables developers to continue using MATLAB to modify their algorithms and generate a reference model as needed.

In this article, we describe how to use Catalytic MCS to greatly ease the task of integrating MATLAB algorithms into existing C-code. Throughout the article, we use a vertical image scaling algorithm as a running example.

Example: Vertical Image Scaler
For this example, we have developed a vertical image scaler in MATLAB. The scaler algorithm takes a single image and a scaling ratio as input and computes a scaled image, using a simple linear interpolation between pixel rows. Eventually, our goal is to integrate this algorithm with a video decoder to scale an input video stream to a variety of display types. For example, the algorithm could be used to scale an HDTV video (720 scan lines) to an NTSC video display (480 scan lines).

The video decoding application has already been developed using Microsoft's DirectShow, a C++-based environment for developing audio and video applications. We have created a skeleton DirectShow image transform filter, into which we will integrate the vertical scaling code. The resulting application will open an input video file, scale each frame, and play the resulting video stream on the screen.


Figure 1. Overview of Algorithm and Testbench in MATLAB and DirectShow
1  2  3 

print

email

rss

Bookmark and Share

Joinpost comment



Comments


Saugata

3/5/2008 12:38 PM EST

I wanted to convert Matlab code into C code.
Don't know how to do this. If anyone could assist me, I would be grateful.

Sign in to Reply


shaboo

9/8/2008 12:33 AM EDT

if anyone can convert matlab code please inform me

Sign in to Reply


bangu

10/21/2008 1:10 AM EDT

I wanted to convert Matlab code into C code.
Don't know how to do this. If anyone could assist me, I would be thankful.

Sign in to Reply


bangu

10/21/2008 1:12 AM EDT

Where can i get Catalytic MCS??

Sign in to Reply


Dmitry_Yakovlev

1/28/2009 12:34 PM EST

I do no know where to get Catalytic MCS, but...

What is the use case for this conversion?
Why Matlab and not C# or Java? Which part of Matlab you plan to convert to C?
Realy, for me it is very interesting.

Thanks.

Sign in to Reply


mrdsk

4/3/2009 5:29 PM EDT

Yes, I can help you in this. I have converted plenty of MATLAB codes into C codes. What are you looking for? what kind of conversion do you need? Is this floating point MATLAB to floating point C? If so, this should be pretty easy. The hardest part is converting floating point MATLAB to fixed-point C.

Sign in to Reply


mbaykal

5/12/2009 11:04 AM EDT

i wanna convert functions like fft and [y,I]=max(xm) to c code but for now i could not do this.
pls help i really appreciate it

Sign in to Reply


Swathi.reddy

2/10/2010 12:52 AM EST

Hello I want to implement filters and doing them in matlab is very easy but doing them in C is a bit tough.can we directly convert the matlab codes to C.can anyone help me with that??

Sign in to Reply


Please sign in to post comment

Navigate to related information

Product Parts Search

Enter part number or keyword
PartsSearch

FeedbackForm