gpt4 book ai didi

awk - 使用 awk 的移动平均线

转载 作者:行者123 更新时间:2023-12-01 22:37:24 25 4
gpt4 key购买 nike

我想对一列中的每 100 个点取平均值,然后将平均值放在中间 - 第 50 点。

我尝试使用此脚本计算移动平均线:

 BEGIN {
P = 100;
}

{
x = $2;
i = NR % P;
MA += (x - Z[i]) / P;
Z[i] = x;
print $1,"\t",$2,"\t",MA;
}

但我需要知道如何将它放在中间?

输入示例:

Depth          Velocity
1150.315 434.929
1150.468 434.929
1150.62 434.929
1150.772 434.929
1150.925 434.929
1151.077 434.929
1151.23 434.929
1151.382 434.929
1151.534 434.929
1151.687 434.929
1151.839 434.929
1151.992 434.929
1152.144 434.929
1152.296 434.929
1152.449 434.929
1152.601 434.929
1152.754 434.929
1152.906 434.929
1153.058 434.929
1153.211 434.929
1153.363 434.929
1153.516 434.929
1153.668 434.929
1153.82 434.929
1153.973 434.929
1154.125 434.929
1154.278 434.929
1154.43 434.929
1154.582 434.929
1154.735 434.929
1154.887 434.929
1155.04 434.929
1155.192 434.929
1155.344 434.929
1155.497 434.929
1155.649 434.517
1155.802 434.105
1155.954 433.693
1156.106 433.233
1156.259 432.773
1156.411 432.313
1156.564 431.853
1156.716 431.853
1156.868 431.853
1157.021 431.853
1157.173 431.853
1157.326 431.853
1157.478 431.853
1157.63 431.853
1157.783 431.853
1157.935 431.853
1158.088 431.853
1158.24 431.853
1158.392 431.853
1158.545 431.853
1158.697 431.853
1158.85 431.853
1159.002 431.853
1159.154 432.642
1159.307 433.431
1159.459 434.221
1159.612 437.791
1159.764 441.363
1159.916 444.933
1160.069 448.505
1160.221 448.037
1160.374 447.569
1160.526 447.101
1160.678 455.151
1160.831 463.208
1160.983 471.259
1161.136 473.544
1161.288 475.826
1161.44 478.111
1161.593 465.778
1161.745 453.435
...... .......

输出应该是深度和平滑速度 - 平均 - 并且将删除前 50 个点和最后 50 个点。

最佳答案

您可以尝试以下脚本:

awk -vn=100 -f a.awk file

a.awk 在哪里

BEGIN {
m=int((n+1)/2)
}
{L[NR]=$2; sum+=$2}
NR>=m {d[++i]=$1}
NR>n {sum-=L[NR-n]}
NR>=n{
a[++k]=sum/n
}
END {
for (j=1; j<=k; j++)
print d[j],a[j]
}

例如给定的测试数据:

1  2
2 2
3 3
4 3
5 4
6 14
7 5
8 5
9 6
10 6
11 7
12 7
13 8
14 8
15 9

并运行 awk -vn=5 -f a.awk file 给出

3 2.8
4 5.2
5 5.8
6 6.2
7 6.8
8 7.2
9 5.8
10 6.2
11 6.8
12 7.2
13 7.8

关于awk - 使用 awk 的移动平均线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20949704/

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