# fixpt_look1_func_approx

Optimize fixed-point approximation of nonlinear function by interpolating lookup table data points

## Syntax

```[xdata,ydata,errworst] = fixpt_look1_func_approx('func',...xmin,xmax,xdt,xscale,ydt,yscale,rndmeth,errmax,nptsmax)[xdata,ydata,errworst] = fixpt_look1_func_approx('func',...xmin,xmax,xdt,xscale,ydt,yscale,rndmeth,errmax,[])[xdata,ydata,errworst] = fixpt_look1_func_approx('func',...xmin,xmax,xdt,xscale,ydt,yscale,rndmeth,[],nptsmax)[xdata,ydata,errworst] = fixpt_look1_func_approx('func',...xmin,xmax,xdt,xscale,ydtydt,yscale,rndmeth,errmax,nptsmax,spacing)```

## Description

```[xdata,ydata,errworst] = fixpt_look1_func_approx('func',...xmin,xmax,xdt,xscale,ydt,yscale,rndmeth,errmax,nptsmax)``` returns the optimal breakpoints of a lookup table, an ideal function applied to the breakpoints, and the worst-case approximation error. The lookup table satisfies the maximum acceptable error and maximum number of points that you specify.

```[xdata,ydata,errworst] = fixpt_look1_func_approx('func',...xmin,xmax,xdt,xscale,ydt,yscale,rndmeth,errmax,[])``` returns the optimal breakpoints of a lookup table, an ideal function applied to the breakpoints, and the worst-case approximation error. The lookup table satisfies the maximum acceptable error that you specify.

```[xdata,ydata,errworst] = fixpt_look1_func_approx('func',...xmin,xmax,xdt,xscale,ydt,yscale,rndmeth,[],nptsmax)``` returns the optimal breakpoints of a lookup table, an ideal function applied to the breakpoints, and the worst-case approximation error. The lookup table satisfies the maximum number of points that you specify.

```[xdata,ydata,errworst] = fixpt_look1_func_approx('func',...xmin,xmax,xdt,xscale,ydtydt,yscale,rndmeth,errmax,nptsmax,spacing)``` returns the optimal breakpoints of a lookup table, an ideal function applied to the breakpoints, and the worst-case approximation error. The lookup table satisfies the maximum acceptable error, maximum number of points, and breakpoint spacing that you specify.

In each case, `fixpt_look1_func_approx` interpolates between lookup table data points to optimize the fixed-point approximation. The inputs `xmin` and `xmax` specify the range over which to approximate the breakpoints. The inputs `xdt`, `xscale`, `ydt`, `yscale`, and `rndmeth` follow conventions used by fixed-point Simulink® blocks.

The inputs `errmax`, `nptsmax`, and `spacing` are optional. Of these inputs, you must specify at least `errmax` or `nptsmax`. If you omit one of those two inputs, you must use brackets, `[]`, in place of the omitted input. `fixpt_look1_func_approx` ignores that requirement for the lookup table.

If you do not specify spacing, and more than one spacing satisfies `errmax` and `nptsmax`, `fixpt_look1_func_approx` chooses in this order: power-of-2 spacing, even spacing, uneven spacing. This behavior applies when you specify both `errmax` and `nptsmax`, but not when you specify just one of the two.

## Input Arguments

`func`

Function of `x` for which to approximate breakpoints. Enclose this expression in single quotes, for example, `'sin(2*pi*x)'`.

`xmin`

Minimum value of `x`.

`xmax`

Maximum value of `x`.

`xdt`

Data type of `x`.

`xscale`

Scaling for the `x` values.

`ydt`

Data type of `y`.

`yscale`

Scaling for the `y` values.

`rndmeth`

Rounding mode supported by fixed-point Simulink blocks:

 `'Ceiling'` Round to the nearest representable number in the direction of positive infinity. `'Floor'` (default) Round to the nearest representable number in the direction of negative infinity. `'Nearest'` Round to the nearest representable number. `'Toward Zero'` Round to the nearest representable number in the direction of zero.

`errmax`

Maximum acceptable error between the ideal function and the approximation given by the lookup table.

`nptsmax`

Maximum number of points for the lookup table.

`spacing`

Spacing of breakpoints for the lookup table:

 `'even'` Even spacing `'pow2'` Even, power-of-2 spacing `'unrestricted'` (default) Uneven spacing

If you specify...The breakpoints of the lookup table...

`errmax` and `nptsmax`

Meet both criteria, if possible.

The `errmax` requirement has higher priority than `nptsmax`. If the breakpoints cannot meet both criteria with the specified spacing, `nptsmax` does not apply.

`errmax` only

Meet the error criteria, and `fixpt_look1_func_approx` returns the fewest number of points.

`nptsmax` only

Meet the points criteria, and `fixpt_look1_func_approx` returns the smallest worst-case error.

## Output Arguments

 `xdata` Vector of breakpoints for the lookup table. `ydata` Vector of values from applying the ideal function to the breakpoints. `errworst` Worst-case error, which is the maximum absolute error between the ideal function and the approximation given by the lookup table.

## Examples

Approximate a fixed-point sine function using a lookup table:

```func = 'sin(2*pi*x)'; % Define the range over which to optimize breakpoints xmin = 0; xmax = 0.25; % Define the data type and scaling for the inputs xdt = ufix(16); xscale = 2^-16; % Define the data type and scaling for the outputs ydt = sfix(16); yscale = 2^-14; % Specify the rounding method rndmeth = 'Floor'; % Define the maximum acceptable error errmax = 2^-10; % Choose even, power-of-2 spacing for breakpoints spacing = 'pow2'; % Create the lookup table [xdata,ydata,errworst] = fixpt_look1_func_approx(func,... xmin,xmax,xdt,xscale,ydt,yscale,rndmeth,errmax,[],spacing); ```