【问题标题】:butterworth filter - output x (-1)?巴特沃斯滤波器 - 输出 x (-1)?
【发布时间】:2016-02-19 13:15:41
【问题描述】:

我正在尝试应用 Butterworth 过滤器,就像在这个很棒的回复 How to implement band-pass Butterworth filter with Scipy.signal.butter 中一样。但是,当我从那里使用函数时,结果似乎是错误的 (x(-1)):

FIGURE: applied Butterworth filter

怎么了? (我认为这是错误的?)

from scipy.signal import butter, lfilter

def butter_bandpass(lowcut, highcut, fs, order=5):
    nyq = 0.5 * fs
    low = lowcut / nyq
    high = highcut / nyq
    b, a = butter(order, [low, high], btype='band')
    return b, a


def butter_bandpass_filter(data, lowcut, highcut, fs, order=5):
    b, a = butter_bandpass(lowcut, highcut, fs, order=order)
    y = lfilter(b, a, data)
    return y

x=np.array(range(100))
y1=np.array([math.sin(2*3.14*xx/3) for xx in x])
y2=np.array([2*math.sin(2*3.14*xx/30) for xx in x])
y0=x*0.05
y=y1+y2+y0

lowcut=1./10000000.
highcut=1./20.

plt.figure(3)
plt.clf()

plt.plot(x, y, label='Noisy signal',lw=2.5,color='blue')
plt.plot(x,y0,ls='--',color='blue')
plt.plot(x,y1,ls='--',color='blue')
plt.plot(x,y2,ls='--',color='blue')

ysm = butter_bandpass_filter(y, lowcut, highcut, fs, order=6)

plt.plot(x, ysm, label='Filtered signal',lw=2.5,color='red')
plt.grid(True)
plt.axis('tight')
plt.legend(loc='upper left')

plt.show()

【问题讨论】:

    标签: python filter scipy signals


    【解决方案1】:

    没有错。您看到的是由 IIR 滤波器产生的正常相移。

    如果该相移不可接受,则一种选择是更改此行:

    y = lfilter(b, a, data)
    

    y = filtfilt(b, a, data)
    

    并将filtfilt 添加到从scipy.signal 导入的名称中。 filtfilt 两次应用相同的滤波器,一次向前,一次向后,因此相移“取消”。如果您使用filtfilt,您可以降低过滤器的顺序,因为您应用了两次。

    另一种选择是使用 FIR 滤波器而不是 IIR 滤波器。最好在 dsp.stackexchange.com 上询问有关过滤器行为的问题。

    【讨论】:

    • 嗨沃伦,非常感谢您的快速回复!这是有道理的,看来我需要进一步研究它们是如何工作的。使用 filtfilt 和 order=3 过滤后的信号看起来是同相的,但会发生偏移和重新缩放(平均值
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-09
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多