Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

One-button MATLAB-to-C Conversion


[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

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.