This example shows how to replace a custom function with a lookup table approximation function using the MATLAB® Coder™ app.
This example requires the following products:
MATLAB
MATLAB Coder
Fixed-Point Designer™
C compiler
See Supported Compilers.
You can use mex -setup
to change the default
compiler. See Change Default Compiler (MATLAB).
In a local, writable folder:
Create a MATLAB function, custom_fcn.m
which
is the function that you want to replace.
function y = custom_fcn(x) y = 1./(1+exp(-x)); end
Create a wrapper function, call_custom_fcn.m
,
that calls custom_fcn.m
.
function y = call_custom_fcn(x) y = custom_fcn(x); end
Create a test file, custom_test.m
,
that uses call_custom_fcn
.
close all clear all x = linspace(-10,10,1e3); for itr = 1e3:-1:1 y(itr) = call_custom_fcn( x(itr) ); end plot( x, y );
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.
To add the entry-point function call_custom_fcn
to
the project, browse to the file call_custom_fcn.m
,
and then click Open. By default, the app saves
information and settings for this project in the current folder in
a file named call_custom_fcn.prj
.
Set Numeric Conversion to Convert
to fixed point
.
Click Next to go to the Define Input Types step.
The app screens call_custom_fcn.m
for code
violations and code generation issues. The app opens the Review
Code Generation Readiness page.
Click Review Issues. The app
indicates that the exp
function is not supported
for fixed-point conversion. You can ignore this warning because you
are going to replace custom_fcn
, which is the function
that calls exp
.
Click Next to go to the Define Input Types step.
Add custom_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 Check for Run-Time Issues step.
The Check for Run-Time Issues step generates
instrumented MEX. It runs the test file custom_test
replacing
calls to call_custom_fcn
with calls to the generated
MEX function. If the app finds issues, it provides warning and error
messages. You can click a message to highlight the problematic code
in a pane where you can edit the code.
On the Check for Run-Time Issues page,
the app populates the test file field with custom_test
,
the test file that you used to define the input types.
Click Check for Issues.
The app does not detect issues.
Click Next to go to the Convert to Fixed Point step.
Select the Function Replacements tab.
The app indicates that you must replace the exp
function.
Enter the name of the function to replace, custom_fcn
,
select Lookup Table
, and then click .
The app adds custom_fcn
to the list of functions
that it will replace with a Lookup Table. By default, the lookup
table uses linear interpolation and 1000 points. The app sets Design
Min and Design Max to Auto
which
means that 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 call_custom_test
.
Click Analyze.
The simulation runs. The app displays simulation minimum and maximum ranges on the Variables tab. 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 Convert option is now enabled.
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.
Click Convert.
The app validates the proposed types and generates a fixed-point
version of the entry-point function, call_custom_fcn_fixpt.m
.
In the Output Files list, select call_custom_fcn_fixpt.m
.
The conversion process generates a lookup table approximation, replacement_custom_fcn
,
for the custom_fcn
function. 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.
The generated fixed-point function, call_custom_fcn_fixpt.m
,
calls this approximation instead of calling custom_fcn
.
function y = call_custom_fcn_fixpt(x) fm = get_fimath(); y = fi(replacement_custom_fcn(x), 0, 16, 16, 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 and then regenerate code.