2017-03-17 203 views
1

[電流]繪製平均曲線個gnuplot的

我導入其中第一列具有模擬時間(0〜150)的第二列具有延遲(0.01〜0.02)的文本文件。

1.000000 0.010007 
1.000000 0.010010 
2.000000 0.010013 
2.000000 0.010016 
. 
. 
. 
149.000000 0.010045 
149.000000 0.010048 
150.000000 0.010052 
150.000000 0.010055 

這使我的情節: Instantanous Delay in Seconds


[希望]

我需要繪製的平均行上它像示出與紅線如下圖中:

Average red line

回答

1

編輯

更新的問題是關於moving average

根據this demo,您可以單獨使用gnuplot以有限的方式完成此操作。

但在我看來,使用編程語言如pythonruby預處理您的數據會更加靈活,併爲您需要的任何移動平均類型添加額外的列。

原來的答案如下保留:


您可以使用fit。看來你想適應一個不變的功能。像這樣:

f(x) = c 

fit f(x) 'S1_delay_120_LT100_LU15_MU5.txt' using 1:2 every 5 via c 

然後,你可以繪製它們兩個。

plot 'S1_delay_120_LT100_LU15_MU5.txt' using 1:2 every 5, \ 
f(x) with lines 

請注意,這是技術可以用於任意功能,而不僅僅是常數或lineair功能。

+0

我已經更新了我的問題,好心的看看 – saif

1

這裏是一個gnuplot的唯一樣本數據的解決方案:

set table "test.data" 
set samples 1000 
plot rand(0)+sin(x) 
unset table 

您應該檢查gnuplot demo頁面正在運行的平均值。我將通過動態構建函數來概括這個演示。這使得改變平均點包含的點數變得更容易。

這是腳本:

# number of points in moving average 
n = 50 

# initialize the variables 
do for [i=1:n] { 
    eval(sprintf("back%d=0", i)) 
} 

# build shift function (back_n = back_n-1, ..., back1=x) 
shift = "(" 
do for [i=n:2:-1] { 
    shift = sprintf("%sback%d = back%d, ", shift, i, i-1) 
} 
shift = shift."back1 = x)" 
# uncomment the next line for a check 
# print shift 

# build sum function (back1 + ... + backn) 
sum = "(back1" 
do for [i=2:n] { 
    sum = sprintf("%s+back%d", sum, i) 
} 
sum = sum.")" 
# uncomment the next line for a check 
# print sum 

# define the functions like in the gnuplot demo 
# use macro expansion for turning the strings into real functions 
samples(x) = $0 > (n-1) ? n : ($0+1) 
avg_n(x) = (shift_n(x), @sum/samples($0)) 
shift_n(x) = @shift 

# the final plot command looks quite simple 
set terminal pngcairo 
set output "moving_average.png" 
plot "test.data" using 1:2 w l notitle, \ 
    "test.data" using 1:(avg_n($2)) w l lc rgb "red" lw 3 title "avg\\_".n 

這是結果:

moving average

平均滯後相當多的身後,從算法預期的數據點。也許50分太多了。或者,可以考慮實施一個居中移動平均線,但這超出了這個問題的範圍。 而且,我也認爲你用外部程序更靈活:)