用Python实现心电60hz噪声滤波

2024-06-16 12:04:19 发布

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

我从我工作的实验室开发的一个采集电路中得到了一些心电图数据,我正试图实现一个60赫兹的陷波滤波器,以尽量减少背景噪声。在

代码如下:

from scipy import fft
from scipy import ifft
import matplotlib.pyplot as plt

def notchfilter(ECGdata):
""" Filters the data using notch filter

    Description:
        Digital filter which returns the filtered signal using 60Hz
        notch filter. Transforms the signal into frequency domain
        using the fft function of the Scipy Module. Then, suppresses
        the 60Hz signal by equating it to zero. Finally, transforms
        the signal back to time domain using the ifft function.
    Input:
        ECGdata -- list of integers (ECG data)
    Output:
        ifft(fftECG) -- inverse fast fourier transformed array of filtered ECG data
"""         
fftECG = fft(ECGdata)
for i in range(len(fftECG)):
    if 590<i<620 or 880<i<910: fftECG[i]=0
return ifft(fftECG)

data = open('ECG_LOG4.TXT','r')
y = data.readlines()
data.close()

t = range(len(y))

plt.plot(t,y)
plt.show()

但我得到了一个错误:

“TypeError:无法根据规则“safe”将数组数据从dtype('U1')强制转换为dtype('complex128')。”

输入数据(“ECG_LOG4.txt”)是一个包含采样数据的列表:

312\n 319\n 312\n 290\n 296\n 309\n 309\n。。。在

有什么想法吗?我是一个Python乞丐,所以我不知道从哪里开始解决。 提前谢谢。在


Tags: ofthe数据fromimportfftdatasignal