Sie sind auf Seite 1von 7

M-file for Hw-2 Pan-Thomkins Algorithm.

clear all
load ECG3.txt
x = ECG3;
SAMPLE_NUM = length(x);
fs = 200;
%In order to make band pass filter we cascaded
% a lowpass and a high pass filter
% LOW PASS FILTER %
for n=1:length(x)
if n>12 % Low pass filter makes calculations with (n-12)
% So we must not make calculation up to 13
y(n)=2*y(n-1)-y(n-2)+(x(n)-2*x(n-6)+x(n-12))/32;
else
y(n)=x(n);
end
end
% HIGH PASS FILTER %
for n=1:length(y)
if n>32% High pass filter makes calculations with (n-32)
% So we must not make calculation up to 33
hip(n)=x(n-16)-(y(n-1)+x(n)-x(n-32))/32;
else
hip(n) = y(n);
end
end
% DIFFERENTIATOR %
for n=1:length(hip)
if n>4% Differentiator makes calculations with (n-4)
% So we must not make calculation up to 5
diff(n)=(1/8)*(2*hip(n)+hip(n-1)-hip(n-3)-2*hip(n-4));
else
diff(n)=hip(n);
end
end
% SQUARING This is done to avoid negative sign%
squ=diff.^2;% for the array diff every array member is squared and placed
%in the squ array
% INTEGRATOR %
N=30;%This is window width.30 is suitable for fs = 200 hertz.
for n=1:N
itg(n)=squ(n);
end

for n=N:length(squ)
itg(n)=0;
for a=0:N-1
itg(n)=itg(n)+squ(n-a);
end
end
%I chose a threshold high and a threshold low
%and colored re to be seen easily in the 4th plot
thres_low(1:length(itg))=0.8*mean(itg(50:4000));
thres_high(1:length(itg))=1.1*max(itg(50:4000));
figure(1)
subplot(4,1,1)
plot(hip)
subplot(4,1,2)
plot(diff)
subplot(4,1,3)
plot(squ)
axis([0 length(squ) 0 200000])
subplot(4,1,4)
plot(itg)
hold on
plot(thres_low,'r')
plot(thres_high,'r')
axis([0 length(itg) 0 max(itg(50:4000))*1.1])
%Here we make:
%We take the signal between the threshold levels,
%so between the threshold levels important.
%We keep the index numbers of the interception.
%The starting point is 50 because there is an unexcepted
%mistake at the graph,The beginning of the array after panthomkins
%calculations, there is an unexcepted region
c1 = 1;
for n = 50:4000
if itg(n) > thres_low(n)
if itg(n) < thres_high(n)
caps_1(c1)= n;
c1 = c1 + 1;
end
end
n=n+1;
end
%I counted the heard beat from continuing signal

%If there is un continuity we encounter a new heard beat


Temp1 = 0;
Temp2 = 0;
heard_beat = 0;
for z= 1:length(caps_1)-1
Temp1 = caps_1(z);
Temp2 = caps_1(z+1);
if Temp2 == Temp1+1 %Testing the index in the itg if
else
%if continuity no action
heard_beat = heard_beat+1;%if dis continuity new heard beat
end
end
heard_beat = heard_beat+1;%This is the heard beat at that signal
%We added 1 beat because the in caps array we must calculate the last beat
%So there is no new beat after the last beat, we must add the last beat
%after for loop.
beats_per_min= (fs*60*heard_beat)/SAMPLE_NUM;%beats per min calculation.
%Fs/Sample quantity is duration in time
%This value is multiplied by beat quantity and 60
%We used caps_2 to identify start and end points of continuity
caps_2(1) = caps_1(1);
z1 = 2;
for z= 1:length(caps_1)-1
Temp1 = caps_1(z);
Temp2 = caps_1(z+1);
if Temp2 == Temp1+1
else
caps_2(z1)=Temp1;
caps_2(z1+1)=Temp2;
z1 = z1 + 2;
end
end
caps_2(z1)=caps_1(length(caps_1));
%Caps_2 constructed...
% With the values in caps_2 we will test the itg array and find the picks,
% pick values, and take the indexes of pick values, so we can test it again,
% In RR_indexes array we calculated the index values of the picks in the
% ECG
%
m=1;
Temp1 = 0;
Temp2 = 0;

for k = 1:2:length(caps_2)
for l = caps_2(k):caps_2(k+1)-1
if Temp1 < itg(l)
Temp1 = itg(l);
Temp2 = l;
end
end
RR_indexes(m) = Temp2;
m = m+1;
Temp1 = 0;
Temp2 = 0;
end
%Here we calculated the distances with for loops
%RR_index_distances array is constructed for calculations of average and
%standard deviation values in ms.
k1=1;
for i = 1:length(RR_indexes)-1
RR_index_distances(k1) = RR_indexes(i+1)-RR_indexes(i);
k1=k1+1;
end
%We calculated for distances as indexes,So converting to time value(ms)
%We must calculate with 5.
Average_RR = mean(RR_index_distances)*5;% We multiplied with 5 ms
Standard_Deviation_RR = std(RR_index_distances)*5;%The values are for
%frequency,
%200hertz T = 5ms
%For QRS_width average we used caps_2 array and calculated
%the widths of the beats at the below threshold level.
%The tolerance is acceptable
for i = 1:2:length(caps_2)-1
caps_3(i) = caps_2(i+1)-caps(i);
end
Average_QRS=mean(caps_3)*5;%(Again time(ms) value)

Graphs:
ECG3.txt

ECG4:

ECG5:

ECG6:

Das könnte Ihnen auch gefallen