简体   繁体   English

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

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

I am trying to apply a Butterworth filter as in this great reply How to implement band-pass Butterworth filter with Scipy.signal.butter . 我正在尝试应用Butterworth过滤器,就像在这篇很好的回复中: 如何使用Scipy.signal.butter实现带通Butterworth过滤器 However, when I use the functions from there, the result seems to be the wrong way round (x(-1)): 但是,当我从那里使用函数时,结果似乎是错误的处理方式(x(-1)):

FIGURE: applied Butterworth filter 图:应用的巴特沃思滤波器

What is wrong? 怎么了? (I assume this is wrong?) (我认为这是错的吗?)

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

Nothing is wrong. 没有错误。 What you are seeing is the normal phase shift created by an IIR filter. 您将看到IIR滤波器产生的正常相移。

If that phase shift is unacceptable, one option is to change this line: 如果该相移是不可接受的,则一种选择是更改此行:

y = lfilter(b, a, data)

to

y = filtfilt(b, a, data)

and add filtfilt to the names imported from scipy.signal . 并将filtfilt添加到从scipy.signal导入的名称中。 filtfilt applies the same filter twice, once forward and once backward, so the phase shifts "cancel". filtfilt应用相同的滤波器两次,一次向前,一次向后,因此相移为“取消”。 If you use filtfilt , you can lower the order of the filter because you are applying it twice. 如果使用filtfilt ,则可以降低过滤器的顺序,因为您要应用两次。

Another option is to use a FIR filter instead of an IIR filter. 另一种选择是使用FIR滤波器而不是IIR滤波器。 Questions about the behavior of filters are probably best asked over at dsp.stackexchange.com. 有关过滤器行为的问题最好在dsp.stackexchange.com上提出。

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

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