4
votes

I have a series of data points that are scattered (non-uniformly) across a 2-dimensional grid. I want to interpolate those scattered data points onto a uniform grid. Is there a convenient built in function in Julia that will allow me to do this? Or how about an extra package that I can add (I have been looking at Interpolations.jl, Grid.jl, and GridInterpolations.jl, but I have no idea how to use them for this purpose)? I am looking for something similar to Matlab's griddata. Here is an example (with randomly chosen values) to demonstrate what I am looking for:

# x and y position of known data points
x = [ 1.5 , 8.8 , 2.9 , 7.2 , 7.1 , 3.8 , 8.4 , 2.1 , 0.8 , 5.1 , 7.5 ]
y = [ 6.1 , 9.3 , 5.2 , 7.7 , 9.8 , 7.7 , 8.5 , 6.4 , 5.8 , 9.0 , 8.7 ]

# value of known data points
val = [ 153.9 , 211.8 , 443.6 , 370.8 , 233.8 , 307.2 , 580.3 , 440.9 , 322.2 , 109.3 , 190.8 ]

# x and y positions to describe the interpolation grid
x_interp = [ 0.5 , 2.5 , 4.5 , 6.5 , 8.5 , 10.5 ]
y_interp = [ 1.0 , 2.0 , 3.0 , 4.0 , 5.0 , 6.0 , 7.0 , 8.0 , 9.0 ]

# Some function to interpolate the scattered data onto the grid
val_grid = SomeInterpolationFunction(x,y,val,x_interp,y_interp)

Is there a function in Julia that is capable of doing this?

2

2 Answers

1
votes

I have found one possible approach. I will post it here in case others run into a similar problem.

using PyCall
@pyimport scipy.interpolate as si

# Some 2D function
f(x,y) = sin(x)*cos(y)

# Location of random points to sample the function at
np = 2500
xmin = 0.
xmax = 50.
ymin = 10.
ymax = 95.
x = xmin + xmax*rand(np)
y = ymin + ymax*rand(np)
points = [x y]

# Value of the function at the random points
val = zeros(np)
for ip = 1:np
    val[ip] = f(x[ip],y[ip])
end

# Create a uniform grid to interpolate onto
nx = 50
ny = 75
xgrid = collect(linspace(xmin,xmax,nx))
ygrid = collect(linspace(ymin,ymax,ny))
grid_x = kron(ones(ny),xgrid')
grid_y = kron(ygrid,ones(1,nx))

# Perform the interpolation
grid_val = si.griddata(points,val,(grid_x,grid_y),method="cubic")
0
votes

Dierckx provides this functionality.