Julia:如何在网格上插入非均匀间隔的二维数据?

时间:2016-12-16 02:10:31

标签: julia interpolation

我有一系列在二维网格上散布(非均匀)的数据点。我想将这些分散的数据点插入到统一网格中。朱莉娅有一个方便的内置功能,可以让我这样做吗?或者我可以添加一个额外的包(我一直在看Interpolations.jl,Grid.jl和GridInterpolations.jl,但我不知道如何将它们用于此目的)?我正在寻找类似于Matlab # 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) 的东西。以下是一个示例(使用随机选择的值)来演示我要查找的内容:

 myThread = new Thread( new Runnable()
    {
        public void run()
        {
            try
            {
                while( true )
                    new HTTPSession( myServerSocket.accept());
            }
            catch ( IOException ioe )
            {
                Log.d(IRobotSensors.TAG,"Accept failure: "+ioe.getMessage());
            }
        }
    });

朱莉娅有没有能够做到这一点的功能?

1 个答案:

答案 0 :(得分:1)

我找到了一种可行的方法。我会在这里发布,以防其他人遇到类似的问题。

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")