首页 > 解决方案 > 具有复数的 Python interp2D

问题描述

我需要像 matlab 的 interp2 函数一样对 python 进行 interp2

我尝试使用与 matlabs inter2 相同的 scipy interp2d 函数

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: Casting complex values to real 会丢弃虚部"

如何与复数进行插值?

标签: pythonscipy

解决方案


一种解决方案是执行两种不同的插值:“如果 V 包含复数,则 interp2 分别对实部和虚部进行插值。” 来自interp2 matlab 文档

使用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)

推荐阅读