-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bike_ERSP_Plot.m
1266 lines (1000 loc) · 48.8 KB
/
Bike_ERSP_Plot.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 BikeOut_ERSP_2_Plot(ALLEEG, conds, EEG, elec_names, electrode, electrode_loc,...
ersp, exp, Filename, freqs, itc, Pathname, perms, powbase, subs, times,...
trialevent)
% |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
% INFORMATION
% |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
% SPECTOGRAM
% A spectogram is a 3d figure that plots time on the x-axis, frequency on the
% y-axis, and shows you the power or phase-locking value for each point.
% We compute spectograms if we have power and phase information, averaged
% across trials, for at least one electrode.
% This can help us understand the changes of power and phase throughout the
% trial.
% TOPOPLOT
% A Topoplot is a graph of the distribution of power or phase-locking magnitude
% across the scalp, averaging a range of both time and frequency.
% We make a topoplot if we have power or phase-locking information, averaged
% across the trials, for every electrode.
% This can help us understand the scalp distribution of power or phase-locking
% at a crucial time and at the frequency of maximal effect.
% |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
% :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
% |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
% Variables working with:
% ersp(i_sub,i_cond,i_perm,i_chan,:,:)
% itc(i_sub,i_cond,i_perm,i_chan,:,:)
% powbase,times,freqs
% The variables ersp and itc will be a 6D variable:
% (participants x conditions x events x electrodes x frequencies x timepoints)
eeglab redraw
% /////////////////////////////////////////////////////////////////////////
%% ERSP plots averaged over subjects and channels: Out vs In
% /////////////////////////////////////////////////////////////////////////
for i_event = 1:length(trialevent)
% (participants x conditions x events x electrodes x frequencies x timepoints)
% in_ersp = squeeze(mean(mean(ersp(:,1,i_event,:,:,:),4),1));
% out_ersp = squeeze(mean(mean(ersp(:,2,i_event,:,:,:),4),1));
pref_ersp = squeeze(mean(mean(ersp(:,1,i_event,:,:,:),4),1));
non_pref_ersp = squeeze(mean(mean(ersp(:,2,i_event,:,:,:),4),1));
figure;
CLim = [-1.5 1.5];
colormap('jet')
% Subplot 1: non-pref
subplot(3,1,1);
imagesc(times,freqs,non_pref_ersp);
title(['ERSP: Preferred; ' char(trialevent{i_event})]);
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)');
colorbar
% Subplot 1: pref
subplot(3,1,2);
imagesc(times,freqs,pref_ersp);
title(['ERSP: Non-Preferred; ' char(trialevent{i_event})]);
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)');
colorbar
% Subplot 1: out-in
subplot(3,1,3);
imagesc(times,freqs,pref_ersp-non_pref_ersp);
title(['ERSP: Preferred-Non-preferred; ' char(trialevent{i_event})]);
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)'); xlabel('Time (ms)');
colorbar
% Overall subplot title
% supertitle(['ERSP: ' char(trialevent{i_event})],'FontSize',12);
clear non_pref_ersp pref_ersp
end
clear i_event
% /////////////////////////////////////////////////////////////////////////
%% ITC plots averaged over subjects and channels: Out vs In
% /////////////////////////////////////////////////////////////////////////
for i_event = 1:length(trialevent)
% The variable will be a 6D variable:
% (participants x conditions x events x electrodes x frequencies x timepoints)
in_itc = squeeze(mean(mean(abs(itc(:,1,i_event,:,:,:)),4),1));
out_itc = squeeze(mean(mean(abs(itc(:,2,i_event,:,:,:)),4),1));
CLim = [-.5 .5];
figure;
colormap('jet')
% Subplot 1: out
subplot(3,1,1);
imagesc(times,freqs,out_itc,CLim);
title(['ITC: Out; ' char(trialevent{i_event})]);
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)');
colorbar
% Subplot 1: in
subplot(3,1,2);
imagesc(times,freqs,in_itc,CLim);
title(['ITC: In; ' char(trialevent{i_event})]);
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)');
colorbar
% Subplot 1: out-in
subplot(3,1,3);
imagesc(times,freqs,out_itc-in_itc,CLim);
title(['ERSP: Out-In; ' char(trialevent{i_event})]);
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)'); xlabel('Time (ms)');
colorbar
%Overall subplot title
% supertitle('ITC: Overall Average','FontSize',12)
% close all
% eeglab redraw
clear out_itc in_itc
end
clear i_event
% /////////////////////////////////////////////////////////////////////////
%% ERSP plots averaged over subjects for each channel
% /////////////////////////////////////////////////////////////////////////
CLim = [-1.5 1.5];
for i_event = 1:length(trialevent)
for ch_ersp = 1:length(electrode)
% ERSP values by electrode
% (participants x conditions x events x electrodes x frequencies x timepoints)
pref_ersp_chan = squeeze(mean(ersp(:,1,i_event,ch_ersp,:,:),1));
npref_ersp_chan = squeeze(mean(ersp(:,2,i_event,ch_ersp,:,:),1));
figure;
colormap('jet')
% Subplot 1: out
subplot(3,1,1);
imagesc(times,freqs,npref_ersp_chan,CLim);
title({['ERSP: ' char(trialevent{i_event}) ', NonPref']; char(elec_names(ch_ersp))});
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)');
colorbar
% Subplot 1: in
subplot(3,1,2);
imagesc(times,freqs,pref_ersp_chan,CLim);
title({['ERSP: ' char(trialevent{i_event}) ', Pref']; char(elec_names(ch_ersp))});
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)');
colorbar
% Subplot 1: out-in
subplot(3,1,3);
imagesc(times,freqs,npref_ersp_chan-pref_ersp_chan,CLim);
title({['ERSP: ' char(trialevent{i_event}) ', np-p']; char(elec_names(ch_ersp))});
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)'); xlabel('Time (ms)');
colorbar
%Overall subplot title
% supertitle(['ERSP: ' char(elec_names(ch_ersp))],'FontSize',12)
clear in_ersp_chan out_ersp_chan
end
clear ch_ersp
end
clear i_event
% close all
% eeglab redraw
% /////////////////////////////////////////////////////////////////////////
%% ITC plots averaged over subjects for each channel
% /////////////////////////////////////////////////////////////////////////
CLim = [-.5 .5];
for i_event = 1:length(trialevent)
for ch_itc = 1:length(electrode)
% ITC values by electrode
% (participants x conditions x events x electrodes x frequencies x timepoints)
in_itc_chan = squeeze(mean(abs(itc(:,1,i_event,ch_itc,:,:)),1));
out_itc_chan = squeeze(mean(abs(itc(:,2,i_event,ch_itc,:,:)),1));
figure;
colormap('jet')
% Subplot 1: out
subplot(3,1,1);
imagesc(times,freqs,out_itc_chan,CLim);
title({['ITC: ' char(trialevent{i_event}) ', Out']; char(elec_names(ch_itc))});
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)');
colorbar
% Subplot 1: in
subplot(3,1,2);
imagesc(times,freqs,in_itc_chan,CLim);
title({['ITC: ' char(trialevent{i_event}) ', In']; char(elec_names(ch_itc))});
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)');
colorbar
% Subplot 1: out-in
subplot(3,1,3);
imagesc(times,freqs,out_itc_chan-in_itc_chan,CLim);
title({['ITC: ' char(trialevent{i_event}) ', Out-In']; char(elec_names(ch_itc))});
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)'); xlabel('Time (ms)');
colorbar
%Overall subplot title
% supertitle(['ITC: ' char(elec_names(ch_itc))],'FontSize',12)
clear in_itc_chan out_itc_chan
end
clear ch_itc
end
% close all
% eeglab redraw
clear i_event
% .........................................................................
% /////////////////////////////////////////////////////////////////////////
%% ERSP Top Plots
% /////////////////////////////////////////////////////////////////////////
% .........................................................................
%A Topoplot needs to collapse across frequency and time so it can show the
% data across electrodes
% .........................................................................
% Set the range of frequency to consider
flims{1} = [1 3]; % delta
flims{2} = [4 7]; % theta
flims{3} = [8 12]; % alpha
flims{4} = [13 18]; % beta1
flims{5} = [19 22]; % beta2
flims{6} = [23 30]; % gamma
% flims{1} = [1 3]; % delta
% flims{1} = [4 7]; % theta
% flims{3} = [8 12]; % alpha
% flims{4} = [19 22]; % beta2
% flims{5} = [23 30]; % gamma
% .........................................................................
% Set the range of time to consider
% tlims{1} = [-400 -200];
% tlims{2} = [-200 0];
% tlims{3} = [0 200];
% tlims{4} = [200 400];
% tlims{5} = [400 600];
tlims{1} = [-200 -100];
tlims{2} = [-100 0];
tlims{3} = [0 100];
tlims{4} = [100 200];
tlims{5} = [200 300];
tlims{6} = [300 400];
tlims{7} = [400 500];
tlims{8} = [500 600];
tlims{9} = [600 700];
% .........................................................................
CLim = [-1.5 1.5];
% .........................................................................
for i_event = 1:length(trialevent) %loop through events
for fq_i = 1:length(flims) %loop through set of frequencies
iflims = flims{fq_i}; %select each frequency range
%this code finds the frequencies you want from the freqs variable
freq_lims = find(freqs>= iflims(1),1):find(freqs>= iflims(2),1)-1;
if isempty(freq_lims) %in case the upper frequency < iflims(2)
freq_lims = find(freqs>= iflims(1),1):find(freqs>= (iflims(2)-1),1)-1;
end
figure('OuterPosition',[313 537 1515 545]) %new figure for every frequency range
for tl_i = 1:length(tlims) %loop through set of times
itlims = tlims{tl_i}; %select each time range
%this code finds the times you want from the timess variable
time_lims = find(times>= itlims(1),1):find(times>= itlims(2),1)-1;
% .....................................................................
%Here you need a 1D variable, electrodes.
%By default it will take the mean across participants, events, times and frequencies, and show the data for each set
% (participants x conditions x events x electrodes x frequencies x timepoints)
in_ersp_top = squeeze(mean(mean(mean(ersp(:,1,i_event,electrode,freq_lims,...
time_lims),5),6),1));
out_ersp_top = squeeze(mean(mean(mean(ersp(:,2,i_event,electrode,freq_lims,...
time_lims),5),6),1));
diff_ersp_top = out_ersp_top - in_ersp_top;
% .....................................................................
% Creating plots
% Subplot Out Condition
subtightplot(3,length(tlims),tl_i,[0.01,0.01],[0.05,0.07],[0.05,0.05]);
% subplot(3,length(tlims),tl_i);
%This code creates the topoplots. You need to replace all the non-brain electrodes
% with NaN.
topoplot([out_ersp_top' NaN NaN NaN],EEG.chanlocs,'maplimits',CLim,'plotrad',0.6,...
'colormap',jet,'plotchans',electrode,'emarker',{'.','k',9,1}); colorbar;
title(['Out: ' num2str(itlims(1)) ' to ' num2str(itlims(2)) ' ms'],'FontSize',8);
% Subplot In Condition
subtightplot(3,length(tlims),length(tlims)+tl_i,[0.01,0.01],[0.05,0.07],[0.05,0.05]);
%This code creates the topoplots. You need to replace all the non-brain electrodes
% with NaN.
topoplot([in_ersp_top' NaN NaN NaN],EEG.chanlocs,'maplimits',CLim,'plotrad',0.6,...
'colormap',jet,'plotchans',electrode,'emarker',{'.','k',9,1}); colorbar;
title(['In: ' num2str(itlims(1)) ' to ' num2str(itlims(2)) ' ms'],'FontSize',8);
% Subplot Out-In Condition
subtightplot(3,length(tlims),(length(tlims)*2)+tl_i,[0.01,0.01],[0.05,0.07],[0.05,0.05]);
%This code creates the topoplots. You need to replace all the non-brain electrodes
% with NaN.
topoplot([diff_ersp_top' NaN NaN NaN],EEG.chanlocs,'maplimits',CLim,'plotrad',0.6,...
'colormap',jet,'plotchans',electrode,'emarker',{'.','k',9,1}); colorbar;
title(['Out-In: ' num2str(itlims(1)) ' to ' num2str(itlims(2)) ' ms'],'FontSize',8);
clear in_ersp_top out_ersp_top diff_ersp_top tflims time_lims itlims
end
% Overall subplot title
supertitle({['ERSP: ' char(trialevent{i_event})]; [num2str(iflims(1)) ' to ' num2str(iflims(2)) ' Hz']},...
'FontSize',12)
clear iflims freq_lims
end
end
clear tlims flims fq_i tl_i i_event
% close all
% eeglab redraw
% .........................................................................
% /////////////////////////////////////////////////////////////////////////
%% ITC Top Plots
% /////////////////////////////////////////////////////////////////////////
% .........................................................................
%A Topoplot needs to collapse across frequency and time so it can show the
% data across electrodes
% .........................................................................
% Set the range of frequency to consider
% flims{1} = [1 3]; % delta
% flims{1} = [4 7]; % theta
% flims{2} = [8 12]; % alpha
% flims{3} = [13 18]; % beta1
% flims{4} = [19 22]; % beta2
% flims{5} = [23 30]; % gamma
flims{1} = [1 3]; % delta
flims{2} = [4 7]; % theta
flims{3} = [8 12]; % alpha
flims{4} = [13 18]; % beta1
% .........................................................................
% Set the range of time to consider
tlims{1} = [0 100];
tlims{2} = [100 200];
tlims{3} = [200 300];
tlims{4} = [300 400];
tlims{5} = [400 500];
% tlims{1} = [0 50];
% tlims{2} = [50 100];
% tlims{3} = [100 150];
% tlims{4} = [150 200];
% tlims{5} = [200 250];
% tlims{6} = [250 300];
% tlims{7} = [300 350];
% tlims{8} = [350 400];
% .........................................................................
CLim = [-0.5 0.5];
% .........................................................................
for i_event = 1:length(trialevent) %loop through events
for fq_i = 1:length(flims) %loop through set of frequencies
iflims = flims{fq_i}; %select each frequency range
%this code finds the frequencies you want from the freqs variable
freq_lims = find(freqs>= iflims(1),1):find(freqs>= iflims(2),1)-1;
if isempty(freq_lims) %in case the upper frequency < iflims(2)
freq_lims = find(freqs>= iflims(1),1):find(freqs>= (iflims(2)-1),1)-1;
end
figure('OuterPosition',[313 537 1515 545]) %new figure for every frequency range
for tl_i = 1:length(tlims) %loop through set of times
itlims = tlims{tl_i}; %select each time range
%this code finds the times you want from the timess variable
time_lims = find(times>= itlims(1),1):find(times>= itlims(2),1)-1;
% .....................................................................
%Here you need a 1D variable, electrodes.
%By default it will take the mean across participants, events, times and frequencies, and show the data for each set
% (participants x conditions x events x electrodes x frequencies x timepoints)
in_itc_top = squeeze(mean(mean(mean(abs(itc(:,1,i_event,electrode,freq_lims,time_lims)),5),6),1));
out_itc_top = squeeze(mean(mean(mean(abs(itc(:,2,i_event,electrode,freq_lims,time_lims)),5),6),1));
diff_itc_top = out_itc_top - in_itc_top;
% .....................................................................
% Creating plots
% Subplot Out Condition
subtightplot(3,length(tlims),tl_i,[0.01,0.01],[0.05,0.07],[0.05,0.05]);
%This code creates the topoplots. You need to replace all the non-brain electrodes
% with NaN.
topoplot([out_itc_top' NaN NaN NaN],EEG.chanlocs,'maplimits',CLim,'plotrad',0.6,...
'colormap',jet,'plotchans',electrode,'emarker',{'.','k',9,1}); colorbar;
title(['Out: ' num2str(itlims(1)) ' to ' num2str(itlims(2)) ' ms'],'FontSize',8);
% Subplot In Condition
subtightplot(3,length(tlims),length(tlims)+tl_i,[0.01,0.01],[0.05,0.07],[0.05,0.05]);
%This code creates the topoplots. You need to replace all the non-brain electrodes
% with NaN.
topoplot([in_itc_top' NaN NaN NaN],EEG.chanlocs,'maplimits',CLim,'plotrad',0.6,...
'colormap',jet,'plotchans',electrode,'emarker',{'.','k',9,1}); colorbar;
title(['In: ' num2str(itlims(1)) ' to ' num2str(itlims(2)) ' ms'],'FontSize',8);
% Subplot Out-In Condition
subtightplot(3,length(tlims),(length(tlims)*2)+tl_i,[0.01,0.01],[0.05,0.07],[0.05,0.05]);
%This code creates the topoplots. You need to replace all the non-brain electrodes
% with NaN.
topoplot([diff_itc_top' NaN NaN NaN],EEG.chanlocs,'maplimits',CLim,'plotrad',0.6,...
'colormap',jet,'plotchans',electrode,'emarker',{'.','k',9,1}); colorbar;
title(['Out-In: ' num2str(itlims(1)) ' to ' num2str(itlims(2)) ' ms'],'FontSize',8);
clear in_ersp_top out_ersp_top diff_ersp_top tflims time_lims itlims
end
% Overall subplot title
supertitle({['ITC: ' char(trialevent{i_event})]; [num2str(iflims(1)) ' to ' num2str(iflims(2)) ' Hz']},...
'FontSize',12)
clear iflims freq_lims
end
end
clear tlims flims fq_i tl_i i_event
% /////////////////////////////////////////////////////////////////////////
%% ERSP plots averaged over subjects and channels: Targets vs Standards
% /////////////////////////////////////////////////////////////////////////
% (participants x conditions x events x electrodes x frequencies x timepoints)
% standards
in_stand_ersp = squeeze(mean(mean(ersp(:,1,1,:,:,:),4),1));
out_stand_ersp = squeeze(mean(mean(ersp(:,2,1,:,:,:),4),1));
% targets
in_targ_ersp = squeeze(mean(mean(ersp(:,1,2,:,:,:),4),1));
out_targ_ersp = squeeze(mean(mean(ersp(:,2,2,:,:,:),4),1));
% targets - standards
in_edif_ersp = in_targ_ersp - in_stand_ersp;
out_edif_ersp = out_targ_ersp - out_stand_ersp;
figure;
CLim = [-1.5 1.5];
colormap('jet')
% Subplot 1: out
subplot(3,1,1);
imagesc(times,freqs,out_edif_ersp,CLim);
title('ERSP: Targets - Standards; Out');
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)');
colorbar
% Subplot 1: in
subplot(3,1,2);
imagesc(times,freqs,in_edif_ersp,CLim);
title('ERSP: Targets - Standards; In');
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)');
colorbar
% Subplot 1: out-in
subplot(3,1,3);
imagesc(times,freqs,out_edif_ersp-in_edif_ersp,CLim);
title('ERSP: Targets - Standards; Out-In');
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)'); xlabel('Time (ms)');
colorbar
% Overall subplot title
% supertitle('ERSP: Targets - Standards','FontSize',12);
clear out_edif_ersp in_edif_ersp in_targ_ersp in_stand_ersp out_targ_ersp out_stand_ersp
% /////////////////////////////////////////////////////////////////////////
%% ITC plots averaged over subjects and channels: Targets vs Standards
% /////////////////////////////////////////////////////////////////////////
% (participants x conditions x events x electrodes x frequencies x timepoints)
% standards
in_stand_itc = squeeze(mean(mean(abs(itc(:,1,1,:,:,:)),4),1));
out_stand_itc = squeeze(mean(mean(abs(itc(:,2,1,:,:,:)),4),1));
% targets
in_targ_itc = squeeze(mean(mean(abs(itc(:,1,2,:,:,:)),4),1));
out_targ_itc = squeeze(mean(mean(abs(itc(:,2,2,:,:,:)),4),1));
% targets - standards
in_edif_itc = in_targ_itc - in_stand_itc;
out_edif_itc = out_targ_itc - out_stand_itc;
CLim = [-.5 .5];
figure;
colormap('jet')
% Subplot 1: out
subplot(3,1,1);
imagesc(times,freqs,out_edif_itc,CLim);
title('ITC: Targets - Standards; Out');
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)');
colorbar
% Subplot 1: in
subplot(3,1,2);
imagesc(times,freqs,in_edif_itc,CLim);
title('ITC: Targets - Standards; In');
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)');
colorbar
% Subplot 1: out-in
subplot(3,1,3);
imagesc(times,freqs,out_edif_itc-in_edif_itc,CLim);
title('ITC: Targets - Standards; Out-In');
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)'); xlabel('Time (ms)');
colorbar
%Overall subplot title
% supertitle('ITC: Overall Average','FontSize',12)
clear out_edif_itc in_edif_itc in_targ_itc in_stand_itc out_targ_itc out_stand_itc
% close all
% eeglab redraw
%%
% #########################################################################
% $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
% #########################################################################
% NORMALIZED PLOTS
% #########################################################################
% $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
% #########################################################################
% .........................................................................
% /////////////////////////////////////////////////////////////////////////
%% Normalized ERSP plots averaged over subjects & channels
% /////////////////////////////////////////////////////////////////////////
% .........................................................................
% ERSP values by condition and event
% (participants x conditions x events x electrodes x frequencies x timepoints)
% Standards
in_stand_ersp_temp = squeeze(mean(mean(ersp(:,1,1,:,:,:),4),1));
out_stand_ersp_temp = squeeze(mean(mean(ersp(:,2,1,:,:,:),4),1));
% Targets
in_targ_ersp_temp = squeeze(mean(mean(ersp(:,1,2,:,:,:),4),1));
out_targ_ersp_temp = squeeze(mean(mean(ersp(:,2,2,:,:,:),4),1));
%Here we are also going to take the difference from the average of the other conditions.
% diffs = [1:2]; diffs(1) = [];
diff_ersp_norm = squeeze(mean(mean(mean(mean(ersp(:,:,:,:,:,:),1),2),3),4));
% .........................................................................
% Create normalized standards and targets separately
% Normalize Standards
out_stand_ersp_norm = squeeze(out_stand_ersp_temp-diff_ersp_norm);
in_stand_ersp_norm = squeeze(in_stand_ersp_temp-diff_ersp_norm);
% Normalize Targets
out_targ_ersp_norm = squeeze(out_targ_ersp_temp-diff_ersp_norm);
in_targ_ersp_norm = squeeze(in_targ_ersp_temp-diff_ersp_norm);
% .........................................................................
% Spectogram plot of normalized data
% .........................................................................
%This variable sets the scale of the color axis, which corresponds to the itc or power values.
CLim = ([-1.5 1.5]);
% .........................................................................
% Standards Figure
figure;
colormap('jet')
% Subplot 1: out
subplot(3,1,1);
imagesc(times,freqs,out_stand_ersp_norm,CLim);
title(['Normalized ERSP: Out; ' char(trialevent{1})]);
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)');
colorbar
% Subplot 2: in
subplot(3,1,2);
imagesc(times,freqs,in_stand_ersp_norm,CLim);
title(['Normalized ERSP: In; ' char(trialevent{1})]);
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)');
colorbar
% Subplot 3: out-in
subplot(3,1,3);
imagesc(times,freqs,out_stand_ersp_norm-in_stand_ersp_norm,CLim);
title(['Normalized ERSP: Out-In; ' char(trialevent{1})]);
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)'); xlabel('Time (ms)');
colorbar
% Targets Figure
figure;
colormap('jet')
% Subplot 1: out
subplot(3,1,1);
imagesc(times,freqs,out_targ_ersp_norm,CLim);
title(['Normalized ERSP: Out; ' char(trialevent{2})]);
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)');
colorbar
% Subplot 2: in
subplot(3,1,2);
imagesc(times,freqs,in_targ_ersp_norm,CLim);
title(['Normalized ERSP: In; ' char(trialevent{2})]);
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)');
colorbar
% Subplot 3: out-in
subplot(3,1,3);
imagesc(times,freqs,out_targ_ersp_norm-in_targ_ersp_norm,CLim);
title(['Normalized ERSP: Out-In; ' char(trialevent{2})]);
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)'); xlabel('Time (ms)');
colorbar
% Targets-Standards Figure
figure;
colormap('jet')
% Subplot 1: out
subplot(3,1,1);
imagesc(times,freqs,out_targ_ersp_norm-out_stand_ersp_norm,CLim);
title('Normalized ERSP: Out; Targets - Standards');
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)');
colorbar
% Subplot 2: in
subplot(3,1,2);
imagesc(times,freqs,in_targ_ersp_norm-in_stand_ersp_norm,CLim);
title('Normalized ERSP: In; Targets - Standards');
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)');
colorbar
% Subplot 3: out-in
subplot(3,1,3);
imagesc(times,freqs,(out_targ_ersp_norm-out_stand_ersp_norm) - (in_targ_ersp_norm-in_stand_ersp_norm),...
CLim);
title('Normalized ERSP: Out-In; Targets - Standards');
set(gca,'Ydir','Normal')
line([0 0],[min(freqs) max(freqs)],'Color','m','LineStyle','--','LineWidth',1.5)
ylabel('Freq (Hz)'); xlabel('Time (ms)');
colorbar
clear diff_ersp_norm out_targ_ersp_norm in_targ_ersp_norm out_stand_ersp_norm...
in_stand_ersp_norm in_stand_ersp_temp out_stand_ersp_temp in_targ_ersp_temp...
out_targ_ersp_temp
% close all
% eeglab redraw
% .........................................................................
% /////////////////////////////////////////////////////////////////////////
%% Normalized ERSP Top Plots
% /////////////////////////////////////////////////////////////////////////
% .........................................................................
%A Topoplot needs to collapse across frequency and time so it can show the
% data across electrodes
% .........................................................................
% Set the range of frequency to consider
flims{1} = [1 3]; % delta
flims{2} = [4 7]; % theta
flims{3} = [8 12]; % alpha
flims{4} = [13 18]; % beta1
flims{5} = [19 22]; % beta2
flims{6} = [23 29]; % beta3
flims{7} = [30 35]; % gamma
% .........................................................................
% Set the range of time to consider
tlims{1} = [-300 -150];
tlims{2} = [-150 0];
tlims{3} = [0 150];
tlims{4} = [150 300];
tlims{5} = [300 450];
tlims{6} = [450 600];
% tlims{1} = [-100 0];
% tlims{2} = [0 100];
% tlims{3} = [100 200];
% tlims{4} = [200 300];
% tlims{5} = [300 400];
% tlims{6} = [400 500];
% tlims{7} = [500 600];
% tlims{8} = [600 700];
% .........................................................................
CLim = [-1.5 1.5];
% .........................................................................
% Create plots
for i_event = 1:length(trialevent) %loop through events
for fq_i = 1:length(flims) %loop through set of frequencies
iflims = flims{fq_i}; %select each frequency range
%this code finds the frequencies you want from the freqs variable
freq_lims = find(freqs>= iflims(1),1):find(freqs>= iflims(2),1)-1;
if isempty(freq_lims) %in case the upper frequency < iflims(2)
freq_lims = find(freqs>= iflims(1),1):find(freqs>= (iflims(2)-1),1)-1;
end
figure('OuterPosition',[311 397 1273 650]) %new figure for every frequency range
for tl_i = 1:length(tlims) %loop through set of times
itlims = tlims{tl_i}; %select each time range
%this code finds the times you want from the timess variable
time_lims = find(times>= itlims(1),1):find(times>= itlims(2),1)-1;
% .....................................................................
% Difference ERSP for normalization
diff_ersp_norm = squeeze(mean(mean(mean(mean(mean(ersp(:,:,:,electrode,freq_lims,...
time_lims),1),2),3),5),6));
% .....................................................................
%By default it will take the mean across participants, events, times and frequencies, and show the data for each set
% (participants x conditions x events x electrodes x frequencies x timepoints)
in_normersp_top = squeeze(squeeze(mean(mean(mean(ersp(:,1,i_event,electrode,freq_lims,...
time_lims),5),6),1)) - diff_ersp_norm);
out_normersp_top = squeeze(squeeze(mean(mean(mean(ersp(:,2,i_event,electrode,freq_lims,...
time_lims),5),6),1)) - diff_ersp_norm);
diff_normersp_top = out_normersp_top - in_normersp_top;
% .....................................................................
% Creating plots
% Subplot Out Condition
subtightplot(3,length(tlims),tl_i,[0.01,0.01],[0.05,0.07],[0.05,0.05]);
% subplot(3,length(tlims),tl_i);
%This code creates the topoplots. You need to replace all the non-brain electrodes
% with NaN.
topoplot([out_normersp_top' NaN NaN NaN],EEG.chanlocs,'maplimits',CLim,'plotrad',0.6,...
'colormap',jet,'plotchans',electrode,'emarker',{'.','k',9,1}); colorbar;
title(['Out: ' num2str(itlims(1)) ' to ' num2str(itlims(2)) ' ms'],'FontSize',8);
% Subplot In Condition
subtightplot(3,length(tlims),length(tlims)+tl_i,[0.01,0.01],[0.05,0.07],[0.05,0.05]);
%This code creates the topoplots. You need to replace all the non-brain electrodes
% with NaN.
topoplot([in_normersp_top' NaN NaN NaN],EEG.chanlocs,'maplimits',CLim,'plotrad',0.6,...
'colormap',jet,'plotchans',electrode,'emarker',{'.','k',9,1}); colorbar;
title(['In: ' num2str(itlims(1)) ' to ' num2str(itlims(2)) ' ms'],'FontSize',8);
% Subplot Out-In Condition
subtightplot(3,length(tlims),(length(tlims)*2)+tl_i,[0.01,0.01],[0.05,0.07],[0.05,0.05]);
%This code creates the topoplots. You need to replace all the non-brain electrodes
% with NaN.
topoplot([diff_normersp_top' NaN NaN NaN],EEG.chanlocs,'maplimits',CLim,'plotrad',0.6,...
'colormap',jet,'plotchans',electrode,'emarker',{'.','k',9,1}); colorbar;
title(['Out-In: ' num2str(itlims(1)) ' to ' num2str(itlims(2)) ' ms'],'FontSize',8);
clear in_normersp_top out_normersp_top diff_normersp_top tflims time_lims itlims...
diff_ersp_norm
end
% Overall subplot title
supertitle({['Normalized ERSP: ' char(trialevent{i_event})]; [num2str(iflims(1)) ' to ' num2str(iflims(2)) ' Hz']},...
'FontSize',12)
clear iflims freq_lims
end
end
clear tlims flims fq_i tl_i i_event
% close all
% eeglab redraw
% /////////////////////////////////////////////////////////////////////////
%% Targets - Standards Normalized ERSP Top Plots
% /////////////////////////////////////////////////////////////////////////
%A Topoplot needs to collapse across frequency and time so it can show the
% data across electrodes
% .........................................................................
% Set the range of frequency to consider
flims{1} = [1 3]; % delta
flims{2} = [4 7]; % theta
flims{3} = [8 12]; % alpha
flims{4} = [13 18]; % beta1
flims{5} = [19 22]; % beta2
flims{6} = [23 29]; % beta3
flims{7} = [30 35]; % gamma
% .........................................................................
% Set the range of time to consider
tlims{1} = [-300 -150];
tlims{2} = [-150 0];
tlims{3} = [0 150];
tlims{4} = [150 300];
tlims{5} = [300 450];
tlims{6} = [450 600];
% tlims{1} = [-100 0];
% tlims{2} = [0 100];
% tlims{3} = [100 200];
% tlims{4} = [200 300];
% tlims{5} = [300 400];
% tlims{6} = [400 500];
% tlims{7} = [500 600];
% tlims{8} = [600 700];
% .........................................................................
CLim = [-1.5 1.5];
% .........................................................................
% Plots
for fq_i = 1:length(flims) %loop through set of frequencies
iflims = flims{fq_i}; %select each frequency range
%this code finds the frequencies you want from the freqs variable
freq_lims = find(freqs>= iflims(1),1):find(freqs>= iflims(2),1)-1;
if isempty(freq_lims) %in case the upper frequency < iflims(2)
freq_lims = find(freqs>= iflims(1),1):find(freqs>= (iflims(2)-1),1)-1;
end
figure('OuterPosition',[311 397 1273 650]) %new figure for every frequency range
for tl_i = 1:length(tlims) %loop through set of times
itlims = tlims{tl_i}; %select each time range
%this code finds the times you want from the timess variable
time_lims = find(times>= itlims(1),1):find(times>= itlims(2),1)-1;
% .....................................................................
%By default it will take the mean across participants, events, times and frequencies, and show the data for each set
% (participants x conditions x events x electrodes x frequencies x timepoints)
% .....................................................................
% Difference ERSP for normalization
diff_ersp_norm = squeeze(mean(mean(mean(mean(mean(ersp(:,:,:,electrode,freq_lims,...
time_lims),1),2),3),5),6));
% .....................................................................
% *Standards*
in_stand_normersp_top = squeeze(squeeze(mean(mean(mean(ersp(:,1,1,electrode,freq_lims,...
time_lims),5),6),1)) - diff_ersp_norm);
out_stand_normersp_top = squeeze(squeeze(mean(mean(mean(ersp(:,2,1,electrode,freq_lims,...
time_lims),5),6),1)) - diff_ersp_norm);
diff_stand_normersp_top = out_stand_normersp_top - in_stand_normersp_top;
% *Targets*
in_targ_normersp_top = squeeze(squeeze(mean(mean(mean(ersp(:,1,2,electrode,freq_lims,...
time_lims),5),6),1)) - diff_ersp_norm);
out_targ_normersp_top = squeeze(squeeze(mean(mean(mean(ersp(:,2,2,electrode,freq_lims,...
time_lims),5),6),1)) - diff_ersp_norm);
diff_targ_normersp_top = out_targ_normersp_top - in_targ_normersp_top;
% .....................................................................
% Creating plots
% Subplot Out Condition
subtightplot(3,length(tlims),tl_i,[0.01,0.01],[0.05,0.07],[0.05,0.05]);
% subplot(3,length(tlims),tl_i);
%This code creates the topoplots. You need to replace all the non-brain electrodes
% with NaN.
topoplot([(out_targ_normersp_top-out_stand_normersp_top)' NaN NaN NaN],...
EEG.chanlocs,'maplimits',CLim,'plotrad',0.6,'colormap',jet,'plotchans',electrode,...
'emarker',{'.','k',9,1}); colorbar;
title(['Out: ' num2str(itlims(1)) ' to ' num2str(itlims(2)) ' ms'],'FontSize',9);
% Subplot In Condition
subtightplot(3,length(tlims),length(tlims)+tl_i,[0.01,0.01],[0.05,0.07],[0.05,0.05]);
%This code creates the topoplots. You need to replace all the non-brain electrodes
% with NaN.
topoplot([(in_targ_normersp_top-in_stand_normersp_top)' NaN NaN NaN],...
EEG.chanlocs,'maplimits',CLim,'plotrad',0.6,'colormap',jet,'plotchans',electrode,...
'emarker',{'.','k',9,1}); colorbar;
title(['In: ' num2str(itlims(1)) ' to ' num2str(itlims(2)) ' ms'],'FontSize',9);
% Subplot Out-In Condition
subtightplot(3,length(tlims),(length(tlims)*2)+tl_i,[0.01,0.01],[0.05,0.07],[0.05,0.05]);
%This code creates the topoplots. You need to replace all the non-brain electrodes
% with NaN.
topoplot([(diff_targ_normersp_top - diff_stand_normersp_top)' NaN NaN NaN],EEG.chanlocs,...
'maplimits',CLim,'plotrad',0.6,'colormap',jet,'plotchans',electrode,...
'emarker',{'.','k',9,1}); colorbar;
title(['Out-In: ' num2str(itlims(1)) ' to ' num2str(itlims(2)) ' ms'],'FontSize',9);
clear tflims time_lims itlims in_targ_normersp_top in_stand_normersp_top...
out_targ_normersp_top out_stand_normersp_top diff_stand_normersp_top...
diff_targ_normersp_top diff_ersp_norm
end
% Overall subplot title
supertitle({'Normalized ERSP: Targets - Standards'; [num2str(iflims(1)) ' to ' num2str(iflims(2)) ' Hz']},...
'FontSize',12)