-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpspm_ecg_editor.m
1579 lines (1348 loc) · 49.9 KB
/
pspm_ecg_editor.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function varargout = pspm_ecg_editor(varargin)
% ● Description
% pspm_ecg_edtior allows manual correction of ecg data and creates a hb
% output. Function can be called seperately.
% ● Format
% [sts, R] = pspm_ecg_editor(pt)
% [sts, R] = pspm_ecg_editor(fn, channel, options)
% ● Arguments
% pt: A struct() from pspm_convert_ecg2hb detection.
% fn: A file to data file containing the ecg channel to be edited
% channel: Channel id of ecg channel in the data file
% ┌──options: A struct() of options
% ├─.channel: Channel id of the existing hb channel
% ├────.semi: Defines whether to navigate between potentially wrong hb events
% │ only (semi = 1), or between all hb events (semi = 0 => manual
% │ mode)
% ├.artefact: Epoch file with epochs of artefacts (to be ignored)
% └──.factor: To what factor should potentially wrong hb events
% deviate from the standard deviation. (Default: 1)
% variable r
% r(1,:) ... original r vector
% r(2,:) ... r vector containing potential faulty labeled qrs compl.
% r(3,:) ... removed
% r(4,:) ... added
% ● History
% Introduced in PsPM 3.1
% Written in 2013-2016 Philipp C Paulus, Tobias Moser
% (Dresden University of Technology, University of Zurich)
%% Initialise
global settings
if isempty(settings)
pspm_init;
end
sts = -1;
% Last Modified by GUIDE v2.5 31-Oct-2016 16:40:26
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @pspm_ecg_editor_OpeningFcn, ...
'gui_OutputFcn', @pspm_ecg_editor_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1}) && ...
(numel(regexp(varargin{1}, [filesep])) == 0)
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before pspm_ecg2hb_qc is made visible.
function pspm_ecg_editor_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to pspm_ecg2hb_qc (see VARARGIN)
pspm_ui(hObject, handles, 'ecg_editor');
% Choose default command line output for pspm_ecg2hb_qc
handles.output = hObject;
% -------------------------------------------------------------------------
% set default status for GUI
handles.edit_mode = '';
handles.gui_mode = ''; % file or inline
handles.artefact_mode = ''; % file or inline
handles.hb_chan = -1;
handles.data_chan = -1;
handles.write_chan = -1;
handles.options = struct();
handles.draw_selection = false;
handles.selection = struct('p', -1, 'sh', []);
handles.fn = '';
handles.action=[];
handles.k=1; % counter for the potential mislabeled qrs complexes
handles.s=[];
handles.s_h = [];
handles.e=0; % flag for the status of the ecg plot.
handles.plot.p = -1;
handles.sts=[]; % outputvariable
handles.R=[];
handles.jo=0; % default value for jump only - 0; plot data!
handles.artefact_fn = '';
handles.artefact_epochs = [];
handles.update_selection = true;
handles.plot.artefact_layer = [];
set(handles.togg_add,'Value',0);
set(handles.togg_remove,'Value',0);
% settings for manual mode
handles.manualmode=0; % default: deactivated
set(handles.cbManualMode, 'Value', handles.manualmode);
handles.winsize=4; % winsize for the manual mode
handles.zoom_factor = 1;
handles.data = {};
% plot settings
handles.plot.factr = 1;
handles.plot.limits.upper = 120;
handles.plot.limits.lower = 40;
handles.plot.ecg = [];
handles.plot.r = [];
handles.plot.sr = 1;
handles.plot.dynamic_R = [];
handles.plot.faulties = [];
% -------------------------------------------------------------------------
% set color values
handles.clr{1}=[.0627 .3059 .5451; 0.0863 0.4510 0.8157]; % blue for ecg plot
handles.clr{2}=[0 .75 1; 0.6 0.9020 1]; % skyblue for correct ones
handles.clr{3}=[1 .6471 0; 1.0000 0.8588 0.6000]; % dark yellow for possibly wrong ones
handles.clr{4}=[.5412 .1686 .8863; 0.8039 0.6471 0.9529]; % violet for deleted ones
handles.clr{5}=[0 .3922 0; 0 0.8 0]; % darkgreen for added ones
% -------------------------------------------------------------------------
set(handles.edtArtefactFile, 'Enable', 'off');
set(handles.edtArtefactFile, 'String', '');
set(handles.pbArtefactsDisable, 'Enable', 'off');
set(handles.rbShowArtefacts, 'Enable', 'off');
set(handles.rbDisableArtefactDetection, 'Enable', 'off');
set(handles.rbHideArtefactEvents, 'Enable', 'off');
set(handles.rbIncludeArtefactQRS, 'Enable', 'off');
set(handles.rbExcludeArtefactQRS, 'Enable', 'off');
% -------------------------------------------------------------------------
set(handles.lstEvents, 'Value', 1);
% -------------------------------------------------------------------------
guidata(hObject,handles);
% load settings
load_settings(hObject, handles, varargin{:});
handles = guidata(hObject);
% reload hb channel
reload_hb_chan(hObject, handles);
handles = guidata(hObject);
reload_plot(hObject, handles);
handles = guidata(hObject);
% -------------------------------------------------------------------------
if strcmpi(handles.gui_mode, 'file')
set(handles.pnlFileIO, 'visible', 'on');
else
set(handles.pnlFileIO, 'visible', 'off');
end
% -------------------------------------------------------------------------
% Update handles structure
guidata(hObject, handles);
% -------------------------------------------------------------------------
% set detection settings
set(handles.edtFactor, 'String', num2str(handles.plot.factr));
set(handles.edtUpperLimit, 'String', num2str(handles.plot.limits.upper));
set(handles.edtLowerLimit, 'String', num2str(handles.plot.limits.lower));
% -------------------------------------------------------------------------
% UIWAIT makes pspm_ecg2hb_qc wait for user response (see UIRESUME)
uiwait(handles.figure1);
% -------------------------------------------------------------------------
% --- Outputs from this function are returned to the command line.
function varargout = pspm_ecg_editor_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
handles=guidata(hObject);
if not(isempty(handles.sts))
varargout{1} = handles.sts;
else
varargout{1} = -1;
end
% -------------------------------------------------------------------------
if varargout{1} == -1
varargout{2} = [];
elseif not(isempty(handles.R))
if strcmpi(handles.gui_mode, 'inline')
varargout{2} = handles.R;
else
varargout{2} = handles.write_chan;
end
else
varargout{2} = [];
end
delete(hObject);
% -------------------------------------------------------------------------
% --- Executes on button press in togg_add.
function togg_add_Callback(hObject, eventdata, handles)
% hObject handle to togg_add (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of togg_add
pan off;
set(handles.togg_remove,'Value',0)
if strcmpi(handles.edit_mode, 'add_qrs')
exitModus;
else
handles.edit_mode = 'add_qrs';
set(handles.figure1,'Pointer','crosshair');
guidata(hObject, handles);
end
% --- Executes on button press in togg_remove.
function togg_remove_Callback(hObject, eventdata, handles)
% hObject handle to togg_remove (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of togg_remove
% -------------------------------------------------------------------------
pan off;
if strcmpi(handles.edit_mode, 'remove_qrs')
exitModus;
else
handles.edit_mode = 'remove_qrs';
set(handles.figure1,'Pointer','crosshair');
guidata(hObject, handles);
end
% -------------------------------------------------------------------------
% --- Executes on button press in push_cancel.
function push_cancel_Callback(hObject, eventdata, handles)
% hObject handle to push_cancel (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
exitModus;
handles.sts=-1;
handles.R=[];
% Update handles structure
guidata(hObject,handles);
% -------------------------------------------------------------------------
uiresume
% delete(hObject);
% --- Executes on button press in push_next.
function push_next_Callback(hObject, eventdata, handles)
% hObject handle to push_next (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% -------------------------------------------------------------------------
exitModus;
if ~handles.manualmode
handles.k=handles.k+1;
else % manual mode
handles.count=handles.count+(handles.winsize/2)*handles.zoom_factor;
end
check_navigation_buttons(hObject, handles);
handles.jo=1;
% call pp_plot
pp_plot(hObject,handles)
% --- Executes on button press in push_last.
function push_last_Callback(hObject, eventdata, handles)
% hObject handle to push_last (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% -------------------------------------------------------------------------
exitModus;
if ~handles.manualmode
handles.k=handles.k-1;
else % manual mode
handles.count=handles.count-(handles.winsize/2)*handles.zoom_factor;
end
check_navigation_buttons(hObject, handles);
handles.jo=1;
% call pp_plot
pp_plot(hObject,handles)
% --- Executes on button press in push_done.
function push_done_Callback(hObject, eventdata, handles)
% hObject handle to push_done (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles=guidata(hObject);
handles.edit_mode = '';
% -------------------------------------------------------------------------
r=handles.plot.r;
% get original R from reload_hb_chan
orig_R = handles.plot.R;
sr=handles.plot.sr;
% restore all events
r(1,orig_R) = 1;
% add mutatios
r(1,r(3,:)==1)=NaN; % deleted QRS markers
r(1,r(4,:)==1)=1; % added QRS markers
% remove artefact markers
if get(handles.rbExcludeArtefactQRS, 'Value') == 1 && any(handles.plot.artefact_layer)
r(1, handles.plot.artefact_layer) = NaN;
end
handles.R=[];
handles.R=find(r(1,:)==1);
handles.sts=1;
% write channel accordingly
if strcmpi(handles.gui_mode, 'file') && numel(handles.R) > 0
% assemble output settings
output_settings = get(handles.rbAddChan, 'Value') + ...
get(handles.rbReplaceHbChan, 'Value')*2;
% prepare outputs
out_d = struct();
out_d.data = handles.R/sr;
out_d.header = struct();
out_d.header.chantype = 'hb';
out_d.header.sr = 1;
out_d.header.units = 'events';
% transpose if necessary
if max(size(out_d.data,1)) ~= length(out_d.data)
out_d.data = transpose(out_d.data);
end
switch output_settings
case 1
w_action = 'add';
w_chan = 0;
case 2
w_action = 'replace';
w_chan = handles.hb_chan;
end
op = struct('channel', w_chan);
[nsts, infos] = pspm_write_channel(handles.fn, out_d, w_action, op);
if nsts ~= -1
handles.write_chan = infos.channel;
else
warning('ID:invalid_input', 'Could not write channel.');
handles.sts = nsts;
end
end
guidata(hObject,handles);
uiresume
% -------------------------------------------------------------------------
% delete(hObject);
% --- plots the current segment
function load_settings(hObject,handles, varargin)
% parse input
if numel(varargin) == 0 || ~isstruct(varargin{1})
handles.gui_mode = 'file';
if numel(varargin) > 2
handles.options = varargin{3};
else
handles.options = struct();
end
handles.options = pspm_options(handles.options, 'ecg_editor');
if isfield(handles.options, 'hb')
handles.hb_chan = handles.options.channel;
end
if isfield(handles.options, 'factor')
handles.plot.factr = handles.options.factor;
end
if isfield(handles.options, 'semi')
handles.manualmode = ~handles.options.semi;
end
if isfield(handles.options, 'artefact') && ~isempty(handles.options.artefact)
load_data_artefacts(hObject, handles, handles.options.artefact);
% update handles
handles = guidata(hObject);
end
if isfield(handles.options, 'limits')
if isfield(handles.options.limits, 'lower')
handles.plot.limits.lower = handles.options.limits.lower;
end
if isfield(handles.options.limits, 'upper')
handles.plot.limits.upper = handles.options.limits.upper;
end
end
if numel(varargin) >= 2
handles.data_chan = varargin{2};
end
if numel(varargin) >= 1
load_data_file(hObject, handles, varargin{1});
% update handles
handles = guidata(hObject);
end
else
handles.data = varargin{1};
handles.gui_mode = 'inline';
% IBIs larger than mean(IBI)+(factr*std(IBI)) will
% be marked for checking as well as IBIs smaller
% than mean(IBI)-(factor*std(IBI))
handles.plot.factr=handles.data.settings.outfact;
sr=handles.data.settings.filt.sr;
% -------------------------------------------------------------------------
% QRS complexes
ecg = transpose(handles.data.data.x(:,1));
handles.plot.sr = sr;
handles.plot.ecg = ecg;
end
% -------------------------------------------------------------------------
% output.
handles.count = 0;
set(handles.cbManualMode, 'Value', handles.manualmode);
% Update handles structure
guidata(hObject,handles);
% --- update hb channel
function reload_hb_chan(hObject, handles)
ecg = handles.plot.ecg;
sr = handles.plot.sr;
if isstruct(handles.data)
R = handles.data.set.R;
r = transpose(handles.data.data.r);
% set modification rows
r(3:4, :) = NaN;
handles.manualmode = 1 && get(handles.cbManualMode, 'Value');
else
if handles.hb_chan ~= -1
hb = handles.data{handles.hb_chan}.data;
set(handles.cbManualMode, 'Enable', 'on');
set(handles.rbReplaceHbChan, 'Enable', 'on');
handles.manualmode = 1 && get(handles.cbManualMode, 'Value');
else
set(handles.rbReplaceHbChan, 'Enable', 'off');
set(handles.cbManualMode, 'Value', 1);
set(handles.cbManualMode, 'Enable', 'off');
if get(handles.rbReplaceHbChan, 'Value')
set(handles.rbAddChan, 'Value', 1);
end
handles.manualmode = 1;
set(handles.cbManualMode, 'Value', 1);
hb = {};
end
r = zeros(4,numel(ecg));
if numel(hb) >= 1
R = transpose(round(hb*sr));
r(1,R) = 1;
handles.manualmode = get(handles.cbManualMode, 'Value');
else
R = [];
end
end
handles.plot.R = R;
handles.plot.r = r;
sr = handles.plot.sr;
y=1/sr:1/sr:length(r)/sr;
handles.plot.y = y;
guidata(hObject, handles);
% --- discriminate hb events
function discriminate_hb_events(hObject, handles)
up_lim = handles.plot.limits.upper;
lw_lim = handles.plot.limits.lower;
factr = handles.plot.factr;
R = handles.plot.R;
sr = handles.plot.sr;
r = handles.plot.r;
% create artefact layer
% ------------------------------------------------------------------------
a_lay = false(1, length(r));
for i=1:length(handles.artefact_epochs)
a_coord = handles.artefact_epochs(i, 1:end);
start = max(1, round(a_coord(1)*handles.plot.sr));
stop = min(length(a_lay), round(a_coord(2)*handles.plot.sr));
a_lay(start:stop) = 1;
end
% reset old detections
r(1,R) = 1;
r(2,R) = 0;
% complexes
ibi=diff(R); % duration of IBI intervalls
flag=zeros(size(ibi));% flag variable to identify potential mislabeled
flag(end+1) = 0;
ibi_filter = ~a_lay(R);
if get(handles.rbDisableArtefactDetection, 'Value') || ...
get(handles.rbHideArtefactEvents, 'Value')
ibi_f = ibi(ibi_filter(1:end-1));
else
ibi_f = ibi;
end
% -------------------------------------------------------------------------
% create vectors for potential mislabeled qrs complexes
flag(ibi>(mean(ibi_f)+(factr*std(ibi_f))))=1; % too short
flag(ibi<(mean(ibi_f)-(factr*std(ibi_f))))=1; % too long
flag(ibi/sr < 60/up_lim)=1; % get all ibis > 120 bpm
flag(ibi/sr > 60/lw_lim)=1; % get all ibis < 40 bpm
% transfer settings
r(2,R(flag==1))=1;
r(1,R(flag==1))=0;
% set default maxk
maxk=length(find(flag==1));
% reset according to artefact epochs
if ~isempty(handles.artefact_epochs) && numel(R) > 0
chans = [];
if get(handles.rbDisableArtefactDetection, 'Value')
chans = 2;
elseif get(handles.rbHideArtefactEvents, 'Value')
chans = [1,2];
end
% update maxk if necessary
if ~isempty(chans)
maxk = length(find(flag==1 & ibi_filter));
end
if ~isempty(chans) && any(a_lay)
r(chans, a_lay) = 0;
end
end
% if detection is disabled and detected as faulty then channel 1 and
% are empty but flag is set; in order to display the blue marker we have to
% reset the flags in the first row.
idx = r(2,R)==0 & flag==1;
if get(handles.rbDisableArtefactDetection, 'Value') && any(idx)
r(1,R(idx)) = 1;
end
% make zeros NaN
r(r==0)=NaN;
handles.plot.ibi = ibi;
handles.plot.r = r;
handles.plot.artefact_layer = a_lay;
if exist('handles.maxk', 'var') == 0 && exist('maxk', 'var')
handles.maxk = maxk;
end
% update dynamic R / make it global because its quite an expensive
% operation
handles.plot.dynamic_R = find(nansum(handles.plot.r));
handles.plot.faulties = find(handles.plot.r(2,:) == 1);
guidata(hObject, handles);
% --- plot data
function pp_plot(hObject,handles)
% where are potential mislabeled qrs complexes?
if any(not(isnan(handles.plot.r(2,:)))) && ~handles.manualmode
fl = handles.plot.faulties;
sample_id = fl(handles.k);
count=sample_id/handles.plot.sr;
else
count=handles.count;
custom_R = handles.plot.dynamic_R;
if count >= 0
% find sample_id
d = abs(custom_R - count*handles.plot.sr);
[~, idx] = min(d);
sample_id = custom_R(idx);
else
sample_id = 1;
end
end
% -------------------------------------------------------------------------
if handles.jo==0 % check only if changes were done.
% plot ecg signal
if handles.e==0
hold on;
handles.plot.p = plot(handles.plot.y,handles.plot.ecg,'color',handles.clr{1}(1,:));
ylim([min(handles.plot.ecg)*(1-.1), max(handles.plot.ecg)*(1+.1)]);
handles.e=1;
end
% -------------------------------------------------------------------------
if not(isempty(handles.s))
try
for i = 1:length(handles.s)
if handles.s(i) > 0
delete(handles.s(i));
end
end
handles.s = [];
catch
warning('Could not properly clean up stem markers.');
end
end
% ---------------------------------------------------------------------
if ~isempty(handles.selection.sh)
try
for i =1:length(handles.selection.sh)
delete(handles.selection.sh);
end
handles.selection.sh = [];
catch
end
end
% ---------------------------------------------------------------------
% plot normal stems
handles.s = plot_stems(handles, handles.s, [1 length(handles.plot.r)], 1);
handles.jo = 1;
end
% plot or update highlight stems
if ~isempty(handles.plot.R)
handles.selection.sh = plot_stems(handles, handles.selection.sh, [sample_id, sample_id], 2);
end
% -------------------------------------------------------------------------
if ~handles.manualmode
left_border = count-2*handles.zoom_factor;
right_border = count+2*handles.zoom_factor;
xlim([left_border right_border]);
if left_border<=0
left_border=1;
elseif right_border<=0
right_border=1;
end
y_lower_boud = min(handles.plot.ecg(round(left_border*handles.plot.sr):round(right_border*handles.plot.sr)));
y_upper_boud = max(handles.plot.ecg(round(left_border*handles.plot.sr):round(right_border*handles.plot.sr)));
ylim([y_lower_boud y_upper_boud]);
else
left_border = count-(handles.winsize/2)*handles.zoom_factor;
right_border = count+(handles.winsize/2)*handles.zoom_factor;
xlim([left_border right_border]);
if left_border<=0
left_border=1;
elseif right_border<=0
right_border=1;
end
y_lower_boud = min(handles.plot.ecg(round(left_border*handles.plot.sr):round(right_border*handles.plot.sr)));
y_upper_boud = max(handles.plot.ecg(round(left_border*handles.plot.sr):round(right_border*handles.plot.sr)));
ylim([y_lower_boud y_upper_boud]);
end
xlabel('time in seconds [s]')
% -------------------------------------------------------------------------
handles.count=count; % set current position.
% Update handles structure
guidata(hObject,handles);
update_selected(hObject, handles);
check_navigation_buttons(hObject, handles);
% --- Update selected
function update_selected(hObject, handles)
if handles.update_selection
lst_id = get(handles.lstEvents, 'Value');
if handles.manualmode
custom_R = handles.plot.dynamic_R;
if ~isempty(custom_R)
if lst_id > length(custom_R)
lst_id = 1;
end
lst_count = custom_R(lst_id);
lst_sample = lst_count;
sel_sample = handles.count;
if lst_sample ~= sel_sample
d = abs(custom_R-sel_sample*handles.plot.sr);
idx = find(d == min(d));
set(handles.lstEvents, 'Value', idx);
end
end
else
if lst_id ~= handles.k
set(handles.lstEvents, 'Value', handles.k);
end
end
end
% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.sts=-1;
handles.R=[];
% Update handles structure
guidata(hObject,handles);
% -------------------------------------------------------------------------
uiresume
% Hint: delete(hObject) closes the figure
% delete(hObject);
% --- Executes on key press with focus on figure1 and none of its controls.
function figure1_KeyPressFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.FIGURE)
% Key: name of the key that was pressed, in lower case
% Character: character interpretation of the key(s) that was pressed
% Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles structure with handles and user data (see GUIDATA)
if strcmpi(eventdata.Key, 'escape')
exitModus;
end
% -------------------------------------------------------------------------
function exitModus()
handles = guidata(gca);
set(handles.figure1, 'Pointer', 'Arrow');
handles.edit_mode = '';
guidata(gca, handles);
% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if strcmpi(handles.edit_mode,'remove_qrs')
handles.draw_selection = true;
pt = get(handles.axes, 'CurrentPoint');
handles.selection.start = pt(1,1:2);
guidata(hObject, handles);
end
% --- Executes on button press in zoomIn.
function zoomIn_Callback(hObject, eventdata, handles)
% hObject handle to zoomIn (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.zoom_factor = handles.zoom_factor / 2;
pp_plot(hObject, handles);
% --- Executes on button press in zoomOut.
function zoomOut_Callback(hObject, eventdata, handles)
% hObject handle to zoomOut (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.zoom_factor = handles.zoom_factor * 2;
pp_plot(hObject, handles);
function edtDataFile_Callback(hObject, eventdata, handles)
% hObject handle to edtDataFile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edtDataFile as text
% str2double(get(hObject,'String')) returns contents of edtDataFile as a double
set(hObject, 'String', handles.fn);
% --- Executes during object creation, after setting all properties.
function edtDataFile_CreateFcn(hObject, eventdata, handles)
% hObject handle to edtDataFile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pbChangeFile.
function pbChangeFile_Callback(hObject, eventdata, handles)
% hObject handle to pbChangeFile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[fname, fpath] = uigetfile({'*.mat'}, 'Select file with ECG data');
if ischar(fname) && ~isempty(fname)
fn = fullfile(fpath, fname);
%handles.hb_chan = -1;
load_data_file(hObject, handles, fn);
handles = guidata(hObject);
% reload hb channel
reload_hb_chan(hObject, handles);
handles = guidata(hObject);
reload_plot(hObject, handles);
end
% --- Executes on selection change in ppHbChan.
function ppHbChan_Callback(hObject, ~, handles)
% hObject handle to ppHbChan (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns ppHbChan contents as cell array
% contents{get(hObject,'Value')} returns selected item from ppHbChan
if ~isempty(handles.data)
contents = cellstr(get(hObject,'String'));
new_hb_chan = contents{get(hObject,'Value')};
if strcmpi(new_hb_chan, 'None')
handles.hb_chan = -1;
else
handles.hb_chan = str2double(new_hb_chan);
end
% reload hb channel
reload_hb_chan(hObject, handles);
handles = guidata(hObject);
reload_plot(hObject, handles);
end
% --- Executes on selection change in ppEcgChan.
function ppEcgChan_Callback(hObject, ~, handles)
% hObject handle to ppEcgChan (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns ppEcgChan contents as cell array
% contents{get(hObject,'Value')} returns selected item from ppEcgChan
if ~isempty(handles.data)
contents = cellstr(get(hObject,'String'));
new_ecg_chan = contents{get(hObject,'Value')};
handles.ecg_chan = str2double(new_ecg_chan);
% reload hb channel
reload_hb_chan(hObject, handles);
handles = guidata(hObject);
handles.e = 0;
if handles.plot.p ~= -1
delete(handles.plot.p);
handles.plot.p = -1;
end
reload_plot(hObject, handles);
end
% --- Executes during object creation, after setting all properties.
function ppEcgChan_CreateFcn(hObject, eventdata, handles)
% hObject handle to ppEcgChan (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Load Data file
function load_data_file(hObject, handles, fn)
[sts, ~, handles.data, ~] = pspm_load_data(fn);
if sts == 1
ecg_chans = find(cellfun(@(x) strcmpi(x.header.chantype, 'ecg'), handles.data));
% set possible ecg chans
sel_ecg_chan = find(ecg_chans == handles.data_chan, 1, 'first');
if isempty(sel_ecg_chan)
sel_ecg_chan = 1;
end
handles.data_chan = ecg_chans(sel_ecg_chan);
set(handles.ppEcgChan, 'String', ecg_chans);
set(handles.ppEcgChan, 'Value', sel_ecg_chan);
hb_chans = find(cellfun(@(x) strcmpi(x.header.chantype, 'hb'), handles.data));
hb_chan_list = cell(1,length(hb_chans)+1);
hb_chan_list{1} = 'None';
hb_chan_list(2:end) = num2cell(hb_chans);
if handles.hb_chan == -1
sel_hb_chan = -2;
set(handles.cbManualMode, 'Enable', 'off');
else
sel_hb_chan = find(cell2mat(hb_chan_list(2:end)) == handles.hb_chan,1) + 1;
if isempty(sel_hb_chan)
sel_hb_chan = -2;
end
end
if sel_hb_chan == -2
if length(hb_chans) == 1
sel_hb_chan = 2;
else
sel_hb_chan = 1;
end
end
set(handles.ppHbChan, 'String', hb_chan_list);
set(handles.ppHbChan, 'Value', sel_hb_chan);
handles.hb_chan = hb_chan_list{sel_hb_chan};
if strcmpi(handles.hb_chan, 'None')
handles.hb_chan = -1;
set(handles.rbReplaceHbChan, 'Enable', 'off');
end
handles.fn = fn;
set(handles.edtDataFile, 'String', fn);
% filter data
ecg = handles.data{handles.data_chan}.data;
sr = handles.data{handles.data_chan}.header.sr;
handles.plot.ecg = ecg;
handles.e = 0;
if handles.plot.p ~= -1
delete(handles.plot.p);
handles.plot.p = -1;
end
handles.plot.sr = sr;
guidata(hObject, handles);
end
% --- Reload plot settings
function reload_plot(hObject, handles)
if ~isempty(handles.data)
% get selected timestamp
if ~handles.manualmode
fl = handles.plot.faulties;
if ~isempty(fl)
count = fl(handles.k)/handles.plot.sr;
else
count = 1/handles.plot.sr;
end
else
count = handles.count;
end
sel_sample = count*handles.plot.sr;
% set values
discriminate_hb_events(hObject,handles);
handles=guidata(hObject);
% restore selected element
if ~handles.manualmode
fl = handles.plot.faulties;
[~, idx] = min(abs(fl-sel_sample));
handles.k = idx;
else
R = handles.plot.dynamic_R;
[~, idx] = min(abs(R-sel_sample));
handles.count = R(idx)/handles.plot.sr;
if isempty(handles.count)
handles.count = 0;
end
end
% update event list
update_event_list(hObject, handles);
% plot
handles.jo = 0;
pp_plot(hObject,handles);
handles=guidata(hObject);
% check nex_prev button
check_navigation_buttons(hObject, handles);
end
% --- Check navigation buttons
function check_navigation_buttons(hObject, handles)
% activate buttons accordingly
if handles.maxk==1
set(handles.push_next,'enable','off')
set(handles.push_last,'enable','off')
else
if ~handles.manualmode
next = handles.k + 1;
maximum = handles.maxk;
minimum = 1;
prev = handles.k - 1;
else
next = handles.count + (handles.winsize/2)*handles.zoom_factor;
maximum = length(handles.plot.r)/handles.plot.sr;
minimum = 0;
prev = handles.count - (handles.winsize/2)*handles.zoom_factor;
end
if next > maximum
set(handles.push_next,'enable','off');
else
set(handles.push_next, 'enable', 'on');
end
if prev < minimum
set(handles.push_last, 'enable', 'off');
else
set(handles.push_last, 'enable', 'on');
end
end
% --- Update event list
function update_event_list(hObject, handles)