Spline interpolations
Interpolating data
BSplineKit can interpolate evenly and unevenly-distributed data.
For example, we can try interpolating random data on randomly-distributed data points:
using Random
rng = MersenneTwister(42)
Ndata = 20
xs = range(0, 1; length = Ndata) .+ 0.01 .* randn(rng, Ndata)
sort!(xs) # make sure coordinates are sorted
xs[begin] = 0; xs[end] = 1; # not strictly necessary; just to set the data limits
ys = sinpi.(xs) .+ 0.02 .* randn(rng, Ndata);
Let's start by plotting the generated data:
using CairoMakie
scatter(xs, ys; label = "Data", color = :black)
To interpolate the data using splines, we may choose any arbitrary B-spline order $k$ (in particular, $k = 4$ corresponds to cubic splines). The main interpolation function is interpolate
.
using BSplineKit
S = interpolate(xs, ys, BSplineOrder(4))
SplineInterpolation containing the 20-element Spline{Float64}:
basis: 20-element BSplineBasis of order 4, domain [0.0, 1.0]
order: 4
knots: [0.0, 0.0, 0.0, 0.0, 0.107311, 0.159983, 0.213316, 0.268371, 0.321046, 0.36973 … 0.625816, 0.681882, 0.732199, 0.783289, 0.837281, 0.886254, 1.0, 1.0, 1.0, 1.0]
coefficients: [0.0183804, 0.15391, 0.315331, 0.496405, 0.598167, 0.712468, 0.930592, 0.875045, 0.990095, 1.00157, 0.981121, 1.0346, 0.933992, 0.850968, 0.735805, 0.666223, 0.459099, 0.407906, -0.123718, -0.0129226]
interpolation points: [0.0, 0.0518416, 0.109298, 0.160794, 0.209856, 0.269298, 0.325959, 0.367882, 0.415349, 0.465107, 0.524201, 0.570411, 0.635975, 0.671061, 0.738611, 0.786926, 0.824332, 0.900587, 0.933845, 1.0]
Let's plot the result:
lines!(0..1, S; label = "k = 4", color = Cycled(4 - 3))
We can also choose other interpolation orders for comparison:
for k ∈ (5, 6, 8)
S = interpolate(xs, ys, BSplineOrder(k))
lines!(0..1, S; label = "k = $k", color = Cycled(k - 3))
end
axislegend()
We see larger and larger oscillations, especially near the boundaries, as the spline order increases. We can try to fix this using natural splines, which impose some derivatives to be zero at the boundaries.
Natural splines
Natural splines usually refer to cubic splines (order $k = 4$) with the additional constraint $S''(a) = S''(b) = 0$ at the boundaries ($x ∈ \{a, b\}$).
In BSplineKit, this concept is generalised for all even spline orders $k$, by setting all derivatives of order $2, 3, …, k / 2$ to be zero at the boundaries. For instance, for $k = 6$ (quintic splines), the constraint is $S'' = S''' = 0$. We achieve this by using basis recombination to implicitly impose the wanted boundary conditions.
The natural boundary condition not only allows to suppress oscillations near the boundaries, but is also quite convenient for interpolations, as it reduces the number of degrees of freedom such that the number of unique spline knots is equal to the number of data points. This simply means that one can set the knots to be equal to the data points. All of this is done internally when using this boundary condition.
To impose natural boundary conditions, one just needs to pass Natural
to interpolate
, as illustrated below.
k = 8
S = interpolate(xs, ys, BSplineOrder(k)) # without BCs
Snat = interpolate(xs, ys, BSplineOrder(k), Natural()) # with natural BCs
SplineInterpolation containing the 20-element Spline{Float64}:
basis: 20-element RecombinedBSplineBasis of order 8, domain [0.0, 1.0], BCs {left => (D{2}, D{3}, D{4}), right => (D{2}, D{3}, D{4})}
order: 8
knots: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0518416, 0.109298 … 0.900587, 0.933845, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]
coefficients: [0.0137583, 0.142842, 0.381383, 0.482671, 0.653978, 0.576594, 1.10858, 0.718314, 1.09114, 0.975321, 0.940988, 1.12046, 0.863254, 0.931811, 0.613254, 0.83853, 0.206394, 0.679701, 0.046989, -0.0095833]
interpolation points: [0.0, 0.0518416, 0.109298, 0.160794, 0.209856, 0.269298, 0.325959, 0.367882, 0.415349, 0.465107, 0.524201, 0.570411, 0.635975, 0.671061, 0.738611, 0.786926, 0.824332, 0.900587, 0.933845, 1.0]
Let's look at the result:
scatter(xs, ys; label = "Data", color = :black)
lines!(0..1, S; label = "k = $k (original)", linewidth = 2)
lines!(0..1, Snat; label = "k = $k (natural)", linestyle = :dash, linewidth = 4)
axislegend()
Clearly, the spurious oscillations are strongly suppressed near the boundaries.
Extrapolations
One can use extrapolation to evaluate splines outside of their domain of definition. A few different extrapolation strategies are implemented in BSplineKit. See Extrapolation methods for details.
Below we compare a few possible extrapolation strategies included in BSplineKit.
First, we generate and interpolate new data:
xs = 0.2:0.2:1.2
ys = 2 * cospi.(xs)
S = interpolate(xs, ys, BSplineOrder(4))
SplineInterpolation containing the 6-element Spline{Float64}:
basis: 6-element BSplineBasis of order 4, domain [0.2, 1.2]
order: 4
knots: [0.2, 0.2, 0.2, 0.2, 0.6, 0.8, 1.2, 1.2, 1.2, 1.2]
coefficients: [1.61803, 1.13682, -0.301508, -2.08779, -2.14709, -1.61803]
interpolation points: 0.2:0.2:1.2
One can directly evaluate these interpolations outside of the domain $[0, 1]$, but the result will always be zero:
S(-0.32)
0.0
To enable extrapolations, one must call extrapolate
with the desired extrapolation strategy (see Extrapolation methods for a list). Here we compare both Flat
and Smooth
methods:
E_flat = extrapolate(S, Flat())
E_linear = extrapolate(S, Linear())
E_smooth = extrapolate(S, Smooth())
SplineExtrapolation containing the 6-element Spline{Float64}:
basis: 6-element BSplineBasis of order 4, domain [0.2, 1.2]
order: 4
knots: [0.2, 0.2, 0.2, 0.2, 0.6, 0.8, 1.2, 1.2, 1.2, 1.2]
coefficients: [1.61803, 1.13682, -0.301508, -2.08779, -2.14709, -1.61803]
extrapolation method: Smooth()
fig = Figure(size = (600, 400))
ax = Axis(fig[1, 1])
scatter!(ax, xs, ys; label = "Data", color = :black)
lines!(ax, -0.5..1.5, S; label = "No extrapolation", linewidth = 2)
lines!(ax, -0.5..1.5, E_smooth; label = "Smooth", linestyle = :dash, linewidth = 2)
lines!(ax, -0.5..1.5, E_linear; label = "Linear", linestyle = :dashdot, linewidth = 2)
lines!(ax, -0.5..1.5, E_flat; label = "Flat", linestyle = :dot, linewidth = 2)
axislegend(ax)
fig
This page was generated using Literate.jl.