-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQRSClassify2.asv
161 lines (148 loc) · 5.64 KB
/
QRSClassify2.asv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
function [classifications] = QRSClassify2(record, beats, Fs)
signalFileName = sprintf("%sm.mat", record);
S = load(signalFileName);
sig = S.val(1,:);
Fc = 2;
fsig = HPFilter(sig, Fc, 1/Fs);
% [averageBeat, threshold] = getAverageBeat(fsig, beats, Fs);
%[averageBeat, thresholds] = getAverageBeatMV(fsig, beats, Fs);
if isnan(averageBeat)
classifications = NaN;
return
end
limLower = floor(Fs*0.06);
limUpper = round(Fs*0.1);
fpPoints = beats(:,1);
classifications = [];
for i = 1:length(fpPoints)
currFp = fpPoints(i);
if currFp+limUpper <= length(fsig)
currBeat = fsig(currFp-limLower:currFp+limUpper);
%currLabel = classifyBeat(currBeat, averageBeat, threshold);
[currLabel, threshold] = classifyBeatAdaptiveT(currBeat, averageBeat, threshold);
%currLabel = classifyBeatMV(currBeat, averageBeat, thresholds);
[currLabel, thresholds] = classifyBeatMVAdaptiveT(currBeat, averageBeat, thresholds);
classifications = [classifications, currLabel];
else
currBeat = fsig(currFp-limLower:end);
tempAverageBeat = averageBeat(1:length(currBeat));
%currLabel = classifyBeat(currBeat, tempAverageBeat, threshold);
[currLabel, threshold] = classifyBeatAdaptiveT(currBeat, tempAverageBeat, threshold);
%currLabel = classifyBeatMV(currBeat, tempAverageBeat, thresholds);
[currLabel, thresholds] = classifyBeatMVAdaptiveT(currBeat, tempAverageBeat, thresholds);
classifications = [classifications, currLabel];
end
end
end
function [averageBeat, threshold] = getAverageBeat(sig, beats, Fs)
maxSample = Fs*300;
fpPointsAll = beats(:,1);
fpPointsAll = fpPointsAll(beats(:,2)==0);
fpPoints = fpPointsAll(fpPointsAll<=maxSample);
if isempty(fpPoints) % couldn't learn a representation of normal beat
averageBeat = NaN;
threshold = NaN;
return
end
averageBeat = zeros(1,round(Fs*0.16));
limLower = floor(Fs*0.06);
limUpper = round(Fs*0.1);
for i=1:length(fpPoints)
currFp = fpPoints(i);
currBeat = sig(currFp-limLower:currFp+limUpper);
averageBeat = averageBeat + currBeat;
end
averageBeat = averageBeat ./ length(fpPoints);
threshold = 0;
N = length(averageBeat);
for i=1:length(fpPoints)
currFp = fpPoints(i);
currBeat = sig(currFp-limLower:currFp+limUpper);
% currDist = (1/N) * sum(abs(currBeat-averageBeat)); %d1
% currDist = sqrt((1/N)*sum(abs(currBeat-averageBeat)).^2); %d2
currDist = max(abs(currBeat-averageBeat)); %dInf
threshold = threshold + currDist;
end
threshold = threshold / length(fpPoints);
% * 2.6 for d1
% * 2.5 for d2
% * 2.2 for dInf
threshold = threshold * 2.2;
end
function [class] = classifyBeat(currBeat, averageBeat, threshold)
N = length(averageBeat);
dist = (1/N) * sum(abs(currBeat-averageBeat)); %d1
% dist = sqrt((1/N)*sum(abs(currBeat-averageBeat)).^2); %d2
% dist = max(abs(currBeat-averageBeat)); %dInf
if dist > threshold
class = 1; % V
else
class = 0; % N
end
end
function [class, newThreshold] = classifyBeatAdaptiveT(currBeat, averageBeat, threshold)
N = length(averageBeat);
% dist = (1/N) * sum(abs(currBeat-averageBeat)); %d1
% dist = sqrt((1/N)*sum(abs(currBeat-averageBeat)).^2); %d2
dist = max(abs(currBeat-averageBeat)); %dInf
alpha = 0.0005;
if dist > threshold
class = 1; % V
newThreshold = threshold;
else
class = 0; % N
newThreshold = alpha*2.2*dist + (1-alpha)*threshold;
end
end
function [averageBeat, thresholds] = getAverageBeatMV(sig, beats, Fs)
maxSample = Fs*300;
fpPointsAll = beats(:,1);
fpPointsAll = fpPointsAll(beats(:,2)==0);
fpPoints = fpPointsAll(fpPointsAll<=maxSample);
if isempty(fpPoints) % couldn't learn a representation of normal beat
averageBeat = NaN;
thresholds = NaN;
return
end
averageBeat = zeros(1,round(Fs*0.16));
limLower = floor(Fs*0.06);
limUpper = round(Fs*0.1);
for i=1:length(fpPoints)
currFp = fpPoints(i);
currBeat = sig(currFp-limLower:currFp+limUpper);
averageBeat = averageBeat + currBeat;
end
averageBeat = averageBeat ./ length(fpPoints);
thresholds = [0,0,0];
N = length(averageBeat);
for i=1:length(fpPoints)
currFp = fpPoints(i);
currBeat = sig(currFp-limLower:currFp+limUpper);
currDist1 = (1/N) * sum(abs(currBeat-averageBeat)); %d1
currDist2 = sqrt((1/N)*sum(abs(currBeat-averageBeat)).^2); %d2
currDist3 = max(abs(currBeat-averageBeat)); %dInf
thresholds = thresholds + [currDist1, currDist2, currDist3];
end
thresholds = thresholds / length(fpPoints);
% * 2.6 for d1
% * 2.5 for d2
% * 2.2 for dInf
thresholds = thresholds .* [2.6, 2.5, 2.2];
end
function [class, newThresholds] = classifyBeatMVAdaptiveT(currBeat, averageBeat, thresholds)
N = length(averageBeat);
dist1 = (1/N) * sum(abs(currBeat-averageBeat)); %d1
dist2 = sqrt((1/N)*sum(abs(currBeat-averageBeat)).^2); %d2
dist3 = max(abs(currBeat-averageBeat)); %dInf
distances = [dist1, dist2, dist3];
voteResult = sum(distances>thresholds);
alpha1 = 0.005; alpha2=0.0005;
if voteResult >= 2
class = 1;
newThresholds = thresholds;
else
class = 0;
newThresholds = [alpha1*2.6*dist1+(1-alpha1)*thresholds(1),...
alpha1*2.5*dist1+(1-alpha1)*thresholds(2), alpha2*2.2*dist1+(1-alpha2)*thresholds(3)];
end
end