python 插值处理一维数据 interpolate

scipy库:

原码: https://docs.scipy.org/doc/scipy/reference/generated/scipy.interpolate.interp1d.html

  • kind可选”linear”、”nearest”、”nearest-up”、”zero”、”slinear”、”quadratic”、”cubic”、”previous”或”next”之一。
  • “zero”、”slinear”、”quadratic”、”cubic”是指零阶、一阶、二阶或三阶样条插值;
  • “previous”或”next”简单地返回该点的上一个或下一个值;
  • 在插入半整数(例如 0.5、1.5)时,”nearest-up”和”nearest”不同,”nearest-up”向上舍入,”nearest”向下舍入。
  • kind默认为”线性”

fig = plt.figure(figsize=(15,5))

for i in range(2):
    x = data[i]
    t = np.arange(0,len(x))
    tn =  np.linspace(0, len(x), 352)
    f = interpolate.interp1d(t, x, kind='quadratic',bounds_error=False)
    xnew = f(tn)
    ax = fig.add_subplot(2, 1, i+1)
    ax.plot(tn, xnew,'r',label='重采样')
    ax.plot(t, x, 'b--',label='原数据')
    ax.legend(loc='upper left')
    plt.tight_layout()
plt.show()

python 插值处理一维数据 interpolate

python 插值处理一维数据 interpolate
重采样的最后两个值是nan,需要截掉

numpy库:

可以选定需要插值的位置

  • numpy.interp(x, xp, fp, left=None, right=None, period=None)
  • x – 表示将要计算的插值点x坐标
  • xp – 表示已有的xp数组
  • fp – 表示对应于已有的xp数组的值
  • left – 表示当x值在xp中最小值左边时,x对应y的值为left
  • right – 表示当x值在xp中最大值右边时,x对应y的值为right
  • (left和right表示x在xp的域外时,y的取值)

fig = plt.figure(figsize=(15,5))

for i in range(2):
    x = data[i]
    t = np.arange(0,len(x))
    tn =  np.linspace(0, len(x), 352)
    xnew = np.interp(tn, t, x )
    ax = fig.add_subplot(2, 1, i+1)
    ax.plot(tn, xnew,'r',label='重采样')
    ax.plot(t, x, 'b--',label='原数据')
    ax.legend(loc='upper left')
    plt.tight_layout()
plt.show()

python 插值处理一维数据 interpolate

python 插值处理一维数据 interpolate

Original: https://blog.csdn.net/qq_40456702/article/details/128333912
Author: 踩坑记录
Title: python 插值处理一维数据 interpolate

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/724737/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球