Documentation Center

  • Trial Software
  • Product Updates

Create Lookup Tables for a Sine Function

Introduction

The sections that follow explain how to use the function fixpt_look1_func_approx to create lookup tables. It gives examples that show how to create lookup tables for the function sin(2πx) on the interval from 0 to 0.25.

Parameters for fixpt_look1_func_approx

To use the function fixpt_look1_func_approx, you must first define its parameters. The required parameters for the function are

  • funcstr — Ideal function

  • xmin — Minimum input of interest

  • xmax — Maximum input of interest

  • xdt — x data type

  • xscale — x data scaling

  • ydt — y data type

  • yscale — y data scaling

  • rndmeth — Rounding method

In addition there are three optional parameters:

  • errmax — Maximum allowed error of the lookup table

  • nptsmax — Maximum number of points of the lookup table

  • spacing — Spacing allowed between breakpoints

You must use at least one of the parameters errmax and nptsmax. The next section, Setting Function Parameters for the Lookup Table, gives typical settings for these parameters.

Using Only errmax

If you use only the errmax parameter, without nptsmax, the function creates a lookup table with the fewest points, for which the worst-case error is at most errmax. See Using errmax with Unrestricted Spacing.

Using Only nptsmax

If you use only the nptsmax parameter without errmax, the function creates a lookup table with at most nptsmax points, which has the smallest worse case error. See Using nptsmax with Unrestricted Spacing.

The section Specifying Both errmax and nptsmax describes how the function behaves when you specify both errmax and nptsmax.

Spacing

You can use the optional spacing parameter to restrict the spacing between breakpoints of the lookup table. The options are

  • 'unrestricted' — Default.

  • 'even' — Distance between any two adjacent breakpoints is the same.

  • 'pow2' — Distance between any two adjacent breakpoints is the same and the distance is a power of two.

The section Restricting the Spacing and the examples that follow it explain how to use the spacing parameter.

Setting Function Parameters for the Lookup Table

To do the examples in this section, you must first set parameter values for the fixpt_look1_func_approx function. To do so, type the following at the MATLAB® prompt:

funcstr = 'sin(2*pi*x)'; %Define the sine function
xmin = 0; %Set the minimum input of interest
xmax = 0.25; %Set the maximum input of interest
xdt = ufix(16); %Set the x data type
xscale = 2^-16; %Set the x data scaling
ydt = sfix(16); %Set the y data type
yscale = 2^-14; %Set the y data scaling
rndmeth = 'Floor'; %Set the rounding method
errmax = 2^-10; %Set the maximum allowed error
nptsmax = 21; %Specify the maximum number of points

If you exit the MATLAB software after typing these commands, you must retype them before trying any of the other examples in this section.

Using errmax with Unrestricted Spacing

The first example shows how to create a lookup table that has the fewest data points for a specified worst-case error, with unrestricted spacing. Before trying the example, enter the same parameter values given in the section Setting Function Parameters for the Lookup Table, if you have not already done so in this MATLAB session.

You specify the maximum allowed error by typing

errmax = 2^-10;

Creating the Lookup Table

To create the lookup table, type

[xdata ydata errworst] = fixpt_look1_func_approx(funcstr, ...
xmin,xmax,xdt,xscale,ydt,yscale,rndmeth,errmax,[]);

Note that the nptsmax and spacing parameters are not specified.

The function returns three variables:

  • xdata, the vector of breakpoints of the lookup table

  • ydata, the vector found by applying ideal function sin(2πx) to xdata

  • errworst, which specifies the maximum possible error in the lookup table

The value of errworst is less than or equal to the value of errmax.

You can find the number of X data points by typing

length(xdata)
ans =

    16

This means that 16 points are required to approximate sin(2πx) to within the tolerance specified by errmax.

You can display the maximum error by typing errworst. This returns

errworst
errworst =

   9.7656e-04

Plotting the Results

You can plot the output of the function fixpt_look1_func_plot by typing

fixpt_look1_func_plot(xdata,ydata,funcstr,xmin,xmax,xdt, ...
xscale,ydt,yscale,rndmeth);

The upper plot shows the ideal function sin(2πx) and the fixed-point lookup approximation between the breakpoints. In this example, the ideal function and the approximation are so close together that the two graphs appear to coincide. The lower plot displays the errors.

In this example, the Y data points, returned by the function fixpt_look1_func_approx as ydata, are equal to the ideal function applied to the points in xdata. However, you can define a different set of values for ydata after running fixpt_look1_func_plot. This can sometimes reduce the maximum error.

You can also change the values of xmin and xmax in order to evaluate the lookup table on a subset of the original interval.

To find the new maximum error after changing ydata, xmin or xmax, type

errworst = fixpt_look1_func_plot(xdata,ydata,funcstr,xmin,xmax, ...
xdt,xscale,ydt,yscale,rndmeth)

Using nptsmax with Unrestricted Spacing

The next example shows how to create a lookup table that minimizes the worst-case error for a specified maximum number of data points, with unrestricted spacing. Before starting the example, enter the same parameter values given in the section Setting Function Parameters for the Lookup Table, if you have not already done so in this MATLAB session.

Setting the Number of Breakpoints

You specify the number of breakpoints in the lookup table by typing

nptsmax = 21;

Creating the Lookup Table

Next, type

[xdata ydata errworst] = fixpt_look1_func_approx(funcstr, ...
xmin,xmax,xdt,xscale,ydt,yscale,rndmeth,[],nptsmax);

The empty brackets, [], tell the function to ignore the parameter errmax, which is not used in this example. Omitting errmax causes the function fixpt_look1_func_approx to return a lookup table of size specified by nptsmax, with the smallest worst-case error.

The function returns a vector xdata with 21 points. You can find the maximum error for this set of points by typing errworst at the MATLAB prompt.

errworst
errworst =

   5.1139e-04

Plotting the Results

To plot the lookup table along with the errors, type

fixpt_look1_func_plot(xdata,ydata,funcstr,xmin,xmax,xdt, ...
xscale,ydt,yscale,rndmeth);

Restricting the Spacing

In the previous two examples, the function fixpt_look1_func_approx creates lookup tables with unrestricted spacing between the breakpoints. You can restrict the spacing to improve the computational efficiency of the lookup table, using the spacing parameter.

The options for spacing are

  • 'unrestricted' — Default.

  • 'even' — Distance between any two adjacent breakpoints is the same.

  • 'pow2' — Distance between any two adjacent breakpoints is the same and is a power of two.

Both power of two and even spacing increase the computational speed of the lookup table and use less command read-only memory (ROM). However, specifying either of the spacing restrictions along with errmax usually requires more data points in the lookup table than does unrestricted spacing to achieve the same degree of accuracy. The section Effects of Spacing on Speed, Error, and Memory Usage discusses the tradeoffs between different spacing options.

Using errmax with Even Spacing

The next example shows how to create a lookup table that has evenly spaced breakpoints and a specified worst-case error. To try the example, you must first enter the parameter values given in the section Setting Function Parameters for the Lookup Table, if you have not already done so in this MATLAB session.

Next, at the MATLAB prompt type

spacing = 'even';
[xdata ydata errworst] = fixpt_look1_func_approx(funcstr, ...
xmin,xmax,xdt,xscale,ydt,yscale,rndmeth,errmax,[],spacing);

You can find the number of points in the lookup table by typing:

length(xdata)
ans =

    20

To plot the lookup table along with the errors, type

fixpt_look1_func_plot(xdata,ydata,funcstr,xmin,xmax,xdt, ...
xscale,ydt,yscale,rndmeth);

Using nptsmax with Even Spacing

The next example shows how to create a lookup table that has evenly spaced breakpoints and minimizes the worst-case error for a specified maximum number of points. To try the example, you must first enter the parameter values given in the section Setting Function Parameters for the Lookup Table, if you have not already done so in this MATLAB session.

Next, at the MATLAB prompt type

spacing = 'even';
[xdata ydata errworst] = fixpt_look1_func_approx(funcstr, ...
xmin,xmax,xdt,xscale,ydt,yscale,rndmeth,[],nptsmax,spacing);

The result requires 21 evenly spaced points to achieve a maximum absolute error of 2^-10.2209.

To plot the lookup table along with the errors, type

fixpt_look1_func_plot(xdata,ydata,funcstr,xmin,xmax,xdt, ...
xscale,ydt,yscale,rndmeth);

Using errmax with Power of Two Spacing

The next example shows how to construct a lookup table that has power of two spacing and a specified worst-case error. To try the example, you must first enter the parameter values given in the section Setting Function Parameters for the Lookup Table, if you have not already done so in this MATLAB session.

Next, at the MATLAB prompt type

spacing = 'pow2';
[xdata ydata errworst] = ...
fixpt_look1_func_approx(funcstr,xmin, ...
xmax,xdt,xscale,ydt,yscale,rndmeth,errmax,[],spacing);

To find out how many points are in the lookup table, type

length(xdata)
ans =

    33

This means that 33 points are required to achieve the worst-case error specified by errmax. To verify that these points are evenly spaced, type

widths = diff(xdata)

This generates a vector whose entries are the differences between consecutive points in xdata. Every entry of widths is 2-7.

To find the maximum error for the lookup table, type

errworst
errworst =

   3.7209e-04

This is less than the value of errmax.

To plot the lookup table data along with the errors, type

fixpt_look1_func_plot(xdata,ydata,funcstr,xmin,xmax,xdt, ...
xscale,ydt,yscale,rndmeth);

Using nptsmax with Power of Two Spacing

The next example shows how to create a lookup table that has power of two spacing and minimizes the worst-case error for a specified maximum number of points. To try the example, you must first enter the parameter values given in the section Setting Function Parameters for the Lookup Table, if you have not already done so in this MATLAB session:

spacing = 'pow2';
[xdata ydata errworst] = ...
fixpt_look1_func_approx(funcstr,xmin, ...
xmax,xdt,xscale,ydt,yscale,rndmeth,errmax,[],spacing);

The result requires 17 points to achieve a maximum absolute error of 2^-9.6267.

To plot the lookup table along with the errors, type

fixpt_look1_func_plot(xdata,ydata,funcstr,xmin,xmax,xdt, ...
xscale,ydt,yscale,rndmeth);

Specifying Both errmax and nptsmax

If you include both the errmax and the nptsmax parameters, the function fixpt_look1_func_approx tries to find a lookup table with at most nptsmax data points, whose worst-case error is at most errmax. If it can find a lookup table meeting both conditions, it uses the following order of priority for spacing:

  1. Power of two

  2. Even

  3. Unrestricted

If the function cannot find any lookup table satisfying both conditions, it ignores nptsmax and returns a lookup table with unrestricted spacing, whose worst-case error is at most errmax. In this case, the function behaves the same as if the nptsmax parameter were omitted.

Using the parameters described in the section Setting Function Parameters for the Lookup Table, the following examples illustrate the results of using different values for nptsmax when you enter

[xdata ydata errworst] = fixpt_look1_func_approx(funcstr, ...
xmin,xmax,xdt,xscale,ydt,yscale,rndmeth,errmax,nptsmax);

The results for three different settings for nptsmax are as follows:

  • nptsmax = 33; — The function creates the lookup table with 33 points having power of two spacing, as in Example 3.

  • nptsmax = 21; — Because the errmax and nptsmax conditions cannot be met with power of two spacing, the function creates the lookup table with 20 points having even spacing, as in Example 5.

  • nptsmax = 16; — Because the errmax and nptsmax conditions cannot be met with either power of two or even spacing, the function creates the lookup table with 16 points having unrestricted spacing, as in Example 1.

Comparison of Example Results

The following table summarizes the results for the examples. Note that when you specify errmax, even spacing requires more data points than unrestricted, and power of two spacing requires more points than even spacing.

ExampleOptionsSpacingWorst-Case ErrorNumber of Points in Table

1

errmax=2^-10

'unrestricted'

2^-10

16

2

nptsmax=21

'unrestricted'

2^-10.933

21

3

errmax=2^-10

'even'

2^-10.0844

20

4

nptsmax=21

'even'

2^-10.2209

21

5

errmax=2^-10

'pow2'

2^-11.3921

33

6

nptsmax=21

'pow2'

2^-9.627

17

Was this topic helpful?