如何用立方插值法来扩大数组并访问所有计算出的值?

2024-04-26 01:11:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图得到一个数据集的三次插值,例如y=[0,100,200,300,400]和x=[0,28,54,78,100]。使用numpy库中的线性插值,一切正常,但我需要一个平滑的数据集。在下面的代码示例中,结果是如下所示的数据集:enter image description here

相反,我需要一个如下所示的数据集: enter image description here 对我来说重要的是,插值给我一个数组,让我每2秒访问一次DistanceLong值,这是一个因子。使用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)

Tags: 数据lentimenpzerosrangepltarray

热门问题