Ecg Extraction Code
-
Upload
abdkabeer-akande -
Category
Documents
-
view
113 -
download
3
Transcript of Ecg Extraction Code
Contents
n ZERO CROSSING REMOVAL
n DETECT R_PEAK
n Calculate R in the actual Signal
n After R Peak Tracking ... DETCET OTHERS
n ONSET
n P Peak
n Q Detection
n S Detection
n T Peak
n END OF T
% Load original 1D signal.
clc;
clear all;
close all;
ELEVATED=[];
[fname path]=uigetfile('*.mat');
fname=strcat(path,fname);
load(fname );
z=zeros(100,1);
A=val(1,:);
v1=val(1,:)-val(1,1);
A=v1;
A=A';
zc=A(1);
A=[z;A;z];
% Plot Actual Signal s = A(1:1:400);
s = A;
ls = length(s);
figure(1)
plot(s);
title('Original Signal'),grid on
% Perform decomposition at level 8 of s using db4
[c,l]=wavedec(s,8,'db4');
ca1=appcoef(c,l,'db4',1);
ca2=appcoef(c,l,'db4',2);
ca3=appcoef(c,l,'db4',3);
ca4=appcoef(c,l,'db4',4);
ca5=appcoef(c,l,'db4',5);
ca6=appcoef(c,l,'db4',6);
ca7=appcoef(c,l,'db4',7);
ca8=appcoef(c,l,'db4',8);
%Plot the coefficient
figure(2)
plot(c),title('decomposed signal'),grid on
figure(3)
subplot(4,2,1)
Generated by Foxit PDF Creator © Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
plot(ca1),title('1st level approximation'),grid on
subplot(4,2,2)
plot(ca2),title('2nd level approximation'),grid on
subplot(4,2,3)
plot(ca3),title('3rd level approximation'),grid on
subplot(4,2,4)
plot(ca4),title('4th level approximation'),grid on
subplot(4,2,5)
plot(ca5),title('5th level approximation'),grid on
subplot(4,2,6)
plot(ca6),title('6th level approximation'),grid on
subplot(4,2,7)
plot(ca7),title('7th level approximation'),grid on
subplot(4,2,8)
plot(ca8),title('8th level approximation'),grid on
% Extract detail coefficients at levels
% 1, 2 and 3, from wavelet decomposition
% structure [c,l].
[cd1,cd2,cd3,cd4,cd5,cd6,cd7,cd8] = detcoef(c,l,[1 2 3 4 5 6 7 8]);
figure(4)
subplot(4,2,1)
plot(cd1),title('detail coeff: level 1 cd1'),grid on
subplot(4,2,2)
plot(cd2),title('detail coeff: level 2 cd2'),grid on
subplot(4,2,3)
plot(cd3),title('detail coeff: level 3 cd3'),grid on
subplot(4,2,4)
plot(cd4),title('detail coeff: level 4 cd4'),grid on
subplot(4,2,5)
plot(cd5),title('detail coeff: level 5 cd5'),grid on
subplot(4,2,6)
plot(cd6),title('detail coeff: level 6 cd6'),grid on
subplot(4,2,7)
plot(cd7),title('detail coeff: level 7 cd7'),grid on
subplot(4,2,8)
plot(cd8),title('detail coeff: level 8 cd8'),grid on
Generated by Foxit PDF Creator © Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
Generated by Foxit PDF Creator © Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
ZERO CROSSING REMOVAL
base_corrected=ca2;
y=base_corrected-zc;
figure(5)
plot(y),grid on
title('Noise Free signal')
Generated by Foxit PDF Creator © Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
DETECT R_PEAK
y1=y;
m1=max(y1)-max(y1)*.60;
P=find(y1>=m1);
% it will give two two points ... remove one point each
P1=P;
P2=[];
last=P1(1);
P2=[P2 last];
for(i=2:1:length(P1))
if(P1(i)>(last+10))
last=P1(i);
P2=[P2 last];
end
end
Rt=y1(P2);
figure(6)
plot(y1),grid on,hold on
plot(P2,Rt,'*');
title('Rpos in downsampled signal');
Generated by Foxit PDF Creator © Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
C� � � � � � te R � n t� e � � t� � � S� � n� �
P3=P2*4;
Rpos=[];
for( i=1:1:length(P3))
range= [P3(i)-20:P3(i)+20];
m=max(A(range));
l=find(A(range)==m);
pos=range(l);
Rpos=[Rpos pos];
end
Ramp=A(Rpos);
figure(7)
plot(A),grid on,hold on
plot(Rpos,Ramp,'*');
title('Detected R peak in actual Signal');
R_peaks = length(Rpos)
%disp([num2str(R_peaks) ' peaks'])
R_peaks =
13
Generated by Foxit PDF Creator © Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
A� ter R Pe� � Tr� � � � n� � � � DETCET OT� ERS
ONSET
X=Rpos;
y1=A;
for(i=1:1:1)
for(j=1:1:length(X))
a=X(j)-100:X(j)-50;
m=max(y1(a));
b=find(y1(a)==m);
b=b(1);
b=a(b);
fnd=0;
for k=b-20:+1:b
if((y1(k)<=0) && (y1(k-1)>0))
qon1=k;
fnd=1;
break
end
end
if(fnd==0)
Qrange=b-20:+1:b;
qon1=find(y1(Qrange)==max(y1(Qrange)));
qon1=Qrange(qon1);
end
Generated by Foxit PDF Creator © Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
P Pe� �
� Dete� t� on
RON(i,j)=qon1(1);
fnd;
for k=b:+1:b+20
if((y1(k)<=0) && (y1(k-1)>0))
qon1=k;
fnd=1;
break
end
end
if(fnd==0)
Qrange=b:+1:b+20;
qon1=find(y1(Qrange)==max(y1(Qrange)));
qon1=Qrange(qon1);
end
ROF(i,j)=qon1(1);
a=Rpos(i,j)-100:Rpos(i,j)-10;
m=max(y1(a));
b=find(y1(a)==m);
b=b(1);
b=a(b);
Ppos(i,j)=b;
Pamp(i,j)=m;
end
a=Rpos(i,j)-50:Rpos(i,j)-10;
m=min(y1(a));
b=find(y1(a)==m);
b=b(1);
b=a(b);
Qpos(i,j)=b;
Qamp(i,j)=m;
%%%%% ONSET
fnd=0;
for k=b-20:+1:b
if((y1(k)<=0) && (y1(k-1)>0))
qon1=k;
fnd=1;
break
end
end
if(fnd==0)
Qrange=b-20:+1:b;
qon1=find(y1(Qrange)==max(y1(Qrange)));
qon1=Qrange(qon1);
end
QON(i,j)=qon1(1);
fnd;
for k=b:+1:b+20
if((y1(k)<=0) && (y1(k-1)>0))
qon1=k;
Generated by Foxit PDF Creator © Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
S Dete� t� on
T Pe� �
fnd=1;
break
end
end
if(fnd==0)
Qrange=b:+1:b+20;
qon1=find(y1(Qrange)==max(y1(Qrange)));
qon1=Qrange(qon1);
end
QOF(i,j)=qon1(1);
a=Rpos(i,j)+5:Rpos(i,j)+50;
m=min(y1(a));
b=find(y1(a)==m);
b=b(1);
b=a(b);
Spos(i,j)=b;
Samp(i,j)=m;
%%%% onset off
fnd=0;
for k=b-5:+1:b
if((y1(k)<=0) && (y1(k-1)>0))
qon1=k;
fnd=1;
break
end
end
if(fnd==0)
Qrange=b-20:+1:b;
qon1=find(y1(Qrange)==max(y1(Qrange)));
qon1=Qrange(qon1);
end
SON(i,j)=qon1(1);
fnd=0;
for k=b:+1:b+20
if((y1(k)<=0) && (y1(k-1)>0))
qon1=k;
fnd=1;
break
end
end
if(fnd==0)
Qrange=b:+1:b+20;
qon1=find(y1(Qrange)==max(y1(Qrange)));
qon1=Qrange(qon1);
end
SOFF(i,j)=qon1(1);
a=Rpos(i,j)+25:Rpos(i,j)+100;
m=max(y1(a));
b=find(y1(a)==m);
b=b(1);
b=a(b);
Generated by Foxit PDF Creator © Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
END O� T
Tpos(i,j)=b;
Tamp(i,j)=m;
%%%% onset off
fnd=0;
for k=b-20:+1:b
if((y1(k)<=0) && (y1(k-1)>0))
qon1=k;
fnd=1;
break
end
end
if(fnd==0)
Qrange=b-20:+1:b;
qon1=find(y1(Qrange)==max(y1(Qrange)));
qon1=Qrange(qon1);
end
TON(j,i)=qon1(1);
fnd=0;
for k=b:+1:b+20
if((y1(k)<=0) && (y1(k-1)>0))
qon1=k;
fnd=1;
break
end
end
if(fnd==0)
Qrange=b:+1:b+20;
qon1=find(y1(Qrange)==max(y1(Qrange)));
qon1=Qrange(qon1);
end
TOFF(j,i)=qon1(1);
if(Tamp(i,j)<Pamp(i,j))
a=Rpos(i,j)+25:Rpos(i,j)+70;
m=min(y1(a));
b=find(y1(a)==m);
b=b(1);
b=a(b);
Tpos(i,j)=b;
Tamp(i,j)=m;
ELEVATED=[ELEVATED j];
end
end
figure;
k=1;
for(i=1:1:1)
%subplot(6,1,k);
plot(y1), hold on;
plot(Rpos(i,:),Ramp(i,:),'*'),hold on;
plot(Qpos(i,:),Qamp(i,:),'+'),hold on;
plot(Spos(i,:),Samp(i,:),'+'),hold on;
plot(Ppos(i,:),Pamp(i,:),'.'), hold on
plot(Tpos(i,:),Tamp(i,:),'^')
Generated by Foxit PDF Creator © Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
legend('Ramp','Qamp','Samp','Pamp','Tamp')
grid on;
k=k+1;
if(k>6)
k=1;
figure;
end
end
% Detected no of peaks
R_peaks = length(Rpos)
P_peaks = length(Ppos)
T_peaks = length(Tpos)
S_peaks = length(Spos)
Q_peaks = length(Qpos)
clc;
flag=0;
if(length(ELEVATED)>ceil(.8*length(Rpos)))
disp('T inverted (MI Detected) with T inverted Logic')
return;
else
flag=1;
end
R_peaks = length(Rpos)
P_peaks = length(Ppos)
T_peaks = length(Tpos)
S_peaks = length(Spos)
Q_peaks = length(Qpos)
R_peaks =
13
P_peaks =
13
T_peaks =
13
S_peaks =
13
Q_peaks =
13
R_peaks =
13
Generated by Foxit PDF Creator © Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.
Published with MATLAB® 7.14
P_peaks =
13
T_peaks =
13
S_peaks =
13
Q_peaks =
13
Generated by Foxit PDF Creator © Foxit Softwarehttp://www.foxitsoftware.com For evaluation only.