Python interp2D 与复数

标签 python scipy

我需要以与 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/

相关文章:

python - Python从立体声波形文件中读取单个 channel 的数据

python - 如何在 scipy.integrate.solve_ivp 中使用事件

python - 从列表 Python 中删除两个可能值之一

python - 来自 Pandas 数据框的 plotly 折线图

python - 为什么这个 memoizer 在递归函数上工作?

python - 根据规则将数字转换为另一个数字

python - 使用 LowLevelCallable : How to pass user_data? 与 scipy quad 集成

python - 转换为稀疏矩阵 - TypeError : no supported conversion for types: (dtype ('0' ), )

Python:将时间戳值字典转换为时间戳列表以进行信号处理

python - 使用 scipy.linalg.solve_triangular 求解 xA=b