我有一个肌电图数据信号,我应该(科学论文的明确建议)使用 RMS 进行平滑处理。
我有以下工作代码,产生所需的输出,但它比我想象的要慢得多。
#!/usr/bin/python
import numpy
def rms(interval, halfwindow):
""" performs the moving-window smoothing of a signal using RMS """
n = len(interval)
rms_signal = numpy.zeros(n)
for i in range(n):
small_index = max(0, i - halfwindow) # intended to avoid boundary effect
big_index = min(n, i + halfwindow) # intended to avoid boundary effect
window_samples = interval[small_index:big_index]
# here is the RMS of the window, being attributed to rms_signal 'i'th sample:
rms_signal[i] = sqrt(sum([s**2 for s in window_samples])/len(window_samples))
return rms_signal
我已经看到了一些关于优化移动窗口循环的建议deque
和建议,也来自 numpy,但我无法弄清楚如何使用它们来完成我想要的。itertools
convolve
此外,我不再关心避免边界问题,因为我最终得到了大数组和相对较小的滑动窗口。
谢谢阅读