This example shows how to generate single-precision C code from double-precision MATLAB® code by using the MATLAB Coder™ app.
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 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
and ex_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 |
Navigate to the work folder that contains the file for this example.
On the MATLAB Toolstrip Apps tab, under Code Generation, click the app icon.
To add the entry-point function ex_2ndOrder_filter
to
the project, browse to the file ex_2ndOrder_filter.m
,
and then click Open. By default, the app saves
information and settings for this project in the current folder in
a file named ex_2ndOrder_filter.prj
.
Set Numeric Conversion to Convert
to single precision
.
Click Next to go to the Define Input Types step.
The app screens ex_2ndOrder_filter.m
for
code violations and code generation readiness
issues. The app does not find issues in ex_2ndOrder_filter.m
.
On the Define Input Types page,
to add ex_2ndOrder_filter_test
as a test file,
browse to ex_2ndOrder_filter_test
. Click Open.
Click Autodefine Input Types.
The test file runs and displays the outputs of the filter for
each of the input signals. The app determines that the input type
of x
is double(1x256)
.
Click Next to go to the Check for Run-Time Issues step.
To detect and fix single-precision conversion issues, perform the Check for Run-Time Issues step.
On the Check for Run-Time Issues page,
the app populates the test file field with ex_2ndOrder_filter_test
,
the test file that you used to define the input types.
Click Check for Issues.
The app generates a single-precision MEX function from ex_2ndOrder_filter
.
It runs the test file ex_2ndOrder_filter_test
replacing
calls to ex_2ndOrder_filter
with calls to the
generated MEX function. If the app finds issues, it provides warning
and error messages. Click a message to highlight the problematic code
in a window where you can edit the code. In this example, the app
does not detect issues.
Click Next to go to the Generate Code page.
In the Generate dialog box,
set Build type to Static Library
.
Set Language to C.
For other settings, use the default values.
To generate the code, click Generate.
MATLAB
Coder builds the project and generates a C static
library and supporting files in the default subfolder, codegen/lib/ex_2ndOrder_filter
.
The app displays the generated code for ex_2ndOrder_filter.c
.
Double-precision variables have type float
in
the C code.
The index i
is an integer.
When you generate single-precision code, the app enables highlighting of potential data type issues in the code generation report. If the app cannot remove a double-precision operation, the report highlights the MATLAB expression that results in the operation.
To open the code generation report, click the View Report link.
Click the Code Insights tab. Expand Potential data type issues. The absence of double-precision operations indicates that no double-precision operations remain.