Search code examples
juliainterpolationspline

How to perform the B-spline interpolation in Julia?


I want to interpolate the following 2x1 matrix (example for clarity of question):

x=collect(300:50:900)
y=rand(13)

the query points 'xq' are:

xq=collect(290:4:910)

in this case, how can I get the query point of y 'yq'?

I tried some interpolation methods (such as interpolations and Diercks..), but nothing worked (or I did wrong way..). I'm so confused. please share your know-how.


Solution

  • What have you tried? What did not work?

    Before looking at the interpolation, note that you usually don't have to use collect - it creates unnecessary arrays and thereby allocations and negates the benefits of working with the lightweight StepRange type created by an expression like 300:50:900.

    Here's an example with BSplineKit following the first example in the docs:

    julia> using BSplineKit
    
    julia> x = 300:50:900; y = rand(length(x));
    
    julia> itp = interpolate(x, y, BSplineOrder(4))
    SplineInterpolation containing the 13-element Spline{Float64}:
     basis: 13-element BSplineBasis of order 4, domain [300.0, 900.0]
     order: 4
     knots: [300.0, 300.0, 300.0, 300.0, 400.0, 450.0, 500.0, 550.0, 600.0, 650.0, 700.0, 750.0, 800.0, 900.0, 900.0, 900.0, 900.0]
     coefficients: [0.164315, 0.399353, 0.866037, 0.325909, 1.16957, 0.990489, 0.74577, 0.882066, 0.698501, 0.684713, 1.27336, -0.49067, 0.985016]
     interpolation points: 300:50:900
    
    
    julia> itp.(290:4:910)
    156-element Vector{Float64}:
     0.0
     0.0
     0.0
     0.1785050188621375
     0.8996276319427731
     0.0
     0.0
     0.0
    

    The only thing I've changed from the docs is to broadcast the itp function over the range of query points.

    Here's what the result looks like:

    enter image description here