-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathecgViewer.m
2364 lines (2124 loc) · 92.5 KB
/
ecgViewer.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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Copyright (C) 2010, John T. Ramshur, [email protected]
%
% 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 3 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, see <http://www.gnu.org/licenses/>.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function ecgViewer()
% ecgViewer: Creates a GUI to preview ecg, filter ecg, detect beats, and
% filter ibi. It also alows the user to export ecg and ibi in several
% formats.
%
% Version: 1.2.2 - 10/21/10
%
% NOTES: 1. To take advantage of multi core/proc processing Matlab must run
% matlabpool command. When loading the GUI ask if you want to use
% multiple cores/processors.
% 2. dblclick on ecg plot to add annotion, right click or ctrl
% click to mark file as completed
% 3. Before using the database feature the first time you must
% create a datasourse in your Windows environment. See
% http://matlab.izmiran.ru/help/toolbox/database/instal12.html#18933.
% This is only done once.
% 4. dblclick on list of annotations to get details of that
% annotation.
% 5. Use the up and down keyboard arrow keys to move to
% the next(up) and previous (down) outlier. Use left and right to
% move one ECG window back and forward. Note...you must first
% click on a blank area in the ECG plot for these functions
% to work.
%% Initialize Variables
%global variables
% sldStep: increment that slider moves when clicked (samples)
% dx: number of ecg samples in plot window (samples)
% fileList: (todo)
% ecg1: unfilterd original ecg. I keep it in memory so i don't have
% to reload it if user decideds to not use ecg filters after already
% applying a filter.
% ecgf: filterd ecg
% path1: path to .mat files
% x: array containing x/time values of ecg (seconds)
% rate: ecg sample rate (samples/second)
% nx: number of total ecg samples (samples)
% indexR: index locations of beats (samples)
% ibi: 2 dim array of inter-beat intervals
% (seconds,samples)
% h: structure containing handles to all gui controls
% art: array containg a logical array of ibi outliers
% flagReady: boolean flag that lets other fxns know if at least one
% ecg file has been previewed.
% Ann: matrix of annotions retrieved when clicking on filename
%
%
%
global sldStep dx fileList ecg1 ecgf path1 x rate nx
global indexR ibi h art flagReady flagMultiCore flagUseDB Ann rxy2
dataSource= 'ann_db';%specifies the datasourse to use for annotations.
%Make sure this matches the datasource
%that is defined in Windows
%% Defaults for gui
def_dir=what; def_dir=def_dir.path;
def_win=10; %window size of plot 10% (%)
%ECG filter
def_ecgfilt=3; %1=none, 2=wavelet, 3=fastsmooth
def_wavelet=2; %1=db, 2=sym, 3=coif
def_wavelet2=3;
def_wavLevel='8';
def_wavRemove='1,2,8';
def_smoothLP='5';
def_smoothHP='500';
def_smoothLevel=2; %1=rectangular, 2=triangular, 3=pseudo-gaussian
def_replaceNAN=true; %true=replace nan w/ zero, false=do nothing
%Beat detection
def_template=fullfile(def_dir,'templates','sample_template.mat');
def_tempUT='0.45'; %upper thresh for template matching
def_tempLT='0.35'; %lower thresh for template matching
%IBI filters (0=off,1=on)
def_P=0; %percent filter flag
def_Pval='20'; %value (%)
def_SD=1; %sd filter flag
def_SDval='3'; %value (std)
def_M=0; %median filter flag
def_Mval1='4';
def_Mval2='5';
def_AT=0; %above thresh filter flag
def_ATval='0.24';% value (s)
def_BT=0; %below thresh filter flag
def_BTval='0.125';%value (s)
def_C=0; %custom filter flag
def_Cval1='3';
def_Cval2='160';
%% Create Main Figure
%set some sizes for creating the gui
pathH=0.05; pathB=0.95; %dir/path panel
toolsH=0.20; toolsB=0; %tools panel
filesH=1-toolsH; filesB=toolsH; %file list panel
ecgH=0.6; ecgB=filesB; %ecg plot panel
ibiH=1-ecgH-toolsH-pathH; ibiB=ecgH+toolsH; %ibi plot panel
%Main Figure
h=struct;
h.MainFigure = figure('Name','ECG Viewer', ...
'HandleVisibility','on', ...
'Position',[20 40 1100 675 ],...
'Toolbar','figure','Menubar','figure',...
'CloseRequestFcn',@closeGUI,'KeyPressFcn',@keyPress);
% Create extra File Menu Items
f = uimenu('Label','ECGViewer');
uimenu(f,'Label','Export Beat Locations','Callback',@menuExport_Callback);
uimenu(f,'Label','Plot Rxy','Callback',@menuPlotRxy_Callback);
%% Create Current Dir Conrtrols
h.panelDir = uipanel('Parent',h.MainFigure,...
'Units', 'normalized', ...
'backgroundcolor',[0.702 0.7216 0.8235], ...
'Position',[0.2 pathB 0.8 pathH]);
h.txtDir=uicontrol(h.panelDir,'Style','edit',...
'String',fullfile(def_dir,'sampleData'),...
'Units', 'normalized', ...
'Position',[.005 .15 .88 .7],...
'BackgroundColor','white',...
'HorizontalAlignment','left',...
'Callback', @txtDir_Callback);
h.btnBrowse=uicontrol(h.panelDir,'Style','pushbutton',...
'String','Select Dir...', ...
'Units', 'normalized', ...
'Position',[.895 .15 .1 .7], ...
'Callback', @btnOpen_Callback);
%% Create File List Controls
h.panelPreviewFiles = uipanel('Parent',h.MainFigure,...
'Units', 'normalized', ...
'Position',[0 filesB .2 filesH]);
%Label: Heading for ecg file panel
h.lblHeading=uicontrol(h.panelPreviewFiles,'Style','text',...
'String','ECG Information',...
'Units', 'normalized', ...
'Position',[.02 .97 .55 .03],...
'HorizontalAlignment','left');
%Label: ECG Sample Rate
h.lblRate=uicontrol(h.panelPreviewFiles,'Style','text',...
'String','Sample Rate (Hz):',...
'Units', 'normalized', ...
'Position',[.13 .91 .55 .03],...
'HorizontalAlignment','right');
%textBox: ECG Sample Rate
h.txtRate=uicontrol(h.panelPreviewFiles,'Style','edit',...
'String','1000',...
'Units', 'normalized', ...
'Position',[.705 .91 .27 .03],...
'BackgroundColor','white');
%Label: custom start time
h.lblCustomStart=uicontrol(h.panelPreviewFiles,'Style','text',...
'String','Start Time (hh:mm:ss):',...
'Units', 'normalized', ...
'Position',[.13 .864 .55 .03],...
'HorizontalAlignment','right');
%textBox: custom start time
h.txtCustomStart=uicontrol(h.panelPreviewFiles,'Style','edit',...
'String','00:00:00',...
'Units', 'normalized', ...
'Position',[.705 .864 .27 .03],...
'BackgroundColor','white');
%Label: custom segment length
h.lblCustomLen=uicontrol(h.panelPreviewFiles,'Style','text',...
'String','Length (hh:mm:ss):',...
'Units', 'normalized', ...
'Position',[.13 .83 .55 .03],...
'HorizontalAlignment','right');
%textBox: custom segment length
h.txtCustomLen=uicontrol(h.panelPreviewFiles,'Style','edit',...
'String','00:00:00',...
'Units', 'normalized', ...
'Position',[.705 .83 .27 .03],...
'BackgroundColor','white');
%Label: ECG file list heading
h.lblListHeading=uicontrol(h.panelPreviewFiles,'Style','text',...
'String','ECG Files:',...
'Units', 'normalized',...
'Position',[.02 .75 .3 .03],...
'HorizontalAlignment','left');
h.listFiletype = uicontrol(h.panelPreviewFiles,'Style','popupmenu',...
'String',{'MATLAB (*.mat)','Text (*.txt)','All (*.*)'}, ...
'Value', 1, ...
'BackgroundColor','white','fontsize',7,...
'Units', 'normalized', 'Position',[.27 .752 .713 .04],...
'Callback', @listFiletype_Callback);
%List: ECG files
h.listFiles = uicontrol(h.panelPreviewFiles,'Style','listbox',...
'Units', 'normalized', ...
'String','',...
'Position',[.02 .312 .96 .44],...
'BackgroundColor','white',...
'Callback', @lstFilesClk_Callback);
%Label: Annotation list heading
h.lblAnnHeading=uicontrol(h.panelPreviewFiles,'Style','text',...
'String','Annotations:',...
'Units', 'normalized', ...
'Position',[.02 .277 .55 .03],...
'HorizontalAlignment','left');
%List: Annotations
h.listAnn = uicontrol(h.panelPreviewFiles,'Style','listbox',...
'Units', 'normalized', ...
'String','',...
'Position',[.02 .08 .96 .2],...
'BackgroundColor','white', ...
'Callback', @lstAnnClk_Callback);
h.btnPreview=uicontrol(h.panelPreviewFiles,'Style','pushbutton',...
'String','Open ECG','fontweight','bold',...
'Units', 'normalized', ...
'Position',[.12 .01 .76 .06],...
'Callback', @btnPreview_Callback);
h.btnPrevFile=uicontrol(h.panelPreviewFiles,'Style','pushbutton',...
'String','<',...
'Units', 'normalized', ...
'Position',[.02 .01 .1 .06],...
'TooltipString','previous file',...
'Callback', @btnPrevFile_Callback);
h.btnNextFile=uicontrol(h.panelPreviewFiles,'Style','pushbutton',...
'String','>',...
'Units', 'normalized', ...
'Position',[.88 .01 .1 .06],...
'TooltipString','next file',...
'Callback', @btnNextFile_Callback);
%% Create ECG Plot Controls
%plot panel
h.panelPreviewECG = uipanel('Parent',h.MainFigure,...
'Units', 'normalized', ...
'Position',[.2 ecgB .8 ecgH]);
%axes handle
h.axesECG = axes('Parent', h.panelPreviewECG, ...
'HandleVisibility','callback', ...
'Units', 'normalized', 'fontsize',8, ...
'Position',[.06 0.27 0.85 0.65],...
'ButtonDownFcn',@clickEvent);
box(h.axesECG,'on') %create black box all around plot window
xlabel(h.axesECG,'Time (s)','fontsize',8);
ylabel(h.axesECG,'Amplitude','fontsize',8);
%---------------------------------------------
%container for controls
h.containerX = uipanel('Parent',h.panelPreviewECG,...
'Position',[.06 .02 .85 .15]);
%slider
h.slider = uicontrol(h.containerX,'Style','slider',...
'Max',100,'Min',1,'Value',1,...
'SliderStep',[def_win/100 0.2],...
'Units', 'normalized', ...
'Position',[.105 .6 .788 .3],...
'BackgroundColor','white',...
'Callback', @hSlider_Callback);
%Label: min
h.txtWinMin=uicontrol(h.containerX,'Style','edit',...
'String',min(x),'fontsize',8,...%min(x),...
'Value',1,...
'Units', 'normalized', ...
'Position',[.003 .6 .1 .3],...
'HorizontalAlignment','center');
%Label: max
h.txtWinMax=uicontrol(h.containerX,'Style','edit',...
'String',max(x),...%min(x)+dx,...
'Value',dx,...
'Units', 'normalized', ...
'Position',[.895 .6 .1 .3],...
'HorizontalAlignment','center');
%textBox: current position
h.txtWinCurrent=uicontrol(h.containerX,'Style','edit',...
'String',1,...%x(1),...
'Units', 'normalized', ...
'Position',[.793 .2 .1 .3],...
'BackgroundColor','white',...
'Callback',@txtWinCurrent_Callback);
%Lable: current position
h.lblWinCurrent=uicontrol(h.containerX,'Style','edit',...
'String','Current Position >> ',...
'Units', 'normalized', ...
'Position',[.591 .2 .2 .3],...
'Enable','inactive',...
'HorizontalAlignment','right');
%Label: window size
h.lblWinSize=uicontrol(h.containerX,'Style','edit',...
'String',' << Zoom (%)',...
'Units', 'normalized', ...
'Position',[.207 .2 .2 .3],...
'Enable','inactive',...
'HorizontalAlignment','left');
%Button: reduce window size
h.btnWinSizeDown=uicontrol(h.containerX,'Style','pushbutton',...
'String','-',...
'Units', 'normalized', ...
'Position',[.105 .2 .02 .3],...
'Callback',@btnWinSizeDown_Callback);
%Textbox: window size
h.txtWinSize=uicontrol(h.containerX,'Style','edit',...
'String','',...
'Units', 'normalized', ...
'Value', def_win,...
'String',def_win,...
'Visible','on',...
'Position',[.125 .2 .06 .3],...
'BackgroundColor','white',...
'Callback', @txtWinSize_Callback);
%Button: increase window size
h.btnWinSizeUp=uicontrol(h.containerX,'Style','pushbutton',...
'String','+',...
'Units', 'normalized', ...
'Position',[.185 .2 .02 .3],...
'Callback', @btnWinSizeUp_Callback);
%--------------------------------------------------
%Textbox: Lower y limit
h.txtYlimit1=uicontrol(h.panelPreviewECG,'Style','edit',...
'String',num2str(min(ecgf)),...
'Units', 'normalized','fontsize',8, ...
'Value', min(ecgf),...
'Visible','on',...
'Position',[.915 .27 .065 .034],...
'Enable','inactive',...
'BackgroundColor',[.95 .95 .95],...
'Callback', @txtYlimit1_Callback);
%Textbox: Upper y limit
h.txtYlimit2=uicontrol(h.panelPreviewECG,'Style','edit',...
'String',num2str(max(ecgf)),...
'Units', 'normalized','fontsize',8, ...
'Value', max(ecgf),...
'Enable','inactive',...
'Visible','on',...
'Position',[.915 .886 .065 .034],...
'BackgroundColor',[.95 .95 .95],...
'Callback',@txtYlimit2_Callback);
%chk: auto scale y limit
h.chkYlimitAuto=uicontrol(h.panelPreviewECG,'Style','checkbox',...
'String','Auto','fontsize',8,...
'value',1,...
'Units', 'normalized', ...
'Position',[.915 .578 .065 .044],...
'Callback', @chkYlimitAuto_Callback);
%% Create IBI Plot
%plot panel
h.panelPreviewIBI = uipanel('Parent',h.MainFigure,...
'Units', 'normalized', ...
'Position',[.2 ibiB .8 ibiH]);
%axes handle
h.axesIBI = axes('Parent', h.panelPreviewIBI, ...
'HandleVisibility','callback', ...
'Units', 'normalized','fontsize',6, ...
'Position',[.06 .15 .85 .8]);
box(h.axesIBI,'on') %create black box all around plot window
ylabel(h.axesIBI,'IBI (ms)','fontsize',8)
%% Create Panel to hold Tools
h.panelTools = uipanel('Parent',h.MainFigure,...
'Units', 'normalized', ...
'Position',[0 toolsB 1 toolsH]);
%% Create ECG Filter Controls
h.panelFilter = uipanel('Parent',h.panelTools, ...
'title','ECG Filtering',...
'Units', 'normalized', 'Position',[0 0 .2 1]);
h.lblFilter=uicontrol(h.panelFilter,'Style','text',...
'String','Method :',...
'Units', 'normalized', 'Position',[.05 .8 .25 .1],...
'HorizontalAlignment','right');
h.listFilter = uicontrol(h.panelFilter,'Style','popupmenu',...
'String',{'None','Wavelet','Fast Smooth'},'Value',def_ecgfilt, ...
'BackgroundColor','white',...
'Units', 'normalized', 'Position',[.35 .8 .5 .1],...
'Callback', @listFilter_Callback);
h.chkReplaceNAN = uicontrol(h.panelFilter,'Style','checkbox',...
'String','Replace NaN with zero.','Value',def_replaceNAN,...
'Units', 'normalized',...
'Position',[.1 .05 .7 .12], ...
'visible','on', 'Callback',@filtEcgChange_Callback);
%Wavelet Options
h.lblWaveletType=uicontrol(h.panelFilter,'Style','text',...
'String','Wavelet :', 'Units', 'normalized',...
'Position',[.04 .6 .3 .1],'Visible','off',...
'HorizontalAlignment','right');
h.listWaveletType = uicontrol(h.panelFilter,'Style','popupmenu',...
'String',{'db','sym','coif'},'Value',def_wavelet, ...
'BackgroundColor','white', 'Units', 'normalized',...
'Position',[.37 .6 .27 .1],'Visible','off',...
'Callback', @listWaveletType_Callback);
h.listWaveletType2 = uicontrol(h.panelFilter,'Style','popupmenu',...
'String',{'2','3','4','5','6','7','8','9','10'}, ...
'Value',def_wavelet2, 'BackgroundColor','white',...
'Units', 'normalized', ...
'Position',[.65 .6 .2 .1],'Visible','off',...
'Callback', @filtEcgChange_Callback);
h.lblWaveletLevel=uicontrol(h.panelFilter,'Style','text',...
'String','Level :', 'Units', 'normalized',...
'Position',[.37 .35 .3 .15],'Visible','off',...
'HorizontalAlignment','right');
h.txtWaveletLevel=uicontrol(h.panelFilter,'Style','edit',...
'String',def_wavLevel, 'Units', 'normalized',...
'Position',[.7 .35 .15 .15],'Visible','off',...
'HorizontalAlignment','center','BackgroundColor','white',...
'Callback', @filtEcgChange_Callback);
h.lblWaveletRemove=uicontrol(h.panelFilter,'Style','text', ...
'String','Remove Sub-band :', 'Units', 'normalized',...
'Position',[.06 .2 .6 .15],'Visible','off',...
'HorizontalAlignment','right');
h.txtWaveletRemove=uicontrol(h.panelFilter,'Style','edit',...
'String',def_wavRemove, 'Units', 'normalized',...
'Position',[.7 .2 .15 .15],'Visible','off',...
'HorizontalAlignment','center','BackgroundColor','white',...
'Callback', @filtEcgChange_Callback);
%Smoothing Options
h.lblSmoothLevel=uicontrol(h.panelFilter,'Style','text', ...
'String','Type/Lev :', 'Units', 'normalized',...
'Position',[.05 .6 .25 .1],'Visible','off',...
'HorizontalAlignment','right');
h.listSmoothLevel = uicontrol(h.panelFilter,'Style','popupmenu',...
'String',{'Rectangular (1x)', 'Triangular (2x)', ...
'Pseudo-Gaussian (3x)'},...
'Value',def_smoothLevel, ...
'BackgroundColor','white', 'Units', 'normalized', ...
'Position',[.35 .6 .5 .1],'Visible','off',...
'Callback', @filtEcgChange_Callback);
h.lblSmoothLP=uicontrol(h.panelFilter,'Style','text', ...
'String','LPF Span :', 'Units', 'normalized',...
'Position',[.15 .35 .4 .15],'Visible','off',...
'HorizontalAlignment','right');
h.txtSmoothLP=uicontrol(h.panelFilter,'Style','edit', ...
'String',def_smoothLP, 'Units', 'normalized', ...
'Position',[.58 .35 .27 .15],'Visible','off',...
'HorizontalAlignment','center','BackgroundColor','white',...
'Callback', @filtEcgChange_Callback);
h.lblSmoothHP=uicontrol(h.panelFilter,'Style','text', ...
'String','HPF Span :',...
'Units', 'normalized', 'Position',[.15 .2 .4 .15], ...
'Visible','off','HorizontalAlignment','right');
h.txtSmoothHP=uicontrol(h.panelFilter,'Style','edit',...
'String',def_smoothHP, 'Units', 'normalized',...
'Position',[.58 .2 .27 .15],'Visible','off',...
'HorizontalAlignment','center','BackgroundColor','white',...
'Callback', @filtEcgChange_Callback);
%% Create Beat Detection Controls
h.panelRwave = uipanel('Parent',h.panelTools,...
'title','Beat Detection',...
'Units', 'normalized', 'Position',[.2 0 .2 1]);
h.lblRwaveMethod=uicontrol(h.panelRwave,'Style','text', ...
'String','Method :',...
'Units', 'normalized', 'Position',[.05 .8 .25 .1],...
'HorizontalAlignment','right');
h.listRwaveMethod = uicontrol(h.panelRwave,'Style','popupmenu',...
'String',{'None','Template Matching','Self Template'}, ...
'Value',2,'BackgroundColor','white',...
'Units', 'normalized', 'Position',[.35 .8 .6 .1],...
'Callback', @listRwaveMethod_Callback);
h.txtRwaveTemplate=uicontrol(h.panelRwave,'Style','edit', ...
'String',def_template, 'Units', 'normalized',...
'Position',[.05 .53 .9 .15],'Visible','on',...
'BackgroundColor','white','Callback', @rwaveChange_Callback);
h.btnRwaveTemplate=uicontrol(h.panelRwave,'Style','pushbutton', ...
'String','Select Template', ...
'Units', 'normalized', 'Position',[.45 .35 .5 .17],...
'Visible','on', 'Callback', @btnRwaveTemplate_Callback);
h.lblRwaveUT=uicontrol(h.panelRwave,'Style','text', ...
'String','Upper Thresh:',...
'Units', 'normalized', 'Position',[.35 .2 .4 .12],'Visible','on');
h.txtRwaveUT=uicontrol(h.panelRwave,'Style','edit', ...
'String',def_tempUT,...
'Units', 'normalized','Position',[.8 .2 .15 .12],'Visible','on',...
'BackgroundColor','white',...
'Callback', @rwaveChange_Callback);
h.lblRwaveLT=uicontrol(h.panelRwave,'Style','text', ...
'String','Lower Thresh:',...
'Units', 'normalized', 'Position',[.35 .05 .4 .12],'Visible','on');
h.txtRwaveLT=uicontrol(h.panelRwave,'Style','edit', ...
'String',def_tempLT,...
'Units','normalized','Position',[.8 .05 .15 .12],'Visible','on',...
'BackgroundColor','white',...
'Callback', @rwaveChange_Callback);
%% Create IBI Filter Controls
h.panelOutliers = uipanel('Parent',h.panelTools, ...
'title','IBI Filtering / Outlier Detection',...
'Units', 'normalized', 'Position',[.4 0 .2 1]);
h.chkIbiFiltP = uicontrol(h.panelOutliers,'Style','checkbox',...
'String','percent','Value',def_P,...
'Units', 'normalized', 'Position',[.45 .8 .5 .12],...
'Callback',@filtIbiChange_Callback);
h.chkIbiFiltSD = uicontrol(h.panelOutliers,'Style','checkbox',...
'String','sd','Value',def_SD,...
'Units', 'normalized', 'Position',[.45 .7 .5 .12],...
'Callback',@filtIbiChange_Callback);
h.chkIbiFiltM = uicontrol(h.panelOutliers,'Style','checkbox',...
'String','median','Value',def_M,...
'Units', 'normalized', 'Position',[.45 .6 .5 .12],...
'Callback',@filtIbiChange_Callback);
h.chkIbiFiltAT = uicontrol(h.panelOutliers,'Style','checkbox',...
'String','above thresh','Value',def_AT,...
'Units', 'normalized', 'Position',[.45 .5 .5 .12],...
'Callback',@filtIbiChange_Callback);
h.chkIbiFiltBT = uicontrol(h.panelOutliers,'Style','checkbox',...
'String','below thresh','Value',def_BT,...
'Units', 'normalized', 'Position',[.45 .4 .5 .12],...
'Callback',@filtIbiChange_Callback);
h.chkIbiFiltC = uicontrol(h.panelOutliers,'Style','checkbox',...
'String','custom','Value',def_C,...
'Units', 'normalized', 'Position',[.45 .05 .5 .12],...
'Callback',@filtIbiChange_Callback);
align([h.chkIbiFiltP h.chkIbiFiltSD h.chkIbiFiltM h.chkIbiFiltAT ...
h.chkIbiFiltBT h.chkIbiFiltC],'Left','Distribute');
h.txtIbiFiltP = uicontrol(h.panelOutliers,'Style','edit',...
'String',def_Pval,'BackgroundColor','white',...
'Units', 'normalized', 'Position',[.25 .8 .15 .12],...
'Callback',@filtIbiChange_Callback);
h.txtIbiFiltSD = uicontrol(h.panelOutliers,'Style','edit',...
'String',def_SDval,'BackgroundColor','white',...
'Units', 'normalized', 'Position',[.25 .7 .15 .12],...
'Callback',@filtIbiChange_Callback);
h.txtIbiFiltM2 = uicontrol(h.panelOutliers,'Style','edit',...
'String',def_Mval2,'BackgroundColor','white',...
'Units', 'normalized', 'Position',[.25 .6 .15 .12],...
'Callback',@filtIbiChange_Callback);
h.txtIbiFiltAT = uicontrol(h.panelOutliers,'Style','edit',...
'String',def_ATval,'BackgroundColor','white',...
'Units', 'normalized', 'Position',[.25 .5 .15 .12],...
'Callback',@filtIbiChange_Callback);
h.txtIbiFiltBT = uicontrol(h.panelOutliers,'Style','edit',...
'String',def_BTval,'BackgroundColor','white',...
'Units', 'normalized', 'Position',[.25 .4 .15 .12],...
'Callback',@filtIbiChange_Callback);
h.txtIbiFiltC2 = uicontrol(h.panelOutliers,'Style','edit',...
'String',def_Cval2,'BackgroundColor','white',...
'Units', 'normalized', 'Position',[.25 .05 .15 .12],...
'Callback',@filtIbiChange_Callback);
align([h.txtIbiFiltP h.txtIbiFiltSD h.txtIbiFiltM2 h.txtIbiFiltAT ...
h.txtIbiFiltBT h.txtIbiFiltC2],'Left','Distribute');
p=get(h.txtIbiFiltM2,'position');
h.txtIbiFiltM1 = uicontrol(h.panelOutliers,'Style','edit',...
'String',def_Mval1,'BackgroundColor','white',...
'Units', 'normalized', 'Position',[.1 p(2) .15 p(4)],...
'Callback',@filtIbiChange_Callback);
p=get(h.txtIbiFiltC2,'position');
h.txtIbiFiltC1 = uicontrol(h.panelOutliers,'Style','edit',...
'String',def_Cval1,'BackgroundColor','white',...
'Units', 'normalized', 'Position',[.1 p(2) .15 p(4)],...
'Callback',@filtIbiChange_Callback);
%% Create Results
h.panelResults = uipanel('Parent',h.panelTools,'title','Summary', ...
'Units', 'normalized','Position',[.8 0 .2 1]);
h.axesResultsTbl = axes('parent', h.panelResults,...
'Position',[0 0 1 1],...
'YColor','white','YTickLabel',{},'ylim',[0 1],...
'XColor','white','XTickLabel',{},'xlim',[0 1]);
%create Table for Results and return handles of text objects
h.text.results = createResultsTbl();
%% Create Export Controls
h.panelExport = uipanel('Parent',h.panelTools,'title','Export', ...
'Units', 'normalized','Position',[.6 0 .2 1]);
h.btnExport=uicontrol(h.panelExport,'Style','pushbutton',...
'String','Export ECG','Units', 'normalized', ...
'Position',[.1 .7 .8 .2],'Callback', @btnExport_Callback);
h.btnExportIBI=uicontrol(h.panelExport,'Style','pushbutton',...
'String','Export IBI','Units', 'normalized', ...
'Position',[.1 .45 .8 .2],'Callback', @btnExportIBI_Callback);
h.chkSelectedOnly=uicontrol(h.panelExport,'Style','checkbox',...
'String','Export current window only', 'value',0,...
'Units', 'normalized', 'Position',[.1 .2 .8 .2]);
%% Create Progress Display
p=get(h.axesECG,'position');
width=.2; height=.1;
left=p(1)+p(3)/2-width/2;
bot=p(2)+p(4)/2-height/2;
h.lblProgress=uicontrol(h.panelPreviewECG,'Style','edit', ...
'String','<< Loading >>',...
'Units', 'normalized', 'Position', [left bot width height],...
'BackgroundColor','white', 'FontSize',12, ...
'ForegroundColor', 'red',...
'HorizontalAlignment','center', 'enable','off', 'visible','off');
%% Initialization tasks
showProgress('<< Initializing >>');
flagReady=false; %flag that disables user controls until ecg is loaded
%check if db toolbox is installed. If not then all db
%annotation features will be disabled
flagUseDB=license('test','Database_Toolbox');
%check for datasource
if flagUseDB
d = getdatasources; %get datasouces
flagUseDB=any(strcmp(d,dataSource)); %do any match
end
%load any saved data from last time
loadParameters();
%simulate filter list change to ensure options are displayed
listFilter_Callback;
%warn user if db toolbox is not installed
if ~flagUseDB
warndlg(['The MATLAB Database Toolbox and a valid datasource '...
'required for using annotions.',...
'All annotation functions will be disabled.']);
end
%populate file list
path1=get(h.txtDir,'string');
fType=getFiletype();
populateFiles(path1,fType);
%check for availability of using parallel computing for
%template matching
% if license('test','Distrib_Computing_Toolbox') %check license
% msgbox('Dist Comp Toolbox lic found.');
% if matlabpool('size')==0 %if not already started
% matlabpool; %start matlab pool w/ defaults
% end
% flagMultiCore=true;
% else
% flagMultiCore=false;
% end
showProgress('');
%% Callbacks
function menuExport_Callback(hObject, eventdata)
% Callback function for file menu "Export Beat Locations"
fName=get(get(h.axesECG,'title'),'string');
[p, name, extn] = fileparts(fullfile(path1,fName));
%get filename and path to export
[fName, fPath] = uiputfile( ...
{'*.xls','Excel (*.xls)';...
'*.txt','Text (*.txt)'},...
'Export As',...
fullfile(p,[name '.xls']));
%if user selected a filename and path
if ~isequal(fName,0) || ~isequal(fPath,0)
%update progress
showProgress('<< Exporting Locations >>');
[p, ~, extn] = fileparts(fName);
%prepare data
xs=indexR; %location samplen number
xt=x(indexR); % location unit time (s)
rr=rxy2(indexR)'; %corr coef
dat=[xs,xt,rr];
%Export ibi according to extension
switch extn
case '.txt' %Text
dlmwrite(fullfile(fPath,fName), dat, ...
'delimiter', ',' ,'precision', '%.4f')
case '.mat' %Matlab Binary
save(fullfile(fPath,fName),'dat','-v7')
case '.xls' %Excel
c1=num2cell(dat);
ch={'h1' 'h2' 'h3'};
tmp=[ch ;c1];
[status, message] = ...
xlswrite(fullfile(fPath,fName),tmp);
%make sure there was no error
if ~status
error(message.message)
end
otherwise
error('Choose a valid file extension.')
end
%Update progress
showProgress('')
end
end
function menuPlotRxy_Callback(hObject, eventdata)
% Callback function for file menu "Export Beat Locations"
figure;
plot(rxy2);
grid('on');
title('Correlation Coef. - Rxy')
xlabel('Sample Number')
end
function hSlider_Callback(hObject, eventdata)
% Callback function run when the slider is moved
set(h.slider,'value',ceil(get(h.slider,'value')))
updatePreview();
end
function txtWinCurrent_Callback(hObject, eventdata)
%Callback function run if txtCurrentWindow changes
%get desired pos from user
pos=floor(str2double(get(hObject,'string'))*rate);
if (pos>1 && pos<(nx-dx))
set(h.slider,'value',pos);
updatePreview();
end
end
function btnWinSizeUp_Callback(hObject, eventdata) %#ok<*INUSD>
% Callback function run when the window size button is pressed
s=str2double(get(h.txtWinSize,'String'));
if s < 99
set(h.txtWinSize,'String',num2str(s+1))
end
txtWinSize_Callback();
end
function btnWinSizeDown_Callback(hObject, eventdata)
s=str2double(get(h.txtWinSize,'String'));
if s > 1
set(h.txtWinSize,'String',num2str(s-1))
end
txtWinSize_Callback();
end
function txtWinSize_Callback(hObject, eventdata)
% dx is the width of the axis 'window'
sldStep = str2double(get(h.txtWinSize,'String'))/100;
dx = floor(sldStep*nx);
%change slider steps
set(h.slider,'SliderStep',[sldStep 0.1]);
updatePreview();
end
function txtYlimit1_Callback(hObject, eventdata)
% Callback function run when the lower ylimit
% textbox is changed
updatePreview();
end
function txtYlimit2_Callback(hObject, eventdata)
% Callback function run when the upper ylimit
% textbox is changed
updatePreview();
end
function chkYlimitAuto_Callback(hObject, eventdata)
% Callback function run when the auto ylimit
% btn is pressed
%autoscale plot if checkbox is checked
if get(h.chkYlimitAuto,'value')==1
%make y-axis limit textboxes look disabled
set(h.txtYlimit1,'Enable','inactive', ...
'BackgroundColor',[.95 .95 .95]);
set(h.txtYlimit2,'Enable','inactive', ...
'BackgroundColor',[.95 .95 .95]);
x1=get(h.txtWinMin,'Value');
x2=get(h.txtWinMax,'Value');
y1=min(ecgf(x1:x2)); %min y value in window
y2=max(ecgf(x1:x2)); %max y value in window
set(h.txtYlimit1,'String',num2str(y1));
set(h.txtYlimit2,'String',num2str(y2));
drawnow %force matlab to redraw it now
updatePreview();
else
%make y-axis limit textboxes look enabled
set(h.txtYlimit1,'Enable','on','BackgroundColor','white');
set(h.txtYlimit2,'Enable','on','BackgroundColor','white');
end
end
function txtDir_Callback(hObject, eventdata)
% Callback function run btnBrowse is pressed
path1=get(h.txtDir,'string');
if exist(path1,'dir')
fType=getFiletype();
populateFiles(path1,fType);
else
disp([path1 ' contains no .mat files.'])
end
end
function btnOpen_Callback(hObject, eventdata)
% Callback function run btnBrowse is pressed
%get directory path
if exist(get(h.txtDir,'string'),'dir')
path1=get(h.txtDir,'string');
else
path1=what;
path1=path1.path;
end
path1 = uigetdir(path1, ...
'Select directory containg subject files to export:');
if path1~=0
if exist(path1,'dir')
set(h.txtDir,'string',path1)
fType=getFiletype();
populateFiles(path1,fType);
else
disp([path1 ' not a valth directory.'])
end
end
end
function btnPrevFile_Callback(hObject, eventdata)
f=get(h.listFiles,'string');
i=get(h.listFiles,'value');
if (~isempty(f) && i>1)
set(h.listFiles,'value',i-1);
btnPreview_Callback();
end
end
function btnNextFile_Callback(hObject, eventdata)
f=get(h.listFiles,'string');
i=get(h.listFiles,'value');
m=length(fileList);
if (~isempty(f) && i<m)
set(h.listFiles,'value',i+1);
btnPreview_Callback();
end
end
function lstAnnClk_Callback(hObject, eventdata)
s=get(h.MainFigure,'SelectionType'); %type of mouse click
switch s
case 'open' % case double click
i=get(h.listAnn,'value');
if ~isempty(i)
str={['Annotation: ' Ann{i,6}], ...
['RelTime: ' num2str(Ann{i,5}) ' s'],...
['Notes: ' Ann{i,7}],...
['ID: ' num2str(Ann{i,1})],...
['CreatedDate: ' Ann{i,2}],...
['RecordDate: ' Ann{i,3}],...
['FileName: ' Ann{i,4}]};
msgbox(str,['Annotation: ' Ann{i,6}])
end
end
end
function lstFilesClk_Callback(hObject, eventdata)
s=get(h.MainFigure,'SelectionType'); %type of mouse click
if strcmp(s,'open') % case double click
btnPreview_Callback;
end
if ~flagUseDB; return; end;%exit if db toolbox doesn't exist exit
f=get(h.listFiles,'string');
i=get(h.listFiles,'value');
f=removeHTML(f{i});
% DATABASE Stuff
conn = database(dataSource,'',''); %db connection
cursorA = exec(conn, ... %query db
['SELECT ALL ID,CreatedDate,RecordDate,FileName,RelTime,'...
'Annotation,Notes FROM tblAnn WHERE FileName = ''' f '''']);
cursorA = fetch(cursorA); %fecth records
Ann=cursorA.data; %only need data
close(conn);
if ~(size(Ann,2)<=1) && ~isempty(Ann)
nAnn=size(Ann,1);
str=cell(nAnn,1);
for i=1:nAnn
tmp=sprintf('%0.1f s',str2double(Ann{i,5}));
str{i}= [Ann{i,6} ' - ' tmp];
end
%setting value=1 prevents Matlab warning/error
set(h.listAnn,'value',1)
set(h.listAnn,'string', str)
else
set(h.listAnn,'string', '')
Ann=[];
end
end
function btnPreview_Callback(hObject, eventdata)
% Callback function run when the Preview btn is pressed
if ~isempty(get(h.listFiles,'string'))%make sure a file is selected
% try
%Update progress
showProgress('<< Loading >>');
%check user inputs
if (checkFiltInputs()<0) && (checkRwaveInputs()<0) && ...
(checkIbiFiltInputs()<0)
showProgress('');
return;
end
%check sample rate
rate=str2double(get(h.txtRate,'string'));
if isnan(rate) || rate<=0
error('invalid input for sample rate')
return;
end
%name of input file
fn=get(h.listFiles,'string');
fn=fn{get(h.listFiles,'value')};
%remove any html code
fn=removeHTML(fn);
%read ecg file
[~,~,ext]=fileparts(fn); %get file extension
switch lower(ext)
case '.mat'
%This load method assumes that ecg data is contained
%within a field named "ecg" or "ECG". Otherwise the 1st
%field is used.
data = load(fullfile(path1,fn)); %load data file
n=fieldnames(data); %get fields/var of .mat file data
if isfield(data,'ecg')
data=data.ecg;
elseif isfield(data,'ECG')
data=data.ecg;
else
data=data.(n{1}); %get data from 1st field/var
end
case {'.txt'}
%read text file
data=load(fullfile(path1,fn),'-ascii');
otherwise
warning('Selected file is not a valid filetype.')
return;
end
%check data dimensions
dim=size(data);
if dim(1) < dim(2); data=data'; end
%assign time values to x
if size(data,2)<2
x=(0:size(data,1))./rate; %generate
else
x=data(:,1);
rate=floor(1/(x(2)-x(1))); %compute sample rate from data
set(h.txtRate,'string',num2str(rate)); %display rate on gui
end
%assign amplitude value to ecg1
ecg1=data(:,2);
clear data; %clear memory
%determine what part of ecg file to use
start=get(h.txtCustomStart,'string'); %get starting point
len=get(h.txtCustomLen,'string'); %get length
if ~isempty(start) && ~isempty(len) ...
&& ~(strcmp('00:00:00',len))
%if: make sure in correct format
if isempty(strfind(start,':'))
error('Incorrect custom "Start Time" format.')
return
else
%add (hours*3600) + (minutes*60) + seconds
nSeconds = str2double(datestr(start,'HH'))*3600 ...
+ str2double(datestr(start,'MM'))*60 ...
+ str2double(datestr(start,'SS'));
x1=nSeconds*rate+1;
end
%if: make sure correct format was used
if isempty(strfind(len,':'))
error('Incorrect syntax for custom "length".')
return
else
%add (hours*3600) + (minutes*60) + seconds
nSeconds = str2double(datestr(len,'HH'))*3600 ...