# Interpolation Using Prelookup

Use precalculated index and fraction values to accelerate approximation of N-dimensional function

Lookup Tables

## Description

The Prelookup block calculates the index and interval fraction that specify how its input value `u` relates to the breakpoint data set. The Interpolation Using Prelookup block works best with the Prelookup block. Feed the resulting index and fraction values into an Interpolation Using Prelookup block to interpolate an n-dimensional table. These two blocks have distributed algorithms. When combined together, they perform the same operation as the integrated algorithm in the n-D Lookup Table block. However, the Prelookup and Interpolation Using Prelookup blocks offer greater flexibility that can provide more efficient simulation and code generation. For more information, see Efficiency of Performance in the Simulink® documentation.

### Supported Block Operations

To use the Interpolation Using Prelookup block, you specify a set of table data values directly on the dialog box or feed values into the `T` input port. Typically, these table values correspond to the breakpoint data sets specified in Prelookup blocks. The Interpolation Using Prelookup block generates output by looking up or estimating table values based on index and interval fraction values fed from Prelookup blocks. Labels for the index and interval fraction appear as `k` and `f` on the Interpolation Using Prelookup block icon.

When inputs for index and interval fraction...The Interpolation Using Prelookup block...
Map to values in breakpoint data setsOutputs the table value at the intersection of the row, column, and higher dimension breakpoints
Do not map to values in breakpoint data sets, but are within rangeInterpolates appropriate table values, using the Interpolation method you select
Do not map to values in breakpoint data sets, and are out of rangeExtrapolates the output value, using the Extrapolation method you select

### How The Block Interpolates a Subset of Table Data

You can use the Number of sub-table selection dimensions parameter to specify that interpolation occur only on a subset of the table data. To activate this interpolation mode, set this parameter to a positive integer. This value defines the number of dimensions to select, starting from the highest dimension of table data. Therefore, the value must be less than or equal to the Number of table dimensions.

Suppose that you have 3-D table data in your Interpolation Using Prelookup block. This behavior applies.

Number of Selection DimensionsAction by the BlockBlock Appearance
0Interpolates the entire table and does not activate subtable selectionDoes not change
1Interpolates the first two dimensions and selects the third dimensionDisplays an input port with the label `sel1` that you use to select and interpolate 2-D tables
2Interpolates the first dimension and selects the second and third dimensionsDisplays two input ports with the labels `sel1` and `sel2` that you use to select and interpolate 1-D tables

Subtable selection uses zero-based indexing. For an example of interpolating a subset of table data, type `sldemo_bpcheck` at the MATLAB® command prompt.

## Data Type Support

The Interpolation Using Prelookup block accepts as input:

• Real signals of any numeric data type supported by Simulink software, except Boolean.

• Fixed-point data types for signals, table data, and intermediate results

• Bus data types

## Parameters

### Main Tab

Number of dimensions

Specify the number of dimensions that the table data must have. This value defines the number of independent variables for the table. Enter an integer between `1` and `30` into this field.

Require index and fraction as bus

Input index and fraction signals to a bus port, with the index and fraction signals combined in a nonvirtual bus.

If you select this parameter, subtable selection ports continue to work in nonbus mode.

To enable the Prelookup block to supply input to the Interpolation Using Prelookup block, set:

• Output selection to ```Index and fraction as bus```

• Output to ```Bus: <object name>```, where `<object name>` must be a valid bus object name accessible to the model

Specification

Specify whether to enter table data directly or use a lookup table object. If you set this parameter to:

• `Explicit values`, the Source and Value parameters are visible on the dialog box.

• `Lookup table object`, the Name parameter is visible on the dialog box.

Source

Specify whether to enter table data in the dialog box or to inherit the data from an input port. This parameter is available when Specification is set to `Explicit values`. If you set Source to:

• `Dialog`, enter table data in the text box under Value.

• `Input port`, verify that an upstream signal supplies table data to the table input port.

Value

If you set Specification to ```Explicit values``` and Source to `Dialog`, explicitly specify the table data.

You can edit the block diagram without specifying a correctly dimensioned matrix by entering an empty matrix (`[]`) or an undefined workspace variable in the Value edit field. For information about how to construct multidimensional arrays in MATLAB, see Multidimensional Arrays (MATLAB).

If you set Source to ```Input port```, verify that an upstream signal supplies table data to the T input port. The size of table data must match the Number of table dimensions. For this option, your block inherits table attributes from the T input port.

To edit lookup tables using the Lookup Table Editor, click (see Edit Lookup Tables in the Simulink documentation).

Name

Specify the name of an existing `Simulink.LookupTable` object. An existing lookup table object references Simulink breakpoint objects.

Interpolation method

Select `Flat`, `Nearest`, or `Linear`. See Interpolation Methods in the Simulink documentation for more information.

Extrapolation method

Select `Clip` or `Linear`. See Extrapolation Methods in the Simulink documentation for more information. The Extrapolation method parameter is visible only when you select `Linear` as the Interpolation method parameter.

The Interpolation Using Prelookup block does not support `Linear` extrapolation when the input or output signals specify integer or fixed-point data types.

Valid index input may reach last index

Specify how block inputs for index (`k`) and interval fraction (`f`) access the last elements of n-dimensional table data. Index values are zero based.

Check BoxBlock Behavior

Selected

Returns the value of the last element in a dimension of its table when:

• `k` indexes the last table element in the corresponding dimension

• `f` is `0`

Cleared

Returns the value of the last element in a dimension of its table when:

• `k` indexes the next-to-last table element in the corresponding dimension

• `f` is `1`

This check box is visible only when:

• Interpolation method is `Linear`.

• Extrapolation method is `Clip`.

### Tip

When you select Valid index input may reach last index for an Interpolation Using Prelookup block, you must also select Use last breakpoint for input at or above upper limit for all Prelookup blocks that feed it. This action allows the blocks to use the same indexing convention when accessing the last elements of their breakpoint and table data sets.

Diagnostic for out-of-range input

Specify whether to produce a warning or error when the input `k` or `f` is out of range. Options include:

• `None` — Simulink software takes no action.

• `Warning` — Simulink software displays a warning and continues the simulation.

• `Error` — Simulink software terminates the simulation and displays an error.

Remove protection against out-of-range index in generated code

Specify whether or not to include code that checks for out-of-range index inputs.

Check BoxResultWhen to Use

Selected

Generated code does not include conditional statements to check for out-of-range index inputs.

When the input `k` or `f` is out-of-range, it may cause undefined behavior for generated code and simulations using accelerator mode.

For code efficiency

Cleared

Generated code includes conditional statements to check for out-of-range index inputs.

For safety-critical applications

If your input is not out-of-range, you can select the Remove protection against out-of-range index in generated code check box for code efficiency. By default, this check box is cleared. For safety-critical applications, do not select this check box. If you want to select the Remove protection against out-of-range index in generated code check box, first check that your model inputs are in range. For example:

1. Clear the Remove protection against out-of-range index in generated code check box.

2. Set the Diagnostic for out-of-range input parameter to `Error`.

3. Simulate the model in normal mode.

4. If there are out-of-range errors, fix them to be in range and run the simulation again.

5. When the simulation no longer generates out-of-range input errors, select the Remove protection against out-of-range index in generated code check box.

### Note

When you select the Remove protection against out-of-range index in generated code check box and the input `k` or `f` is out-of-range, the behavior is undefined for generated code and simulations using accelerator mode.

Depending on your application, you can run the following Model Advisor checks to verify the usage of this check box:

• By Product > Embedded Coder > Identify lookup table blocks that generate expensive out-of-range checking code

• By Product > Simulink Check > Modeling Standards > DO-178C/DO-331 Checks > Check usage of lookup table blocks

This check box has no effect on the generated code when one of the following is true:

• The Prelookup block feeds index values to the Interpolation Using Prelookup block.

Because index values from the Prelookup block are always valid, no check code is necessary.

• The data type of the input `k` restricts the data to valid index values.

For example, unsigned integer data types guarantee nonnegative index values. Therefore, unsigned input values of `k` do not require check code for negative values.

Number of sub-table selection dimensions

Specify the number of dimensions of the subtable that the block uses to compute the output. Follow these rules:

• To enable subtable selection, enter a positive integer.

This integer must be less than or equal to the Number of table dimensions.

• To disable subtable selection, enter `0` to interpolate the entire table.

For more information, see How The Block Interpolates a Subset of Table Data.

Sample time

### Note

This parameter is not visible in the block dialog box unless it is explicitly set to a value other than `-1`. To learn more, see Blocks for Which Sample Time Is Not Recommended.

### Note

The parameters for table attributes (data type, minimum, and maximum) are not available when you set Source to ```Input port```. In this case, the block inherits all table attributes from the `T` input port.

Table data > Data Type

Specify the table data type. You can set it to:

• A rule that inherits a data type, for example, ```Inherit: Same as output```

• The name of a built-in data type, for example, `single`

• The name of a data type object, for example, a `Simulink.NumericType` object

• An expression that evaluates to a data type, for example, `fixdt(1,16,0)`

Click the button to display the Data Type Assistant, which helps you set the table data type.

### Tip

Specify a table data type different from the output data type for these cases:

• Lower memory requirement for storing table data that uses a smaller type than the output signal

• Sharing of prescaled table data between two Interpolation Using Prelookup blocks with different output data types

• Sharing of custom storage table data in Simulink Coder™ generated code for blocks with different output data types

Table data > Minimum

Specify the minimum value for table data. The default value is `[]` (unspecified).

Table data > Maximum

Specify the maximum value for table data. The default value is `[]` (unspecified).

Intermediate results > Data Type

Specify the intermediate results data type. You can set it to:

• A rule that inherits a data type, for example, ```Inherit: Same as output```

• The name of a built-in data type, for example, `single`

• The name of a data type object, for example, a `Simulink.NumericType` object

• An expression that evaluates to a data type, for example, `fixdt(1,16,0)`

Click the button to display the Data Type Assistant, which helps you set the intermediate results data type.

### Tip

Use this parameter to specify higher precision for internal computations than for table data or output data.

Output > Data Type

Specify the output data type. You can set it to:

• A rule that inherits a data type, for example, ```Inherit: Inherit via back propagation```

• The name of a built-in data type, for example, `single`

• The name of a data type object, for example, a `Simulink.NumericType` object

• An expression that evaluates to a data type, for example, `fixdt(1,16,0)`

Click the button to display the Data Type Assistant, which helps you set the output data type.

Output > Minimum

Specify the minimum value that the block should output. The default value is `[]` (unspecified). Simulink software uses this value to perform:

Output > Maximum

Specify the maximum value that the block should output. The default value is `[]` (unspecified). Simulink software uses this value to perform:

Internal rule priority

Specify the internal rule for intermediate calculations. Select `Speed` for faster calculations. If you do, a loss of accuracy might occur, usually up to 2 bits.

Lock data type settings against changes by the fixed-point tools

Select to lock all data type settings of this block against changes by the Fixed-Point Tool and the Fixed-Point Advisor. For more information, see Lock the Output Data Type Setting (Fixed-Point Designer).

Integer rounding mode

Specify the rounding mode for fixed-point operations. For more information, see Rounding (Fixed-Point Designer) in the Fixed-Point Designer™ documentation.

Block parameters always round to the nearest representable value. To control the rounding of a block parameter, enter an expression using a MATLAB rounding function in the mask field.

Saturate on integer overflow

ActionReasons for Taking This ActionWhat Happens for OverflowsExample

Select this check box.

Your model has possible overflow, and you want explicit saturation protection in the generated code.

Overflows saturate to either the minimum or maximum value that the data type can represent.

The maximum value that the `int8` (signed, 8-bit integer) data type can represent is 127. Any block operation result greater than this maximum value causes overflow of the 8-bit integer. With the check box selected, the block output saturates at 127. Similarly, the block output saturates at a minimum output value of -128.

Do not select this check box.

You want to optimize efficiency of your generated code.

You want to avoid overspecifying how a block handles out-of-range signals. For more information, see Check for Signal Range Errors.

Overflows wrap to the appropriate value that is representable by the data type.

The maximum value that the `int8` (signed, 8-bit integer) data type can represent is 127. Any block operation result greater than this maximum value causes overflow of the 8-bit integer. With the check box cleared, the software interprets the overflow-causing value as `int8`, which can produce an unintended result. For example, a block result of 130 (binary 1000 0010) expressed as `int8`, is -126.

When you select this check box, saturation applies to every internal operation on the block, not just the output or result. Usually, the code generation process can detect when overflow is not possible. In this case, the code generator does not produce saturation code.

## Examples

### Prelookup with External Breakpoint Specification

In the following model, a Constant block feeds the table data values to the `T` input port of the Interpolation Using Prelookup block.

The Interpolation Using Prelookup block inherits the following table attributes from the `T` input port:

Table AttributeValue
Minimum`Inf`
Maximum`Inf`
Data type`single`

Similarly, a Constant block feeds the breakpoint data set to the `bp` input port of the Prelookup block, which inherits the following breakpoint attributes:

Breakpoint AttributeValue
Minimum`Inf`
Maximum`Inf`
Data type`single`

Simulink uses double-precision, floating-point data to perform the computations in this model. However, the model stores the breakpoint and table data as single-precision, floating-point data. Using a lower-precision data type to store breakpoint and table data reduces the memory requirement.

For other examples, see Prelookup and Interpolation Blocks in the Simulink documentation.

### Use a Bus for Index and Fraction

You can use a bus object to specify the index (`k`) and fraction (`f`) in the Prelookup block and then use that bus as input to an Interpolation Using Prelookup block. This example shows how to use a bus signal and how to get the same results without using a bus object.

Open the model and simulate it.

```open_system(docpath(fullfile(docroot,'toolbox','simulink',... 'examples','ex_prelookup_bus.slx'))) sim('ex_prelookup_bus')```

At the top of the model, open the `Prelookup` block parameters dialog box. In the Main tab, note that Output selection is set to ```Index and fraction as bus```.

In the Data Types tab, note that Output is set to `Bus: myBus`.

In the Simulink Editor, select File > Model Properties and open the Callbacks tab. The code defines the bus object `myBus`, which specifies the index as the first bus element and the fraction as the second element.

In the `Interpoloation Using Prelookup` block parameters dialog box, in the Main tab, note that Require index and fraction as bus check box is selected. That option configures the block to use the bus output from the Prelookup block.

## Characteristics

 Data Types Double | Single | Base Integer | Fixed-Point Sample Time Inherited from driving block Direct Feedthrough Yes Multidimensional Signals Yes Variable-Size Signals No Zero-Crossing Detection No Code Generation Yes