Main Content

fixed.interp1

1-D data interpolation (table lookup)

Since R2024a

Description

vq = fixed.interp1(x,v,xq) returns interpolated values of a 1-D function at specific query points using linear interpolation. Vector x contains the coordinates of the sample points and v contains the corresponding function values at each sample point. The variable xq contains the coordinates of the query points.

If you have multiple sets of data that are sampled at the same point coordinates, then you can pass v as an array. Each column of array v contains a different set of 1-D sample values.

example

vq = fixed.interp1(v,xq) returns interpolated values and assumes a default set of sample point coordinates. The default points are the sequence of numbers from 1 to n, where n depends on the shape of v:

  • When v is a vector, the default points are 1:length(v).

  • When v is an array, the default points are 1:size(v,1).

Use this syntax when you are not concerned about the absolute distances between points.

vq = fixed.interp1(___,method) specifies an alternative interpolation method: "linear", "nearest", "previous", or "next". The default method is "linear".

vq = fixed.interp1(___,method,extrapval) specifies extrapval, a scalar value that is assigned to all queries that lie outside the domain of the sample points.

Examples

collapse all

This example shows how to implement a one-dimensional fixed-point lookup table using fixed.interp1.

Run the example multiple times to see the approximation over different query points.

Create Lookup Table for Function

Define a function f(x) to replace with a lookup table approximation.

clearvars
f = @(x) exp(x);

Define breakpoints x for the lookup table. Use the following values, which were computed to estimate exp(x) by the Lookup Table Optimizer and targeted 16-bit fixed-point types and on-curve table values.

x = [-11, -6.80029296875, -5.49609375, -4.708984375, -4.1484375, -3.70849609375, ...
    -3.3466796875, -3.04150390625, -2.7763671875, -2.54150390625, -2.33251953125, ...
    -2.142578125, -1.96875, -1.8095703125, -1.66259765625, -1.525390625, -1.3974609375, ...
    -1.27685546875, -1.16357421875, -1.05712890625, -0.95556640625, -0.85791015625, ...
    -0.76611328125, -0.677734375, -0.5927734375, -0.51171875, -0.43359375, -0.35791015625, ...
    -0.28515625, -0.21533203125, -0.1484375, -0.08349609375, -0.0205078125, 0];

Generate on-curve table values v corresponding to the breakpoints.

v = f(x);

Plot the table values and notice that the breakpoints x are not linearly spaced.

plot(x,v,'o-')

Figure contains an axes object. The axes object contains an object of type line.

Query Lookup Table

Choose a random query point xq in the range of x.

xq = fixed.example.realUniformRandomArray(x(1),x(end),1);

Cast the inputs to 16-bit fixed-point.

x = fi(x);
v = fi(v);
xq = fi(xq);

The fixed.interp1 function computes vq, the lookup table approximation of f(xq). That is, vqf(xq).

vq = fixed.interp1(x,v,xq)
vq = 
    0.1307

          DataTypeMode: Fixed-point: binary point scaling
            Signedness: Signed
            WordLength: 16
        FractionLength: 14

Compare Lookup Approximation to Actual Function Value

Compare vq to the actual function evaluation f(xq).

vq_expected = f(double(xq))
vq_expected = 
0.1303
err = double(vq) - vq_expected
err = 
4.5947e-04

Plot f(x).

clf;
plot(x,v)
xlabel('x')
ylabel('v')
hold on

Plot vq, the lookup table approximation of f(xq), using a red stem plot.

stem(xq,vq,'Filled','red')
legend('f(x)','vq = Fixed-point lookup-table approximation of f(xq)','location','best')

Figure contains an axes object. The axes object with xlabel x, ylabel v contains 2 objects of type line, stem. These objects represent f(x), vq = Fixed-point lookup-table approximation of f(xq).

Input Arguments

collapse all

Sample points, specified as a row or column vector of real numbers. The values in x must be strictly monotonically increasing. The length of x must conform to one of the following requirements:

  • If v is a vector, then length(x) must equal length(v).

  • If v is an array, then length(x) must equal size(v,1).

The inputs x, v, and xq must be the same data type: fi, half, single, or double. When using fi data, you can use the shortened function name interp1.

Example: fi(1:10,0,8,0)

Example: half(1:10)

Data Types: fi | single | double

Sample values, specified as a vector, matrix, or array of real or complex numbers. If v is a matrix or an array, then each column contains a separate set of 1-D values.

If v contains complex numbers, then fixed.interp1 interpolates the real and imaginary parts separately.

The inputs x, v, and xq must be the same data type: fi, half, single, or double. When using fi data, you can use the shortened function name interp1.

Example: fi(rand(10,1),0,12,8)

Example: half(rand(10,1))

Data Types: fi | single | double
Complex Number Support: Yes

Query points, specified as a scalar, vector, matrix, or array of real numbers.

The inputs x, v, and xq must be the same data type: fi, half, single, or double. When using fi data, you can use the shortened function name interp1.

Example: fi(5,0,12,8)

Example: half(5)

Example: half(1:0.05:10)

Example: half((1:0.05:10)')

Example: half([0 1 2 7.5 10])

Data Types: fi | single | double

Interpolation method, specified as one of the options in this table.

Method

Description

Continuity

Comments

"linear"

Linear interpolation. The interpolated value at a query point is based on linear interpolation of the values at neighboring grid points in each respective dimension. This method is the default interpolation method.

C0

  • Requires at least 2 points

  • Requires more memory and computation time than nearest neighbor

"nearest"

Nearest neighbor interpolation. The interpolated value at a query point is the value at the nearest sample grid point.

Discontinuous

  • Requires at least 2 points

  • Modest memory requirements

  • Fastest computation time

"next"

Next neighbor interpolation. The interpolated value at a query point is the value at the next sample grid point.

Discontinuous

  • Requires at least 2 points

  • Same memory requirements and computation time as "nearest"

"previous"

Previous neighbor interpolation. The interpolated value at a query point is the value at the previous sample grid point.

Discontinuous

  • Requires at least 2 points

  • Same memory requirements and computation time as "nearest"

Function value outside the domain of x, specified as a real or complex scalar. fixed.interp1 returns this constant value for all points outside the domain of x. If the scalar value is nonzero and outside the range of the sample values v, then this value is set to the minimum or maximum value of v, whichever is closer.

The data type of extrapval must be the same as x, v, and xq.

The default behavior with fi input data is to return 0 for query points outside the domain. The default behavior with half, single, or double input data is to return NaN for query points outside the domain.

Example: fi(5,0,12,8)

Example: half(5)

Data Types: fi | single | double

Note

The default behavior of the interp1 function is to return NaN when a query point is outside the domain. The fixed.interp1 function with fi input data is not consistent with this behavior because fi casts NaN to 0.

Output Arguments

collapse all

Interpolated values, returned as a scalar, vector, matrix, or array. The size of vq depends on the shape of v and xq. The data type of vq is the same as that of the sample values v.

Shape of vShape of xqSize of VqExample
VectorVectorsize(xq)If size(v) = [1 100]
and size(xq) = [1 500],
then size(vq) = [1 500].
VectorMatrix
or N-D Array
size(xq)If size(v) = [1 100]
and size(xq) = [50 30],
then size(vq) = [50 30].
Matrix
or N-D Array
Vector[length(xq) size(v,2),...,size(v,n)]If size(v) = [100 3]
and size(xq) = [1 500],
then size(vq) = [500 3].
Matrix
or N-D Array
Matrix
or N-D Array
[size(xq,1),...,size(xq,n),... size(v,2),...,size(v,m)]If size(v) = [4 5 6]
and size(xq) = [2 3 7],
then size(vq) = [2 3 7 5 6].

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

HDL Code Generation
Generate VHDL, Verilog and SystemVerilog code for FPGA and ASIC designs using HDL Coder™.

Version History

Introduced in R2024a