python-scipy 生成巴特沃斯带通滤波器

#####################################
# 带通滤波,0.5~70hz  
#####################################

def butterBandPassFilter(lowcut, highcut, samplerate, order):
    "生成巴特沃斯带通滤波器"
    semiSampleRate = samplerate*0.5
    low = lowcut / semiSampleRate
    high = highcut / semiSampleRate
    b,a = signal.butter(order,[low,high],btype='bandpass')
    print("bandpass:","b.shape:",b.shape,"a.shape:",a.shape,"order=",order)
    # print("b=",b)
    # print("a=",a)
    return b,a

SampleRate = 360


plt.figure(figsize=(16,4))
# x = np.fromfile(PATH + str('.dat'),dtype=np.float32)
x = (wfdb.rdrecord(PATH, physical=False, channels=[0,],sampfrom = Sampfrom  , sampto = Sampfrom + Signallength).d_signal)/1000  
plt.plot(x[0:2000], label = 'before')

#带通滤波

#进行带通滤波0.5~45hz

b,a = butterBandPassFilter(0.5,70,SampleRate,order=4) #b,a: IIR滤波器的分子(b)和分母(a)多项式系数向量
x = signal.lfilter(b,a,x)   #lfilter 滤波后的波形有偏移,filtfilt滤波后的没有偏移
plt.plot(x,label = 'after')
plt.legend()

输出如下图:
在这里插入图片描述

biosppy内置有滤波接口

#####################################
# 0.5~45hz  滤波
#####################################
import biosppy
from biosppy.signals.tools import filter_signal

Sampfrom = 184000
Signallength = 1000

#心律失常数据库360hz,
ecg = ((wfdb.rdrecord(PATH, physical=False, channels=[0,],sampfrom = Sampfrom  , sampto = Sampfrom + Signallength).d_signal)/1000)[0:Signallength, 0]  
plt.plot(ecg,label = 'before')
ecg_after = filter_signal(ecg,ftype='FIR',band='bandpass',order=50,frequency=[0.5,45],sampling_rate=360)[0]
plt.plot(ecg_after,label = 'after')
plt.legend()

结果为:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/HJ33_/article/details/121379384