Replace the exp Function with a Lookup Table
This example shows how to replace the exp
function
with a lookup table approximation in fixed-point code generated using
the Fixed-Point Converter app.
Prerequisites
To complete this example, you must install the following products:
MATLAB®
Fixed-Point Designer™
C compiler
See Supported Compilers.
You can use
mex -setup
to change the default compiler. See Change Default Compiler.
Create Algorithm and Test Files
Create a MATLAB function,
my_fcn.m
, that calls theexp
function.function y = my_fcn(x) y = exp(x); end
Create a test file,
my_fcn_test.m
, that usesmy_fcn.m
.close all x = linspace(-10,10,1e3); for itr = 1e3:-1:1 y(itr) = my_fcn( x(itr) ); end plot( x, y );
Open the Fixed-Point Converter App
Navigate to the work folder that contains the file for this example.
On the MATLAB Toolstrip Apps tab, under Code Generation, click the app icon.
Select Source Files
To add the entry-point function
my_fcn
to the project, browse to the filemy_fcn.m
, and then click Open. By default, the app saves information and settings for this project in the current folder in a file namedmy_fcn.prj
.Click Next to go to the Define Input Types step.
The app screens
my_fcn.m
for code violations and fixed-point conversion readiness issues. The app opens the Review Code Generation Readiness page.
Review Code Generation Readiness
Click Review Issues. The app indicates that the
exp
function is not supported for fixed-point conversion. In a later step, you specify a lookup table replacement for this function.Click Next to go to the Define Input Types step.
Define Input Types
Add
my_fcn_test
as a test file and then click Autodefine Input Types.The test file runs. The app determines from the test file that
x
is a scalar double.Click Next to go to the Convert to Fixed Point step.
Replace exp Function with Lookup Table
Select the Function Replacements tab.
The app indicates that you must replace the
exp
function.On the Function Replacements tab, right-click the
exp
function and selectLookup Table
.The app moves the
exp
function to the list of functions that it will replace with a Lookup Table. By default, the lookup table uses linear interpolation and 1000 points. Design Min and Design Max are set toAuto
which means that the app uses the design minimum and maximum values that it detects by either running a simulation or computing derived ranges.Click the Analyze arrow , select Log data for histogram, and verify that the test file is
my_fcn_test
.Click Analyze.
The simulation runs. On the Variables tab, the app displays simulation minimum and maximum ranges. Using the simulation range data, the software proposes fixed-point types for each variable based on the default type proposal settings, and displays them in the Proposed Type column. The app enables the Convert option.
Examine the proposed types and verify that they cover the full simulation range. To view logged histogram data for a variable, click its Proposed Type field. The histogram provides range information and the percentage of simulation range covered by the proposed data type.
Convert to Fixed Point
Click Convert.
The app validates the proposed types, and generates a fixed-point version of the entry-point function,
my_fcn_fixpt.m
.In the Output Files list, select
my_fcn_fixpt.m
.The conversion process generates a lookup table approximation,
replacement_exp
, for theexp
function.The generated fixed-point function,
my_fcn_fixpt.m
, calls this approximation instead of callingexp
. The fixed-point conversion process infers the ranges for the function and then uses an interpolated lookup table to replace the function. By default, the lookup table uses linear interpolation, 1000 points, and the minimum and maximum values detected by running the test file.function y = my_fcn_fixpt(x) fm = get_fimath(); y = fi(replacement_exp(x), 0, 16, 1, fm); end
You can now test the generated fixed-point code and compare the results against the original MATLAB function. If the behavior of the generated fixed-point code does not match the behavior of the original code closely enough, modify the interpolation method or number of points used in the lookup table. Then, regenerate the code.