【发布时间】:2013-02-01 18:03:08
【问题描述】:
我有一个看起来像这样的txt 文件:
0.065998 81
0.319601 81
0.539613 81
0.768445 81
1.671893 81
1.785064 81
1.881242 954
1.921503 193
1.921605 188
1.943166 81
2.122283 63
2.127669 83
2.444705 81
第一列是以字节为单位的数据包到达和第二个数据包大小。
我需要获取每秒字节的平均值。例如,在第一秒,我只有值为 81 的数据包,因此平均比特率为81*8= 648bit/s。然后我应该以秒为单位绘制一个图表 x 轴时间,每秒 y 轴平均比特率。
到目前为止,我只设法将我的数据作为数组上传:
import numpy as np
d = np.genfromtxt('data.txt')
x = (d[:,0])
y = (d[:,1 ])
print x
print(y*8)
我是 Python 新手,因此非常感谢您从哪里开始提供任何帮助!
这是结果脚本:
import matplotlib.pyplot as plt
import numpy as np
x, y = np.loadtxt('data.txt', unpack=True)
bins = np.arange(60+1)
totals, edges = np.histogram(x, weights=y, bins=bins)
counts, edges = np.histogram(x, bins=bins)
print counts
print totals*0.008/counts
plt.plot(totals*0.008/counts, 'r')
plt.xlabel('time, s')
plt.ylabel('kbit/s')
plt.grid(True)
plt.xlim(0.0, 60.0)
plt.show()
脚本读取包含数据包大小(字节)和到达时间的 .txt 文件,并绘制一个时间段内的平均比特率/秒。用于监控服务器传入/传出流量!
【问题讨论】:
-
第一秒,你只收到了 81*4 位,对吧?
-
您能否指定“平均比特率”的定义。想要自数据开始以来的平均值?还是对平均值的最新估计?
-
是的,第一秒我收到了 4 个 81 字节的数据包。我想获得每秒的平均数据包大小(以比特为单位)。假设我们在第一秒收到了 10 个不同大小的数据包,所以我需要在第一秒获取这 10 个数据包的平均值,以此类推。