MIT-BIH 데이터 읽기, 데이터 세트 만들기
5066 단어 MATLAB 읽기 MIT 데이터
1.R파를 101점으로 하고 앞으로 100점, 뒤로 199점을 취하여 300점을 구성하여 하나의 심전주기로 한다(샘플링 확률은 360HZ이기 때문에 300점은 하나의 심전주기를 충분히 보장할 수 있다)
2.데이터 이름 + n번째 R파 + 질병 (N: 정상, V: 부정맥, O: 기타) 에 따라 이름 짓기
3.txt 형식과 좌표축이 없는 png 형식으로 저장합니다.
코드는 다음과 같습니다.
clc; clear all;
% 48 。
%------ SPECIFY DATA ------------------------------------------------------
PATH= 'S:\MIT'; % path, where data are saved
d = dir('S:\MIT\*.dat');
a = dir('S:\MIT\*.atr');
v= dir('S:\MIT\*.hea');
l=2;%1 MLII ,2 VI
for c=1:length(d)
HEADERFILE= v(c).name; % header-file in text format
ATRFILE= a(c).name; % attributes-file in binary format
DATAFILE=d(c).name; % data-file
SAMPLES2READ=660000; % number of samples to be read
% in case of more than one signal:
% 2*SAMPLES2READ samples are read
%------ LOAD HEADER DATA --------------------------------------------------
fprintf(1,'\
$> WORKING ON %s ...
', HEADERFILE);
signalh= fullfile(PATH, HEADERFILE);
fid1=fopen(signalh,'r');
z= fgetl(fid1);
A= sscanf(z, '%*s %d %d %d',[1,3]);
nosig= A(1); % number of signals
sfreq=A(2); % sample rate of data
clear A;
for k=1:nosig
z= fgetl(fid1);
A= sscanf(z, '%*s %d %d %d %d %d',[1,5]);
dformat(k)= A(1); % format; here only 212 is allowed
gain(k)= A(2); % number of integers per mV
bitres(k)= A(3); % bitresolution
zerovalue(k)= A(4); % integer value of ECG zero point
firstvalue(k)= A(5); % first integer value of signal (to test for errors)
end
fclose(fid1);
clear A;
signald= fullfile(PATH, DATAFILE); % data in format 212
fid2=fopen(signald,'r');
A= fread(fid2, [3, SAMPLES2READ], 'uint8')'; % matrix with 3 rows, each 8 bits long, = 2*12bit
fclose(fid2);
M2H= bitshift(A(:,2), -4); % ,
M1H= bitand(A(:,2), 15); %
M( : , 1)= bitshift(M1H,8)+ A(:,1); %
M( : , 2)= bitshift(M2H,8)+ A(:,3); %
M = (M-1024)/200; % M
clear A M1H M2H PRR PRL;
fprintf(1,'\
$> LOADING DATA FINISHED
');
%------ LOAD ATTRIBUTES DATA ----------------------------------------------
atrd= fullfile(PATH, ATRFILE); % attribute file with annotation data
fid3=fopen(atrd,'r');
A= fread(fid3, [2, inf], 'uint8')';
fclose(fid3);
ATRTIME=[];
ANNOT=[];
sa=size(A);
saa=sa(1);
i=1;
while i<=saa
annoth=bitshift(A(i,2),-2);
if annoth==59
ANNOT=[ANNOT;bitshift(A(i+3,2),-2)];
ATRTIME=[ATRTIME;A(i+2,1)+bitshift(A(i+2,2),8)+...
bitshift(A(i+1,1),16)+bitshift(A(i+1,2),24)];
i=i+3;
elseif annoth==60
% nothing to do!
elseif annoth==61
% nothing to do!
elseif annoth==62
% nothing to do!
elseif annoth==63
hilfe=bitshift(bitand(A(i,2),3),8)+A(i,1);
hilfe=hilfe+mod(hilfe,2);
i=i+hilfe/2;
else
ATRTIME=[ATRTIME;bitshift(bitand(A(i,2),3),8)+A(i,1)];
ANNOT=[ANNOT;bitshift(A(i,2),-2)];
end
i=i+1;
end
ANNOT(length(ANNOT))=[]; % last line = EOF (=0)
ATRTIME(length(ATRTIME))=[]; % last line = EOF
clear A;
ATRTIME= (cumsum(ATRTIME)); % R 。
ind= find(ATRTIME <= SAMPLES2READ);
ATRTIMED= ATRTIME(ind);
ANNOT=round(ANNOT);
ANNOTD= ANNOT(ind);
%------ DISPLAY DATA ------------------------------------------------------
figure(1); clf, box on, hold on
for k=1:length(ATRTIMED)
text(ATRTIMED(k),0,num2str(ANNOTD(k)));
end
%xlim([TIME(1), TIME(end)]);
xlabel('Time / s'); ylabel('Voltage / mV');
string=['ECG signal ',DATAFILE];
title(string);
fprintf(1,'\
$> DISPLAYING DATA FINISHED
');
% -------------------------------------------------------------------------
fprintf(1,'\
$> ALL FINISHED
');
%----------------------- ----------------
len1=length(ATRTIME);
for t=4:len1-3 % ,
str=num2str(t,'%04d');
if(ANNOT(t)==1) %
z=78;
else
if(ANNOT(t)==28)
z=86;
else
z=79;
end
end
fd=num2str(c);
name=['S:\MIT-txt\',v(c).name(1:end-4) '-' str, '-' z,'.txt']; %
fid=fopen(name,'wt');
f=100;% 100
b=199;% 100
m1=ATRTIME(t)-f;
m2=ATRTIME(t)+b;
n=1;
for e=m1:1:m2
fprintf(fid,'%5.3f
',M(e,l));
end
figure;
plot(M(m1:m2,l));
set(gcf,'visible','off');
axis off;
saveas(gcf,['S:\MIT-png\', v(c).name(1:end-4) '-' str, '-' z,'.png'])%
delete(gcf);
fclose(fid);
end
end