Engee documentation

pchip

Piecewise Cubic Hermite Interpolating Polynomial (PCHIP).

Library

EngeeDSP

Syntax

Function call

Arguments

Input arguments

# x — sample points

+ vector

Details

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

Типы данных

Float32, Float64

# y — function values at the sample points

+ vector | the matrix | array

Details

The values of the function at the sample points, specified as a numeric vector, matrix, or array. Arguments x and y they must have the same length.

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.

Типы данных

Float32, Float64

# xq — query points

+ scalar | vector | the matrix | array

Details

Query points specified as a scalar, vector, matrix, or array. The points specified in the argument xq, represent -coordinates of the interpolated function values yq, calculated using the function pchip.

Типы данных

Float32, Float64

Output arguments

# p — 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 of the argument p related to the size of the arguments y and xq:

  • If y — vector, then p 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(p) returns [Ny(1:end-1) length(xq)].

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

Examples

Data interpolation using spline and pchip

Details

Let’s compare the interpolation results obtained using the functions spline and pchip for two different data sets. All these functions implement various forms of piecewise cubic Hermite interpolation. Each function has a different way of calculating the slopes of the interpolant, which leads to different behavior depending on whether there are flat areas or irregularities in the source data.

Let’s compare the results of interpolation on sample data connecting flat regions. Creating vectors of values x, the values of the functions at these points y and query points xq. Let’s calculate the interpolations at the query points using the functions spline and pchip. Let’s plot the interpolated function values at the query points for comparison.

import EngeeDSP.Functions: pchip, spline

x = -3:3
y = [-1, -1, -1, 0, 1, 1, 1]
xq1 = -3:0.01:3

p = pchip(x, y, xq1)
s = spline(x, y, xq1)

plot(x, y,
     seriestype = :scatter,
     markercolor = :white,
     label = "Sample Points",
     legend = :bottomright)
plot!(xq1, p, label = "pchip")
plot!(xq1, s, linestyle = :dashdot, label = "spline")

pchip 1

In this case, the function pchip avoids outliers and can accurately connect flat areas.

Let’s perform the second comparison using the sampling oscillation function.

import EngeeDSP.Functions: pchip, spline, besselj

x = 0:15
y = besselj.(1, x)
xq2 = 0:0.01:15

p = pchip(x, y, xq2)
s = spline(x, y, xq2)

plot(x, y,
     seriestype = :scatter,
     markercolor = :white,
     label = "Sample Points")
plot!(xq2, p, label = "pchip")
plot!(xq2, s, linestyle = :dashdot, label = "spline")

pchip 1 2

If the base function is oscillatory, spline it captures movement between points better than pchip, which is strongly smoothed out near local extremes.

Additional Info

Shape-preserving piecewise cubic interpolation

Details

Function pchip interpolates using a piecewise cubic polynomial with the properties listed below.

  • On each subinterval the polynomial is a cubic Hermite interpolation polynomial for given data points with given derivatives (slopes) at the interpolation points.

  • interpolates That is , , and the first derivative continuous. The second derivative probably not continuous, so there may be jumps at the point .

  • Cubic interpolant retains its shape. Slopes at a point they are selected in such a way that it kept the data form and monotony. Therefore, at intervals where the data is monotonic, It is also monotonic, and at points where the data has a local extremum., it is also monotonous.

If — the matrix, satisfies these properties for each row .

Recommendations

Function spline builds almost the same as the function pchip builds . However spline selects the slopes at a point in another way, namely to do continuous. This difference has several effects:

  • spline allows you to get a smoother result, that is continuous.

  • spline gives a more accurate result if the data consists of values of a smooth function.

  • pchip It has no outliers and less fluctuations if the data is not smooth.

  • pchip less expensive to set up.

  • Evaluating both options is equally costly.

Literature

  1. Fritsch, F. N. and R. E. Carlson. «Monotone Piecewise Cubic Interpolation.» SIAM Journal on Numerical Analysis. Vol. 17, 1980, pp.238–246.

  2. Kahaner, David, Cleve Moler, Stephen Nash. Numerical Methods and Software. Upper Saddle River, NJ: Prentice Hall, 1988.