Documente Academic
Documente Profesional
Documente Cultură
10382535
load ecgpvcs.mat
y=length(ecg1);
d=[1:y]/200;
ecg1=ecg1/1000;
%%%%%%%%%%%%%plot
figure(1)
plot(d,ecg1)
xlabel 'seconds'
ylabel 'Volts'
title 'Time domain: original signal'
N = 2048;
X = abs(fft(ecg1,N));
X = fftshift(X);
F = [-N/2:N/2-1]/N*200;
figure(2)
plot(F,X)
xlabel 'frequency'
title 'Frequency domain: original signal'
Owen Humphreys
10382535
Owen Humphreys
10382535
%%%%%%%%%%%%%%%%
filter
% 50 Hz notch filter
wo = 50/(500/2); bw = wo/35;
[num,den] = iirnotch(wo,bw);
ecg1=filter(num,den,ecg1);
% 60 Hz notch filter
wo = 60/(500/2); bw = wo/35;
[num,den] = iirnotch(wo,bw);
ecg1=filter(num,den,ecg1);
% high pass filter
Fs=200;
Fn=Fs/2;
Ws=2/Fn;
Wp=0.1/Fn;
Rp=0.1;
Rs=40;
[n,Wn] = buttord(Wp,Ws,Rp,Rs);
[num,den]=butter(n,Wn,'high');
ecg1=filter(num,den,ecg1);
Owen Humphreys
10382535
Figure 3 unfiltered ecg signal in frequency domain shows a very large peak at around 0 Hz. There is no
peak at 50 or 60 Hz so the notch filters have no effect. After the low pass filter the large peak at around
0 Hz is attenuated. Figure 5 closer view of filtered ecg signal in time domainshows a closer view of the
signal in the time domain; the low frequency noise due to body movement has been removed and the
ECG signal has become straighter. The removal of the DC or very low frequency signal results in the
baseline at 0 V as expected, whereas it was at 2 V before filtering.
Owen Humphreys
10382535
%smooth signal
%find R peaks
[~,R]=findpeaks(ecg1,'MinPeakHeight',0.4);
%separate into R peaks into R Normal and R PVC
R_normal = R(ecg1(R)>0.4 & ecg1(R)<0.65);
R_pvc= R(ecg1(R)>0.65);
%find S Peaks
ecg1_inverted=-ecg1;
%invert ecg to find S peak
[~,S]=findpeaks(ecg1_inverted,'MinPeakHeight',0.3);
[Rmag, Smag] = deal(ecg1(R), ecg1(S));
%%%%%%%%%plot
figure
hold on
plot(ecg1)
xlabel 'seconds'
ylabel 'Volts'
title 'Time domain: peak detection'
plot(R_normal,ecg1(R_normal),'rv','MarkerFaceColor','r');
plot(R_pvc,ecg1(R_pvc),'rv','MarkerFaceColor','y');
plot(S,ecg1(S),'rv','MarkerFaceColor','g');
Owen Humphreys
10382535
Figure 8 Closer view of ECG with Normal R, PVC R, and S peaks isolated
This method can detect the normal R peak (red), PVC R peak (orange), normal S peak (green), and PVC S
peak (also green, they have not been separated). The distance between these peaks could then be
calculated, this would give more features to classify the PVC beats than the Pan Tompkin. Although the
signal needs to be much smoother because there are many false detections. Therefore the Pan Tompkin
code is used instead.
Owen Humphreys
10382535
%%%%%%%%%%Pan Tompkin
[qrs_amp_raw,qrs_i_raw,delay]=pan_tompkin(ecg1,200,1);
Owen Humphreys
10382535
The Pan Tompkin first applies a low pass filter to remove high frequency noise. It then applies a high
pass filter to remove low frequencies caused by body movement. It squares the magnitude to reduce
low amplitude signals and amplify the large amplitude signals. This creates just two peaks per beat
which makes it easier for peak detection. The signal is then averaged over 30 samples to create a single
peak for peak detection.
Owen Humphreys
10382535
The red line is the moving average of the amplitude of the peaks. A PVC R peak is identified if it is above
this red line. The Pan Tompkin also calculated the distance between the R peaks, this is used as the
second feature for classification.
The Pan Tompkin outputs the amplitude of the R wave, and the sample number it occurred at.
Comparing the Pan Tompkin output to the original ECG signal it is clear that the Pan Tompkin output is
shifted. The output points do not need to fit absolutely on the ECG, they just need to be correct relative
to each other. It is only the relative heights and distances between the points that is needed to identify
PVC beats.
Figure 13 Comparing original ECG signal to output of Pan Tompkin (green circles)
Owen Humphreys
10382535
Owen Humphreys
10382535
Feature 1
Feature 2
1.0000
0.9530
0.1644
0.9530
1.0000
0.0731
0.1644
0.0731
1.0000
The correlation function shows a very high correlation between the class and the height of the R peak.
This can also be seen with a scatter plot of the data.
There is a very clear separation between the two groups of data on the magnitude y axis. A hyperplane
could be drawn between the two sets of data at about 110 samples on the x axis (the distance between
the R peaks), but the data is much closer together and the correlation is not as good.
Good hyperplane
Owen Humphreys
10382535
Owen Humphreys
10382535
Feature
Train Data
Test Data: Feature1 R Magnitude
Test Data: Feature2 Distance between R peaks
Test Data: Feature1 & Feature2
Accuracy
100 %
97.16 %
68.79 %
96.45 %
Conclusion: The most accurate feature to classify the test data is the R peak magnitude alone.