使用awk的移动平均 我想在一个列中平均每100个点,然后把平均值放在中间点50点。< /P>
我尝试使用以下脚本计算移动平均值:使用awk的移动平均 我想在一个列中平均每100个点,然后把平均值放在中间点50点。< /P>,awk,moving-average,Awk,Moving Average,我尝试使用以下脚本计算移动平均值: BEGIN { P = 100; } { x = $2; i = NR % P; MA += (x - Z[i]) / P; Z[i] = x; print $1,"\t",$2,"\t",MA; } 但是我需要知道怎么把它放在中间? 输入的示例: Depth Ve
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个点将被删除 如果没有输入,就很难理解您想要实现的目标,下面是一个5点移动平均值和3点索引的示例,根据您的需要实施
awk '
BEGIN{
OFS = "\t"
No_of_row = 20
Average_Point = 5
index_Point = 3
print "Column1" OFS "Column2"
for(i=1;i<=No_of_row;i++)
{
C1[i]=i
C2[i]=i
print i OFS i
}
print RS "index" OFS "Average" OFS "Data_used" OFS "Sum" OFS "No_of_Point"
}
END{
for(i=1;i<=No_of_row;i++){
for(j=1;j<=Average_Point;j++){
flag = 0
if(C1[i+j] || j == 1 && C1[i])
{
add = j==1 ? C2[i] : C2[(i+j)-1]
sum += add
ind = j==index_Point ? C1[(i+j)-1] : ind
flag = 1
s = s ? s "+" add : add
}
}
if(flag == 1){
print ind,sum/Average_Point,s,sum,Average_Point
}
sum = ind = s = ""
}
}
' /dev/null
您必须修改行数以适合您的文件,输入应包含两列,一列作为索引,另一列将被平均,输出将减少100个点,每边50个
# Moving average over the Second column of a data file
BEGIN{
OFS = "\t"
No_of_row = 10294
Average_Point = 100
index_Point = 50
}
{
for(i=1;i<=No_of_row;i++){
NR == i &&
C1[i]= $1
NR == i &&
C2[i]= $2 } }
END { for(i=1;i<=No_of_row - Average_Point;i++){
for(j=1;j<=Average_Point;j++){
if(C1[i+j] || j == 1 && C1[i])
{
add = j==1 ? C2[i] : C2[(i+j)-1]
sum += add
ind = j==index_Point ? C1[(i+j)-1] : ind
}
}
{
print ind,sum/Average_Point
}
sum = ind = ""
}
}
#数据文件第二列上的移动平均值
开始{
OFS=“\t”
第行的编号=10294
平均点=100
指数_点=50
}
{
对于(i=1;i您可以尝试以下脚本:
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]
}
运行awk-vn=5-fa.awk文件
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
对于案例P=2
,你能说明你的输入和预期输出应该是什么样子吗?你能在你的代码中添加一些注释来解释它是如何工作的吗?输出与我想要的类似,但我无法理解它是如何工作的。它比其他脚本花费更少的时间。结果也是一样的。谢谢只是作为一个注释:脚本工作正常,它很有用请让我为自己找到一个定制的解决方案。但为了将来的答案,如果您可以使用更长、更具声明性的变量名,那就太好了。这样,您想做什么就更清楚了。我知道,不是很奇怪
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
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