繁体   English   中英

巴特沃思滤波器-输出x(-1)?

[英]butterworth filter - output x (-1)?

我正在尝试应用Butterworth过滤器,就像在这篇很好的回复中: 如何使用Scipy.signal.butter实现带通Butterworth过滤器 但是,当我从那里使用函数时,结果似乎是错误的处理方式(x(-1)):

图:应用的巴特沃思滤波器

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

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()

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

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

y = lfilter(b, a, data)

y = filtfilt(b, a, data)

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM