Main Content

coder.approximation

Create function replacement configuration object

Description

q = coder.approximation(function_name) 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 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.

example

q = coder.approximation('Function',function_name,Name,Value) creates a function replacement configuration object using additional options specified by one or more name-value pair arguments.

example

Examples

collapse all

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');	

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_');	

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);	 

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

collapse all

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 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

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

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.

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

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_’

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]

Interpolation degree, specified as the comma-separated pair consisting of 'InterpolationDegree' and1 (linear), 0 (none), 2 (quadratic), or 3 (cubic).

Number of points in lookup table, specified as the comma-separated pair consisting of 'NumberOfPoints' and a 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.

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.

Option to enable pipelining, specified as the comma-separated pair consisting of 'PipelinedArchitecture' and a logical value.

Output Arguments

collapse all

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.

PropertyDefault Value

Auto-replace function

''

InputRange

[]

FunctionNamePrefix

'replacement_'

Architecture

LookupTable (read only)

NumberOfPoints

1000

InterpolationDegree

1

ErrorThreshold

0.001

OptimizeLUTSize

false

OptimizeIterations

25

Version History

Introduced in R2014b