Newsletter

DSP DesignLine  >  Design Center

MATLAB to C using MCS: Advanced topics

We highlight advanced topics in MATLAB to C conversion using the mixture-of-Gaussians background subtraction method.

Page 1 of 3

DSP DesignLine

This article is a sequel to the two-part series on background subtraction. The series used a low-complexity frame-difference method to illustrate the basics of MATLAB to C conversion. In this article, we use the more complicated mixture-of-Gaussians method to highlight advanced topics in MATLAB to C conversion.

Translating even a simple MATLAB function to C is fraught with peril—once you're aware of the pitfalls. This previous article reviews a number of challenges in detail, but we'll quickly highlight a few here:

MATLAB doesn't require definition of data types. This is a mixed blessing. It makes it very easy to put together a proof of concept, but when it comes time to translate to C, a lot hinges on correctly interpreting the intent of the MATLAB designer—as opposed to working off a precise specification.

Resulting manual effort – The types and sizes of all variables (and possibly memory allocation) must be declared.

MATLAB is a vector language. Translating a vector-based description to a scalar-based description that will run on a sequential processor is harder than it sounds. Beyond the obvious fact that the code becomes a lot more verbose, there is the potential to miss data dependencies, which prevent you from simply turning a vector operation into a "scalarized" loop.

Resulting manual effort – You may need to create several copies of sub-functions for each signature and carefully analyze data dependencies.

MATLAB supports type polymorphism. Polymorphism means that operations and functions behave differently when called with different types of arguments. For example, a multiplication can be a real multiplication or a complex one.

Resulting manual effort – you may need to create several versions of the code for a given operation, and several copies of sub-functions for each possible signature

Each one of these challenges may lead to incorrect C code. The designer also faces the challenge of verifying and debugging the C code. Once the C code is available, you can verify it against the original MATLAB in several ways:

  • Write a MEX wrapper to simulate the C code back in MATLAB
  • Translate the MATLAB testbench and analysis m-file to C and run the C as a stand-alone application
  • Exchanging data files between MATLAB and C

Each of these methods is tedious and error-prone. And there is no easy way to determine whether errors that arise are occuring in the main C code or the verification environment.

Automatic translation: the pros
The obvious key benefit of automated translation is the automation. Agility developed MCS to translate existing, unmodified MATLAB code to C. While MCS does not support absolutely all MATLAB constructs and functions, it supports most of them and, in particular, it does not require the user to define the type of any variable other than input parameters.

Furthermore, MCS is unique in that it does not require knowledge of the size of any variable in the design. This fact is crucial to understanding why MCS can process essentially unmodified MATLAB code. Examples of situations where this is needed:

  • Functions that take input parameters of unknown (or variable) sizes
  • Operations (such as "find"), which return an unknown number of elements
Translating the mixture of Gaussians MATLAB function
Let us apply MCS to the mixture-of-Gaussians method described in part I.

As expected, the only piece of information that MCS requires is knowledge of the mixture of Gaussian funtion's input parameter types (but not size). These types are defined in MCS by use of the "must-be functions" ('mbfunctions') as follows:


The first line indicates that the input variables fr,mean,w, and sd are 3D arrays (of unknown size). The second line indicates that thresh, width, height, alpha, sd_init, C, D, and M are real scalars.

With this information, MCS can analyze the MATLAB and present a view of the code as it stands to be translated to C. Triggering of this analysis can be done in MATLAB with the following command line argument:

>> ctmcs –gui mixture_of_gaussians_c


(Click to enlarge)

Figure 1. MCS GUI showing analysis of the mixture of Gaussians m-file.

Figure 1 shows the MCS GUI with the mixture of Gaussians MATLAB code loaded. The right pane lists all the variables MCS has inferred and their associated types. Square brackets indicate the dimension of a variable.

For example:


(Click to enlarge)

MCS can additionally highlight all the places where a given variable is used (read to or written from), and allows you to easily navigate through any sub-function called by the main program.

Page 2: Translation to C  

Page 1 | 2 | 3

Related Links:
  • Background subtraction, part 2: MATLAB to C using MCS
  • Background subtraction, part 1: MATLAB models
  • Embedded MATLAB, part 1: From MATLAB to embedded C


  • Rate this article
    WORSE | BETTER
    1 2 3 4 5




    Related Content

    TECH PAPER
    1. Fujitsu and Inova Help Automakers and Tier-One Suppliers Design and Manufacture High-Performance Graphics Display Systems

    TECH PAPER
    2. Video Analytics for Digital Signage Deployment

    WEBINAR
    3. Achieve greater productivity and ease of use with Targeted Design Platforms enabled by Virtex-6 and Spartan-6 FPGAs

    TECH PAPER
    4. FPGA Design Methods for Fast Turn Around

     


     Featured Jobs
    Accenture seeking Project Management Team Lead in Charlotte, NC

    Accenture seeking Software Engineer in Salt Lake City, UT

    Boeing Company seeking Software Engineer in Herndon, VA

    Switch and Data seeking Customer Solutions Engineer in Dallas, TX

    Chart Industries seeking Sr. Developer in Cleveland, OH

    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.