coder.approximation
Create function replacement configuration object
Syntax
Description
creates a function replacement configuration object for use during
code generation or fixed-point conversion. The configuration object
specifies how to create a lookup table approximation for the MATLAB® function
specified by q
= coder.approximation(function_name
)function_name
. To associate this approximation
with a coder.FixptConfig
object for use with thecodegen
function, use the coder.FixptConfig
configuration
object addApproximation
method.
Use this syntax only for the functions that coder.approximation
can
replace automatically. These functions are listed in the function_name
argument
description.
Examples
Replace log
Function with Default Lookup Table
Create a function replacement configuration object using the default settings. The resulting lookup table in the generated code uses 1000 points.
logAppx = coder.approximation('log');
Replace log
Function with Uniform Lookup Table
Create a function replacement configuration object. Specify the input range and prefix to add to the replacement function name. The resulting lookup table in the generated code uses 1000 points.
logAppx = coder.approximation('Function','log','InputRange',[0.1,1000],... 'FunctionNamePrefix','log_replace_');
Replace log
Function with Optimized Lookup Table
Create a function replacement configuration
object using the 'OptimizeLUTSize'
option to specify
to replace the log
function with an optimized lookup
table. The resulting lookup table in the generated code uses less
than the default number of points.
logAppx = coder.approximation('Function','log','OptimizeLUTSize', true,... 'InputRange',[0.1,1000],'InterpolationDegree',1,'ErrorThreshold',1e-3,... 'FunctionNamePrefix','log_optim_','OptimizeIterations',25);
Replace Custom Function with Optimized Lookup Table
Create a function replacement configuration
object that specifies to replace the custom function, saturateExp
,
with an optimized lookup table.
Create a custom function, saturateExp
.
saturateExp = @(x) 1/(1+exp(-x));
Create a function replacement configuration object that
specifies to replace the saturateExp
function with
an optimized lookup table. Because the saturateExp
function
is not listed as a function for which coder.approximation
can
generate an approximation automatically, you must specify the CandidateFunction
property.
saturateExp = @(x) 1/(1+exp(-x)); custAppx = coder.approximation('Function','saturateExp',... 'CandidateFunction', saturateExp,... 'NumberOfPoints',50,'InputRange',[0,10]);
Input Arguments
function_name
— Name of the function to replace
'acos'
| 'acosd'
| 'acosh'
| 'acoth'
| 'asin'
| 'asind'
| 'asinh'
| 'atan'
| 'atand'
| 'atanh'
| 'cos'
| 'cosd'
| 'cosh'
| 'erf '
| 'erfc'
| 'exp'
| 'log'
| 'normcdf'
| 'reallog'
| 'realsqrt'
| 'reciprocal'
| 'rsqrt'
| 'sin'
| 'sinc'
| 'sind'
| 'sinh'
| 'sqrt'
| 'tan'
| 'tand'
Name of function to replace, specified as a string. The function must be one of the listed functions.
Example: 'sqrt'
Data Types: char
Name-Value Arguments
Specify optional pairs of arguments as
Name1=Value1,...,NameN=ValueN
, where Name
is
the argument name and Value
is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.
Before R2021a, use commas to separate each name and value, and enclose
Name
in quotes.
Example: 'Function', 'log'
Architecture
— Architecture of lookup table approximation
'LookupTable'
(default) | 'Flat'
Architecture of the lookup table approximation, specified as
the comma-separated pair consisting of 'Architecture'
and
a string. Use this argument when you want to specify the architecture
for the lookup table. The Flat
architecture does
not use interpolation.
Data Types: char
CandidateFunction
— Function handle of the replacement function
function handle | string
Function handle of the replacement function, specified as the
comma-separated pair consisting of 'CandidateFunction'
and
a function handle or string referring to a function handle. Use this
argument when the function that you want to replace is not listed
under function_name
. Specify the function handle
or string referring to a function handle of the function that you
want to replace. You can define the function in a file or as an anonymous
function.
If you do not specify a candidate function, then the function
you chose to replace using the Function
property
is set as the CandidateFunction
.
Example: 'CandidateFunction', @(x) (1./(1+x))
Data Types: function_handle
| char
ErrorThreshold
— Error threshold value used to calculate optimal lookup table size
0.001 (default) | nonnegative scalar
Error threshold value used to calculate optimal lookup table
size, specified as the comma-separated pair consisting of 'ErrorThreshold'
and
a nonnegative scalar. If 'OptimizeLUTSize'
is true
,
this argument is required.
Function
— Name of function to replace with a lookup table approximation
function_name
Name of function to replace with a lookup table approximation,
specified as the comma-separated pair consisting of 'Function'
and
a string. The function must be continuous and stateless. If you specify
one of the functions that is listed under function_name
,
the conversion process automatically provides a replacement function.
Otherwise, you must also specify the 'CandidateFunction'
argument
for the function that you want to replace.
Example: 'Function'
,'log'
Example: 'Function'
, 'my_log'
,'CandidateFunction'
,@my_log
Data Types: char
FunctionNamePrefix
— Prefix for generated fixed-point function names
'replacement_' (default) | string
Prefix for generated fixed-point function names, specified as
the comma-separated pair consisting of 'FunctionNamePrefix'
and
a string. The name of a generated function consists of this prefix,
followed by the original MATLAB function name.
Example: ‘log_replace_’
InputRange
— Range over which to replace the function
[ ]
(default) | 2x1 row vector | 2xN matrix
Range over which to replace the function, specified as the comma-separated
pair consisting of 'InputRange'
and a 2-by-1 row
vector or a 2-by-N matrix.
Example: [-1 1]
InterpolationDegree
— Interpolation degree
1
(default) | 0
| 2
| 3
Interpolation degree, specified as the comma-separated pair
consisting of 'InterpolationDegree'
and1
(linear), 0
(none), 2
(quadratic),
or 3
(cubic).
NumberOfPoints
— Number of points in lookup table
1000
(default) | positive integer
Number of points in lookup table, specified as the comma-separated
pair consisting of 'NumberOfPoints'
and a positive
integer.
OptimizeIterations
— Number of iterations
25 (default) | positive integer
Number of iterations to run when optimizing the size of the
lookup table, specified as the comma-separated pair consisting of 'OptimizeIterations'
and
a positive integer.
OptimizeLUTSize
— Optimize lookup table size
false
(default) | true
Optimize lookup table size, specified as the comma-separated
pair consisting of 'OptimizeLUTSize'
and a logical
value. Setting this property to true
generates
an area-optimal lookup table, that is, the lookup table with the minimum
possible number of points. This lookup table is optimized for size,
but might not be speed efficient.
PipelinedArchitecture
— Option to enable pipelining
false
(default) | true
Option to enable pipelining, specified as the comma-separated
pair consisting of 'PipelinedArchitecture'
and
a logical value.
Output Arguments
q
— Function replacement configuration object, returned as a coder.mathfcngenerator.LookupTable
or a coder.mathfcngenerator.Flat
configuration object
coder.mathfcngenerator.LookupTable
configuration
object | coder.mathfcngenerator.Flat
configuration
object
Function replacement configuration
object. Use the coder.FixptConfig
configuration
object addApproximation
method to associate this
configuration object with a coder.FixptConfig
object.
Then use the codegen
function -float2fixed
option
with coder.FixptConfig
to convert floating-point MATLAB code
to fixed-point code.
Property | Default Value |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Version History
Introduced in R2014b
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)