如何比较 scipy 噪声滤波器?

How to compare scipy noise filters?

我需要减少数据中的噪音行为。我尝试了一种名为 Savitzky-Golay Filter 的方法。另一方面,我需要找到最快的方法,因为过滤算法将在我的代码中最多 运行 个脚本中。

我不熟悉信号处理方法。您能简单地建议一下更快的方法和用法吗?

我不需要低通、高通等复杂结构(我知道有成千上万种)。尽可能快的平滑方法是我想用的。

这是我的测试脚本:

import numpy as np
import matplotlib.pyplot as plt

noisyData=np.array([
   2.77741650e+43,   1.30016392e+42,   8.05792443e+42,   1.74277713e+43,
   2.33814198e+43,   6.75553976e+42,   2.56642073e+43,   4.71467220e+43,
   4.25047666e+43,   3.07095152e+43,   7.30694187e+43,   7.54411548e+43,
   1.29555422e+43,   8.09272000e+42,   9.18193162e+43,   2.25447063e+44,
   3.43044832e+41,   7.02901256e+43,   2.54438379e+43,   8.72303015e+43,
   7.80333557e+42,   7.55039871e+43,   7.70164773e+43,   4.38740319e+43,
   8.43139041e+43,   6.12168640e+43,   5.64352020e+43,   3.63824769e+42,
   2.35296604e+43,   4.66272666e+43,   5.03660902e+44,   1.65071897e+44,
   2.81055925e+44,   1.46401444e+44,   5.44407940e+43,   4.50672710e+43,
   1.60833084e+44,   1.68038069e+44,   1.08588606e+44,   7.00867980e+43])

xAxis=np.arange(len(noisyData))

# ------------- Savitzky-Golay Filter ---------------------
windowLength = len(xAxis) - 5 
polyOrder = 6

from scipy.signal import savgol_filter

# Function
def set_SavgolFilter(noisyData,windowLength,polyOrder):
    return savgol_filter(noisyData, windowLength, polyOrder)

plt.plot(xAxis,noisyData,alpha=0.5)
plt.plot(xAxis,set_SavgolFilter(noisyData,windowLength,polyOrder))

# ------------- Time Comparison ----------------------
import time
start_time = time.time()
for i in range(50):
    savgolfilter1 = set_SavgolFilter(noisyData,windowLength,polyOrder)
print(" %s seconds " % (time.time() - start_time))

# === OTHER METHODS WILL BE HERE

除非您真的需要基于多项式的平滑,否则 Savitzky-Golay 没有任何特别的优势。它基本上是一个糟糕的低通滤波器。有关详细信息,请参阅 https://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=5888646

改用基本的巴特沃斯低通滤波器:

from scipy.signal import butter, filtfilt
b, a = butter(5, .2)
datafilt = filtfilt(b, a, noisyData)

filtfilt 调用似乎比 savgol_filter 快几倍。你需要多快?使用 scipy 中的 lfilter 至少快 10 倍,但结果会相对于输入信号有所延迟。