-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMapHDsEMG.m
2901 lines (2260 loc) · 107 KB
/
MapHDsEMG.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 = MapHDsEMG(varargin)
% MAPHDSEMG MATLAB code for MapHDsEMG.fig
% MAPHDSEMG, by itself, creates a new MAPHDSEMG or raises the existing
% singleton*.
%
% H = MAPHDSEMG returns the handle to a new MAPHDSEMG or the handle to
% the existing singleton*.
%
% MAPHDSEMG('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MAPHDSEMG.M with the given input arguments.
%
% MAPHDSEMG('Property','Value',...) creates a new MAPHDSEMG or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before MapHDsEMG_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to MapHDsEMG_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help MapHDsEMG
% Last Modified by GUIDE v2.5 15-Aug-2013 12:24:40
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @MapHDsEMG_OpeningFcn, ...
'gui_OutputFcn', @MapHDsEMG_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
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 MapHDsEMG is made visible.
function MapHDsEMG_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 MapHDsEMG (see VARARGIN)
if ~isempty (varargin)
handles.data = varargin{1};
end
handles.signalvalue = 0;
handles.mepmeanvalue = 0;
handles.mepsvalue = 0;
handles.linevalue = 0;
handles.triggervalue = 0;
handles.mepminmaxvalue = 0;
handles.ampthresholdvalue = 0;
handles.latencyvalue = 0;
% Data to export
handles.data.amp_pp_sim_diff = 0;
handles.data.amp_rms_sim_diff = 0;
handles.data.latency_sim = 0;
handles.data.fmed_sim_diff = 0;
handles.data.cog_cluster_diff_pp = [0 0];
handles.data.size_cluster_diff_pp = 0;
handles.data.cluster_amp_pp_diff = 0;
handles.data.freq_cluster_pp = 0;
handles.data.cog_cluster_diff_rms = [0 0];
handles.data.size_cluster_diff_rms = 0;
handles.data.cluster_amp_rms_diff = 0;
handles.data.freq_cluster_rms = 0;
handles.data.cvelocity = 0;
handles.data.correl_coef = 0;
handles.data.iz_row = 0;
handles.data.mean_total_rms = 0;
handles.data.mean_offset_rms = 0;
handles.data.latency_avarege = 0;
handles.data.outliers_mep = {};
handles.data.outliers_trig = {};
handles.data.channel = 1;
for i = 1:size(handles.data.signal.emg_map{handles.data.channel},2)
handles.data.outliers_trig{i} = [];
handles.data.outliers_mep{i} = [];
end
channels_names = '';
for i = 1:length(handles.data.configuration.emg_mode)
channels_names = strcat(channels_names,num2str(i),'-',handles.data.configuration.emg_mode(i),'__');
end
% figure name
name = get(handles.figure1, 'Name');
fig_name = [name ' ' handles.data.configuration.patient_id '_' handles.data.configuration.emg_side];
set(handles.figure1,'Name',fig_name)
set(handles.text_ChannelLegend, 'String',channels_names)
set(handles.text_Channel, 'String',num2str(handles.data.channel))
set(handles.edit_id_number, 'String',num2str(handles.data.configuration.patient_id))
set(handles.edit_hemisphere_side, 'String',num2str(handles.data.configuration.emg_side))
set(handles.edit_number_angle_stim, 'String',num2str(handles.data.configuration.angle_stim))
% Plot signal on each axes
for i = 1:length(handles.data.signal.emg_map)
axes(eval(strcat('handles.axes',num2str(i))))
hold on
for j = 1:size(handles.data.signal.emg_map{handles.data.channel},2)
handles.data.xs{j}{i} = (1:length(handles.data.signal.emg_map{i}(:,j)))/handles.data.configuration.fsample;
handles.hsignal{j}(i) = plot(handles.data.xs{j}{i},handles.data.signal.emg_map{i}(:,j),'Visible','off');
end
set(eval(strcat('handles.axes',num2str(i))),'HitTest','on')
update_waitbar(handles,i/length(handles.data.signal.emg_map))
end
set(handles.hsignal{handles.data.channel},'Visible','on')
set(handles.checkbox_ViewSignal,'Value',1)
% Generate latency cell and initial total amplitude
for i = 1:size(handles.data.signal.emg_map{handles.data.channel},2)
for j = 1:length(handles.data.signal.emg_map)
handles.data.latency{i}{j} = [];
handles.data.duration{i}{j} = [];
handles.hlatencystart{i}{j} = [];
handles.hlatencystop{i}{j} = [];
end
end
% Choose default command line output for MapHDsEMG
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes MapHDsEMG wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = MapHDsEMG_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
varargout{1} = handles.output;
% --- Executes on button press in button_Trigger.
function button_Trigger_Callback(hObject, eventdata, handles)
% hObject handle to button_Trigger (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.checkbox_ViewSignal,'Value',0)
checkbox_ViewSignal_Callback(hObject, eventdata, handles)
for j = 1:size(handles.data.signal.emg_map{handles.data.channel},2)
if isfield(handles,'hsignal')
if ishandle(handles.hsignal{j})
delete(handles.hsignal{j})
end
end
for i = 1:length(handles.data.signal.emg_map)
if isfield(handles,'htrigger')
if ishandle(handles.htrigger{j, i})
delete(handles.htrigger{j, i})
end
end
if isfield(handles,'hline') == 1
if ishandle(handles.hline{j, i})
delete(handles.hline{j, i})
end
end
end
end
clear handles.data.slope
handles.data.slope = get(handles.edit_Threshold,'String');
handles.data.slope = str2double(handles.data.slope);
hp_filter = str2double(get(handles.edit_HPF,'String'));
lp_filter = str2double(get(handles.edit_LPF,'String'));
[b,a] = butter(2,[hp_filter lp_filter]*2/handles.data.configuration.fsample);
% ad_range - of the emg equipment
% conv_uv - multiplication factor to see signal in microVolts
% TODO: put a text box in the UI for the user to change the conversion
% factor
ad_range = 5.0;
conv_uv = 1000000.0;
handles.data.signal.emg_data = filtfilt(b,a,(handles.data.signal.emg_data*ad_range*conv_uv)/(handles.data.configuration.signal_gain*2^(handles.data.configuration.ad_bits)));
handles.data.signal.emg_data_mono = [nan(length(handles.data.signal.emg_data), 1) handles.data.signal.emg_data(:, 1:11)...
nan(length(handles.data.signal.emg_data), 1) handles.data.signal.emg_data(:, 12:50) nan(length(handles.data.signal.emg_data), 1)...
handles.data.signal.emg_data(:, 51:end) nan(length(handles.data.signal.emg_data), 1)];
handles.data.signal.emg_data_diff = diff(handles.data.signal.emg_data,1,2);
handles.data.signal.emg_data_diff = [nan(length(handles.data.signal.emg_data_diff), 1) handles.data.signal.emg_data_diff(:, 1:10)...
nan(length(handles.data.signal.emg_data_diff), 1) nan(length(handles.data.signal.emg_data_diff), 1) handles.data.signal.emg_data_diff(:, 12:23)...
nan(length(handles.data.signal.emg_data_diff), 1) handles.data.signal.emg_data_diff(:, 25:36)...
nan(length(handles.data.signal.emg_data_diff), 1) handles.data.signal.emg_data_diff(:, 38:49)...
nan(length(handles.data.signal.emg_data_diff), 1) nan(length(handles.data.signal.emg_data_diff), 1)...
handles.data.signal.emg_data_diff(:, 51:end) nan(length(handles.data.signal.emg_data_diff), 1) nan(length(handles.data.signal.emg_data_diff), 1)];
handles.data.signal.emg_data_all = {handles.data.signal.emg_data_mono handles.data.signal.emg_data_diff};
% trigger computation based no HDsEMG aux channel with trigger signal
if handles.data.slope == 0
trigger = handles.data.signal.raw_data(:,65)*5/(2^(handles.data.configuration.ad_bits-1));% Triggering EMGs
% finding samples denoting trigger onset
samples_triggeron = find(trigger>4);
samples_triggeron = (samples_triggeron(diff([-inf;samples_triggeron])>1));
% rewritting the emg_map and plotting its new signal beacuse of the
% butterworth applied just before
% TODO: This trick is temporary just to use with emg_diff map,
% fix it to the general case.
for i = 1:length(handles.data.signal.emg_map)
if size(handles.data.signal.emg_map{1}, 2) == 1
handles.data.signal.emg_map{i} = handles.data.signal.emg_data_diff(:, i);
else
for j = 1:size(handles.data.signal.emg_map{handles.data.channel},2)
handles.data.signal.emg_map{i}(:, j) = handles.data.signal.emg_data_all{j}(:, i);
end
end
end
for i = 1:length(handles.data.signal.emg_map)
axes(eval(strcat('handles.axes',num2str(i))))
for j = 1:size(handles.data.signal.emg_map{handles.data.channel},2)
handles.hsignal{j}(i) = plot(handles.data.xs{j}{i},handles.data.signal.emg_map{i}(:,j),'Visible','off');
if sum(isnan(handles.data.signal.emg_map{i}(samples_triggeron, j)))==0
handles.data.trigger{j, i} = [samples_triggeron,...
handles.data.signal.emg_map{i}(samples_triggeron, j)];
else
handles.data.trigger{j, i} = [];
end
% red circulus
if ~isempty(handles.data.trigger{j, i})
handles.htrigger{j, i} = plot(handles.data.xs{j}{i}(handles.data.trigger{j,i}(:,1)),...
handles.data.trigger{j,i}(:,2),'ro', 'Visible', 'off');
else
handles.htrigger{j, i} = nan;
end
% green line
if ~isempty(handles.data.trigger{j, i})
a = line([handles.data.xs{j}{i}(handles.data.trigger{j, i}(:,1))',...
handles.data.xs{j}{i}(handles.data.trigger{j, i}(:,1))'],...
[min(handles.data.signal.emg_map{i}(:,j)),...
max(handles.data.signal.emg_map{i}(:,j))],...
'Color','g','Visible','off');
handles.hline{j, i} = a;
else
handles.hline{j, i} = nan;
end
end
set(eval(strcat('handles.axes',num2str(i))),'HitTest','on')
update_waitbar(handles,i/length(handles.data.signal.emg_map))
end
% trigger computation based on derivative properties
else
for i = 1:length(handles.data.signal.emg_map)
axes(eval(strcat('handles.axes',num2str(i))))
handles.data.trigger{i} = BiopacTrigger(handles.data.signal.emg_map{i}(:,handles.data.channel),handles.data.slope);
% red circulus
if ~isempty(handles.data.trigger{i})
handles.htrigger(i) = plot(handles.data.xs{handles.data.channel}{i}(handles.data.trigger{i}(:,1)),handles.data.trigger{i}(:,2),'ro', 'Visible', 'off');
else
handles.htrigger(i) = nan;
end
set(eval(strcat('handles.axes',num2str(i))),'HitTest','on')
% green line
if ~isempty(handles.data.trigger{i})
a = line([handles.data.xs{handles.data.channel}{i}(handles.data.trigger{i}(:,1))',handles.data.xs{handles.data.channel}{i}(handles.data.trigger{i}(:,1))'],...
[min(handles.data.signal.emg_map{i}(:,handles.data.channel)),...
max(handles.data.signal.emg_map{i}(:,handles.data.channel))],...
'Color','g','Visible','off');
handles.hline{i} = a;
else
handles.hline{i} = nan;
end
update_waitbar(handles,i/length(handles.data.signal.emg_map))
end
end
set(handles.checkbox_ViewSignal,'Value',1)
set(handles.checkbox_ViewTrigger,'Value',1)
set(handles.checkbox_ViewLine,'Value',1)
checkbox_ViewSignal_Callback(hObject, eventdata, handles)
checkbox_ViewTrigger_Callback(hObject, eventdata, handles)
checkbox_ViewLine_Callback(hObject, eventdata, handles)
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in button_Backward.
function button_Backward_Callback(hObject, eventdata, handles)
% hObject handle to button_Backward (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% current values of each checkbox
a1 = get(handles.checkbox_ViewSignal,'Value');
a2 = get(handles.checkbox_ViewMEPsMean,'Value');
a3 = get(handles.checkbox_ViewMEPsMult,'Value');
a4 = get(handles.checkbox_ViewLine,'Value');
a5 = get(handles.checkbox_ViewTrigger,'Value');
a6 = get(handles.checkbox_AmplitudeThreshold,'Value');
a7 = get(handles.checkbox_ViewMinMax,'Value');
a8 = get(handles.checkbox_ViewLatency,'Value');
% set current channel plots as invisible
handles = visibleoff(handles);
% restore checkbox values
handles.signalvalue = a1;
handles.mepmeanvalue = a2;
handles.mepsvalue = a3;
handles.linevalue = a4;
handles.triggervalue = a5;
handles.ampthresholdvalue = a6;
handles.mepminmaxvalue = a7;
handles.latencyvalue = a8;
set(handles.checkbox_ViewSignal,'Value',handles.signalvalue);
set(handles.checkbox_ViewMEPsMean,'Value',handles.mepmeanvalue);
set(handles.checkbox_ViewMEPsMult,'Value',handles.mepsvalue);
set(handles.checkbox_ViewLine,'Value',handles.linevalue);
set(handles.checkbox_ViewTrigger,'Value',handles.triggervalue);
set(handles.checkbox_AmplitudeThreshold,'Value',handles.ampthresholdvalue);
set(handles.checkbox_ViewMinMax,'Value',handles.mepminmaxvalue);
set(handles.checkbox_ViewLatency,'Value',handles.latencyvalue);
% ------------------------
% change text channel
if handles.data.channel <= 1
handles.data.channel = size(handles.data.signal.emg_map{1},2);
set(handles.text_Channel, 'String',num2str(handles.data.channel))
else
handles.data.channel = handles.data.channel-1;
set(handles.text_Channel, 'String',num2str(handles.data.channel))
end
% execute callback for each checkbox with current value
checkbox_ViewSignal_Callback(hObject, eventdata, handles)
checkbox_ViewMEPsMean_Callback(hObject, eventdata, handles)
checkbox_ViewMEPsMult_Callback(hObject, eventdata, handles)
checkbox_ViewLine_Callback(hObject, eventdata, handles)
checkbox_ViewTrigger_Callback(hObject, eventdata, handles)
checkbox_AmplitudeThreshold_Callback(hObject, eventdata, handles)
checkbox_ViewMinMax_Callback(hObject, eventdata, handles)
checkbox_ViewLatency_Callback(hObject, eventdata, handles)
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in button_Forward.
function button_Forward_Callback(hObject, eventdata, handles)
% hObject handle to button_Forward (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% current values of each checkbox
a1 = get(handles.checkbox_ViewSignal,'Value');
a2 = get(handles.checkbox_ViewMEPsMean,'Value');
a3 = get(handles.checkbox_ViewMEPsMult,'Value');
a4 = get(handles.checkbox_ViewLine,'Value');
a5 = get(handles.checkbox_ViewTrigger,'Value');
a6 = get(handles.checkbox_AmplitudeThreshold,'Value');
a7 = get(handles.checkbox_ViewMinMax,'Value');
a8 = get(handles.checkbox_ViewLatency,'Value');
% set current channel plots as invisible
handles = visibleoff(handles);
% restore checkbox values
handles.signalvalue = a1;
handles.mepmeanvalue = a2;
handles.mepsvalue = a3;
handles.linevalue = a4;
handles.triggervalue = a5;
handles.ampthresholdvalue = a6;
handles.mepminmaxvalue = a7;
handles.latencyvalue = a8;
set(handles.checkbox_ViewSignal,'Value',handles.signalvalue);
set(handles.checkbox_ViewMEPsMean,'Value',handles.mepmeanvalue);
set(handles.checkbox_ViewMEPsMult,'Value',handles.mepsvalue);
set(handles.checkbox_ViewLine,'Value',handles.linevalue);
set(handles.checkbox_ViewTrigger,'Value',handles.triggervalue);
set(handles.checkbox_AmplitudeThreshold,'Value',handles.ampthresholdvalue);
set(handles.checkbox_ViewMinMax,'Value',handles.mepminmaxvalue);
set(handles.checkbox_ViewLatency,'Value',handles.mepminmaxvalue);
% ------------------------
% change text channel
if handles.data.channel >= size(handles.data.signal.emg_map{1},2)
handles.data.channel = 1;
set(handles.text_Channel, 'String',num2str(handles.data.channel))
else
handles.data.channel = handles.data.channel+1;
set(handles.text_Channel, 'String',num2str(handles.data.channel))
end
% execute callback for each checkbox with current value
checkbox_ViewSignal_Callback(hObject, eventdata, handles)
checkbox_ViewMEPsMean_Callback(hObject, eventdata, handles)
checkbox_ViewMEPsMult_Callback(hObject, eventdata, handles)
checkbox_ViewLine_Callback(hObject, eventdata, handles)
checkbox_ViewTrigger_Callback(hObject, eventdata, handles)
checkbox_AmplitudeThreshold_Callback(hObject, eventdata, handles)
checkbox_ViewMinMax_Callback(hObject, eventdata, handles)
checkbox_ViewLatency_Callback(hObject, eventdata, handles)
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in button_MEPs.
function button_MEPs_Callback(hObject, eventdata, handles)
% hObject handle to button_MEPs (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% change checkbox values for MEP visualization
set(handles.checkbox_ViewSignal,'Value',0)
set(handles.checkbox_ViewTrigger,'Value',0)
set(handles.checkbox_ViewLine,'Value',0)
checkbox_ViewSignal_Callback(hObject, eventdata, handles)
checkbox_ViewTrigger_Callback(hObject, eventdata, handles)
checkbox_ViewLine_Callback(hObject, eventdata, handles)
% MEP windowing and calculation
handles.MEPStart = round(str2double(get(handles.edit_MEPStart,'String')))/1000;
handles.data.s0 = round(str2double(get(handles.edit_MEPStart,'String'))*handles.data.configuration.fsample/1000);
handles.data.s1 = round(str2double(get(handles.edit_MEPEnd,'String'))*handles.data.configuration.fsample/1000);
handles.data.fmed = {};
handles.data.amp_rms = {};
handles.data.offset_fmed = {};
handles.data.offset_rms = {};
off0 = -1500;
off1 = -10;
% aux_colors = rand(10,3);
% this is a map of colors similar to the jet
% the blue is referring to the first stimuli and the
% red is used for the last stimuli.
aux_colors = [0.0117 0.0343 0.894; 0.0117 0.6078 0.894;...
0.0117 0.89411 0.69411; 0.0117 0.8902 0.3255;...
0.0196 0.8901 0.0196; 0.6274 0.8902 0.3255;...
0.9804 0.9568 0.1137; 0.9804 0.6941 0.1137;...
0.9804 0.3294 0.1137; 0.8627 0.0196 0.0196;...
0.0 0.0 0.0; 0.0 0.0 0.0;...
0.0 0.0 0.0; 0.0 0.0 0.0;...
0.0 0.0 0.0; 0.0 0.0 0.0];
for j = 1:length(handles.data.signal.emg_map) % Electrodes
axes(eval(strcat('handles.axes',num2str(j))))
hold on
for i = 1:size(handles.data.signal.emg_map{handles.data.channel},2) % EMG mode
aux_channel = handles.data.signal.emg_map{j}(:,i);
aux_meps = [];
aux_meps_column = [];
aux_offset_signal_columns = [];
aux_offset_signal = [];
colors = aux_colors;
if ~isempty(handles.data.trigger{i, j})
aux_trigger = handles.data.trigger{i, j}(:,1);
else
aux_trigger = [];
colors = [];
end
for k = 1:length(aux_trigger) % Number of stimulus
% this offset window the signal before the trigger point to
%calculus the mean value and then subtract from MEP values
% aux_offset = aux_channel(aux_trigger(k)+off0:aux_trigger(k)+off1-1);
% offset{i}{j}(k) = mean(aux_offset);
% offset adjustment - didnt understand why is necessary to
% subtract the offset
% handles.data.meps{i}{j}{k} = aux_channel(aux_trigger(k)+handles.data.s0:aux_trigger(k)+handles.data.s1-1) - offset{i}{j}(k);
handles.data.meps{i}{j}{k} = aux_channel(aux_trigger(k)+handles.data.s0:aux_trigger(k)+handles.data.s1-1);
handles.data.offset_signals{i}{j}{k} = aux_channel(aux_trigger(k)-handles.data.s1-30+1:aux_trigger(k)-handles.data.s0-30);
handles.hmeps{i, j}(k) = plot(handles.data.xs{i}{j}(1:handles.data.s1-handles.data.s0)+handles.MEPStart,...
handles.data.meps{i}{j}{k},'-','color',colors(k,:),'Visible', 'off');
if k == 1
aux_meps_column = handles.data.meps{i}{j}{k};
aux_meps = handles.data.meps{i}{j}{k};
aux_offset_signal_columns = handles.data.offset_signals{i}{j}{k};
aux_offset_signal = handles.data.offset_signals{i}{j}{k};
else
aux_meps_column = cat(1, aux_meps_column, handles.data.meps{i}{j}{k});
aux_meps = cat(2, aux_meps, handles.data.meps{i}{j}{k});
aux_offset_signal_columns = cat(1, aux_offset_signal_columns, handles.data.offset_signals{i}{j}{k});
aux_offset_signal = cat(2, aux_offset_signal, handles.data.offset_signals{i}{j}{k});
end
end
if isempty(aux_trigger)
handles.data.meps{i}{j} = [];
end
% median frequency, rms values and mean frequency for meps
[handles.data.fmed{i}{j}, handles.data.amp_rms{i}{j}, handles.data.fmean{i}{j}] = Fmed3cla(aux_meps_column,...
handles.data.configuration.fsample, handles.data.s1-handles.data.s0);
handles.data.fmed{i}{j} = mean(handles.data.fmed{i}{j}, 1);
handles.data.amp_rms{i}{j} = mean(handles.data.amp_rms{i}{j}, 1);
handles.data.fmean{i}{j} = mean(handles.data.fmean{i}{j}, 1);
% median frequency and rms value for the offset signal
[handles.data.offset_fmed{i}{j}, handles.data.offset_rms{i}{j}, ~] = Fmed3cla(aux_offset_signal_columns,...
handles.data.configuration.fsample, handles.data.s1-handles.data.s0);
handles.data.offset_rms{i}{j} = mean(handles.data.offset_rms{i}{j}, 1);
handles.data.offset_fmed{i}{j} = mean(handles.data.offset_fmed{i}{j}, 1);
% mean values, max, min and their backups
handles.data.mepmean{i}{j} = mean(aux_meps,2);
handles.data.mepmean_bkp = handles.data.mepmean;
handles.data.mepmax{i}{j} = max(handles.data.mepmean{i}{j});
handles.data.mepmax_bkp = handles.data.mepmax;
handles.data.mepmin{i}{j} = min(handles.data.mepmean{i}{j});
handles.data.mepmin_bkp = handles.data.mepmin;
handles.data.fmed_bkp = handles.data.fmed;
handles.data.amp_rms_bkp = handles.data.amp_rms;
handles.data.fmean_bkp = handles.data.fmean;
handles.data.offset_rms_bkp = handles.data.offset_rms;
handles.data.offset_fmed_bkp = handles.data.offset_fmed;
% position in x axis of min and max values
handles.data.pos_max{i, j} = find(handles.data.mepmean{i}{j} == handles.data.mepmax{i}{j});
handles.data.pos_min{i, j} = find(handles.data.mepmean{i}{j} == handles.data.mepmin{i}{j});
handles.data.pos_max_init = handles.data.pos_max;
handles.data.pos_min_init = handles.data.pos_min;
% plots
if isempty(aux_trigger)
handles.hmeps{i, j} = nan;
handles.hmepmean{i, j} = nan;
handles.hmepmax{i, j} = nan;
handles.hmepmin{i, j} = nan;
else
handles.hmepmean{i, j} = plot(handles.data.xs{i}{j}(1:handles.data.s1-handles.data.s0)+handles.MEPStart,...
handles.data.mepmean{i}{j},'Visible','off');
handles.hmepmax{i, j} = plot(handles.data.xs{i}{j}(handles.data.pos_max{i, j}(1))+handles.MEPStart,...
handles.data.mepmax{i}{j},'+r','Visible','off');
handles.hmepmin{i, j} = plot(handles.data.xs{i}{j}(handles.data.pos_min{i, j}(1))+handles.MEPStart,...
handles.data.mepmin{i}{j},'+r','Visible','off');
end
end
update_waitbar(handles,j/(length(handles.data.signal.emg_map)))
end
% updating checkbox values for MEP visualization
set(handles.checkbox_ViewMEPsMean,'Value',1)
set(handles.checkbox_ViewMinMax,'Value',1)
set(handles.checkbox_ViewLatency,'Value',1)
checkbox_ViewMEPsMean_Callback(hObject, eventdata, handles)
checkbox_ViewMinMax_Callback(hObject, eventdata, handles)
checkbox_ViewLatency_Callback(hObject, eventdata, handles)
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in button_Export.
function button_Export_Callback(hObject, eventdata, handles)
% hObject handle to button_Export (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
previous_data = [];
[filename, pathname, filterindex] = uiputfile({'*.xls;*.xlsx','MS Excel Files (*.xls,*.xlsx)';...
'*.txt', 'ASCII format (*.txt)'}, 'Export data', 'processed_data.xlsx');
export_data = [{handles.data.configuration.signal_path handles.data.configuration.patient_id...
handles.data.configuration.emg_side handles.data.configuration.angle_stim...
handles.data.amp_pp_sim_diff handles.data.amp_rms_sim_diff handles.data.latency_sim...
handles.data.fmed_sim_diff num2str(handles.data.cog_cluster_diff_pp)...
handles.data.size_cluster_diff_pp handles.data.cluster_amp_pp_diff...
handles.data.freq_cluster_pp num2str(handles.data.cog_cluster_diff_rms)...
handles.data.size_cluster_diff_rms handles.data.cluster_amp_rms_diff...
handles.data.freq_cluster_rms handles.data.cvelocity handles.data.correl_coef...
handles.data.iz_row handles.data.mean_total_rms handles.data.mean_offset_rms...
handles.data.latency_avarege}];
headers = [{'file_name'} {'subject'} {'hemisphere'} {'angle(degrees)'}...
{'sim_amp_pp(uV)'} {'sim_amp_rms(uV)'} {'latency_sim(s)'}...
{'sim_freq_med(Hz)'} {'cog_pp'} {'size_cluster_pp'}...
{'cluster_amp_pp(uV)'} {'freq_cluster_pp(Hz)'} {'cog_rms'}...
{'size_cluster_rms'} {'cluster_amp_rms(uV)'} {'freq_cluster_rms(Hz)'}...
{'cv_central(m/s)'} {'cv_coef_correl'} {'iz(row)'} {'mean_total_rms(uV)'}...
{'mean_offset_rms(uV)'} {'latency_avarege(ms)'}];
switch filterindex
case 1
try
[~, ~, previous_data] = xlsread([pathname filename]);
end
if isempty(previous_data)
xlswrite([pathname filename], [headers; export_data])
else
xlswrite([pathname filename], [previous_data; export_data])
end
case 2
fid = fopen([pathname filename]);
try
previous_data = fgets(fid);
end
the_format = '\n%s %d %s %d %.4f %.4f %.4f %.4f %.4f %.4f %s %d %.4f %.4f %.4f';
if isempty(previous_data)
fid = fopen([pathname filename], 'w');
fprintf(fid, '%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s', headers{1,:});
fprintf(fid, the_format, export_data{1,:});
fclose(fid);
else
fid = fopen([pathname filename], 'a');
fprintf(fid, the_format, export_data{1,:})
fclose(fid);
end
end
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in button_Save.
function button_Save_Callback(hObject, eventdata, handles)
% hObject handle to button_Save (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
file_name_default = [num2str(handles.data.configuration.patient_id) '_' handles.data.configuration.emg_side '_' num2str(handles.data.configuration.angle_stim)];
file_name_default = strcat(file_name_default,'.mat');
update_waitbar(handles,0.5)
[file_name, file_path, index] = uiputfile({'*.mat','MAT-files (*.mat)'},...
'Save data as...', file_name_default);
if index == 1
data = handles.data;
save([file_path file_name], 'data')
end
update_waitbar(handles,1)
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in button_Reset.
function button_Reset_Callback(hObject, eventdata, handles)
% hObject handle to button_Reset (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% set current channel plots as invisible - increase reset velocity
handles = visibleoff(handles);
% delete all plots handles
for j = 1:length(handles.data.signal.emg_map)
for i = 1:size(handles.data.signal.emg_map{handles.data.channel},2)
if isfield(handles,'htrigger')
if ishandle(handles.htrigger{i, j})
delete(handles.htrigger{i, j})
end
end
if isfield(handles,'hline')
if ishandle(handles.hline{i, j})
delete(handles.hline{i, j})
end
end
if isfield(handles,'hampthreshold')
if ishandle(handles.hampthreshold{i, j})
delete(handles.hampthreshold{i, j})
end
end
if isfield(handles,'hmeps')
if ishandle(handles.hmeps{i, j})
delete(handles.hmeps{i, j})
end
end
if isfield(handles,'hmepmean')
if ishandle(handles.hmepmean{i, j})
delete(handles.hmepmean{i, j})
end
end
if isfield(handles,'hmepmax')
if ishandle(handles.hmepmax{i, j})
delete(handles.hmepmax{i, j})
end
end
if isfield(handles,'hmepmin')
if ishandle(handles.hmepmin{i, j})
delete(handles.hmepmin{i, j})
end
end
if isfield(handles,'hlatencystart')
if ishandle(handles.hlatencystart{i}{j})
delete(handles.hlatencystart{i}{j})
end
end
if isfield(handles,'hlatencystop')
if ishandle(handles.hlatencystop{i}{j})
delete(handles.hlatencystop{i}{j})
end
end
end
if isfield(handles,'hsignal')
if ishandle(handles.hsignal{i})
delete(handles.hsignal{i})
end
end
update_waitbar(handles,j/length(handles.data.signal.emg_map))
axes(eval(strcat('handles.axes',num2str(j))))
cla
end
% update checkbox values
set(handles.checkbox_ViewSignal,'Value',0);
set(handles.checkbox_ViewMEPsMean,'Value',0);
set(handles.checkbox_ViewMEPsMult,'Value',0);
set(handles.checkbox_ViewLine,'Value',0);
set(handles.checkbox_ViewTrigger,'Value',0);
set(handles.checkbox_AmplitudeThreshold,'Value',0);
set(handles.checkbox_ViewMinMax,'Value',0);
set(handles.checkbox_ViewLatency,'Value',0);
% clear plots handles variables
clear handles.hsignal handles.htrigger handles.hline handles.hampthreshold...
handles.hmeps handles.hmepmean handles.hmepmax handles.hmepmin...
handles.hlatencystart handles.hlatencystop
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in checkbox_AmplitudeThreshold.
function checkbox_AmplitudeThreshold_Callback(hObject, eventdata, handles)
% hObject handle to checkbox_AmplitudeThreshold (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 checkbox_AmplitudeThreshold
% update amplitude threshold lines visualization according to checkbox value
ampthresholdvalue = get(handles.checkbox_AmplitudeThreshold,'Value');
handles.ampthresholdvalue = ampthresholdvalue;
if isfield(handles,'hampthreshold')
for i = 1:size(handles.hampthreshold, 2)
if ishandle(handles.hampthreshold{handles.data.channel, i})
if ampthresholdvalue == 1
set(handles.hampthreshold{handles.data.channel, i},'Visible','on')
else
set(handles.hampthreshold{handles.data.channel, i},'Visible','off')
end
end
end
end
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in checkbox_ViewLatency.
function checkbox_ViewLatency_Callback(hObject, ~, handles)
% hObject handle to checkbox_ViewLatency (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 checkbox_ViewLatency
% update latency visualization according to checkbox value
latencyvalue = get(handles.checkbox_ViewLatency,'Value');
handles.latencyvalue = latencyvalue;
if isfield(handles,'hlatencystart')
for i = 1:size(handles.hlatencystart{handles.data.channel}, 2)
if ishandle(handles.hlatencystart{handles.data.channel}{i})
if latencyvalue == 1
set(handles.hlatencystart{handles.data.channel}{i},'Visible','on')
set(handles.hlatencystop{handles.data.channel}{i},'Visible','on')
else
set(handles.hlatencystart{handles.data.channel}{i},'Visible','off')
set(handles.hlatencystop{handles.data.channel}{i},'Visible','off')
end
end
end
end
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in checkbox_ViewSignal.
function checkbox_ViewSignal_Callback(hObject, ~, handles)
% hObject handle to checkbox_ViewSignal (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 checkbox_ViewSignal
% update signal visualization according to checkbox value
signalvalue = get(handles.checkbox_ViewSignal,'Value');
handles.signalvalue = signalvalue;
if isfield(handles,'hsignal') == 1
if signalvalue == 1
set(handles.hsignal{handles.data.channel},'Visible','on')
else
set(handles.hsignal{handles.data.channel},'Visible','off')
end
end
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in checkbox_ViewMEPsMean.
function checkbox_ViewMEPsMean_Callback(hObject, eventdata, handles)
% hObject handle to checkbox_ViewMEPsMean (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 checkbox_ViewMEPsMean
% update mep mean signal visualization according to checkbox value
mepmeanvalue = get(handles.checkbox_ViewMEPsMean,'Value');
handles.mepmeanvalue = mepmeanvalue;
if isfield(handles,'hmepmean')
for i = 1:size(handles.hmepmean, 2)
if ishandle(handles.hmepmean{handles.data.channel, i})
if mepmeanvalue == 1
set(handles.hmepmean{handles.data.channel, i},'Visible','on')
else
set(handles.hmepmean{handles.data.channel, i},'Visible','off')
end
end
end
end
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in checkbox_ViewMEPsMult.
function checkbox_ViewMEPsMult_Callback(hObject, eventdata, handles)
% hObject handle to checkbox_ViewMEPsMult (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 checkbox_ViewMEPsMult
% update multiple mep signal visualization according to checkbox value
mepsvalue = get(handles.checkbox_ViewMEPsMult,'Value');
handles.mepsvalue = mepsvalue;
if isfield(handles,'hmeps') == 1
for i = 1:size(handles.hmeps, 2)
if ishandle(handles.hmeps{handles.data.channel, i})
if mepsvalue == 1
set(handles.hmeps{handles.data.channel, i},'Visible','on')
else
set(handles.hmeps{handles.data.channel, i},'Visible','off')
end
end
end
end
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in checkbox_ViewLine.
function checkbox_ViewLine_Callback(hObject, eventdata, handles)
% hObject handle to checkbox_ViewLine (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 checkbox_ViewLine
% update trigger green line visualization according to checkbox value
linevalue = get(handles.checkbox_ViewLine,'Value');
handles.linevalue = linevalue;
if isfield(handles,'hline')
for i = 1:size(handles.hline, 2)
if ishandle(handles.hline{handles.data.channel, i})
if linevalue == 1
set(handles.hline{handles.data.channel, i},'Visible','on')
else
set(handles.hline{handles.data.channel, i},'Visible','off')
end
end
end
end
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in checkbox_ViewTrigger.
function checkbox_ViewTrigger_Callback(hObject, eventdata, handles)
% hObject handle to checkbox_ViewTrigger (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 checkbox_ViewTrigger
% update trigger red circulus visualization according to checkbox value
triggervalue = get(handles.checkbox_ViewTrigger,'Value');
handles.triggervalue = triggervalue;
if isfield(handles,'htrigger')
for i = 1:size(handles.htrigger, 2)
if ishandle(handles.htrigger{handles.data.channel, i})
if triggervalue == 1
set(handles.htrigger{handles.data.channel, i},'Visible','on')
else
set(handles.htrigger{handles.data.channel, i},'Visible','off')
end
end
end
end
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in checkbox_ViewMinMax.
function checkbox_ViewMinMax_Callback(hObject, eventdata, handles)
% hObject handle to checkbox_ViewMinMax (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 checkbox_ViewMinMax
% update min and max red cross visualization according to checkbox value
mepminmaxvalue = get(handles.checkbox_ViewMinMax,'Value');
handles.mepminmaxvalue = mepminmaxvalue;
if isfield(handles,'hmepmax')
for i = 1: length(handles.data.signal.emg_map)
if ishandle(handles.hmepmax{handles.data.channel, i})
if mepminmaxvalue == 1
set(handles.hmepmax{handles.data.channel, i},'Visible','on')
set(handles.hmepmin{handles.data.channel, i},'Visible','on')
else