Generate Single-Precision C Code at the Command Line - MATLAB & Simulink (original) (raw)

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 Code Files

  1. In a local, writable folder, create a functionex_2ndOrder_filter.m.
    function y = ex_2ndOrder_filter(x) %#codegen
    persistent z
    if isempty(z)
    z = zeros(2,1);
    end
    % [b,a] = butter(2, 0.25)
    b = [0.0976310729378175, 0.195262145875635, 0.0976310729378175];
    a = [1, -0.942809041582063, 0.3333333333333333];

y = zeros(size(x));
for i = 1:length(x)
y(i) = b(1)*x(i) + z(1);
z(1) = b(2)*x(i) + z(2) - a(2) * y(i);
z(2) = b(3)*x(i) - a(3) * y(i);
end
end 2. Create a test file, ex_2ndOrder_filter_test.m, to exercise the ex_2ndOrder_filter algorithm.
It is a best practice to create a separate test script for preprocessing and postprocessing such as:

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.
  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, clickex_2ndOrder_filter.c.

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

More About