给定在(X,Y,Z)坐标中定义的一组点,将Z值插值到任意(X,Y) [英] Given a set of points defined in (X, Y, Z) coordinates, interpolate Z-value at arbitrary (X, Y)

查看:143
本文介绍了给定在(X,Y,Z)坐标中定义的一组点,将Z值插值到任意(X,Y)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于(X,Y,Z)坐标中的一组点是表面上的点,我希望能够在任意(X,Y)坐标处插值Z值.我已经发现使用mlab.griddata在网格上进行插值可以取得一些成功,但是我希望能够为任何(X,Y)坐标调用通用函数.

Given a set of points in (X, Y, Z) coordinates that are points on a surface, I would like to be able to interpolate Z-values at arbitrary (X, Y) coordinates. I've found some success using mlab.griddata for interpolating values on a grid, but I want to be able to call a general use function for any (X, Y) coordinate.

这组点形成一个大致半球形的表面.为了简化问题,我尝试编写一种在以下x,y和z坐标所定义的半球已知点之间进行插值的方法.尽管有一个解析解可以找到一个理想球体的z = f(x,y),这样您就不必进行插值,但是实际的点集将不是一个理想球体,因此我们应该假定我们需要在未知(X,Y)坐标处插值. 使用点数据链接到IPython笔记本

The set of points form a roughly hemispherical surface. To simplify the problem, I am trying to write a method that interpolates values between known points of the hemisphere defined by the x, y, and z coordinates below. Although there is an analytical solution to find z = f(x, y) for a perfect sphere, such that you don't have to interpolate, the actual set of points will not be a perfect sphere, so we should assume that we need to interpolate values at unknown (X, Y) coordinates. Link to IPython notebook with point data

resolution = 10
u = np.linspace(-np.pi / 2, np.pi / 2, resolution)
v = np.linspace(0, np.pi, resolution)

U, V = np.meshgrid(u, v)

xs = np.sin(U) * np.cos(V) 
ys = np.sin(U) * np.sin(V)
zs = np.cos(U)

我一直在使用scipy.interpolate.interp2d,它返回一个函数,其调用方法使用样条插值法来查找新点的值."

I have been using scipy.interpolate.interp2d, which "returns a function whose call method uses spline interpolation to find the value of new points."

def polar(xs, ys, zs, resolution=10):
    rs = np.sqrt(np.multiply(xs, xs) + np.multiply(ys, ys))
    ts = np.arctan2(ys, xs)
    func = interp2d(rs, ts, zs, kind='cubic')
    vectorized = np.vectorize(func)

    # Guesses
    ri = np.linspace(0, rs.max(), resolution)
    ti = np.linspace(0, np.pi * 2, resolution)

    R, T = np.meshgrid(ri, ti)
    Z = vectorized(R, T)
    return R * np.cos(T), R * np.sin(T), Z

不幸的是,我得到了非常奇怪的结果,类似于另一个StackOverflow

Unfortunately I get pretty weird results, similarly to another StackOverflow user who tried to use interp2d.

到目前为止,我找到的最成功的例子是使用反平方估计(X,Y)处的Z值.但是该函数对于估算Z = 0附近的Z值并不完美.

The most success I have found thus far is using inverse squares to estimate values of Z at (X, Y). But the function is not perfect for estimating values of Z near Z=0.

给定(x,y,z)中的一组点,如何获得函数z = f(x, y)?我在这里错过了什么吗?我需要的不仅仅是点云,才能可靠地估计曲面上的值?

What can I do to get a function z = f(x, y) given a set of points in (x, y, z)? Am I missing something here... do I need more than a point cloud to reliably estimate a value on a surface?

这是我最后编写的函数.该函数采用xs, ys, zs的输入数组,并使用scipy.interpolate.griddatax, y处进行插值,这不需要规则的网格.我敢肯定,有一种更聪明的方法可以做到这一点,并且希望对它进行任何更新,但这是可行的,而且我并不关心性能.包含一个摘要,以防将来对任何人有帮助.

This is the function that I ended up writing. The function takes input arrays of xs, ys, zs and interpolates at x, y using scipy.interpolate.griddata, which does not require a regular grid. I'm sure there is a smarter way to do this and would appreciate any updates, but it works and I'm not concerned with performance. Including a snippet in case it helps anyone in the future.

def interpolate(x, y, xs, ys, zs):
    r = np.sqrt(x*x + y*y)
    t = np.arctan2(y, x)

    rs = np.sqrt(np.multiply(xs, xs) + np.multiply(ys, ys))
    ts = np.arctan2(ys, xs)

    rs = rs.ravel()
    ts = ts.ravel()
    zs = zs.ravel()

    ts = np.concatenate((ts - np.pi * 2, ts, ts + np.pi * 2))
    rs = np.concatenate((rs, rs, rs))
    zs = np.concatenate((zs, zs, zs))


    Z = scipy.interpolate.griddata((rs, ts), zs, (r, t))
    Z = Z.ravel()
    R, T = np.meshgrid(r, t)
    return Z

推荐答案

您是说您尝试使用griddata.那为什么不起作用呢?如果新点的间距不规则,则griddata也可以使用.例如,

You're saying that you've tried using griddata. So why was that not working? griddata also works if the new points are not regularly spaced. For example,

# Definitions of xs, ys and zs
nx, ny = 20, 30
x = np.linspace(0, np.pi, nx)
y = np.linspace(0, 2*np.pi, ny)

X,Y = np.meshgrid(x, y)

xs = X.reshape((nx*ny, 1))
ys = Y.reshape((nx*ny, 1))

## Arbitrary definition of zs
zs = np.cos(3*xs/2.)+np.sin(5*ys/3.)**2

## new points where I want the interpolations
points = np.random.rand(1000, 2)

import scipy.interpolate
zs2 = scipy.interpolate.griddata(np.hstack((xs, ys)), zs, points)

这不是你所追求的吗?

这篇关于给定在(X,Y,Z)坐标中定义的一组点,将Z值插值到任意(X,Y)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆