我正在尝试应用一个巴特沃斯过滤器,如这个伟大的回复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()
没什么不对的。你看到的是IIR滤波器产生的正常相移。在
如果相移不可接受,一种选择是改变这条线:
到
^{pr2}$并将} 应用相同的滤波器两次,一次向前,一次向后,因此相移“取消”。{cd1>你可以使用两次较低的顺序。在
filtfilt
添加到从scipy.signal
导入的名称中。^{另一个选择是使用FIR滤波器而不是IIR滤波器。关于过滤器行为的问题最好在dsp.stackexchange.com网站. 在
相关问题 更多 >
编程相关推荐