주요 콘텐츠

Replace a Function with a Lookup Table Using the HDL Coder App

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 use the HDL Coder app to replace a custom function with a lookup table.

Create Algorithm and Test Files

In a local, writable folder:

  1. Create a MATLAB function, custom_fcn, which is the function to replace.

    function y = custom_fcn(x)
      y = 1./(1+exp(-x));
    end
  2. Create a wrapper function that calls custom_fcn.

    function y = call_custom_fcn(x)
      y = custom_fcn(x);
    end
  3. Create a test file, custom_test, which uses call_custom_fcn.

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

Create and Set Up an HDL Coder Project

  1. Navigate to the work folder that contains the file for this example.

  2. To open the HDL Coder app, in the MATLAB Command Window, enter hdlcoder. In the Create HDL Coder Project dialog box, set Name to custom_project.mldatx. Set Folder to the current working folder. Click OK to create the custom_project.mldatx project in the specified folder and open the HDL Workflow Advisor.

Define Input Types

  1. In the HDL Workflow Advisor, in the Define Input Types task, for the MATLAB Function parameter, click Browse to browse to the MATLAB function file call_custom_fcn.m. Click Open to add the MATLAB function file to the project. For the MATLAB Test Bench parameter, click the button to browse to the MATLAB test bench file custom_test.m. Click Open to add the MATLAB test bench file to the project.

  2. Click Run. The HDL Workflow Advisor infers the input types automatically by executing the MATLAB test bench. From the test file, HDL Coder™ determines that x is a scalar double.

Replace custom_fcn with Lookup Table

  1. To replace custom_fcn with a Lookup Table, in the Fixed-Point Conversion task, in the Function Replacements tab, in the Enter a function to replace box box, enter custom_fcn. Then, select Lookup Table.

    Fixed-Point Conversion task with custom_fcn entered in the Function Replacements tab and Lookup Table selected as the replacement type

  2. Next, click the button to confirm the function replacement. By default, the lookup table uses linear interpolation and 1000 points. The app determines design minimum and maximum values by using the test bench to run a simulation or compute derived ranges.

    Function Replacements tab showing custom_fcn configured with Linear interpolation, Auto design minimum and maximum, and 1000 points

  3. Click Analyze Design to generate an instrumented MEX function for data type range analysis.

  4. Click Propose Types to perform a range analysis. The app runs a simulation with the test bench and displays simulation minimum and maximum ranges in the Variables tab. The app plots the simulation results in a MATLAB figure.

    Sigmoid curve plot of the custom_fcn simulation results, with x-axis from -10 to 10 and y-axis from 0 to 1

Validate Fixed-Point Types

  1. Click Validate Types. The app validates the proposed types and generates fixed-point code in the codegen/call_custom_fcn/fixpt folder.

  2. To view the generated fixed-point code, in the Output tab, click the call_custom_fcn_fixpt link.

    Output tab showing successful fixed-point code generation with links to the generated call_custom_fcn_fixpt report

    The generated fixed-point function, call_custom_fcn_fixpt.m, calls the lookup table approximation, replacement_custom_fcn, instead of calling custom_fcn.

    function y = call_custom_fcn_fixpt(x)
      fm = get_fimath();
    
      y = fi(replacement_custom_fcn(x), 0, 14, 14, fm);
    end

Verify Fixed-Point Types

  1. In Test Numerics > Plotting and Reporting, select Log all inputs and outputs for comparison plots.

  2. Click Test Numerics.

    The app runs the test bench with the floating-point function and the generated fixed-point function, generates comparison plots, and logs the maximum error difference in the Max Diff column of the Variables tab.

    Variables tab showing proposed fixed-point types for input x and output y, with Max Diff values of 1.951e-03 and 6.671e-04

See Also

Topics