gpt4 book ai didi

gnuplot - 在 gnuplot 中绘制点的平均曲线

转载 作者:行者123 更新时间:2023-12-02 11:08:30 25 4
gpt4 key购买 nike

[当前]

我正在导入一个文本文件,其中第一列具有模拟时间(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

最佳答案

这是带有示例数据的仅 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分太多了。或者,人们可以考虑实现中心移动平均线,但这超出了本问题的范围。而且,我还认为您使用外部程序更加灵活:)

关于gnuplot - 在 gnuplot 中绘制点的平均曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42855285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com