gnuplot 以设定的时间间隔平均随机时间数据块

gnuplot averaging stochastic time data blocks at set intervals of time

您好,我正在使用 gnuplot 绘制来自数据块结构模拟的数据,如下所示:

CurrentTime  CurrentState
0            2
1.234        2
1.990        1
2.462        0


CurrentTime  CurrentState
0            2
0.895        1
1.456        2
2.052        1
3.017        0

数据块的数量不详,但至少有30个块。 请注意,每个 CurrentTime 的间隔数是不同的。 我正在使用以下代码按原样绘制数据

# GNUPlot code
set multiplot layout 2,1 title "Insert title" font ",14"
set tmargin 3
set bmargin 3
set lmargin 5
set rmargin 2
plot "data.txt" every :1 using 1:2:(column(-2)) with linespoints lc variable

由于使用了 multiplot 命令,我接下来要绘制的内容将放在下方的绘图中。我想将该图作为我设置的时间间隔内数据的平均值。在我想要的伪代码中:

# pseudo code
float start, step, stop;
assign start, step, stop;
define Interval=start, by step, to stop; typed another way Interval=start:step:stop 
array sum(size(number of data blocks,length(Interval), length(Interval)))
assign sum=0;
for every data block
    for k=0 to length(CurrentTime)
        for j=0 to length(Interval)-1
           (CurrentTime(k) < Interval(j+1) && CurrentTime(k) > Interval(j-1)) ? sum += CurrentState(k) : sum += 0
average=sum/(Number of data blocks)

我一直在尝试在 gnuplot 中实现它。任何帮助都会很棒!

首先是数据文件,我的一些真实数据是

CurrentTime CurrentState
0       2
4.36393     1
5.76339     2
13.752      1
13.7645     2
18.2609     1
19.9713     2
33.7285     1
33.789      0


CurrentTime CurrentState
0       2
3.27887     1
3.74072     2
3.86885     1
4.97116     0


CurrentTime CurrentState
0       2
1.19854     1
3.23982     2
7.30501     1
7.83872     0

然后我使用 python 在我想要检查平均值的时间间隔内找到数据的平均值。我选择以离散的时间步长进行检查,但它们可以是任何时间步长。以下是我的python代码

#Loading data file: Goal is to calculate average(TimeIntervals)=averageOfTimeIntervals.
import numpy as np
data=np.genfromtxt('data.txt', comments='C')
CurrentState=data[:,1]
CurrentTime=data[:,0]
numberTimeIntervals=101
TimeIntervals=np.linspace(0,numberTimeIntervals-1,numberTimeIntervals)     #gives integer values of time
stateOfTimeIntervals=np.zeros(numberTimeIntervals,dtype=np.float64)
stateOfTimeIntervals[0]=CurrentState[0]  #setting initial state
#main loop
run=0
numberSimTimes=len(CurrentTime)
for j in range(0,len(stateOfTimeIntervals)): #start at 1 b/c we know   initial state
for k in range(0,numberSimTimes-1):
    lengthThisRun=0
    if CurrentTime[k] <= TimeIntervals[j] and CurrentTime[k+1] > TimeIntervals[j]:
        lengthThisRun+=1
        #Goal is to get the length of this run up to the time we decide to check the state
        stateOfTimeIntervals[j]+=CurrentState[k]
else:
        lengthThisRun+=1
#The number of runs can be claculated using
numberRuns=len(CurrentTime) - np.count_nonzero(CurrentTime)
print "Number of Runs=%f" %(numberRuns)
#Compute the average
averageState=stateOfTimeIntervals/numberRuns
#Write to file and plot with gnuplot
np.savetxt('plot2gnu.txt',averageState)

然后使用 gnuplot 我绘制 'plot2gnu.txt' 使用以下代码

# to plot everything on the same plot use "multiplot"
set multiplot layout 2,1 title "Insert title" font ",14"
set tmargin 3
set bmargin 3
set lmargin 5
set rmargin 2
plot "data.txt" every :1 using 1:2:(column(-2)) with linespoints lc variable
plot 'plot2gnu.txt' using 1:2 with linespoints

我想指出在指定线条颜色的第三列中使用伪列 'column(-2)'。 'column(-2)' 表示 "The index number of the current data set within a file that contains multiple data sets." - 来自 'old' gnuplot 4.6 文档。