Main Content

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:

Create a Folder and Copy Relevant Files

  1. Create a local working folder, for example, c:\ex_2ndOrder_filter.

  2. Change to the docroot\toolbox\fixpoint\examples folder. At the MATLAB command line, enter:

    cd(fullfile(docroot, 'toolbox', 'fixpoint', 'examples')) 

  3. Copy the ex_2ndOrder_filter.m and ex_2ndOrder_filter_test.m files to your local working folder.

    TypeNameDescription
    Function codeex_2ndOrder_filter.mEntry-point MATLAB function
    Test fileex_2ndOrder_filter_test.m

    MATLAB script that tests ex_2ndOrder_filter.m

     The ex_2ndOrder_filter Function

     The ex_2ndOrder_filter_test Script

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

  1. 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.

  2. Run the test file ex_2ndOrder_filter_test.m. This file calls the double-precision MATLAB function ex_2ndOrder_filter.m.

    ex_2ndOrder_filter_test

  3. The test file runs and displays the outputs of the filter for each of the input signals.

  4. Run the test file ex_2ndOrder_filter_test, replacing calls to the double-precision ex_2ndOrder_filter function with calls to the single-precision ex_2ndOrder_filter_mex function.

    coder.runTest('ex_2ndOrder_filter_test', 'ex_2ndOrder_filter')
  5. 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

  1. Create a code configuration object for generation of a C static library, dynamic library, or executable.

    cfg = coder.config('lib');
    

  2. 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

| | |

Related Examples

More About