过滤(平滑)连续流数据最有效的方法是什么

2024-04-19 22:58:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在制作我自己的系统监控工具。我正在寻找一个过滤器(像高斯过滤器或类似的)对一个连续的原始数据流,我从一个设备(我的cpu%在这个例子中)。在

数据值集合的长度为n个元素。每当这段代码运行时,它都会附加新的cpu值,并删除最旧的值,使集合的长度保持在n本质上是deque([float('nan')] * n, maxlen=n),其中{}是我绘制到的图的长度。在

然后它通过高斯滤波器过滤整个采集数据,创建平滑的数据点,然后绘制它们,创建一个动画图形,类似于计算机上大多数系统监视器的cpu%图形。在

这个很好用。。。但是,必须有一种更有效的方法来过滤传入的数据,而不是在每次添加新的数据val时对整个数据集运行过滤器(在我的例子中,图形每0.2秒更新一次)

我可以想出不必过滤整个列表的方法,但我不确定它们是否非常有效。在信号处理世界里有什么东西对我有用吗?抱歉,如果我的解释有点混乱,我对这个很陌生。在

from scipy.ndimage.filters import gaussian_filter1d

# Not my actual code but hopefully describes what im doing
def animate():  # function that is called every couple of milliseconds to animate the graph
    # ... other stuff
    values.append(get_new_val) # values = collection of data vals from cpu
    line.set_ydata(gaussian_filter1d(values, sigma=4)) # line = the line object used for graphing 
    # ... other stuff
    graph_line(line)  # function that graphs the line

dr:寻找一种优化的方法来平滑原始流数据,而不是每次都过滤整个数据集。在


Tags: the数据方法from图形过滤器line绘制
1条回答
网友
1楼 · 发布于 2024-04-19 22:58:10

我从来没有用过,但是你需要的东西听起来像是Savitzky–Golay filter的意思。这是一个局部平滑滤波器,可用于使数据更可微(并在我们处理数据时对其进行区分)。在

好消息是scipy supports this filter从0.14版本开始。文件的相关部分:

scipy.signal.savgol_filter(x, window_length, polyorder, deriv=0, delta=1.0, axis=-1, mode='interp', cval=0.0)

  Apply a Savitzky-Golay filter to an array.
  This is a 1-d filter. If x has dimension greater than 1, axis determines the axis along which the filter is applied.
  Parameters:   

  x : array_like
      The data to be filtered. If x is not a single or double precision floating point array, it will be converted to type numpy.float64 before ftering.
  window_length : int
      The length of the filter window (i.e. the number of coefficients). window_length must be a positive odd integer.
  polyorder : int
      The order of the polynomial used to fit the samples. polyorder must be less than window_length.
  [...]

我先确定一对多项式的阶数和窗口大小。与使用完整的n数据点不同,您只需要平滑长度大约为window_length的更小的deque。当每个新的数据点出现时,您必须将其附加到较小的deque,应用Savitzky–Golay过滤器,获取新的过滤点,并将其附加到图形中。在

不过,请注意,在我看来,当方法不在数据集的边缘时,它通常是定义良好的。这可能意味着为了精确起见,您可能需要引入一些测量值的延迟值,以便您可以始终使用给定窗口内的点(我的意思是,对于给定的时间点,您可能需要“未来”数据点来获得可靠的过滤值)。考虑到您的数据每秒测量五次,这可能是一个合理的折衷方案,如果需要的话。在

相关问题 更多 >