Main Content

interp1

Quaternion interpolation (table lookup)

Since R2025a

    Description

    vq = interp1(x,v,xq) interpolates the quaternion values of a 1-D function at specific query points using spherical linear interpolation (SLERP). x specifies the sample points, and v specifies the quaternions that contains the corresponding values v(x). xq specifies the query points. By default, the function uses "slerp-short" interpolation method.

    example

    vq = interp1(x,v,xq,method) specifies the interpolation method.

    example

    vq = interp1(x,v,xq,method,extrapolation) specifies a quaternion value to return for all query points in xq that are outside the domain of x.

    example

    vq = interp1(v,xq) interpolates the quaternion values by assuming a default set of sample points. 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 of quaternions, the default points are 1:length(v).

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

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

    example

    vq = interp1(v,xq,method) specifies the interpolation method and uses the default sample points.

    vq = interp1(v,xq,method,extrapolation) specifies an extrapolation value and uses the default sample points.

    Examples

    collapse all

    Define the sample points x and corresponding sample values v.

    x = [1 2 5 6];
    eul = [-185:45:-50; -20*ones(1,4); zeros(1,4)]';
    v = quaternion(eul,"eulerd","ZYX","frame");

    Define the query points over the range of x.

    xq = [1.5 3 4 5.4];

    Interpolate at the query points.

    vq = interp1(x,v,xq);

    To visualize the result, rotate the same point using the sample and interpolated quaternions.

    pts_samples = rotatepoint(v,[1.05 0 0]);
    pts_query = rotatepoint(vq,[1.05 0 0]);

    Plot a unit sphere, and then plot the sample and interpolated quaternions on the sphere.

    figure
    [X,Y,Z] = sphere;
    surf(X,Y,Z,FaceAlpha=0.5,EdgeAlpha=0.35)
    colormap gray
    hold on
    scatter3(pts_samples(:,1),pts_samples(:,2),pts_samples(:,3),"filled")
    scatter3(pts_query(:,1),pts_query(:,2),pts_query(:,3),"filled")
    exampleHelperAnnotateQuats(pts_samples,pts_query,x,xq,10,1.05)
    axis equal
    title(["Interpolated Quaternions Visualized","Using Rotated Points"])
    legend("","Sample Quaternions (v)","Interpolated Quaternions (vq)")

    Figure contains an axes object. The axes object with title Interpolated Quaternions Visualized Using Rotated Points contains 11 objects of type surface, scatter, text. These objects represent Sample Quaternions (v), Interpolated Quaternions (vq).

    Define a set of function values.

    eul = [-170:20:-70; -20*sind(0:72:360)-25; zeros(1,6)]';
    v = quaternion(eul,"eulerd","ZYX","frame");

    Define a set of query points that fall between the default points, 1:6. In this case, the default points are 1:6 because v is a six-element quaternion array.

    xq = [1.5 2.6 3.5 4.5 5.5];

    Evaluate v at xq using the SQUAD natural interpolation method. Note that you could use the default "slerp-short" interpolation method here, but for the sine-wave rotation that this quaternion function describes, SQUAD produces a smoother interpolated path.

    vq = interp1(v,xq,"squad-natural");

    To visualize the result, rotate a point on the x-axis using the sample and interpolated quaternions.

    pts_samples = rotatepoint(v,[1.05 0 0]); 
    pts_query = rotatepoint(vq,[1.05 0 0]);

    Plot a unit sphere, and then plot the sample and interpolated quaternions on the sphere.

    figure
    [X,Y,Z] = sphere;
    surf(X,Y,Z,FaceAlpha=0.5,EdgeAlpha=0.35)
    colormap gray
    hold on
    scatter3(pts_samples(:,1),pts_samples(:,2),pts_samples(:,3),"filled")
    scatter3(pts_query(:,1),pts_query(:,2),pts_query(:,3),"filled")
    exampleHelperAnnotateQuats(pts_samples,pts_query,1:6,xq,9,1.05)
    axis equal
    title(["Interpolated Quaternions Visualized","Using Rotated Points"])
    legend("","Sample Quaternions (v)","Interpolated Quaternions (vq)")

    Figure contains an axes object. The axes object with title Interpolated Quaternions Visualized Using Rotated Points contains 14 objects of type surface, scatter, text. These objects represent Sample Quaternions (v), Interpolated Quaternions (vq).

    Define three quaternions representing different orientations.

    q0 = quaternion([0 0 0],"euler","ZYX","frame");
    q1 = quaternion([pi/4 pi/6 pi/3],"euler","ZYX","frame");
    q2 = quaternion([pi/2 pi/4 pi/2],"euler","ZYX","frame");

    Create a time vector for the original keyframes

    x = [1 2 3];

    Create a time vector for interpolation

    T = linspace(0,3,300);

    Interpolate using interp1 with the "slerp-natural" and "squad-natural" methods.

    quats_slerp = interp1(x,[q0 q1 q2],T,"slerp-natural")';
    quats_squad = interp1(x,[q0 q1 q2],T,"squad-natural")';

    Calculate the angular velocities for both interpolations methods.

    ang_vel_slerp = angvel(quats_slerp,T(2)-T(1),"frame");
    ang_vel_squad = angvel(quats_squad,T(2)-T(1),"frame");

    Plot the angular velocities.

    tl = tiledlayout(2,1);
    title(tl,"Angular Velocity - SLERP vs SQUAD")

    Plot the angular velocities for the SLERP and SQUAD interpolations. Note that the angular velocity from the SQUAD interpolation is smoother than the interpolation using SLERP.

    nexttile
    plot(T,ang_vel_slerp)
    ylim padded
    title("SLERP Natural")
    legend("X","Y","Z")
    nexttile
    plot(T,ang_vel_squad)
    ylim padded
    title("SQUAD Natural")
    xlabel(tl,"Time")
    ylabel(tl,"Angular Velocity (rad/s)")

    Figure contains 2 axes objects. Axes object 1 with title SLERP Natural contains 3 objects of type line. These objects represent X, Y, Z. Axes object 2 with title SQUAD Natural contains 3 objects of type line.

    Define the sample points x and corresponding sample values v.

    x = [0 1 2];
    eul = [0 30 60; 0 20 60; zeros(1,3)]';
    v = quaternion(eul,"eulerd","ZYX","frame");

    Specify the query points xq so that they extend beyond the domain of x.

    xq = [-0.5 0.5 1.5 2.5];

    Now evaluate v at xq using the "slerp-short" interpolation method, and assign any values outside the domain of x to a quaternion with all parts set to 0.

    vq = interp1(x,v,xq,"slerp-short",ones("quaternion"))'
    vq = 4×1 quaternion array
                1 +        0i +        0j +        0k
          0.98774 + 0.022751i - 0.084907j -  0.12903k
          0.87097 +    0.151i -  0.30755j -  0.35217k
                1 +        0i +        0j +        0k
    
    

    Input Arguments

    collapse all

    Sample points, specified as an N-element vector of real numbers. x must contain at least two sample points, and the values in x must be distinct. The length of x depends on the shape of v.

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

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

    Example: [1 2 3 4 5 6 7 8 9 10]

    Example: 1:10

    Example: [3 7 11 15 19 23 27 31]'

    Sample quaternions, specified as an N-element vector of quaternion objects or an array of quaternion objects.

    If you have multiple sets of data that are sampled at the same query points, then you can specify v as an array. Each column of the array must contain a different set of 1-D sample values and the first dimension must be of length N, where N is the number of specified sample points x.

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

    Example: 5

    Example: 1:0.05:10

    Example: (1:0.05:10)'

    Example: [0 1 2 7.5 10]

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

    MethodDescriptionContinuityComments
    "slerp-short"

    Spherical linear interpolation between sample quaternions by an interpolation coefficient, picking the shortest interpolation path between the sample quaternions.

    Note that, at each sample point, the input and output quaternions might be inverted to ensure the shortest path.

    For more information about the SLERP algorithm, see SLERP Algorithm.

    C0
    • Requires at least 2 sample quaternions

    • Low memory requirements

    • Efficient computation time

    • Ensures shortest path interpolation.

    • Fastest computation time

    "slerp-natural"

    Spherical linear interpolation between sample quaternions by an interpolation coefficient, picking the natural interpolation path between the sample quaternions.

    The input and output quaternions will have the same sign at the sample points.

    For more information about the SLERP algorithm, see SLERP Algorithm.

    C0
    • Requires at least 2 sample quaternions

    • Low memory requirements

    • Efficient computation time

    • The input and output quaternions will have the same sign at the sample points.

    "squad-natural"

    Spherical quadrangle interpolation between sample quaternions, providing smooth and continuous transitions with gradual changes in acceleration.

    The input and output quaternions will have the same sign at the sample points.

    For more information about the SQUAD algorithm, see SQUAD Algorithm.

    C1
    • Requires at least 2 sample quaternions

    • Moderate memory requirements

    • Efficient computation time

    • Ensures smooth transitions and gradual acceleration changes

    "nearest"Nearest neighbor interpolation. The interpolated value at a query point is the value at the nearest sample quaternion.Discontinuous
    • Requires at least 2 sample quaternions

    • Modest memory requirements

    "next"Next neighbor interpolation. The interpolated value at a query point is the value at the next sample quaternion.Discontinuous
    • Requires at least 2 sample quaternions.

    • 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 quaternion.Discontinuous
    • Requires at least 2 sample quaternions.

    • Same memory requirements and computation time as "nearest"

    Extrapolation quaternion, specified as a quaternion object. interp1 returns this quaternion for any xq values that query outside of domain of x.

    Example: interp1([0 1 2],quats,2.5,"slerp-natural") returns a quaternion of value NaN + NaNi + NaNj + NaNk.

    Example: interp1([0 1 2],quats,2.5,"slerp-natural",zeros("quaternion")) returns a quaternion of value 0 + 0i + 0j + 0k.

    Output Arguments

    collapse all

    Interpolated values, returned as a scalar, vector, matrix, or array. The size of vq depends on the shapes of v and xq.

    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].
    ArrayVector[length(xq) size(v,2),...,size(v,n)]If size(v) = [100 3]
    and size(xq) = [1 500],
    then size(vq) = [500 3].
    ArrayMatrix
    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].

    Algorithms

    collapse all

    References

    [1] Shoemake, Ken. "Animating Rotation with Quaternion Curves." ACM SIGGRAPH Computer Graphics 19, no. 3 (July 1985): 245–54. https://doi.org/10.1145/325165.325242.

    [2] Dam, Erik B., Martin Koch, and Martin Lillholm. Quaternions, Interpolation and Animation. Technical Report DIKU-TR-98/5. Department of Computer Science, University of Copenhagen, July 17, 1998. https://web.mit.edu/2.998/www/QuaternionReport1.pdf.

    Extended Capabilities

    expand all

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

    Version History

    Introduced in R2025a

    See Also

    |