import numpy as np
a=[100,101,103,105,106,107,108,109,111,112,113,114,115,116,117,118,119,200,
201,202,203,205,207,208,209,210,212,213,214,215,217,220,221,222,223,228,230,231,232,233,234]
#设置表格样式
#写Excel
for m in a:
PATH="./MIT-BIH/" #path, 这里就是写刚才你保存的数据地址
HEADERFILE=str(m)+".hea" #文件格式为文本格式
#print("HEADERFILE:",HEADERFILE)
#continue
ATRFILE=str(m)+".atr" #attributes-file 文件以二进制格式
DATAFILE=str(m)+".dat" #data-file
SAMPLES2READ=3000 #读取的数据样本点数
####################读取头文件######################
f=open(PATH+HEADERFILE,"r")
z=f.readline().split()
nosig,sfreq=int(z[1]),int(z[2]) #% number of signals,sample rate of data
dformat,gain,bitres,zerovalue,firstvalue=[],[],[],[],[]
for i in range(nosig):
z=f.readline().split()
dformat.append(int(z[1])) #format; here only 212 is allowed
gain.append(int(z[2])) #number of integers per mV
bitres.append(int(z[3])) #bitresolution
zerovalue.append(int(z[4])) #integer value of ECG zero point
firstvalue.append(int(z[5])) #first integer value of signal (to test for errors)
f.close()
####################读取dat文件######################
f=open(PATH+DATAFILE,"rb") #以二进制格式读入dat文件
b=f.read()
f.close()
#print("b:",b)
A_init=np.frombuffer(b,dtype=np.uint8) #将读入的二进制文件转化为unit8格式
A_shape0=int(A_init.shape[0]/3)
A=A_init.reshape(A_shape0,3)[:SAMPLES2READ] #将A转为3列矩阵
M=np.zeros((SAMPLES2READ,2)) #创建矩阵M
M2H=A[:,1]>>4 #字节向右移四位,即取字节的高四位
M1H=A[:,1]&15 #取字节的低四位
PRL=(A[:,1]&8)*(2**9) #sign-bit 取出字节低四位中最高位,向左移九位,等于乘2^9
PRR=A[:,1]&128<<5 #sign-bit 取出字节高四位中最高位,向左移五位
M1H=M1H*(2**8)
M2H=M2H*(2**8)
M[:,0]=A[:,0]+M1H-PRL
M[:,1]=A[:,2]+M2H-PRR
if ((M[1,:]!=firstvalue).any()):
print("inconsistency in the first bit values")
if nosig==2:
M[:, 0] = (M[:, 0] - zerovalue[0]) / gain[0]
M[:, 1] = (M[:, 1] - zerovalue[1]) / gain[1]
TIME=np.linspace(0,SAMPLES2READ-1,SAMPLES2READ)/sfreq
elif nosig==1:
M2=[]
M[:, 0] = M[:, 0] - zerovalue[0]
M[:, 1] = M[:, 1] - zerovalue[1]
for i in range(M.shape[0]):
M2.append(M[:,0][i])
M2.append(M[:,1][i])
M2.append(0)
del M2[0]
M2=np.array(M2)/gain[0]
TIME=np.linspace(0,2*SAMPLES2READ-1,2*SAMPLES2READ)/sfreq
else:
print("")
####################读取atr文件######################
f=open(PATH+ATRFILE,"rb") #主要是读取ATR文件中各周期数据并在之后打印在图中
# print("f:",f)
b=f.read()
f.close()
#print("b:",b,'\n')
A_init=np.frombuffer(b,dtype=np.uint8)
A_shape0=int(A_init.shape[0]/2)
A=A_init.reshape(A_shape0,2)
# print("A_init: ",A_init,"A_shape0:",A_shape0,"A:",A)
ANNOT,ATRTIME=[],[]
i=0
while i < A.shape[0]:
annoth=A[i,1]>>2
if annoth==59:
ANNOT.append(A[i+3,1]>>2)
ATRTIME.append(A[i+2,0]+A[i+2,1]*(2**8)+A[i+1,0]*(2**16)+A[i+1,1]*(2**24))
i+=3
elif annoth==60:pass
elif annoth==61:pass
elif annoth==62:pass
elif annoth==63:
hilfe=(A[i,1]&3)*(2**8)+A[i,0]
hilfe=hilfe+hilfe%2
i+=int(hilfe/2)
else:
ATRTIME.append((A[i,1]&3)*(2**8)+A[i,0])
ANNOT.append(A[i,1]>>2)
i+=1
del ANNOT[len(ANNOT)-1]
del ATRTIME[len(ATRTIME)-1]
ATRTIME=np.array(ATRTIME)
ATRTIME=np.cumsum(ATRTIME)/sfreq
ind=np.where(ATRTIME<=TIME[-1])[0]
ATRTIMED=ATRTIME[ind]
ANNOT=np.round(ANNOT)
num_1=0
num_5=0
num_8=0
num_4=0
num_7=0
num_9=0
num_10=0
num_2=0
num_3=0
num_6=0
num_11=0
for i in ANNOT:
if i==1:
num_1+=1
if i==6:
num_6+=1
elif i==11:
num_11+=1
elif i==2:
num_2+=1
elif i==3:
num_3+=1
elif i==5:
num_5+=1
elif i==8:
num_8+=1
if i==4:
num_4+=1
if i==7:
num_7+=1
if i==9:
num_9+=1
elif i==10:
num_10+=1
print(m,'\t',num_1,'\t',num_2,'\t',num_3,'\t',num_4,'\t',num_5,'\t',num_6,'\t',num_7,'\t',num_8,'\t',num_9,'\t',num_10,'\t',num_11,'\t')
运行结果:
每一个mat的数据导入后workspace有三个:
M为两个导联的心电数据(360hz,单位mv),
ATRTIMED为标注的时间(单位s),
ANNOTD为ATRTIMED处的标记。
% 注释代码 说明
% 0 No TQRS
% 1 N Normal beat 正常搏动
% 2 L Left bundle branch block beat 左束支传导阻滞
% 3 R Right bundle branch block beat 右束支传导阻滞
% 4 a Aberrated atrial premature beat 异常房性早搏
% 5 V Premature ventricular contraction 室性早搏
% 6 F Fusuion of ventricular and normal beat 心室融合心跳
% 7 J Nodal (junctional) premature beat 交界性早搏
% 8 A Atrial premature beat 房性早搏
% 9 S Premature or ectopic supraventricular beat 室上性早搏或者异位心搏
% 10 E Ventricular escape beat 室性逸搏
% 11 j Nodal (junctional) escape beat 交界性逸搏
% 12 / Paced beat 起搏心跳
% 13 Q Unclassifiable beat 未分类心跳
% 14 ~ Signal quality change
% 15 Not specified
% 16 | Isolated QRS-like artifact
% 17 Not specified
% 18 s ST change
% 19 T T-wave change
% 20 * Systole
% 21 D Diastole
% 22 " Comment annotation
% 23 = Measurement annotation
% 24 p P-wave peak
% 25 B Left or right bundle branch block
% 26 ^ Non-conducted pacer spike
% 27 t T-wave peak
% 28 + Rythm change
% 29 u U-wave peak
% 30 ? Learning
% 31 ! Ventricular flutter wave 室扑
% 32 [ Start of ventricular flutter/fibrillation
% 33 ] End of ventricular flutter/fibrillation
% 34 e Atrial escape beat 房性逸搏
% 35 n Supraventricular espace beat 室上性逸搏
% 36 Not specified
% 37 x Non-conducted P-wave (blocked APB)
% 38 f Fusion of paced and normal beat 起搏心跳和正常心跳的融合
% 39 ( Waveform onset, PQ junction(begin of QRS)
% 40 ) Waveform end, JPT(J point, end of QRS)
% 41 r R-on-T premature ventricular contraction R-on-T性室性早搏