Automatic C Code Generation from MATLAB
Marc Barberis Application Engineering Group, MathWorks Inc.
AGENDA
Quick Demo
Benefits of Automatic C Code Generation
In-Depth Example
Comparison between MATLAB Coder and MATLAB Compiler
Fixed-Point Design
Conclusion
Demo: Using Generated C Code
in a Stand-Alone C Project
Why translate MATLAB to C?
Integrate MATLAB algorithms w/ existing C environment using source
code or static libraries
Prototype MATLAB algorithms on desktops as standalone executables
Accelerate user-written MATLAB algorithms
Implement C/C++ code on processors or hand-off to software
engineers
Challenges with Manual Translation
from MATLAB to C/C++
Separate functional and implementation specification
Leads to multiple implementations that are inconsistent
Hard to modify requirements during development
Difficult to keep reference MATLAB code and C code in-sync
Manual coding errors
Time consuming and expensive
Automatic Translation of MATLAB to C
With MATLAB Coder, design engineers can
Maintain one design in MATLAB
Design faster and get to C/C++ quickly
Test more systematically and frequently
Spend more time improving algorithms in MATLAB
Implementation Constraints
function a= foo(b,c)
a = b * c;
C
double foo(double b, double c)
{
return b*c;
}
Element
by
element
multiply
Element
element
multiply
Element
byby
element
multiply
Dot
product
Dot
product
Dot product
Matrix
multiply
Matrix
multiply
Matrix multiply
void foo(const double b[15],
const double c[30], double a[18])
{
int i0, i1, i2;
for (i0 = 0; i0 < 3; i0++) {
for (i1 = 0; i1 < 6; i1++) {
a[i0 + 3 * i1] = 0.0;
for (i2 = 0; i2 < 5; i2++) {
a[i0 + 3 * i1] += b[i0 + 3 * i2] * c[i2 + 5 * i1];
}
}
}
}
logical
integer
real
complex
Implementation Constraints
Polymorphism
Memory allocation
Processing matrices & arrays
Fixed-point data types
7
Lines of MATLAB
107 Lines of C
In-Depth Demo of MATLAB Coder
Coder UI
Code Generation options
Generate code
Browse through report
Supported MATLAB Language
Features and Functions
Broad set of language features and functions/system objects supported for
code generation
Matrices and Arrays
Matrix operations
N-dimensional arrays
Subscripting
Frames
Persistent variables
Global variables
Data Types
Complex numbers
Integer math
Double/single-precision
Fixed-point arithmetic
Characters
Structures
Numeric classes
Variable-sized data
System objects
Classes
Programming Constructs
Arithmetic, relational, and
logical operators
Program control
(if, for, while, switch )
Functions
MATLAB functions and sub-functions
Variable length argument lists
Function handles
Supported algorithms
> 400 MATLAB operators and functions
> 200 System objects for
Signal processing
Communications
Computer vision
Code Generation Readiness Tool
Instant feedback on code generation
compliance of your MATLAB code
Provides estimate of effort needed to
generate C code from your MATLAB code
on a scale of 1 to 5
Provides a list of issues that need to be
resolved in one report
Gives detailed information on unsupported
functions
Other Deployment Options
Deploying Applications with MATLAB Compiler
Share applications
Desktop or Web
software components
MATLAB Compiler
MATLAB
Builder EX
MATLAB
Builder JA
MATLAB
Builder NE
Supports full MATLAB language and
most toolboxes
Requires MCR
Free run-time library
Royalty-free deployment
.exe
.dll
Excel
Java
Web
COM
.NET
Choosing the Right Deployment Solution
MATLAB Coder and MATLAB Compiler
MATLAB Coder
Output
MATLAB support
Runtime requirement
License model
MATLAB Compiler
Portable and readable
C source code
Executable or software
component/library
Subset of language
Some toolboxes
Full language
Most toolboxes
Graphics
None
MATLAB Compiler Runtime (MCR)
Royalty-free
Royalty-free
Fixed Point Design: Motivation
Consideration
Fixed Point
Floating Point
RAM and ROM consumption
Small
Large
Execution time
Faster
Slower
Hardware power consumption
Low
High
Development time
Long
Short
Implementation complexity
More complex. Control of word length,
rounding mode, saturation...
Less
Error Prone
Harder to develop. More prone to
programming errors
Easier to develop
Fixed Point Design: Pitfalls
Arithmetic Pitfalls
Introduces quantization errors
Word length and Fraction Length must be specified
For every variable
Degradation must be analyzed
L-N
N
overflow
Integer + sign
fractional
L
Quantization
Fixed Point Design: Pitfalls
Fixed Point C Pitfalls
No native fixed-point math libraries
No built-in overflow / underflow checks
No tools to determine optimal integer and fractional bits
No visualization of floating and fixed-point representations
L-N
overflow
Integer + sign
fractional
L
Quantization
Fixed-Point Toolbox:
MATLAB Fixed-Point Object
Signed: true
WordLength: 16
FractionLength: 13
RoundMode: round
OverflowMode: saturate
ProductMode: FullPrecision
MaxProductWordLength: 128
SumMode: FullPrecision
MaxSumWordLength: 128
CastBeforeSum: true
Fi Object
Value
NumericType
Fimath
1. Controls output type of operations
2. Allows natural operator syntax
A*B, A+B, pow2(A,3)
Fixed Point Design in MATLAB
Collect histograms for
signals
Run MATLAB code with
floating point data types
Simulation results for
all variables
Analyze simulation
min/max
Demo: Fixed Point Design in MATLAB
Determine best fixed-point settings
Simulate the fixed-point code
Generate fixed-point C code
Benefits of C Code Generation with
MATLAB Coder
Generate C code directly
Automatically generated C code is correct by construction
Reduce verification effort and cost
Maintain floating and fixed-point designs in a unified environment
Run simulations in double precision or fixed-point as needed
Validate fixed-point effects during system design phase