简单的利用有限脉冲响应(FIR)滤波器对心电信号进行降噪(Python)

代码很简单。

复制代码
import numpy as np
import matplotlib.pyplot as plt
 
#------------------------Bandstop Filter Function------------------------
def bandstop(M,low,high,Fs):
    #50Hz removal
    k1 = int( (low/Fs)*M) # index 22
    k2 = int( (high/Fs)*M) # index 27
    #DC removal
    k0 = int( (1/Fs)*M) # index 0

    #Creating the desired frequency response X for the bandstop filter
    X = np.ones(M)  # Frequency response
    #DC removal
    X[0:k0+1]=0 # from index 0 to 0
    #50Hz removal
    X[k1:k2+1]=0 # from index 22 to 27
    #Mirror of the 50Hz removal
    X[M-k2:M-k1+1] = 0 # from index 492 to 477
    
    #Passing the created frequency response to ifft to get impulse response signal
    x = np.real(np.fft.ifft(X)) # signal x - impulse response of system
    return x

#-----------------------------FIR Filter Function---------------------------
def FIR_filter(ecg,h):
    M = len(ecg) # length of ecg
    N = len(h) #length of coefficients h
    filtered = np.zeros( M + N - 1 ) # list of zeros of length M+N-1
    for n in range( M+N ): #iterates from 0 to M+N
        for k in range(N): #iterates from 0 to N
            if 0 <= n-k <= M-1 :  #allows only possible index numbers for ecg
                filtered[n] = filtered[n] + ecg[n-k]*h[k] # convolution formula
    filtered = filtered[int(N/2):] #removing first 250 values
    filtered = filtered[: int(len(filtered) - N/2)] #removing last 250 values
    return filtered 

#----------Importing and preparing the signal before filtering-----------       
data= np.loadtxt('ecg_data.dat')
xval = data[:,0]
ecg = data[:,1]

ampGain = 500; # Amplitude Gain
Fs = 1000; # Sampling Frequency

# Reducing the signal to remove amplitude gain 
ecg = ecg/ampGain #ecg amplitude in mVs
midval =  min(ecg) + (max(ecg)-min(ecg))/2
ecg = ecg-midval #normalising

#---------------------------------Filtering-----------------------------
# Designing a FIR filter using Window method


# Bandstop filter
M = 500 #length/order of filter
x = bandstop(M,45,55,Fs)

# Positioning first half in second half and second half in first half
# making the 45-55hz removal around midpoint
# which accordingly denoise the signal
h = np.zeros(M)
h[0:int(M/2)] = x[int(M/2):M] # 250 to 499
h[int(M/2):M] = x[0:int(M/2)] # 0 to 249

# Hamming window (Taper formed by weighted cosine)
# Maximum value normalised to one
h = np.hamming(M)*h 

#Filtering the whole signal
Filtered_signal = FIR_filter(ecg,h)

#-------------------------------Plotting---------------------------------
plt.figure(1)
plt.plot(xval,ecg)
plt.title('Unfiltered ECG [time domain]')
plt.xlabel('Time [mS]')
plt.ylabel('Amplitude')
plt.grid()

plt.figure(2) 
plt.plot(Filtered_signal)
plt.title("Filtered ECG [time domain]")
plt.xlabel("Time [ms]")
plt.ylabel("Amplitude")
plt.grid()

plt.figure(3)
plt.plot(xval[5000:6000],ecg[5000:6000])
plt.xlabel("Time [ms]")
plt.ylabel("Amplitude")
plt.title("Unfiltered ECG [Momentary]")
plt.grid()

plt.figure(4)
plt.plot(xval[5000:6000],Filtered_signal[5000:6000])
plt.xlabel("Time [ms]")
plt.ylabel("Amplitude")
plt.title("Filtered ECG [Momentary]")    
plt.grid()

#Frequency response for the ECG
fftdata = np.fft.fft(ecg)
faxis = np.linspace(0,Fs, len(fftdata))

plt.figure(5)
plt.plot(faxis, np.abs(fftdata))
plt.title("Unfiltered ECG [frequency domain]")
plt.xlabel("Frequency [Hz]")
plt.ylabel("Amplitude")
plt.grid()

#Frequency response for the filtered ECG
fftdata1 = np.fft.fft(Filtered_signal)
faxis1 = np.linspace(0,Fs, len(fftdata1))

plt.figure(6)
plt.plot(faxis1, np.abs(fftdata1))
plt.title('Filtered ECG [frequency domain]')
plt.xlabel("Frequency [Hz]")
plt.ylabel("Amplitude")
plt.grid()

工学博士,担任《Mechanical System and Signal Processing》《中国电机工程学报》《控制与决策》等期刊审稿专家,擅长领域:现代信号处理,机器学习,深度学习,数字孪生,时间序列分析,设备缺陷检测、设备异常检测、设备智能故障诊断与健康管理PHM等。

相关推荐
不爱写代码的玉子几秒前
HALCON透视矩阵
人工智能·深度学习·线性代数·算法·计算机视觉·矩阵·c#
Java 技术轻分享7 分钟前
《树数据结构解析:核心概念、类型特性、应用场景及选择策略》
数据结构·算法·二叉树··都差速
我很好我还能学20 分钟前
【面试篇 9】c++生成可执行文件的四个步骤、悬挂指针、define和const区别、c++定义和声明、将引用作为返回值的好处、类的四个缺省函数
开发语言·c++
芜湖xin31 分钟前
【题解-洛谷】P1706 全排列问题
算法·dfs
蓝婷儿41 分钟前
6个月Python学习计划 Day 16 - 面向对象编程(OOP)基础
开发语言·python·学习
小喵喵生气气1 小时前
Python60日基础学习打卡Day46
深度学习·机器学习
渣渣盟1 小时前
基于Scala实现Flink的三种基本时间窗口操作
开发语言·flink·scala
chao_7891 小时前
链表题解——两两交换链表中的节点【LeetCode】
数据结构·python·leetcode·链表
糯米导航1 小时前
Java毕业设计:办公自动化系统的设计与实现
java·开发语言·课程设计
糯米导航1 小时前
Java毕业设计:WML信息查询与后端信息发布系统开发
java·开发语言·课程设计