Engee documentation

spline

Cubic spline data interpolation.

Library

EngeeDSP

Syntax

Function call

  • s = spline(x, y, xq) — returns a vector of interpolated values s, corresponding to the query points in xq. Values s determined by cubic spline interpolation x and y.

  • pp = spline(x, y) — returns a piecewise polynomial structure.

Arguments

Input arguments

# x — coordinates of x

+ vector

Details

Coordinates x, set as a vector. Vector x defines the points where the data is set y. Elements x they must be unique.

Cubic spline interpolation requires at least 4 points. If available 2 or 3 Linear or quadratic interpolation is applied to the points, respectively.

Типы данных

Float32, Float64

# y is the value of the function at the specified coordinates x

+ vector | the matrix | array

Details

Function values in coordinates x, specified as a numeric vector, matrix, or array. Arguments x and y they usually have the same length, but y it can also have exactly two more elements than x, to specify the final slopes.

If y — a matrix or an array, then the values in the last dimension, y(:,…​,:,j), are taken as values to be compared with x. In this case, the last dimension is y must have the same length as x, or have exactly two more elements.

The final slopes of a cubic spline follow the following rules:

  • If x and y — the vectors are the same size, then the conditions "not a node" are used.

  • If x or y If is a scalar, then it expands to the same length as the other one, and the conditions "not a node" are used.

  • If y — a vector containing two elements more values than x, then the spline uses the first and last values in y as the final slopes of a cubic spline. For example, if y — vector, then:

    • y(2:end-1) returns the function values at each point x;

    • y(1) returns the slope at the beginning of the interval located at the point min(x);

    • y(end) returns the slope at the end of the interval located at the point max(x).

  • Similarly, if y — the matrix or -a dimensional array with the size size(y,N), equal to length(x)+2 Then:

    • y(:,…​,:,j+1) returns the function values at each point x for j = 1:length(x);

    • y(:,:,…​:,1) returns the slopes at the beginning of the intervals located at the point min(x);

    • y(:,:,…​:,end) returns the slopes at the end of the intervals located at the point max(x).

Типы данных

Float32, Float64

# xq — query points

+ scalar | vector | the matrix | array

Details

Query points specified as a scalar, vector, matrix, or array. Points set in xq, represent the coordinates x interpolated function values yq calculated using a spline.

Типы данных

Float32, Float64

Output arguments

# s — interpolated values at query points

+ scalar | vector | the matrix | array

Details

The interpolated values at the query points returned as a scalar, vector, matrix, or array.

Size s related to size y and xq:

  • If y — vector, then s has the same size as xq.

  • If y — an array of size Ny = size(y), then the following conditions apply:

    • If xq — scalar or vector, then size(s) returns [Ny(1:end-1) length(xq)];

    • If xq — an array, then size(s) returns [Ny(1:end-1) size(xq)].

# pp is a piecewise polynomial

+ structure

Details

A piecewise polynomial returned as a structure. The structure contains the fields shown in the table.

Field Description

form

pp for a piecewise polynomial

breaks

Length vector with strictly increasing elements representing the beginning and the end of each of the intervals

coefs

Matrix size on , in which each line coefs(i,:) contains local coefficients of a polynomial of the order on -m interval, [breaks(i),breaks(i+1)]

pieces

Number of parts

order

Degree of polynomials

dim

Dimension of the goal

Since the coefficients of the polynomials in coefs are the local coefficients for each interval, it is necessary to subtract the lower bound of the corresponding node interval in order to use the coefficients in the usual polynomial equation. In other words, for the coefficients on the interval the corresponding polynomial is:

Examples

Spline interpolation of sinusoidal data

Details

We use spline to interpolate a sinusoidal curve over unevenly spaced sampling points.

import EngeeDSP.Functions: spline
x = [0, 1, 2.5, 3.6, 5, 7, 8.1, 10]
y = sin.(x)
xx = 0:0.25:10
yy = spline(x, y, xx)

plot(x, y, seriestype=:scatter, markersize=6, legend=false)
plot!(xx, yy, linewidth=2, legend=false)

spline 1

Recommendations

Spline interpolation can also be performed using the function interp1 using the command interp1(x,y,xq,"spline"). Function spline performs interpolation along the rows of the input matrix, and the function interp1 — by the columns of the input matrix.

Algorithms

A three-diagonal linear system (possibly with several right-hand sides) is solved to obtain the information necessary to describe the coefficients of the various cubic polynomials that make up the interpolation spline.

Literature

  1. de Boor, Carl. A Practical Guide to Splines. Springer-Verlag, New York: 1978.