Main Content

fixed.interp2

Interpolation for 2-D gridded data in meshgrid format

Since R2024a

    Description

    example

    Vq = fixed.interp2(X,Y,V,Xq,Yq) returns interpolated values of a function of two variables at specific query points using linear interpolation. The results always pass through the original sampling of the function. X and Y contain the coordinates of the sample points. V contains the corresponding function values at each sample point. Xq and Yq contain the coordinates of the query points.

    Vq = fixed.interp2(V,Xq,Yq) assumes a default grid of sample points. The default grid points cover the rectangular region, X=1:n and Y=1:m, where [m,n] = size(V). Use this syntax when you want to conserve memory and are not concerned about the absolute distances between points.

    Vq = fixed.interp2(___,method) specifies an alternative interpolation method: "linear" or "nearest". The default method is "linear".

    Vq = fixed.interp2(___,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 two-dimensional lookup table using fixed.interp2.

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

    Create Lookup Table for Function

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

    clearvars
    f = @(x,y) sin(x)+sin(y)-(x.^2+y.^2)/20;

    Define breakpoints x and y for the lookup table. Note that m and n do not have to be equal and x and y do not have to be linearly spaced.

    m = 16;
    n = 16;
    x = linspace(-5,5,n);
    y = linspace(-5,5,m);
    [X,Y] = meshgrid(x,y);

    Generate lookup table values V corresponding to the breakpoints.

    V = f(X,Y);

    Query Lookup Table

    Choose a random query point (xq,yq) in the ranges of x and y.

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

    Cast the inputs to 16-bit fixed-point.

    x = fi(x);
    y = fi(y);
    V = fi(V);
    xq = fi(xq);
    yq = fi(yq);

    The fixed.interp2 function computes vq, the lookup table approximation of f(xq,yq).

    vq = fixed.interp2(x,y,V,xq,yq)
    vq = 
       -2.0808
    
              DataTypeMode: Fixed-point: binary point scaling
                Signedness: Signed
                WordLength: 16
            FractionLength: 12
    

    Compare Lookup Approximation to Actual Function Value

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

    vq_expected = f(double(xq),double(yq))
    vq_expected = -2.1175
    
    err = double(vq) - vq_expected
    err = 0.0367
    

    Plot f(x,y).

    clf
    mesh(x,y,V,'EdgeAlpha',0.8,'FaceAlpha',0);
    xlabel('x')
    ylabel('y')
    zlabel('v')
    hold on

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

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

    Input Arguments

    collapse all

    Sample grid points, specified as real matrices or vectors. The sample grid points must be strictly monotonically increasing in both dimensions.

    • If X and Y are matrices, then they contain the coordinates of a full grid (in meshgrid format). Use the meshgrid function to create the X and Y matrices together. Both matrices must be the same size.

    • If X and Y are vectors, then they are treated as grid vectors. The values in both vectors must be strictly monotonically increasing.

    The inputs [X,Y], V, and [Xq,Yq] must be the same data type: fi, half, single, or double. When using fi data, you can use the shortened function name interp2.

    Example: [X,Y] = fi(meshgrid(1:30,-10:10),0,12,8)

    Example: [X,Y] = half(meshgrid(1:30,-10:10))

    Data Types: fi | single | double

    Sample values, specified as a real or complex matrix. The size requirements for V depend on the size of X and Y:

    • If X and Y are matrices representing a full grid (in meshgrid format), then V must be the same size as X and Y.

    • If X and Y are grid vectors, then V must be a matrix containing length(Y) rows and length(X) columns.

    If V contains complex numbers, then fixed.interp2 interpolates the real and imaginary parts separately.

    The inputs [X,Y], V, and [Xq,Yq] must be the same data type: fi, half, single, or double. When using fi data, you can use the shortened function name interp2.

    Example: fi(rand(10))

    Example: half(rand(10))

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

    Query points, specified as a real scalars, vectors, matrices, or arrays.

    • If Xq and Yq are scalars, then they are the coordinates of a single query point.

    • If Xq and Yq are vectors of different orientations, then Xq and Yq are treated as grid vectors.

    • If Xq and Yq are vectors of the same size and orientation, then Xq and Yq are treated as scattered points in 2-D space.

    • If Xq and Yq are matrices, then they represent either a full grid of query points (in meshgrid format) or scattered points.

    • If Xq and Yq are N-D arrays, then they represent scattered points in 2-D space.

    The inputs [X,Y], V, and [Xq,Yq] must be the same data type: fi, half, single, or double. When using fi data, you can use the shortened function name interp2.

    Example: [Xq,Yq] = fi(meshgrid((1:0.1:10),(-5:0.1:0)))

    Data Types: fi | single | double

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

    MethodDescriptionContinuityComments
    "linear"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 two grid points in each dimension

    • Requires more memory than "nearest"

    "nearest"The interpolated value at a query point is the value at the nearest sample grid point. Discontinuous
    • Requires two grid points in each dimension.

    • Fastest computation with modest memory requirements

    Function value outside the domain of X and Y, specified as a real or complex scalar. fixed.interp2 returns this constant value for all points outside the domain of X and Y. If the scalar value is nonzero and outside the range of the sample values V, then the 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,Y], V, and [Xq,Yq].

    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)

    Example: half(5+1i)

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

    Note

    The default behavior of the interp2 function is to return NaN when a query point is outside the domain. The fixed.interp2 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 real or complex scalar, vector, or matrix. The size and shape of Vq depends on the syntax you use and, in some cases, the size and value of the input arguments. The data type of Vq is the same as that of the sample values V.

    SyntaxesSpecial ConditionsSize of VqExample
    fixed.interp2(X,Y,V,Xq,Yq),
    fixed.interp2(V,Xq,Yq),
    and variations of these syntaxes that include method or extrapval
    Xq and Yq are scalarsScalarsize(Vq) = [1 1] when you pass Xq and Yq as scalars.
    Same as aboveXq and Yq are vectors of the same size and orientationVector of same size and orientation as Xq and YqIf size(Xq) = [100 1]
    and size(Yq) = [100 1],
    then size(Vq) = [100 1].
    Same as aboveXq and Yq are vectors of mixed orientationMatrix in which the number of rows is length(Yq), and the number of columns is length(Xq)If size(Xq) = [1 100]
    and size(Yq) = [50 1],
    then size(Vq) = [50 100].
    Same as aboveXq and Yq are matrices or arrays of the same sizeMatrix or array of the same size as Xq and YqIf size(Xq) = [50 25]
    and size(Yq) = [50 25],
    then size(Vq) = [50 25].

    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