Python中使用插值法对数据进行处理

https://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html

对数据进行升采样

import numpy as np
from scipy.interpolate import interp1d
import matplotlib.pyplot as plt


x = np.linspace(0, 10, num=11, endpoint=True)
y = np.cos(-x**2/9.0)
f = interp1d(x, y)

xnew = np.linspace(0, 10, num=110, endpoint=True)
plt.plot(x, y, 'o')
plt.plot(xnew, f(xnew), '*')
plt.legend(['data', 'linear'], loc='best')
plt.show()

将时间上不均匀的序列变成时间均匀的序列

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d


x = [0, 0.1, 0.5, 0.7]
y = [0, 170, 170, 0]

new_x = np.linspace(0, 0.7, 70)
f = interp1d(x, y)
y_new = f(new_x)
plt.figure()

plt.plot(x, y, 'o')
plt.plot(new_x, y_new, '*')
plt.show()