2017-06-05 29 views
0

我想获得数据集的三次插值,例如y = [0,100,200,300,400]和x = [0,28,54,78,100]。使用numpy库中的线性插值,一切正常,但我需要一个平滑的数据集。在结果下面的代码示例是一个数据集,看起来像:enter image description here如何使用三次插值法制作更大的阵列并存取所有计算的值?

相反,我希望有一个数据集,看起来像这样: enter image description here 这对我来说重要的是,插值给了我一个数组,它给了我的访问到距离长度值为每2秒,这是因子。随着scipy功能,我没有得到它的工作。我希望你们能给我一些帮助!

import numpy as np 
import matplotlib.pyplot as plt 

def interplan(Timestamp, Distance, factor): 
    Time = np.zeros(len(Timestamp)-1) 
    NewDistance= np.zeros(len(Timestamp)-1) 
    TotalTime = 0 
    TotalDistance = 0 

    for i in range(len(Timestamp)-1): 
     TotalTime += Timestamp[i] 
     Time[i] = TotalTime 
     TotalDistance += Distance[i] 
     NewDistance[i] = TotalDistance 


    Time = np.hstack((0,Time)) 
    NewDistance = np.hstack((0,NewDistance)) 

    k = np.array(range((int(TotalTime+1)))) 
    t = k*factor 
    DistanceLong = np.interp(t,Time,NewDistance) 

    return DistanceLong 


Timestamp = np.array([28, 26, 24, 22,0]) 
Distance = np.array([100, 100, 100, 100,0]) 
factor = 2 

DistanceLong = interplan(Timestamp, Distance, factor) 
BiggestVal = max(DistanceLong) 
IndexLastVal = np.where(DistanceLong == BiggestVal) 
DistanceLong = DistanceLong[0:IndexLastVal[0][1]] 

Speed = np.zeros(len(DistanceLong)) 
for i in range(len(DistanceLong)-1): 
    Speed[i] = ((DistanceLong[i+1]-DistanceLong[i])/2)*3.6 

PerfectSpeed = (Distance/Timestamp)*3.6 

plt.figure(1) 
plt.plot(Speed,linewidth=0.8) 


plt.figure(2) 
plt.plot(PerfectSpeed,linewidth=0.8) 

回答

0

立方插值您可以使用splrep/ splev,或在新SciPy的,CubicSpline,或者在新的还是SciPy的,make_interp_spline

+0

使用CubicSpline时,我无法访问这些值。当我打印它时,我得到:

+0

CubicSpline构造可调用对象,用于评估其参数上的插值。 –