-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqrscorrect.m
435 lines (376 loc) · 9.51 KB
/
qrscorrect.m
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
%qrscorrect() - Correct for false positive and negative QRS peaks
% and align peaks for maximum correlation.
%
% Usage:
% >>Peaks=qrscorrect(Peaks,Ecg,fs)
%
% Inputs:
% Peaks: Indeces of located QRS peaks
% Ecg: vector of ecg data
% sampling frequency.
%
% Ouput:
% Peaks: Corrected peaks
%
% Note: progress bar designed to work as part of fmrib_qrsdetect.
%
% Author: Rami Niazy, FMRIB Centre, University of Oxford.
%
%
%
% Copyright (c) 2004 Rami Niazy, FMRIB Centre, University of Oxford.
%
% Copyright (C) 2004 Rami K. Niazy, FMRIB Centre, University of Oxford
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
%
%
% Dec 23, 2004
% use binary prcorr2
% instead of corrcoef
% Dec 17, 2004
% use corrcoef instead of
% corr2
% Dec 15, 2004
% Median of all peaks used
% for correction instead of
% section median.
% Nov 4, 2004
% Changed order of correction
% Added additional alignment
% Nov 3, 2004
% Added check and
% adjustment of edge peaks clearance
% Oct 26, 2004
% Fixed bug for non-int fs case
function Peaks=qrscorrect(Peaks,Ecg,fs)
% init
%------
P=zeros(1,length(Ecg));
P(Peaks)=1;
dP_std=std(diff(Peaks));
dP_med=median(diff(Peaks));
pad=round(5*fs);
secL=round(10*fs);
sections=floor(length(P)/secL)+1;
if length(P((sections-1)*secL+1:end))<pad
sections=sections-1;
end
barth=2;
barth_step=barth;
if isempty(javachk('desktop'))
clearcom='clc';
else
clearcom='home';
end
% Correct for FP
%---------------
for s=1:sections
%wait bar
if s==1
barth=5;
barth_step=barth;
Flag25=0;
Flag50=0;
Flag75=0;
fprintf('\nStage 2 of 5: Correcting for False Positive detection.\n');
end
%-----------------calc------------------------------------------
f_flag=0;
if s==1
sec=P(1:secL+pad);
elseif s==sections
sec=P((s-1)*secL+1-pad:end);
else
sec=P((s-1)*secL+1-pad:s*secL+pad);
end
sec_P=find(sec==1);
sec_dP=diff(sec_P);
% dP_med=median(sec_dP);
false_p=1;
while ~isempty(false_p)
false_p=find(sec_dP<(dP_med-3*dP_std));%-3*dP_std
if ~isempty(false_p)
f_flag=1;
sec(sec_P(false_p(1)+1))=0;
sec_P=find(sec==1);
sec_dP=diff(sec_P);
false_p=find(sec_dP<(dP_med-3*dP_std));
end
end
if f_flag==1
if s==1
P(1:secL+pad)=sec;
elseif s==sections
P((s-1)*secL+1-pad:end)=sec;
else
P((s-1)*secL+1-pad:s*secL+pad)=sec;
end
end
%---------------------------------------------------------------
%update wait bar
percentdone=floor(s*100/sections);
if floor(percentdone)>=barth
if percentdone>=25 & Flag25==0
fprintf('25%% ')
Flag25=1;
elseif percentdone>=50 & Flag50==0
fprintf('50%% ')
Flag50=1;
elseif percentdone>=75 & Flag75==0
fprintf('75%% ')
Flag75=1;
elseif percentdone==100
fprintf('100%%\n')
else
fprintf('.')
end
while barth<=percentdone
barth=barth+barth_step;
end
if barth>100
barth=100;
end
end
end
%Check Edge Peaks
%----------------
Peaks=find(P==1);
pc1=2;
pc2=1;
while (pc1>1 | pc2>0)
dP=diff(Peaks);
hwinl=round(mean(dP)/2);
searchw=round(0.33*mean(dP));
pc1=1;
if length(Peaks)>5
for p=1:5
try
tmp=Ecg(Peaks(p)-hwinl-searchw:Peaks(p)+hwinl);
break;
catch
pc1=pc1+1;
end
end
end
if pc1>1
Peaks(1:pc1-1)=[];
end
pc2=0;
if length(Peaks)>5
for p=length(Peaks):-1:length(Peaks-4)
try
tmp=Ecg(Peaks(p)-hwinl:Peaks(p)+hwinl+searchw);
break;
catch
pc2=pc2+1;
end
end
end
if pc2>0
Peaks(end-pc2:end)=[];
end
end
%Finding corrected peaks
%-----------------------
searchwL=length(searchw);
peaksL=length(Peaks);
qrs=zeros(hwinl*2+1,peaksL);
qrs2=zeros(hwinl*2+1,peaksL);
barth=2;
% Find average Heart Beat for use in alignment
%---------------------------------------------
for p=1:peaksL
qrs(:,p)=Ecg(Peaks(p)-hwinl:Peaks(p)+hwinl);
end
m_qrs=mean(qrs,2);
% figure;plot(qrs)
% Align Peaks (1)
%----------------
for p=1:peaksL
%wait bar
if p==1
barth=5;
barth_step=barth;
Flag25=0;
Flag50=0;
Flag75=0;
fprintf('\nStage 3 of 5: Aligning QRS Peaks (1)\n');
end
%-----------------calc------------------------------------------
ppn=1;
for B=Peaks(p)-searchw:Peaks(p)+searchw
C(ppn)=prcorr2(Ecg(B-hwinl:B+hwinl),m_qrs);;
ppn=ppn+1;
end
[CV,CP]=max(C);
Beta=CP-(searchw+1);
Peaks(p)=Peaks(p)+Beta;
%---------------------------------------------------------------
%update wait bar
percentdone=floor(p*100/(peaksL));
if floor(percentdone)>=barth
if percentdone>=25 & Flag25==0
fprintf('25%% ')
Flag25=1;
elseif percentdone>=50 & Flag50==0
fprintf('50%% ')
Flag50=1;
elseif percentdone>=75 & Flag75==0
fprintf('75%% ')
Flag75=1;
elseif percentdone==100
fprintf('100%%\n')
else
fprintf('.')
end
while barth<=percentdone
barth=barth+barth_step;
end
if barth>100
barth=100;
end
end
end
barth=2;
P=zeros(1,length(Ecg));
P(Peaks)=1;
% Correct for FN
%---------------
for s=1:sections
%wait bar
if s==1
barth=5;
barth_step=barth;
Flag25=0;
Flag50=0;
Flag75=0;
fprintf('\nStage 4 of 5: Correcting for False Negative detection.\n');
end
%-----------------calc------------------------------------------
f_flag=0;
if s==1
sec=P(1:secL+pad);
elseif s==sections
sec=P((s-1)*secL+1-pad:end);
else
sec=P((s-1)*secL+1-pad:s*secL+pad);
end
sec_P=find(sec==1);
sec_dP=diff(sec_P);
dP_med=median(sec_dP);
false_n=1;
while ~isempty(false_n)
false_n=find(sec_dP>(1.5*dP_med));
if ~isempty(false_n)
f_flag=1;
sec(sec_P(false_n(1))+round(dP_med))=1;%sec_dP(false_n(1)-1)
sec_P=find(sec==1);
sec_dP=diff(sec_P);
false_n=find(sec_dP>(1.5*dP_med));
end
end
if f_flag==1
if s==1
P(1:secL+pad)=sec;
elseif s==sections
P((s-1)*secL+1-pad:end)=sec;
else
P((s-1)*secL+1-pad:s*secL+pad)=sec;
end
end
%---------------------------------------------------------------
%update wait bar
percentdone=floor(s*100/sections);
if floor(percentdone)>=barth
if percentdone>=25 & Flag25==0
fprintf('25%% ')
Flag25=1;
elseif percentdone>=50 & Flag50==0
fprintf('50%% ')
Flag50=1;
elseif percentdone>=75 & Flag75==0
fprintf('75%% ')
Flag75=1;
elseif percentdone==100
fprintf('100%%\n')
else
fprintf('.')
end
while barth<=percentdone
barth=barth+barth_step;
end
if barth>100
barth=100;
end
end
end
barth=2;
Peaks=find(P==1);
peaksL=length(Peaks);
barth=2;
% Align Peaks (2)
%----------------
for p=2:peaksL-1
%wait bar
if p==2
barth=5;
barth_step=barth;
Flag25=0;
Flag50=0;
Flag75=0;
fprintf('\nStage 5 of 5: Aligning QRS Peaks (2)\n');
end
%-----------------calc------------------------------------------
ppn=1;
for B=Peaks(p)-searchw:Peaks(p)+searchw
C(ppn)=prcorr2(Ecg(B-hwinl:B+hwinl),m_qrs);
ppn=ppn+1;
end
[CV,CP]=max(C);
Beta=CP-(searchw+1);
Peaks(p)=Peaks(p)+Beta;
%---------------------------------------------------------------
%update wait bar
percentdone=floor((p-1)*100/(peaksL-2));
if floor(percentdone)>=barth
if percentdone>=25 & Flag25==0
fprintf('25%% ')
Flag25=1;
elseif percentdone>=50 & Flag50==0
fprintf('50%% ')
Flag50=1;
elseif percentdone>=75 & Flag75==0
fprintf('75%% ')
Flag75=1;
elseif percentdone==100
fprintf('100%%\n')
else
fprintf('.')
end
while barth<=percentdone
barth=barth+barth_step;
end
if barth>100
barth=100;
end
end
end
% for p=2:peaksL-1
% qrs2(:,p-1)=Ecg(Peaks(p)-hwinl:Peaks(p)+hwinl);
% end
% figure;plot(qrs2);
return;