主要内容

Replace a Function with a Lookup Table from the Command Line

R2026b

The fixed-point conversion process provides an option to generate lookup table approximations for continuous and stateless single-input, single-output functions in the original MATLAB® code. These functions must be on the MATLAB path.

You can use this capability to handle functions that are not supported for fixed-point and to replace custom functions. The fixed-point conversion process infers the ranges for the function and then uses an interpolated lookup table to replace the function. You can control the interpolation method and number of points in the lookup table. By adjusting these settings, you can tune the behavior of the replacement function to match the behavior of the original function as closely as possible.

The fixed-point conversion process generates one lookup table approximation per call site of the function that needs replacement.

This example shows how to replace a function with a lookup table approximation in the generated fixed-point code using the command-line interface.

Prerequisites

To complete this example, install these:

Create Algorithm and Test Files

  1. Create a MATLAB function, my_fcn.m, that calls the exp function.

    function y = my_fcn(x)
      y = exp(x);
    end
    
  2. Create a test file, my_fcn_test.m, that uses my_fcn.m.

    close all
    
    x = linspace(-10,10,1e3);
    for itr = 1e3:-1:1
       y(itr) = my_fcn( x(itr) );
    end
    plot( x, y );
    

Configure Approximation

Create a function replacement configuration object to approximate the exp function, using the default settings of linear interpolation and 1000 points in the lookup table.

q = coder.approximation("exp");

Set Up Configuration Object

Create a coder.FixptConfig object, fixptcfg. Specify the test file name and enable numerics testing. Associate the function replacement configuration object with the fixed-point configuration object.

fixptcfg = coder.config("fixpt");
fixptcfg.TestBenchName = "my_fcn_test";
fixptcfg.TestNumerics = true;
fixptcfg.DefaultWordLength = 16;
fixptcfg.addApproximation(q);

Convert to Fixed Point

Generate fixed-point MATLAB code.

codegen -float2fixed fixptcfg my_fcn

View Generated Fixed-Point Code

To view the generated fixed-point code, click the my_fcn_fixpt link.

The generated code contains a lookup table approximation, replacement_exp, for the exp function. The fixed-point conversion process infers the ranges for exp and then uses an interpolated lookup table to replace it. By default, the lookup table uses linear interpolation, 1000 points, and the minimum and maximum values that the test file detects.

The generated fixed-point function, my_fcn_fixpt, calls the lookup table approximation instead of calling exp.

function y = my_fcn_fixpt(x)
  fm = get_fimath();

  y = fi(replacement_exp(x), 0, 16, 1, fm);
end

Test the generated fixed-point code and compare the results against the original MATLAB function. If the fixed-point code behavior does not match the original code closely enough, modify the interpolation method or the number of points in the lookup table. Then, regenerate the code.

To replace a custom function with a lookup table, use the Function and CandidateFunction name-value arguments of coder.approximation.

See Also

| |

Topics