Python 中的 ECG 60 Hz 噪声过滤

问题描述 投票:0回答:1

我从我工作的实验室开发的采集电路中获得了一些 ECG 数据,我正在尝试实施 60 Hz 陷波滤波器以最小化背景噪声。

这是代码:

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()

但是我收到这个错误:

“类型错误:无法根据规则‘安全’将数组数据从 dtype(‘U1’) 转换为 dtype(‘complex128’)。”

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

312 319 312 290 296 309 309 ...

有什么想法吗?我是 python 初学者,所以我不知道从哪里开始解决。 提前致谢。

python scipy signal-processing
1个回答
0
投票

我设法使用频率以更清洁的方式做到了这一点。

fs = 160 # pulling rate
N = len(ECGdata)
T = 1/fs
t = np.arange(0,N/fs,T)
f = np.fft.fftfreq(N,T)
fft = np.abs(np.fft.fft(ECGdata))


min = 59.8
max = 60.2

fft[(f>min) & (f<max)] = 0

请记住,您应该微调最小值和最大值😊.

© www.soinside.com 2019 - 2024. All rights reserved.