# How to Interpolate and Extrapolate with unevenly spaced data?

**URL:** https://discourse.julialang.org/t/how-to-interpolate-and-extrapolate-with-unevenly-spaced-data/62812
**Category:** Numerics
**Tags:** interpolations
**Created:** [June 12, 2021, 8:54pm UTC](https://discourse.julialang.org/t/how-to-interpolate-and-extrapolate-with-unevenly-spaced-data/62812 "2021-06-12T20:54:18Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)
#### Post date: [June 12, 2021, 8:54pm UTC](https://discourse.julialang.org/t/how-to-interpolate-and-extrapolate-with-unevenly-spaced-data/62812/1 "2021-06-12T20:54:19Z")

</div>

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!

```julia
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))

```

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [June 13, 2021, 12:15pm UTC](https://discourse.julialang.org/t/how-to-interpolate-and-extrapolate-with-unevenly-spaced-data/62812/2 "2021-06-13T12:15:25Z")

</div>

Interpolations.jl has some [extrapolation options](https://juliamath.github.io/Interpolations.jl/stable/api/#Interpolations.extrapolate-Union%7BTuple%7BET%7D,%20Tuple%7BIT%7D,%20Tuple%7BN%7D,%20Tuple%7BT%7D,%20Tuple%7BAbstractInterpolation%7BT,N,IT%7D,ET%7D%7D%20where%20ET%3C:Union%7BTuple%7BVararg%7BUnion%7BTuple%7BBoundaryCondition,BoundaryCondition%7D,%20BoundaryCondition%7D,N%7D%20where%20N%7D,%20BoundaryCondition%7D%20where%20IT%20where%20N%20where%20T) and can handle unevenly spaced data, but only in one dimension, I think.

---

<div class="post-metadata">

### Author: ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)
#### Post date: [June 13, 2021, 9:00pm UTC](https://discourse.julialang.org/t/how-to-interpolate-and-extrapolate-with-unevenly-spaced-data/62812/3 "2021-06-13T21:00:33Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)
#### Post date: [June 13, 2021, 10:04pm UTC](https://discourse.julialang.org/t/how-to-interpolate-and-extrapolate-with-unevenly-spaced-data/62812/4 "2021-06-13T22:04:12Z")

</div>

I think [Dierckx.jl](https://github.com/kbarbary/Dierckx.jl) has some methods that can use unstructured data( random samples )

---

<div class="post-metadata">

### Author: ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)
#### Post date: [June 13, 2021, 10:58pm UTC](https://discourse.julialang.org/t/how-to-interpolate-and-extrapolate-with-unevenly-spaced-data/62812/5 "2021-06-13T22:58:41Z")

</div>

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

---

<div class="post-metadata">

### Author: ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)
#### Post date: [June 14, 2021, 8:51am UTC](https://discourse.julialang.org/t/how-to-interpolate-and-extrapolate-with-unevenly-spaced-data/62812/6 "2021-06-14T08:51:43Z")

</div>

```julia
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"` .

---

<div class="post-metadata">

### Author: ![CosmoProf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cosmoprof/32/15516_2.png) [@CosmoProf](https://discourse.julialang.org/u/CosmoProf)
#### Post date: [June 20, 2021, 3:33am UTC](https://discourse.julialang.org/t/how-to-interpolate-and-extrapolate-with-unevenly-spaced-data/62812/7 "2021-06-20T03:33:23Z")

</div>

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