矢量的循环缓冲区

时间:2016-01-16 15:17:33

标签: python numpy matplotlib

我曾在stdin获得一个浮点数并进行处理。但是,现在我需要每秒处理几个以空格分隔的值。由于我对python缺乏经验,很难选择合适的数据结构。

  • 我需要计算每个频道的数据平均值。如果数据存储为numpy.matrix,则这很简单。

  • 我还需要为每个频道存储固定数量的历史记录。到目前为止,collections.deque(maxlen=x)做得非常好。

  • 最后,我需要在y_axis中为matplotlib.Axes.plot(x_axis, y_asis)传递迭代次数,以便绘制所有信号。例如,如果数据存储为矩阵的双端队列,那么每一秒我都需要为每个通道组成值列表。

我应该使用什么数据结构?

1 个答案:

答案 0 :(得分:0)

你的过程似乎很慢,所以你有时间在deque中获取信息。只需将numpy世界整合起来,便于平均和情节。它看起来像:

10 buffers

为动画运行main()。

import numpy as np
import matplotlib.pyplot as plt
import collections as co
buffer=[]
for i in range(10):
    buffer.append(co.deque(maxlen=15))

def refresh(n):
    for t in range(10*n):
        x=np.random.rand()+t%10
        buffer[t%10].append(x)

refresh(15) # init  
buff=np.asarray(buffer)
fig = plt.figure()
ax = fig.add_subplot(111)
line = ax.plot(buff.T) 


def main():
    while True:
        refresh(1)
        buff=np.asarray(buffer) 
        print (buff.mean(axis=1))
        for i in range(10) :
            line[i].set_ydata(buff[i])
        fig.canvas.draw()
        for t in range(10**6):pass #tempo