Plotting Average curve for points in gnuplot

后端 未结 5 642
夕颜
夕颜 2020-12-19 10:03

[Current]

I am importing a text file in which the first column has simulation time (0~150) the second column has the delay (0.01~0.02).

1.000000 0.01         


        
5条回答
  •  鱼传尺愫
    2020-12-19 10:03

    For gnuplot >=5.2, probably the most efficient solution is using an array like @Franky_GT's solution. However, it uses the pseudocolumn 0 (see help pseudocolumns). In case you have some empty lines in your data $0 will be reset to 0 which eventually might mess up your average.

    This solution uses an index t to count up the datalines and a second array X[] in case a centered moving average is desired. Datapoints don't have to be equidistant in x. At the beginning there will not be enough datapoints for a centered average of N points so for the x-value it will use every second point and the other will be NaN, that's why set datafile missing NaN is necessary to plot a connected line at the beginning.

    Code:

    ### moving average over N points
    reset session
    
    # create some test data
    set print $Data
        y = 0
        do for [i=1:5000] {
            print sprintf("%g %g", i, y=y+rand(0)*2-1) 
        }
    set print
    
    # average over N values
    N = 250
    array Avg[N]
    array X[N]
    
    MovAvg(col) = (Avg[(t-1)%N+1]=column(col), n = t

    Result:

提交回复
热议问题