根据值的周期性插值二维numpy数组

时间:2019-04-17 19:33:50

标签: python numpy scipy interpolation

我有点(x,y),其值(z)在pi中是周期性的,即零与pi相同。这些点非常粗糙,因此我需要对其进行插值。

我尝试使用scipy.interpolate中的griddata()对值进行插值,从而导致问题。

import matplotlib.pyplot as plt
from scipy.interpolate import griddata
import numpy as np


N = 1000
grid_res = 1000
x = np.random.rand(N)
y = np.random.rand(N)
z = np.ndarray(N)

# calculate some stuff, doesnt really matter
for i in range(0, N):
    if x[i] - 0.5 > 0.:
        z[i] = np.arccos(-(y[i] - .5)/np.sqrt((x[i] - .5)**2 + (y[i] - .5)**2))
    else:
        z[i] = np.pi - np.arccos(-(y[i] - .5)/np.sqrt((x[i] - .5)**2 + (y[i] - .5)**2))

# define grid.
xi = np.linspace(0., 1., grid_res)
yi = np.linspace(0., 1., grid_res)

# interpolate the data on the grid.
zi = griddata((x, y), z, (xi[None, :], yi[:, None]), method='linear')

# plot the values
CS = plt.imshow(zi,
                cmap='hsv',
                origin='lower',
                extent=[0., 1., 0., 1.],
                vmax=np.pi,
                vmin=0.)

plt.show()

仅对与griddata线性的值进行插值会导致不良行为:

例如: [8/10 pi,pi,2/10 pi,]

被内插到 [8/10 pi,9/10 pi,pi,6/10 pi,2/10 pi。]

我希望它成为 [8/10 pi,9/10 pi,pi,1/10 pi,2/10 pi。]

是否有一种方法可以根据其周期性对这些值进行插值?

0 个答案:

没有答案