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

  1. In a local, writable folder, create a function ex_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:

    • Setting up input values.

    • Calling the function under test.

    • Outputting the test results.

    To cover the full intended operating range of the system, the test script runs the ex_2ndOrder_filter function with three input signals: chirp, step, and impulse. The script then plots the outputs.

    % ex_2ndOrder_filter_test
    %
    % Define representative inputs
    N = 256;                   % Number of points
    t = linspace(0,1,N);       % Time vector from 0 to 1 second
    f1 = N/2;                  % Target frequency of chirp set to Nyquist
    x_chirp = sin(pi*f1*t.^2); % Linear chirp from 0 to Fs/2 Hz in 1 second
    x_step = ones(1,N);        % Step
    x_impulse = zeros(1,N);    % Impulse
    x_impulse(1) = 1;
    
    % Run the function under test
    x = [x_chirp;x_step;x_impulse];
    y = zeros(size(x));
    for i = 1:size(x,1)
      y(i,:) = ex_2ndOrder_filter(x(i,:));
    end
    
    % Plot the results
    titles = {'Chirp','Step','Impulse'}
    clf
    for i = 1:size(x,1)
      subplot(size(x,1),1,i)
      plot(t,x(i,:),t,y(i,:))
      title(titles{i})
      legend('Input','Output')
    end
    xlabel('Time (s)')
    figure(gcf)
    
    disp('Test complete.')

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