Function library supports Matlab-to-C translation

By Richard Goering


Easing the pathway for algorithm developers who want to generate C language models from Matlab code, Catalytic Inc. this week (Dec. 3) is rolling out its Catalytic Function Library. Supporting some 300 Matlab functions, the new offering complements Catalytic’s existing Matlab-to-C Synthesis (MCS) product introduced late last year.

MCS converts code written in Matlab, a dynamically typed vector language, into ANSI C, which is a compiled language. MCS synthesizes a subset of the Matlab language, including common data types, operators, and some built-in functions. The initial December 2006 release included support for some 140 functions, but didn’t come with an optional function “library,” as Catalytic is offering now.

With the new library, said Luc Semeria, product manager at Catalytic, the company has more than tripled its function support. “With MCS last year we had good coverage with the language, but the thing we were running into during engagements was always function support,” Semeria said. “It was pretty apparent we had to increase our function support. Now, when we walk into a new account, we support most of the functions they require.”

The new functions supported, Semeria said, include FFTs, image correlation functions, image morphology operations, statistical functions, and linear algebraic functions. The Function Library aims to support the most commonly-used functions, but doesn’t cover them all. Semeria estimated that there are over 2,000 Matlab functions. Some of these, he noted, are visualization functions that are not used for computation within algorithms.

Matt Volckmann, senior analyst at Venture Data Corp., said that Catalytic Function Library is a “relevant addition” for MCS users “that should enable those looking to translate Matlab code into C code to use specific Matlab functions that they may have either avoided using or had to manually translate.” While C is the most commonly used language for algorithm design, Volckmann noted, Matlab is also popular. “It seems that a tool allowing for more flexible and automated translation from Matlab to C domains would be of interest to many engineers in the embedded systems space,” he said.

According to Catalytic, manual translation is required today to convert Matlab functions into C code, a task that can take anywhere from one to six engineer-weeks of effort per function. Catalytic MCS claims to allow easy conversion of Matlab functions without source code modifications.

To do the translation, MCS recognizes the Matlab function call and substitutes it with Catalytic’s equivalent Matlab function. MCS thus doesn’t directly translate the actual Matlab function as created by The Mathworks, Matlab’s creator. “Matlab has its own source code and compiled code, and we can’t make use of that,” Semeria said. “So we developed our own functions. We test those to make sure they give functionally equivalent results to Matlab functions.”


The Catalytic Function Library supports Matlab-to-C Synthesis (MCS). Compiled code can be used for implementation, and brought back into Matlab for verification.

MCS starts by analyzing Matlab code. Since Matlab doesn’t contain explicit type declarations, the goal of the analysis is to infer the types and sizes of variables in the code. After defining the call graph that corresponds to the function hierarchy, MCS analyzes whether data are real or complex, scalar, vectors, or matrices. Structure fields are collected and array sizes are defined.

Results of the analysis are shown in a graphical user interface, where users can navigate their code, and look at the variables and types without actually running the code. MCS is then able to generate ANSI C code for statements and function calls. It also performs optimizations to reduce memory usage and improve locality in the code.

MCS translates a subset of the Matlab language. “There are things that don’t make sense for C code generation because they’re really too dynamic,” Semeria said. “Matlab is an interpreted language, so you have a lot of dynamic run-time decisions that are made.” For example, he noted, MCS doesn’t support Matlab cell arrays, which are used to aggregate data of different types.

After the C language translation, users can bring the compiled code back into the Matlab environment for verification and debugging, using the Matlab C language API. They can thus verify that the C code behaves in the same way as the original Matlab algorithm, Semeria said. If the algorithm contains functions that Catalytic can’t convert to C, those unconverted functions are “called back” to Matlab so they can be included for verification and debugging, he said.

Catalytic tries to match the quality of hand-crafted C code as much as possible, Semeria said. “Typically the [generated] C code is very efficient. Out of the box it may not be as tight as what you would expect if you write it by hand, but you get something that works.”

Different products

The Catalytic Function Library announcement follows by two months The Mathworks’ announcement of Embedded Matlab, a Matlab language subset that allows C code generation. Beyond C code generation, Embedded Matlab lets users import algorithmic intellectual property into Simulink, The Mathworks’ modeling and simulation tool, in the form of function blocks. From here, it can be translated into C or RTL code.

Catalytic doesn’t really view MCS as competitive to Embedded Matlab, Semeria said. “We think they are two different products for different users,” he said. While MCS users are typically the original algorithm developers, Embedded Matlab users are more likely to be the designers who implement the algorithms, he said. MCS doesn’t target Simulink, which provides paths to specific platform implementations.

“With Embedded Matlab out, it’s good to have somebody else in our space,” Semeria said. “It’s good to have more people in the ecosystem and it’s good for the user.”

The Catalytic Function Library is available now as an option for MCS that starts at $5,000 per year.

Volis Written by: