函数:y = np.interp(x, xp, fp, left, right, period)
单调增加样本点的一维线性插值
将一维分段线性插值返回给具有给定离散数据点 (xp, fp) 的函数,在 x 处求值。
x: 所求位置的x坐标
xp:离散点的x坐标
fp:离散点的y坐标
left:当索引超出[xp,fp]时的左侧值(x<xp时在x位置的取值)
right:当索引超出[xp,fp]时的右侧值(y>fp时在x位置的取值)
period:
y:x位置对应的取值
举例:官网给的例子
>>> xp = [1, 2, 3]
>>> fp = [3, 2, 0]
>>> np.interp(2.5, xp, fp)
1.0
>>> np.interp([0, 1, 1.5, 2.72, 3.14], xp, fp)
array([3. , 3. , 2.5 , 0.56, 0. ])
>>> UNDEF = -99.0
>>> np.interp(3.14, xp, fp, right=UNDEF)
-99.0
Plot an interpolant to the sine function:
>>> x = np.linspace(0, 2*np.pi, 10)
>>> y = np.sin(x)
>>> xvals = np.linspace(0, 2*np.pi, 50)
>>> yinterp = np.interp(xvals, x, y)
>>> import matplotlib.pyplot as plt
>>> plt.plot(x, y, 'o')
[<matplotlib.lines.Line2D object at 0x...>]
>>> plt.plot(xvals, yinterp, '-x')
[<matplotlib.lines.Line2D object at 0x...>]
>>> plt.show()
Interpolation with periodic x-coordinates:
>>> x = [-180, -170, -185, 185, -10, -5, 0, 365]
>>> xp = [190, -190, 350, -350]
>>> fp = [5, 10, 3, 4]
>>> np.interp(x, xp, fp, period=360)
array([7.5 , 5. , 8.75, 6.25, 3. , 3.25, 3.5 , 3.75])
总结:其实就是给出[xp,fp]坐标点,利用相邻两点确定一条直线,可以得到一段段相连的折线,根据x的值找到所在位置的线段,进而得到x位置所对应的值。若x超出范围,left和right有则参考,无则选取端点值。