我需要以与 matlab 的 interp2 函数相同的方式对 python 进行 interp2
我尝试使用 scipy interp2d 函数,与 matlabs inter2 相同
Matlab:interp2(x,y,yy,new_xx,new_yy)
x = 37, 39, 41
y = 2.5, 2.75, 3
yy = [[0.6 + 1.6j,0.6 + 1.6j,0.6 + 1.6j], [0.7 + 1.6j, 0.7 + 1.6j, 0.7 + 1.6j], [0.8 + 1.5j, 0.8 + 1.5j , 0.8 + 1.5j]] - 3x3 数组
new_xx = np.linspace(37,41,401)
new_yy = np.linspace(0,3,401)
'''
func = scipy.interpolate.interp2d(x,y,yy)
arr = func(new_xx,new_yy)
'''
运行 func = scipy.interpolate.interp2d(x,y,yy) 时收到错误 “ComplexWarning:将复数转换为实数会丢弃虚部”
如何对复数进行插值?
最佳答案
解决方案是执行两种不同的插值:“如果 V 包含复数,则 interp2 分别对实部和虚部进行插值。”来自interp2 matlab documentation .
使用scipy.interpolate.interp2d
:
import numpy as np
from scipy.interpolate import interp2d
x = np.array([37, 39, 41])
y = np.array([2.5, 2.75, 3])
z = np.array([[0.6 + 1.6j, 0.6 + 1.6j, 0.6 + 1.6j],
[0.7 + 1.6j, 0.7 + 1.6j, 0.7 + 1.6j],
[0.8 + 1.5j, 0.8 + 1.5j, 0.8 + 1.5j]])
# 2D grid interpolation
interpolator_real = interp2d(x, y, np.real(z))
interpolator_imag = interp2d(x, y, np.imag(z))
def interpolator_complex(x, y):
return interpolator_real(x, y) + 1j*interpolator_imag(x, y)
# test
new_x = np.linspace(37, 41, 6)
new_y = np.linspace(2.5, 3, 8)
interpolator_complex(new_x, new_y)
关于Python interp2D 与复数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57596013/