-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocess_CFC_freq_detect.m
1225 lines (1075 loc) · 46.5 KB
/
process_CFC_freq_detect.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 = process_CFC( varargin )
% PROCESS_CFC: Compute the Cross-Frequency Coupling between or within time
% series.
%
% DOCUMENTATION: For more detail, please see the tutorial in https://bsp.hackpad.com/Cross-Frequency-Coupling-cChe95lhDHz
% @=============================================================================
% This software is part of the Brainstorm software:
% http://neuroimage.usc.edu/brainstorm
%
% Copyright (c)2000-2014 University of Southern California & McGill University
% This software is distributed under the terms of the GNU General Public License
% as published by the Free Software Foundation. Further details on the GPL
% license can be found at http://www.gnu.org/copyleft/gpl.html.
%
% FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE
% UNIVERSITY OF SOUTHERN CALIFORNIA AND ITS COLLABORATORS DO NOT MAKE ANY
% WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, NOR DO THEY ASSUME ANY
% LIABILITY OR RESPONSIBILITY FOR THE USE OF THIS SOFTWARE.
%
% For more information type "brainstorm license" at command prompt.
% =============================================================================@
%
% Authors: Hui-Ling Chan 2015
macro_methodcall;
end
%% ===== GET DESCRIPTION =====
function sProcess = GetDescription() %#ok<DEFNU>
% Description the process
sProcess.Comment = 'Cross-Frequency Coupling';
sProcess.FileTag = '';
sProcess.Category = 'Custom';
sProcess.SubGroup = 'Frequency';
sProcess.Index = 660;
% Definition of the input accepted by this process
sProcess.InputTypes = {'raw', 'data', 'results', 'matrix'};
sProcess.OutputTypes = {'timefreq', 'timefreq', 'timefreq', 'timefreq'};
sProcess.nInputs = 1;
sProcess.nMinFiles = 1;
% ==== INPUT ====
sProcess.options.label_in.Comment = '<HTML><B><U>Input options</U></B>:';
sProcess.options.label_in.Type = 'label';
sProcess.options.result_comm.Comment = 'Comment: ';
sProcess.options.result_comm.Type = 'text';
sProcess.options.result_comm.Value = '';
% === TIME WINDOW
sProcess.options.timewindow.Comment = 'Time window:';
sProcess.options.timewindow.Type = 'timewindow';
sProcess.options.timewindow.Value = [];
% === SENSOR SELECTION
sProcess.options.target_data.Comment = 'Sensor types or names (empty=all): ';
sProcess.options.target_data.Type = 'text';
sProcess.options.target_data.Value = 'MEG, EEG';
sProcess.options.target_data.InputTypes = {'data', 'raw'};
% === SCOUTS SELECTION
sProcess.options.scouts.Comment = 'Use scouts';
sProcess.options.scouts.Type = 'scout_confirm';
sProcess.options.scouts.Value = {};
sProcess.options.scouts.InputTypes = {'results'};
% === SCOUT FUNCTION ===
sProcess.options.scoutfunc.Comment = {'Mean', 'Max', 'PCA', 'Std', 'All', 'Scout function:'};
sProcess.options.scoutfunc.Type = 'radio_line';
sProcess.options.scoutfunc.Value = 1;
sProcess.options.scoutfunc.InputTypes = {'results'};
% === SCOUT TIME ===
sProcess.options.scouttime.Comment = {'Before', 'After', 'When to apply the scout function:'};
sProcess.options.scouttime.Type = 'radio_line';
sProcess.options.scouttime.Value = 1;
sProcess.options.scouttime.InputTypes = {'results'};
% === ROW NAMES
sProcess.options.target_tf.Comment = 'Row names or indices (empty=all): ';
sProcess.options.target_tf.Type = 'text';
sProcess.options.target_tf.Value = '';
sProcess.options.target_tf.InputTypes = {'timefreq', 'matrix'};
% ==== ESTIMATOR ====
sProcess.options.label_pac.Comment = '<HTML><BR><B><U>Estimator options</U></B>:';
sProcess.options.label_pac.Type = 'label';
% === PAC MEASURES ===
sProcess.options.pacmeasure.Comment = {'AEC', 'ESC', 'MI', 'CFC measure:'};
sProcess.options.pacmeasure.Type = 'radio_line';
sProcess.options.pacmeasure.Value = 1;
% === TIME LAGGED
sProcess.options.tlag.Comment = 'Time-lagged';
sProcess.options.tlag.Type = 'checkbox';
sProcess.options.tlag.Value = 1;
% === NESTING FREQ
sProcess.options.nesting.Comment = 'Nesting frequency band (low):';
sProcess.options.nesting.Type = 'range';
sProcess.options.nesting.Value = {[2, 30], 'Hz', 2};
% === FREQ
sProcess.options.nestingwidth.Comment = 'Nesting frequency step (low):';
sProcess.options.nestingwidth.Type = 'value';
sProcess.options.nestingwidth.Value = {1, 'Hz', 2};
% === NESTED FREQ
sProcess.options.nested.Comment = 'Nested frequency band (high):';
sProcess.options.nested.Type = 'range';
sProcess.options.nested.Value = {[40, 150], 'Hz', 2};
% === FREQ
sProcess.options.nestedwidth.Comment = 'Nested frequency step (high):';
sProcess.options.nestedwidth.Type = 'value';
sProcess.options.nestedwidth.Value = {10, 'Hz', 2};
% % === TF METHOD ===
% sProcess.options.tfmethod.Comment = {'Hilbert', 'Wavelet', 'STFT', 'TF method:'};
% sProcess.options.tfmethod.Type = 'radio_line';
% sProcess.options.tfmethod.Value = 1;
% width - width of the wavelet filter; default = 7
% nfft - the number of points in fft; default = 200
% num_shf - the number of shuffled data sets to use during significance
% testing; default = 50
% alpha - significance value to use; default = 0.05
% % ==== ESTIMATOR ====
% sProcess.options.label_ad.Comment = '<HTML><BR><B><U>Advanced options</U></B>:';
% sProcess.options.label_ad.Type = 'label';
% % === width of the wavelet filter
% sProcess.options.width.Comment = 'Width of the wavelet filter (cycles):';
% sProcess.options.width.Type = 'value';
% sProcess.options.width.Value = {7, '', 0};
% === the number of points in fft
% sProcess.options.nfft.Comment = 'Number of points in fft:';%only for coherence computation
% sProcess.options.nfft.Type = 'value';
% sProcess.options.nfft.Value = {200, '', 0};
% % === the number of shuffled data sets to use during significance
% sProcess.options.num_shf.Comment = 'Number of shuffled data sets:';
% sProcess.options.num_shf.Type = 'value';
% sProcess.options.num_shf.Value = {50, '', 0};
% % === alpha
% sProcess.options.alpha.Comment = 'Significance value to use:';
% sProcess.options.alpha.Type = 'value';
% sProcess.options.alpha.Value = {0.05, '', 3};
% === Width
% sProcess.options.width.Comment = 'Cycles of wavelet:';
% sProcess.options.width.Type = 'value';
% sProcess.options.width.Value = {7, 'Cycles', 0};
% % === WINDOW LENGTH
% sProcess.options.winlength.Comment = 'Estimator window length: ';
% sProcess.options.winlength.Type = 'value';
% sProcess.options.winlength.Value = {0.128, 'ms ', 1};
% % === Low bound
% sProcess.options.winoverlap.Comment = 'Overlap percentage: ';
% sProcess.options.winoverlap.Type = 'value';
% sProcess.options.winoverlap.Value = {0.75, '% ', 1};
% ==== OUTPUT ====
sProcess.options.label_out.Comment = '<HTML><BR><U><B>Output configuration</B></U>:';
sProcess.options.label_out.Type = 'label';
% === SCOUT FUNCTION ===
sProcess.options.pacmatrix.Comment = {'Auto', 'Cross', 'Both', 'Output:'};
sProcess.options.pacmatrix.Type = 'radio_line';
sProcess.options.pacmatrix.Value = 1;
% === AVERAGE OUTPUT FILES
sProcess.options.avgoutput.Comment = 'Save average CFC across trials (one output file only)';
sProcess.options.avgoutput.Type = 'checkbox';
sProcess.options.avgoutput.Value = 1;
% === SAVE PAC MAPS
sProcess.options.savemax.Comment = 'Save only the maximum CFC values';
sProcess.options.savemax.Type = 'checkbox';
sProcess.options.savemax.Value = 0;
end
%% ===== FORMAT COMMENT =====
function Comment = FormatComment(sProcess) %#ok<DEFNU>
Comment = sProcess.Comment;
end
%% ===== RUN =====
function OutputFiles = Run(sProcess, sInputA) %#ok<DEFNU>
OutputFiles = {};
% ===== GET OPTIONS =====
if isfield(sProcess.options, 'timewindow') && isfield(sProcess.options.timewindow, 'Value') && iscell(sProcess.options.timewindow.Value) && ~isempty(sProcess.options.timewindow.Value)
OPTIONS.TimeWindow = sProcess.options.timewindow.Value{1};
else
OPTIONS.TimeWindow = [];
end
result_comment = sProcess.options.result_comm.Value;
if ~isempty(result_comment)
result_comment = [result_comment ': '];
end
% Get and check frequencies
OPTIONS.BandNesting = sProcess.options.nesting.Value{1};
OPTIONS.BandNested = sProcess.options.nested.Value{1};
if (min(OPTIONS.BandNesting) < 0.5)
bst_report('Error', sProcess, [], 'This function cannot be used to estimate PAC for nesting frequencies below 1Hz.');
return;
end
% if (max(OPTIONS.BandNesting) > min(OPTIONS.BandNested))
% bst_report('Error', sProcess, [], 'The low and high frequency band cannot overlap.');
% return;
% end
% Get target
if ismember(sInputA(1).FileType, {'data','raw'}) && isfield(sProcess.options, 'target_data') && ~isempty(sProcess.options.target_data.Value)
OPTIONS.Target = sProcess.options.target_data.Value;
% elseif strcmpi(sInputA(1).FileType, 'results') && isfield(sProcess.options, 'scouts') && ~isempty(sProcess.options.scouts.Value)
% OPTIONS.Target = sProcess.options.scouts.Value{2};
elseif ismember(sInputA(1).FileType, {'timefreq', 'matrix'}) && isfield(sProcess.options, 'target_tf') && ~isempty(sProcess.options.target_tf.Value)
OPTIONS.Target = sProcess.options.target_tf.Value;
else
OPTIONS.Target = [];
end
% All other options
%OPTIONS.MaxSignals = sProcess.options.max_block_size.Value{1};
%OPTIONS.isParallel = sProcess.options.parallel.Value && (exist('matlabpool', 'file') ~= 0);
%OPTIONS.isMex = sProcess.options.ismex.Value;
OPTIONS.isSaveMax = sProcess.options.savemax.Value;
OPTIONS.isTimeLag = sProcess.options.tlag.Value;
OPTIONS.isAvgOutput = sProcess.options.avgoutput.Value;
if (length(sInputA) == 1)
OPTIONS.isAvgOutput = 0;
end
switch (sProcess.options.pacmatrix.Value)
case 1, OPTIONS.PACmatrix = 'auto';
case 2, OPTIONS.PACmatrix = 'cross';
case 3, OPTIONS.PACmatrix = 'both';
end
switch (sProcess.options.pacmeasure.Value)
case 1, OPTIONS.PACmeasure = 'aec';
case 2, OPTIONS.PACmeasure = 'esc';
%case 3, OPTIONS.PACmeasure = 'epc';
case 3, OPTIONS.PACmeasure = 'mi';
end
sProcess.options.tfmethod.Value = 1;
switch (sProcess.options.tfmethod.Value)
case 1, OPTIONS.TFmethod = 'hilbert';
case 2, OPTIONS.TFmethod = 'wavelet';
case 3, OPTIONS.TFmethod = 'stft';
end
OPTIONS.nfft = 256;
OPTIONS.Width = 7;
% OPTIONS.width = sProcess.options.width.Value{1};
% OPTIONS.nfft = sProcess.options.nfft.Value{1};
% OPTIONS.WinOverlap = sProcess.options.winoverlap.Value{1};
% OPTIONS.SegmentLength = sProcess.options.winlength.Value{1};
% OPTIONS.Width = sProcess.options.width.Value{1};
% OPTIONS.num_shf = sProcess.options.num_shf.Value{1};
% OPTIONS.alpha = sProcess.options.alpha.Value{1};
% ===== GET SCOUTS OPTIONS =====
if strcmpi(sInputA(1).FileType, 'results') && isfield(sProcess.options, 'scouts') && isfield(sProcess.options.scouts, 'Value')
% Selected scouts
sScouts = sProcess.options.scouts.Value{2};
% Override scouts function
switch (sProcess.options.scoutfunc.Value)
case 1, OPTIONS.ScoutFunc = 'mean';
case 2, OPTIONS.ScoutFunc = 'max';
case 3, OPTIONS.ScoutFunc = 'pca';
case 4, OPTIONS.ScoutFunc = 'std';
case 5, OPTIONS.ScoutFunc = 'all';
end
% Scout function order
switch (sProcess.options.scouttime.Value)
case 1, OPTIONS.ScoutTime = 'before';
case 2, OPTIONS.ScoutTime = 'after';
end
% Perform some checks
%if strcmpi(OPTIONS.ScoutTime, 'before') && ismember(OPTIONS.ScoutFunc, {'max', 'std'})
% bst_report('Error', sProcess, [], 'Scout functions MAX and STD should not be applied before estimating the PAC.');
% return;
%end
if strcmpi(OPTIONS.ScoutTime, 'after') && strcmpi(OPTIONS.ScoutFunc, 'pca')
bst_report('Error', sProcess, [], 'Scout function PCA cannot be applied after estimating the PAC.');
return;
end
% % Set input/output scouts functions
% if ~isempty(sScouts)
% OPTIONS.Target = sScouts;
% % Apply function before: get all the scouts time series in advance
% if strcmpi(OPTIONS.ScoutTime, 'before')
% [OPTIONS.Target.Function] = deal(OPTIONS.ScoutFunc);
% % Apply function after: Get all the time series of all the scouts
% elseif strcmpi(OPTIONS.ScoutTime, 'after')
% [OPTIONS.Target.Function] = deal('All');
% end
% end
% Selected scouts
AtlasList = sProcess.options.scouts.Value;
% Set input/output scouts functions
if ~isempty(AtlasList)
OPTIONS.Target = AtlasList;
% Apply function before: get all the scouts time series in advance
if strcmpi(OPTIONS.ScoutTime, 'before')
LoadOptions.TargetFunc = OPTIONS.ScoutFunc;
% Apply function after: Get all the time series of all the scouts
elseif strcmpi(OPTIONS.ScoutTime, 'after')
LoadOptions.TargetFunc = 'all';
end
end
end
% ===== INITIALIZE =====
% Initialize output variables
DirectPAC_avg = [];
LowFreqs = [];
HighFreqs = [];
nAvg = 0;
% Initialize progress bar
if bst_progress('isVisible')
startValue = bst_progress('get');
else
startValue = 0;
end
% Options for LoadInputFile()
if strcmpi(sInputA(1).FileType, 'results')
LoadOptions.LoadFull = 0; % Load kernel-based results as kernel+data
else
LoadOptions.LoadFull = 1; % Load the full file
end
LoadOptions.IgnoreBad = 1; % From raw files: ignore the bad segments
LoadOptions.ProcessName = func2str(sProcess.Function);
BandNesting_vec = OPTIONS.BandNesting(1):sProcess.options.nestingwidth.Value{1}:OPTIONS.BandNesting(2);
BandNested_vec = OPTIONS.BandNested(1):sProcess.options.nestedwidth.Value{1}:OPTIONS.BandNested(2);
% Loop over input files
for iFile = 1:length(sInputA)
% ===== LOAD SIGNALS =====
bst_progress('text', sprintf('PAC: Loading input file (%d/%d)...', iFile, length(sInputA)));
bst_progress('set', round(startValue + (iFile-1) / length(sInputA) * 100));
% Load input signals
[sInput, nSignals, iRows] = bst_process('LoadInputFile', sInputA(iFile).FileName, OPTIONS.Target, OPTIONS.TimeWindow, LoadOptions);
if isempty(sInput) || isempty(sInput.Data)
return;
end
% Get sampling frequency
sRate = 1 / (sInput.Time(2) - sInput.Time(1));
% Check the nested frequencies
if (OPTIONS.BandNested(2) > sRate/3)
% Warning
strMsg = sprintf('Higher nesting frequency is too high (%d Hz) compared with sampling frequency (%d Hz): Limiting to %d Hz', round(OPTIONS.BandNested(2)), round(sRate), round(sRate/3));
disp([10 'process_pac> ' strMsg]);
bst_report('Warning', 'process_pac', [], strMsg);
% Fix higher frequencyy
OPTIONS.BandNested(2) = sRate/3;
end
% Check the extent of bandNested band
if (OPTIONS.BandNested(2) <= OPTIONS.BandNested(1))
bst_report('Error', 'process_pac', [], sprintf('Invalid frequency range: %d-%d Hz', round(OPTIONS.BandNested(1)), round(OPTIONS.BandNested(2))),'n');
continue;
end
% Definitions
% bandNesting = OPTIONS.BandNesting;
% bandNested = OPTIONS.BandNested;
% fmin = min(bandNesting);
% fmax = sRate/3;
% numfreqs = round(sRate/9);
% fstep = sProcess.options.nestingwidth.Value{1};
% % Calculate center frequencies
% temp1 = (0:numfreqs-1) * fstep;
% temp2 = logspace(log10(fmin), log10(fmax), numfreqs);
% temp2 = (temp2-temp2(1)) * ((temp2(end)-temp1(end)) / temp2(end)) + temp2(1);
% chirpCenterFreqs = temp1 + temp2;
% % Remove unused frequencies
% chirpCenterFreqs(chirpCenterFreqs > max(bandNested)) = []; %%% ESTHER
% chirpCenterFreqs((chirpCenterFreqs < min(bandNested)) & (chirpCenterFreqs >= max(bandNesting))) = []; %%% ESTHER
% % % % Indices of center frequencies in the upper frequency range
% % % hfreq = find( chirpCenterFreqs >= min(bandNested) );
% % % % Number of cf bins to evaluate for PAC with lower-frequency oscillations
% % % % lfreq = find(chirpCenterFreqs < min(bandNested));
% % % lfreq = find(chirpCenterFreqs < max(bandNesting)); %%% ESTHER
% BandNested_vec = chirpCenterFreqs(chirpCenterFreqs >= min(bandNested) );
% BandNesting_vec = chirpCenterFreqs(chirpCenterFreqs < max(bandNesting) );
% ===== COMPUTE PAC MEASURE =====
% Number of blocks of signals
%MAX_BLOCK_SIZE = 1;%OPTIONS.MaxSignals;
%nBlocks = ceil(nSignals / MAX_BLOCK_SIZE);
DirectPAC = [];
% Display processing time
%disp(sprintf('BST> PAC: Processing %d blocks of %d signals each.', nBlocks, MAX_BLOCK_SIZE));
% Process each block of signals
%for iBlock = 1:nBlocks
tic
% bst_progress('text', sprintf('PAC: File %d/%d - Block %d/%d', iFile, length(sInputA), iBlock, nBlocks));
% bst_progress('set', round(startValue + (iFile-1)/length(sInputA)*100 + iBlock/nBlocks*100));
% % Indices of the signals
% iSignals = (iBlock-1)*MAX_BLOCK_SIZE+1 : min(iBlock*MAX_BLOCK_SIZE, nSignals);
% Get signals to process
if ~isempty(sInput.ImagingKernel)
Fblock = sInput.ImagingKernel * sInput.Data;
else
Fblock = sInput.Data;
end
% ===== APPLY SOURCE ORIENTATION =====
if strcmpi(sInput.DataType, 'results')
% Number of values per vertex
switch (sInput.nComponents)
case 0
error('PAC metrics are not supported for mixed source models.');
case 1
% Nothing to do
case 2
Fblock = (Fblock(1:2:end,:) + Fblock(2:2:end,:)) / 2;
sInput.RowNames = sInput.RowNames(1:2:end);
case 3
Fblock = (Fblock(1:3:end,:) + Fblock(2:3:end,:) + Fblock(3:3:end,:)) / 3;
sInput.RowNames = sInput.RowNames(1:3:end);
end
end
%[DirectPAC_block, LowFreqs, HighFreqs] = bst_pac(Fblock, sRate, OPTIONS.BandNesting, OPTIONS.BandNested, OPTIONS.isParallel, OPTIONS.isMex);
% [pacmat, freqvec_ph, freqvec_amp] = find_pac_shf (sig_pac, Fs, measure, ...
% sig_mod, ph_freq_vec, amp_freq_vec, plt, waitbar, width, nfft, num_shf, alpha,...
% dataname, sig_pac_name, sig_mod_name)
n=0;
for iSigX = 1:nSignals
for iSigY = 1:nSignals
if iSigX == iSigY && strcmpi(OPTIONS.PACmatrix, 'cross')
continue;
end
if iSigX ~= iSigY && strcmpi(OPTIONS.PACmatrix, 'auto')
continue;
end
% OPTIONS.num_shf = 0;
% OPTIONS.alpha = 1;
% [pacmat, LowFreqs, HighFreqs] = find_pac_shf(Fblock(iSigY,:), sRate, OPTIONS.PACmeasure, Fblock(iSigX,:), BandNesting_vec,BandNested_vec,'n',1,OPTIONS.Width, OPTIONS.nfft, OPTIONS.num_shf, OPTIONS.alpha);
[pacmat, LowFreqs, HighFreqs] = find_pac_var_bandwidth(Fblock(iSigY,:), sRate, OPTIONS.PACmeasure, Fblock(iSigX,:), BandNesting_vec, BandNested_vec, OPTIONS.Width, OPTIONS.nfft, OPTIONS.TFmethod, OPTIONS.isTimeLag);
if isempty(DirectPAC)
RowNames = sInput.RowNames;
if strcmpi(OPTIONS.PACmatrix, 'cross')
sInput.RowNames = cell((nSignals^2)-nSignals,1);
DirectPAC = zeros((nSignals^2)-nSignals,1,size(pacmat,2),size(pacmat,1));
elseif strcmpi(OPTIONS.PACmatrix, 'auto')
DirectPAC = zeros(nSignals,1,size(pacmat,2),size(pacmat,1));
elseif strcmpi(OPTIONS.PACmatrix, 'both')
sInput.RowNames = cell(length(RowNames)^2,1);
DirectPAC = zeros(nSignals*nSignals,1,size(pacmat,2),size(pacmat,1));
end
end
n=n+1;
DirectPAC(n,1,:,:) = pacmat';
if ~strcmpi(OPTIONS.PACmatrix, 'auto')
sInput.RowNames{n,1}=[RowNames{iSigX} '-' RowNames{iSigY}];
end
end
end
LowFreqs = LowFreqs';
HighFreqs = HighFreqs';
% ===== APPLY SOURCE ORIENTATION =====
% Unconstrained sources => SUM for each point
if ismember(sInput.DataType, {'results','scout','matrix'}) && ~isempty(sInput.nComponents) && (sInput.nComponents ~= 1)
[DirectPAC, sInput.GridAtlas, sInput.RowNames] = bst_source_orient([], sInput.nComponents, sInput.GridAtlas, DirectPAC, 'mean', sInput.DataType, sInput.RowNames);
end
% ===== PROCESS SCOUTS =====
% Get scouts
isScout = ~isempty(OPTIONS.Target) && (isstruct(OPTIONS.Target) || iscell(OPTIONS.Target)) && isfield(sInput, 'Atlas') && isfield(sInput.Atlas, 'Scouts') && ~isempty(sInput.Atlas.Scouts);
if isScout
sScouts = sInput.Atlas.Scouts;
end
% If the scout function has to be applied AFTER the PAC computation
if isScout && strcmpi(OPTIONS.ScoutTime, 'after') && ~strcmpi(OPTIONS.ScoutFunc, 'all')
nScouts = length(sScouts);
DirectPAC_scouts = zeros(nScouts, size(DirectPAC,2), size(DirectPAC,3), size(DirectPAC,4));
iVerticesAll = [1, cumsum(cellfun(@length, {sScouts.Vertices})) + 1];
% For each unique row name: compute a measure over the clusters values
for iScout = 1:nScouts
iScoutVert = iVerticesAll(iScout):iVerticesAll(iScout+1)-1;
F = reshape(DirectPAC(iScoutVert,:,:,:), length(iScoutVert), []);
F = bst_scout_value(F, OPTIONS.ScoutFunc);
DirectPAC_scouts(iScout,:,:,:) = reshape(F, [1, size(DirectPAC,2), size(DirectPAC,3), size(DirectPAC,4)]);
end
% Save only the requested rows
sInput.RowNames = {sScouts.Label};
DirectPAC = DirectPAC_scouts;
end
% ===== FILE COMMENT =====
% Base comment
Comment = result_comment;
if OPTIONS.isTimeLag
Comment = [Comment 'Lagged'];
end
if OPTIONS.isSaveMax
Comment = [Comment 'Max' upper(OPTIONS.PACmeasure) ];
else
Comment = [Comment upper(OPTIONS.PACmeasure) ];
end
% Time window (RAW only)
if ~isempty(strfind(sInputA(iFile).Condition, '@raw'))
Comment = [Comment, sprintf('(%ds-%ds)', round(OPTIONS.TimeWindow))];
end
% Scouts
if isScout && (length(sScouts) < 6)
Comment = [Comment, ':'];
for is = 1:length(sScouts)
Comment = [Comment, ' ', sScouts(is).Label];
end
Comment = [Comment, ', ', OPTIONS.ScoutFunc];
if ~strcmpi(OPTIONS.ScoutFunc, 'All')
Comment = [Comment, ' ' OPTIONS.ScoutTime];
end
% Single input
elseif (length(sInput.RowNames) == 1)
if iscell(sInput.RowNames)
Comment = [Comment, ': ' sInput.RowNames{1}];
else
Comment = [Comment, ': #', num2str(sInput.RowNames(1))];
end
end
% ===== SAVE FILE / COMPUTE AVERAGE =====
% Save each as an independent file
if ~OPTIONS.isAvgOutput
nAvg = 1;
OutputFiles{end+1} = SaveFile(DirectPAC, LowFreqs, HighFreqs, nAvg, sInput.iStudy, sInputA(iFile).FileName, sInput, Comment, OPTIONS);
else
% Compute online average of the connectivity matrices
if isempty(DirectPAC_avg)
DirectPAC_avg = DirectPAC ./ length(sInputA);
else
DirectPAC_avg = DirectPAC_avg + DirectPAC ./ length(sInputA);
end
nAvg = nAvg + 1;
end
end
% ===== SAVE AVERAGE =====
if OPTIONS.isAvgOutput
% Output study, in case of average
[tmp, iOutputStudy] = bst_process('GetOutputStudy', sProcess, sInputA);
% Save file
OutputFiles{1} = SaveFile(DirectPAC_avg, LowFreqs, HighFreqs, nAvg, iOutputStudy, [], sInput, Comment, OPTIONS);
end
end
%% ========================================================================
% ===== SUPPORT FUNCTIONS ================================================
% ========================================================================
%% ===== SAVE FILE =====
function NewFile = SaveFile(DirectPAC, LowFreqs, HighFreqs, nAvg, iOuptutStudy, DataFile, sInput, Comment, OPTIONS)
% ===== COMPUTE MAXPAC ======
% Save directPAC values in returned structure only if requested
if OPTIONS.isSaveMax
sPAC.DirectPAC = [];
else
sPAC.DirectPAC = DirectPAC;
end
% Get the maximum DirectPAC value for each signal
[sPAC.ValPAC, indmax] = max(reshape(DirectPAC, size(DirectPAC,1), []), [], 2);
% Find the pair of low/high frequencies for this maximum
[imaxl, imaxh] = ind2sub([size(DirectPAC,3), size(DirectPAC,4)], indmax);
sPAC.NestingFreq = LowFreqs(imaxl)';
sPAC.NestedFreq = HighFreqs(imaxh)';
% Copy list of frequencies
sPAC.LowFreqs = LowFreqs;
sPAC.HighFreqs = HighFreqs;
% ===== PREPARE OUTPUT STRUCTURE =====
% Create file structure
FileMat = db_template('timefreqmat');
FileMat.TF = sPAC.ValPAC;
FileMat.Comment = Comment;
FileMat.DataType = sInput.DataType;
FileMat.RowNames = sInput.RowNames;
FileMat.Time = sInput.Time([1,end]);
FileMat.Method = 'pac';
FileMat.Measure = 'maxpac';
FileMat.DataFile = file_win2unix(DataFile);
FileMat.nAvg = nAvg;
FileMat.Freqs = 0;
% Atlas
if ~isempty(sInput.Atlas)
FileMat.Atlas = sInput.Atlas;
end
if ~isempty(sInput.GridLoc)
FileMat.GridLoc = sInput.GridLoc;
end
if ~isempty(sInput.GridAtlas)
FileMat.GridAtlas = sInput.GridAtlas;
end
if ~isempty(sInput.SurfaceFile)
FileMat.SurfaceFile = sInput.SurfaceFile;
end
% History: Computation
FileMat = bst_history('add', FileMat, 'compute', 'PAC measure (see the field "Options" for input parameters)');
% All the PAC fields and options
FileMat.Options = OPTIONS;
FileMat.sPAC = rmfield(sPAC, 'ValPAC');
% ===== SAVE FILE =====
% Get output study
sOutputStudy = bst_get('Study', iOuptutStudy);
% File tag
if OPTIONS.isSaveMax
fileTag = 'timefreq_pac';
else
fileTag = 'timefreq_pac_fullmaps';
end
% Output filename
NewFile = bst_process('GetNewFilename', bst_fileparts(sOutputStudy.FileName), fileTag);
% Save file
bst_save(NewFile, FileMat, 'v6');
% Add file to database structure
db_add_data(iOuptutStudy, NewFile, FileMat);
end
function [pacmat, freqvec_ph, freqvec_amp] = find_pac_var_bandwidth(sig_pac, Fs, measure, ...
sig_mod, ph_freq_vec, amp_freq_vec, width, nfft, TFmethod, isTLag, num_shf, alpha)
% Checks of input variables %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargin < 11
num_shf=0;
end
if nargin < 12
alpha=0.05;
end
% Check data is columnwise
if size(sig_pac,1)<size(sig_pac,2)
sig_pac = sig_pac';
end
if size(sig_mod,1)<size(sig_mod,2)
sig_mod = sig_mod';
end
if (size(sig_pac,2) ~= size(sig_mod,2))
sprintf('Error - Signals must have the same number of trials')
return
end
total_num_dp = size(sig_pac,1);
num_trials = 1:size(sig_pac,2);
phfreq_low = min(ph_freq_vec);
phfreq_high = max(ph_freq_vec);
phfreq_bw = diff(ph_freq_vec(1:2));
ampfreq_low = min(amp_freq_vec);
ampfreq_high = max(amp_freq_vec);
ampfreq_bw = diff(amp_freq_vec(1:2));
xbins = ceil((phfreq_high - phfreq_low)/phfreq_bw);
ybins = ceil((ampfreq_high - ampfreq_low)/ampfreq_bw);
alpha = alpha/(xbins*ybins); % Uncomment to use Bonferonni Correction
freqvec_amp = zeros(1,ybins);
freqvec_ph = zeros(1,xbins);
amp_filt_signals = zeros(floor(total_num_dp*0.8),max(num_trials));
ph_filt_signals = zeros(floor(total_num_dp*0.8),max(num_trials));
for y=1:ybins
freqvec_amp(y) = ampfreq_low+(y-1)*ampfreq_bw;
end
for x=1:xbins
freqvec_ph(x) = phfreq_low+(x-1)*phfreq_bw;
end
counter = 0;
maxlag = 0;
countermax = xbins*ybins;
%fprintf('\nCalculating CFC values\n');
if (strcmp(measure, 'esc') || strcmp(measure, 'mi') || strcmp(measure, 'epc') || strcmp(measure, 'aec') )
pacmat = zeros(ybins,xbins);
for x=1:xbins
BandBoundsX(1) = max(0,freqvec_ph(x) - 0.5 * phfreq_bw);
BandBoundsX(2) = BandBoundsX(1) + phfreq_bw;
if strcmp(measure, 'esc')
for i3=1:max(num_trials)
ph_filt_signals(:,i3) = bp_vec(sig_mod(:,i3)', BandBoundsX, Fs, width, TFmethod)';
end
elseif strcmp(measure, 'aec')
for i3=1:max(num_trials)
ph_filt_signals(:,i3) = amp_vec(sig_mod(:,i3)', BandBoundsX, Fs, width, TFmethod)';
end
else
for i3=1:max(num_trials)
ph_filt_signals(:,i3) = ph_vec(sig_mod(:,i3)', BandBoundsX, Fs, width, TFmethod)';
end
end
for s = 1:num_shf
shuffled_sig_ph{s} = shuffle_esc(ph_filt_signals);
end
for y=1:ybins
%BandBoundsY(1) = max(0,freqvec_amp(y) - freqvec_ph(x) - phfreq_bw - 1);
BandBoundsY(1) = max(0,freqvec_amp(y) - 0.5*ampfreq_bw);
if BandBoundsY(1) > freqvec_ph(x)
%BandBoundsY(2) = freqvec_amp(y) + freqvec_ph(x) + phfreq_bw + 1;
BandBoundsY(2) = freqvec_amp(y) + ampfreq_bw;
for i3=1:max(num_trials)
amp_filt_signals(:,i3) = amp_vec(sig_pac(:,i3)', BandBoundsY, Fs, width, TFmethod)';
end
if strcmp(measure, 'esc') || strcmp(measure, 'epc')
if isTLag == 1
maxlag = min(floor(Fs/freqvec_ph(x)/2),floor(total_num_dp*0.8)/4);
end
%pacmat(y,x) = esc_measure(ph_filt_signals, amp_filt_signals, 'y');
pacmat(y,x) = lagged_corr(ph_filt_signals,amp_filt_signals,maxlag);
for s = 1:num_shf
shf_pacmat_final(s,y,x) = lagged_corr(shuffled_sig_ph{s}, amp_filt_signals, maxlag);
end
else
pacmat(y,x) = mi_measure(ph_filt_signals, amp_filt_signals);
for s = 1:num_shf
shf_pacmat_final(s,y,x) = mi_measure(shuffled_sig_ph{s}, amp_filt_signals);
end
end
end
counter = counter+1;
% if counter == 1
% fprintf('%03i%% ', floor((counter/countermax)*100));
% else
% fprintf('\b\b\b\b\b%03i%% ', floor((counter/countermax)*100));
% end
% if counter == countermax
% fprintf('\n');
% end
end
end
end
%Find mean and standard deviation of shuffled data sets
if num_shf ~= 0
if strcmp(measure, 'mi')
for i =1:ybins
for j=1:xbins
[shf_data_mean(i,j), shf_data_std(i,j)] = normfit(shf_pacmat_final(:,i,j));
end
end
else
shf_data_mean = squeeze (mean (shf_pacmat_final, 1));
shf_data_std = squeeze (std (shf_pacmat_final, 1));
end
end
% Compute significance %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if num_shf ~= 0
for i = 1:size(pacmat,1)
for j = 1:size(pacmat,2)
[h, p] = my_sig_test(pacmat(i,j), squeeze(shf_pacmat_final(:,i,j)), alpha);
if h == 0
pacmat(i,j) = 0;
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
freqvec_amp = ceil(freqvec_amp)';
freqvec_ph = ceil(freqvec_ph)';
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [pacmat, freqvec_ph, freqvec_amp] = find_pac(sig_pac, Fs, measure, ...
sig_mod, ph_freq_vec, amp_freq_vec, width, nfft, TFmethod, num_shf, alpha)
% Checks of input variables %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargin < 10
num_shf=50;
end
if nargin < 11
alpha=0.05;
end
% Check data is columnwise
if size(sig_pac,1)<size(sig_pac,2)
sig_pac = sig_pac';
end
if size(sig_mod,1)<size(sig_mod,2)
sig_mod = sig_mod';
end
if (size(sig_pac,2) ~= size(sig_mod,2))
sprintf('Error - Signals must have the same number of trials')
return
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Set up some parameters for clarity
xbins = ceil((max(ph_freq_vec) - min(ph_freq_vec))/(diff(ph_freq_vec(1:2))));
ybins = ceil((max(amp_freq_vec) - min(amp_freq_vec))/(diff(amp_freq_vec(1:2))));
%alpha = alpha/(xbins*ybins); % Uncomment to use Bonferonni Correction
% Filtering %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Each cell array element of ph_filt_signals and amp_filt_signals has the
% same dimensions as the original signals i.e. number of columns = number of
% trials
if (strcmp(measure, 'esc')) ||(strcmp(measure, 'mi')) || (strcmp(measure, 'esp'))
[filt_sig_mod, filt_sig_pac] = filt_signals(sig_pac, sig_mod, Fs, ...
ph_freq_vec, amp_freq_vec, measure, width, TFmethod);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Create shuffled datasets and distribution of PAC values %%%%%%%%%%%%%%%%%
if num_shf ~= 0
for s = 1:num_shf
if strcmp(measure, 'esc') || strcmp(measure, 'esp')
shuffled_sig_amp = shuffle_esc(filt_sig_pac, Fs);
shf_pacmat_final(s,:,:) = find_pac_nofilt(shuffled_sig_amp, Fs, 'esc', filt_sig_mod, ph_freq_vec, amp_freq_vec,'n');
end
if strcmp(measure, 'mi')
shuffled_sig_amp = shuffle_esc(filt_sig_pac, Fs);
shf_pacmat_final(s,:,:) = find_pac_nofilt(shuffled_sig_amp, Fs, measure, filt_sig_mod, ph_freq_vec, amp_freq_vec,'n');
end
if strcmp(measure, 'cfc')
shuffled_sig1 = shuffle_esc(sig_pac, Fs);
shf_pacmat_final(s,:,:) = find_pac_nofilt(shuffled_sig1, Fs,measure, sig_mod, ph_freq_vec, amp_freq_vec,'n', 0, width, nfft);
end
% Display current computational step to user
%if waitbar == 1
if s == 1
fprintf('%03i%% ', floor((s/num_shf)*100));
else
fprintf('\b\b\b\b\b%03i%% ', floor((s/num_shf)*100));
end
if s == num_shf
fprintf('\n');
end
%end
end
%Find mean and standard deviation of shuffled data sets
if strcmp(measure, 'mi')
for i =1:ybins
for j=1:xbins
[shf_data_mean(i,j), shf_data_std(i,j)] = normfit(shf_pacmat_final(:,i,j));
end
end
else
shf_data_mean = squeeze (mean (shf_pacmat_final, 1));
shf_data_std = squeeze (std (shf_pacmat_final, 1));
end
end
% Calculate PAC measures %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if strcmp(measure, 'esc') || strcmp(measure, 'esp')
[pacmat, freqvec_ph, freqvec_amp] = find_pac_nofilt(filt_sig_pac, Fs, 'esc', filt_sig_mod, ph_freq_vec, amp_freq_vec, 'n', 1);
end
if strcmp(measure, 'mi')
[pacmat, freqvec_ph, freqvec_amp] = find_pac_nofilt(filt_sig_pac, Fs, measure, filt_sig_mod, ph_freq_vec, amp_freq_vec, 'n', 1);
end
if strcmp(measure, 'cfc')
[pacmat, freqvec_ph, freqvec_amp] = find_pac_nofilt(sig_pac, Fs, measure, sig_mod, ph_freq_vec, amp_freq_vec, 'n', 1, width, nfft);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Compute significance %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if num_shf ~= 0
for i = 1:size(pacmat,1)
for j = 1:size(pacmat,2)
[h, p] = my_sig_test(pacmat(i,j), squeeze(shf_pacmat_final(:,i,j)), alpha);
if h == 0
pacmat(i,j) = 0;
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
function [ph_filt_signals, amp_filt_signals] = filt_signals(sig1,sig2,...
Fs, ph_freq_vec, amp_freq_vec, measure, width, TFmethod)
total_num_dp = size(sig1,1);
num_trials = 1:size(sig1,2);
phfreq_low = min(ph_freq_vec);
phfreq_high = max(ph_freq_vec);
phfreq_bw = diff(ph_freq_vec(1:2));
ampfreq_low = min(amp_freq_vec);
ampfreq_high = max(amp_freq_vec);
ampfreq_bw = diff(amp_freq_vec(1:2));
xbins = ceil((phfreq_high - phfreq_low)/phfreq_bw);
ybins = ceil((ampfreq_high - ampfreq_low)/ampfreq_bw);
% Create structures to store filtered signals
ph_filt_signals = cell(1,xbins);
amp_filt_signals = cell(1,ybins);
for i2=1:xbins
ph_filt_signals{1,i2} = zeros(total_num_dp,max(num_trials));
end
for i2=1:ybins
amp_filt_signals{1,i2} = zeros(total_num_dp,max(num_trials));
end
% Filter and store filtered signals %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Filter sig2 for phase freq
for i2=1:xbins
BandBounds(2) = phfreq_low+(i2*phfreq_bw);
BandBounds(1) = BandBounds(2)-phfreq_bw;
if strcmp(measure, 'esc')
for i3=1:max(num_trials)
ph_filt_signals{1,i2}(:,i3) = bp_vec(sig2(:,i3)', BandBounds, Fs, width, TFmethod)';
end
else
for i3=1:max(num_trials)
ph_filt_signals{1,i2}(:,i3) = ph_vec(sig2(:,i3)', BandBounds, Fs, width, TFmethod)';
end
end
end
% Filter sig1 for amplitude freq
for i2=1:ybins
BandBounds(2) = ampfreq_low+(i2*ampfreq_bw);
BandBounds(1) = BandBounds(2)-ampfreq_bw;
for i3=1:max(num_trials)
amp_filt_signals{1,i2}(:,i3) = amp_vec(sig1(:,i3)', BandBounds, Fs, width, TFmethod)';
end
end
end
function TF = amp_vec(F,BandBounds,Fs,width,TFmethod)
lower_bin = BandBounds(1);
upper_bin = BandBounds(2);
N = size(F,1);
T = size(F,2);
TF = [];
cf = (lower_bin + floor((upper_bin- lower_bin)/2));
w = floor(width*Fs*(1/lower_bin));
for i = 1:N
if strcmp(TFmethod,'wavelet')
F1 = ampvec(cf, F(i,:)', Fs, width)';
elseif strcmp(TFmethod,'hilbert')
% Band-pass filter in one frequency band
Fband = process_bandpass('Compute', F(i,:), Fs, lower_bin, upper_bin, [], 1);
%Fband = Fband(T+(1:T));
% Apply Hilbert transform
F1 = abs(hilbert(Fband')');
F1 = F1(ceil(0.1*T)+(1:floor(0.8*T))-1);
elseif strcmp(TFmethod,'stft')
[S,fvec,tvec]=spectrogram([F(i,end:-1:1) F(i,:) F(i,end:-1:1)],w,w-1,lower_bin:upper_bin,Fs);
ind = (tvec > length(F)/Fs) & (tvec <= 2*length(F)/Fs);
F1 = mean(abs(S(:,ind)));
end
if isempty(TF)
TF = zeros(N,length(F1));
end
TF(i,:) = F1;
end
end
function TF = bp_vec(F,BandBounds,Fs,width,TFmethod)
N = size(F,1);
T = size(F,2);
TF = [];
lower_bin = BandBounds(1);
upper_bin = BandBounds(2);
for i = 1:N
if strcmp(TFmethod,'wavelet')
F1 = bpvec((lower_bin + floor((upper_bin- lower_bin)/2)),F(i,:)', Fs, width);