Lookup Table and Function Blocks

Simulink includes several types of lookup table blocks that implement a variety of algorithms for approximating functions via table indexing, table interpolation, and subtable manipulation. Additional capability is obtained using the various function blocks. Here are some example applications for lookup table blocks to give you ideas for using them in your own designs.


Model the behavior of a Type S thermocouple and implement input signal conversion.
Approximating Nonlinear Relationships: Type S thermocouple example

Explore several techniques for designing and generating waveforms.
Waveform Generation: Sine Waves and Distortion Measurement

Shows the options available from the Lookup Table n-d block using a 2-D data set.
Interpolation Algorithms and Trajectory Animation

Shows uses for the lookup Table Editor.
Lookup Table Editor

Using pre-lookup index search and interpolation n-D blocks.
Optimize with prelookup and sub-table interpolation blocks

Check a model that has pre-lookup index search and interpolation n-D blocks for data mismatches.
Breakpoint Data Management

Selection Guide for Using Lookup Table and Function Blocks

To assist in matching blocks with applications, the following table summarizes the capabilities of these lookup table and function blocks in Simulink; a detailed selection guide follows:


Operation 1-D 2-D dyn n-D direct pre intrp poly fcn eM
Linear interpolation 1-D 2-D 1-D n-D - Y n-D - - Y
Flat (or direct) lookup (+various) 1-D+ 2-D+ 1-D+ n-D Y Y n-D - - Y
Cubic spline interpolation - - - n-D - - - - - *
Polynomial evaluation - - - - - - - Y Y Y
double, single floating point types Y Y Y Y Y Y Y Y Y Y
integer, boolean data types Y Y Y - Y - - - - *
Fixed-point data types Y Y Y - - - - - - -
Complex table data - - - - Y - - - - *
Wide signals, scalar expansion Y Y Y - Y Y Y Y - *
Table as an input (optional) - - Y - (Y) - - - - *
Range checking diagnostics - - - Y Y Y Y - - -
Clip inputs to data range Y Y Y Y Y Y Y - - Y
Extrapolate data range Y' Y' Y' Y Y Y Y Y Y Y
Mixed clipping and extrapolation - - - - - Y Y - - *
Optimization, Code Generation, and Verification Options 1-D 2-D dyn n-D direct pre intrp poly fcn eM
Binary index search Y Y Y Y - Y Y - - *
Linear index search - - - Y - Y Y - - *
Evenly spaced points - - - Y Y Y Y - - *
Power-of-2 spaced points (speed) Y' Y' - - - - - - - *
Mixed index search algorithms - - - - - Y Y - - *
Start at previous index (speed) - - - Y - Y Y - - *
Share index search result (speed) - - - - - Y Y - - *
Sub-table selection (speed) - - - - Y Y Y - - *
Suppress RTW range checks (speed) - - - - - - Y - - *
MISRA C compliant code generation Y' Y' Y' Y' Y Y' Y Y Y *
Suitable for use in embedded applications Y Y Y Y Y Y Y Y Y Y
Coverage tool: table coverage Y Y Y Y Y Y Y - - -

Key: Y : yes/recommended, * : yes with extra effort, - : No or N/A, ' : limitations

Details on Limitations:

If you wish to develop your own algorithms, the Embedded MATLAB block offers a tremendous amount of flexibility with built-in support for Real-Time Workshop and the Real-Time Workshop Embedded Coder. When you need to incorporate hand-written code into your designs, the S-function Builder block offers capabilities for integrating C, C++, Ada, and Fortran; it supports efficient code generation as well.

Lookup Tables and Function Approximation

When available, a full numerical expression will typically give the highest fidelity reproduction of a function. An example of this is the y = sin(u) function. As this computation can take 100 or more clock cycles on a computer, a faster way to compute sin(u) is often needed. If the function is periodic and is computed by a sampled discrete system whose sample rate evenly divides into the function's period, then the function can be approximated most efficiently by accessing a sequence of stored function values precalculated for each sample time in the function's period.

The most basic table lookup operation is merely a table selection. The "Direct Lookup Table n-D" block can efficiently access table elements, columns, and planes by their index, just like these simple array access examples in MATLAB:

y = A(u) for an element in a 1-D array,
y = A(u1,u2,u3,u4) in a 4-D array,
y = A(:,u) for selecting an entire column, and
y = A(:,:,u) for selecting a 2-D plane in a 3-D array.

This technique is not always practical, as the required point spacing needed to achieve an accuracy requirement may require a table that is too big to fit into memory. A common choice in this case is the interpolated table lookup.

Interpolation: choosing between the "Look-up Table n-D" block and the pair of blocks: "Pre-Lookup Index Search", "Interpolation n-D Using Pre-Lookup"

These blocks use the same algorithms for breakpoint searches and interpolations and give equivalent results from a numerical point of view. When generating code for Real-Time Workshop, identical parameter values can be pooled for either approach. The main differences are threefold.

One difference is that the breakpoint search algorithm options for Look-Up Table n-D apply to all inputs simultaneously, while the options can be applied individually to inputs using the Pre-Lookup Index Search block. As an example, if a 2-D table has variable spacing for one breakpoint set and even spacing for the other, you must use either linear or binary index search to find the breakpoint intervals when using the Look-Up Table n-D. With the pre-lookup, the index search method can be different for each breakpoint set, along with the clipping/extrapolation option, and the "search using previous index result" option.

The second difference is potential speed improvements: when the pre-lookup index search operations consume a major portion of execution time (as they frequently do), multiple interpolation n-D blocks can be fed the results from one pre-lookup operation, reducing repetitive calculations.

The third difference is an alternative algorithm choice with the interpolation n-D block. For Release 14, a sub-table selection algorithm is available to dramatically improve speed for certain cases. See the Sub-Table Selection section below.

Choice of index search algorithm

If the input signal can change to cause movement by more than 1-2 intervals per time step for variably spaced breakpoint sets (including software resets, etc.), the binary search algorithm may be the better choice. A binary search has the fastest worst-case convergence (log2(N-1) iterations) compared to a linear search (N-1 iterations). However, if the input signal changes so slowly that it cannot realistically traverse more than 1-2 intervals per time step, then using a linear index search with the "Begin index search using previous index result" option will cause the search to start on the interval found during the previous execution of the block. When the input value is in the same interval as the input value from the previous execution of the block, the search will complete in one pass. Conversely, the binary search algorithm can use the maximum number of iterations, log2(N-1), just to move from one interval to the next "jittery" execution latency.

Finally, if the breakpoints can be made evenly spaced, the "Evenly spaced points" index search method can be very deterministic. It can also be very fast on most modern processors. One numerically stable method to create N evenly spaced points in MATLAB is:

>> pts = M*[0:(N-1)]+B

where B is the bias and M is the spacing. Avoid using formulations like:

>> not_recommended = [B:M:(M*(N-1)+B)]

as the spacing is typically not as uniform as with the first method above.

Using pre-lookup index search blocks with a 2-D or higher table in the interpolation n-D block allows for pre-lookup blocks specifying different index search algorithms. In this way, if one input dimension of the table is connected to a rapidly changing signal and the other input dimension is connected to a slowly changing signal, the pre-lookup blocks can be set to use a binary search and a linear search, respectively, to give computational efficiency.

Common input signals

If two or more tables share the same input signals, is it practical to have one or two breakpoint sets for all the tables using this signal? If so, then you should consider using the pre-lookup index search. By avoiding repeated index searches, the overall application can run faster.

Clipping with the pre-lookup block

If using the pre-lookup index search block to perform clipping and then feeding its output signals directly into one or more interpolation n-D blocks, it is now possible to turn off clipping and index checking for code generation in the interpolation n-D blocks if all the inputs are clipped prelookup index search blocks. This eliminates the generation of what is essentially unreachable code in checking to ensure the index is valid for the breakpoint array.

It is important when using this option that the breakpoint array is correct and has the same number of elements as the corresponding dimension in the table. If you choose "no index checking in the generated code" and the breakpoint index is longer than the number of points in the table, an array bounds read error can occur, resulting in wrong answers or even segmentation violations in code generated by Real-Time Workshop. If the breakpoint array is correct, there is no possibility of this type of behavior.

Sub-table selection

If some dimensions of the lookup table are pure selections and are not to be interpolated, use the interpolation n-D block's "Number of sub-table selection ports" to specify the number of dimensions that are index-only. For example, if you have a 3-D table that is really meant to be a stack of 2-D interpolation tables, the last dimension of the table can be marked as a "selection dimension", meaning that the input for that dimension is an integer used only to make a sub-table selection, such as picking a 2-D plane from a 3-D table. The sub-table is then interpolated normally.

This way, only a 2-D interpolation is performed instead of a 3-D interpolation. Since an N-dimensional interpolation takes (2^N)-1 individual interpolation operations

(y_interp = ylow + f*(yhigh-ylow)),

even just one dimension of selection can almost double the speed of the interpolation. Extreme example: a 5-D table with 3 dimensions of sub-table selection and 2-D interpolation: 5-D interpolation would take 2^5-1 = 31 interpolations, but a 2-D interpolation takes only 2^2 - 1 = 3.



See also:

  1. "Real-Time Workshop User's Guide", Chapter 8, "Optimizing the Model for Code Generation", section "Look-Up Tables and Polynomials".


Related Products

MathWorks offers several products which may come in handy during work in designing implementations of nonlinear functional relationships. For further information see also: