How to Interpolate and Extrapolate with unevenly spaced data?

Hi, I’ve been using BSplineKit to interpolate within my data that is unevenly sampled.

But now I need to extrapolate somewhat past the edges of the data on either side, and that package sets the interpolation exactly to zero at the edges of the data. I need something which continues on more gracefully than that, without a sharp cutoff. Is there anyway to do an interpolation like this which continues more smoothly past the edges? (See a sample code, below, for what I have so far.) Thanks for any suggestions!

using BSplineKit
using Plots
WaveBalSplineOrder = 3
xDats = [1.0; 2.3; 2.9; 4.2]
yDats = [60.0; 15.3; 37.0; 32.1]
Splinefn = spline(BSplineKit.interpolate(xDats, yDats,
                             BSplineOrder(WaveBalSplineOrder)))
xRange = [(0.0 + (0.001)*(i-1)) for i in 1:5001]
scatter(xDats,yDats); plot!(xRange, Splinefn.(xRange), legend=:topright,
    label = string("WaveBalSplineOrder = ",WaveBalSplineOrder))
1 Like

Interpolations.jl has some extrapolation options and can handle unevenly spaced data, but only in one dimension, I think.

Thanks hendri54, I’ve looked into Interpolations.jl, but unevenly spaced data requires their Gridded option, which only goes up to linear interpolation, and I’m trying to use at least 2nd-order or 3rd-order (cubic) here. So I’m still looking.

I think Dierckx.jl has some methods that can use unstructured data( random samples )

Thanks feanor, any suggestions on the syntax for Dierckx.jl equivalent to my example above?

1 Like
using Dierckx
spline = Spline1D(xDats,yDats,k=3,bc = "nearest")
#eval at 0.1 and 0.2
yPoints = spline([0.1,0.2])

For values outside the initial domain there are a few options

The parameter bc specifies the behavior when evaluating the spline outside the support domain, which is (minimum(x), maximum(x)) . The allowed values are "nearest" , "zero" , "extrapolate" , "error" .

1 Like

Hi feanor12, the Dierckx function with the bc = "extrapolate" option is exactly what I was looking for; thank you!

1 Like