心电信号滤波

2024-09-27 07:27:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试过滤从Bioplux传感器获取的ECG信号。我包括低通滤波器,以消除噪声频率超过200赫兹,高通滤波器消除基线漂移,和陷波滤波器,以消除电力线频率60赫兹。我不明白为什么经过高通滤波后,我会得到一个波浪形的输出。有人能提出一些想法来获得正确过滤的心电图信号吗?rawsignal数据文本文件和过滤后获得的输出可以在以下链接中看到:

https://drive.google.com/open?id=1hvvQpMMa_hn9VNlUh4HAqb_H1lCu70or

我写的代码如下:

from scipy import signal
from scipy.signal import butter, iirnotch, lfilter
import numpy as np
import matplotlib.pyplot as plt

def butter_highpass(cutoff, fs, order=5):
    nyq = 0.5*fs
    normal_cutoff = cutoff/nyq
    b, a = butter(order, normal_cutoff, btype='high', analog=False, output='ba')
    return b, a

def butter_lowpass(cutoff, fs, order=5):
    nyq = 0.5*fs
    normal_cutoff = cutoff/nyq
    b, a = butter(order, normal_cutoff, btype='low', analog=False, output='ba')
    return b, a

def notch_filter(cutoff, q):
    nyq = 0.5*fs
    freq = cutoff/nyq
    b, a = iirnotch(freq, q)
    return b, a

def highpass(data, fs, order=5):
    b,a = butter_highpass(cutoff_high, fs, order=order)
    x = lfilter(b,a,data)
    return x

def lowpass(data, fs, order =5):
    b,a = butter_lowpass(cutoff_low, fs, order=order)
    y = lfilter(b,a,data)
    return y

def notch(data, powerline, q):
    b,a = notch_filter(powerline,q)
    z = lfilter(b,a,data)
    return z

def final_filter(data, fs, order=5):
    b, a = butter_highpass(cutoff_high, fs, order=order)
    x = lfilter(b, a, data)
    d, c = butter_lowpass(cutoff_low, fs, order = order)
    y = lfilter(d, c, x)
    f, e = notch_filter(powerline, 30)
    z = lfilter(f, e, y)    
    return x
    return y
    return z

rawdata = np.loadtxt('D:\CANADA TRIP\BiosignalsSample.txt', skiprows=0)
signal = rawdata
fs = 1000

cutoff_high = 0.5
cutoff_low = 200
powerline = 60
order = 6

#print(signal)
plt.figure(1)
ax1 = plt.subplot(321)
plt.plot(signal)
ax1.set_title("Raw signal")

conditioned_signal = final_filter(signal, fs, order)
ax2 = plt.subplot(322)
plt.plot(conditioned_signal)
ax2.set_title("Conditioned signal")

hsignal = highpass(signal, fs, order)
ax3 = plt.subplot(323)
plt.plot(hsignal)
ax3.set_title("Only highpass filter")

lsignal = lowpass(signal, fs, order)
ax4 = plt.subplot(324)
plt.plot(lsignal)
ax4.set_title("Only lowpass filter")

nonotch_1 = highpass(signal, fs, order)
nonotch = lowpass(nonotch_1, fs, order)
ax5 = plt.subplot(325)
plt.plot(nonotch)
ax5.set_title("High and low pass")

onlynotch = notch(signal, powerline, 30)
ax6 = plt.subplot(326)
plt.plot(onlynotch)
ax6.set_title("Notch filter only")

plt.show()

Tags: datasignalreturndeforderpltfilterfs

热门问题