代码:
from scipy.signal import butter, lfilter
import numpy as np
import matplotlib.pyplot as plt
import math
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()

参考资料:
https://stackoverflow.com/questions/33769281/butterworth-filter-output-x-1
本文介绍了一种使用Butterworth滤波器进行信号滤波的方法,通过Python的Scipy库实现带通滤波,有效去除噪声并保留所需频率范围内的信号。演示了如何设置滤波器参数,包括截止频率和滤波器阶数,并展示了滤波前后的信号对比。

9796

被折叠的 条评论
为什么被折叠?



