Generate Single-Precision C Code at the Command Line
This example shows how to generate single-precision C code from double-precision MATLAB® code at the command line.
Prerequisites
To complete this example, install the following products:
MATLAB
MATLAB Coder™
Fixed-Point Designer™
C compiler
See Supported Compilers.
You can use
mex -setup
to change the default compiler. See Change Default Compiler.
Create a Folder and Copy Relevant Files
Create a local working folder, for example,
c:\ex_2ndOrder_filter
.Change to the
docroot\toolbox\fixpoint\examples
folder. At the MATLAB command line, enter:cd(fullfile(docroot, 'toolbox', 'fixpoint', 'examples'))
Copy the
ex_2ndOrder_filter.m
andex_2ndOrder_filter_test.m
files to your local working folder.Type Name Description Function code ex_2ndOrder_filter.m
Entry-point MATLAB function Test file ex_2ndOrder_filter_test.m
MATLAB script that tests
ex_2ndOrder_filter.m
Determine the Type of the Input Argument
To determine the type of the input argument x
,
use coder.getArgTypes
to run the test file ex_2ndOrder_filter_test.m
types = coder.getArgTypes('ex_2ndOrder_filter_test', 'ex_2ndOrder_filter');
The test file runs and displays the outputs of the filter for
each of the input signals. coder.getArgTypes
determines
that the input type of x
is 1x256 double.
Generate and Run Single-Precision MEX to Verify Numerical Behavior
Before you generate single-precision C code, generate a single-precision MEX function that you can use to verify the behavior of the generated single-precision code. To indicate that you want the single-precision MEX code, use the
-singleC
option.codegen -singleC ex_2ndOrder_filter -args types -report
During MEX generation, the code generator detects single-precision conversion issues. Before you generate C/C++ code, fix these issues. This example does not have single-precision conversion issues.
The generated MEX accepts single-precision and double-precision input. You can use the same test file to run the double-precision MATLAB function and the single-precision MEX function. You do not have to modify the test file to call the single-precision MEX function.
Run the test file
ex_2ndOrder_filter_test.m
. This file calls the double-precision MATLAB functionex_2ndOrder_filter.m
.ex_2ndOrder_filter_test
The test file runs and displays the outputs of the filter for each of the input signals.
Run the test file
ex_2ndOrder_filter_test
, replacing calls to the double-precisionex_2ndOrder_filter
function with calls to the single-precisionex_2ndOrder_filter_mex
function.coder.runTest('ex_2ndOrder_filter_test', 'ex_2ndOrder_filter')
The test file runs and displays the outputs of the filter for each of the input signals. The single-precision MEX function produces the same results as the double-precision MATLAB function.
Generate Single-Precision C Code
Create a code configuration object for generation of a C static library, dynamic library, or executable.
cfg = coder.config('lib');
To generate single-precision C code, call
codegen
with the-singleC
option. Enable generation of the code generation report.codegen -config cfg -singleC ex_2ndOrder_filter -args {types{1}} -report
View the Generated Single-Precision C Code
To view the code generation report for the C code generation, click the View Report link.
In the Generated Code pane, click
ex_2ndOrder_filter.c
.
Double-precision variables have type
float
in the C code.The index
i
is an integer.
View Potential Data Type Issues
When you generate single-precision code, codegen
enables
highlighting of potential data type issues in the code generation
report. If codegen
cannot remove a double-precision
operation, the report highlights the MATLAB expression that results
in the operation.
Click the Code Insights tab. Expand Potential data type issues. The absence of double-precision operations indicates that no double-precision operations remain.
See Also
codegen
| coder.config
| coder.getArgTypes
| coder.runTest