-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpsr_lfp_tfa.m
81 lines (68 loc) · 2.77 KB
/
psr_lfp_tfa.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
function timefreq = psr_lfp_tfa(freq,parameters)
% PSR_LFP_TFA - Time-frequency analysis using FieldTrip's FT_FREQANALYSIS
%
% Syntax: timefreq = psr_lfp_tfa(freq,parameters)
%
% Inputs:
% freq - FieldTrip LFP data structure (see README)
% parameters - See PSR_PARAMETERS_ANALYSIS
%
% Outputs:
% timefreq - Output from FT_FREQANALYSIS
%
% See also: FT_FREQANALYSIS
% PASER: Processing and Analysis Schemes for Extracellular Recordings
% https://github.com/tbrouns/paser
% Author: Terence Brouns
% Radboud University, Neurophysiology Dept.
% E-mail address: [email protected]
% Date: 2018
%------------- BEGIN CODE --------------
freq = psr_ft_nan_removal(freq);
cfg = [];
cfg.output = 'pow';
cfg.method = 'mtmconvol';
cfg.channel = 'all';
cfg.trials = 'all';
cfg.keeptrials = 'yes';
cfg.keeptapers = 'no';
if (~isempty_field(parameters,'parameters.analysis.tfa.tapsmofrq')); cfg.tapsmofrq = parameters.analysis.tfa.tapsmofrq; end
if (~isempty_field(parameters,'parameters.analysis.tfa.toi')); cfg.toi = parameters.analysis.tfa.toi; end
if (~isempty_field(parameters,'parameters.analysis.tfa.foi')); cfg.foi = parameters.analysis.tfa.foi; end
if (~isempty_field(parameters,'parameters.analysis.tfa.taper')); cfg.taper = parameters.analysis.tfa.taper; end
if (~isempty_field(parameters,'parameters.analysis.tfa.pad')); cfg.pad = parameters.analysis.tfa.pad; end
if (~isempty_field(parameters,'parameters.analysis.tfa.keepchans')); cfg.keepchans = parameters.analysis.tfa.keepchans; end
if (~isempty_field(parameters,'parameters.analysis.tfa.ncycles')); cfg.t_ftimwin = parameters.analysis.tfa.ncycles ./ cfg.foi;
else, cfg.t_ftimwin = parameters.analysis.tfa.t_ftimwin * ones(size(cfg.foi));
end
% Temporariry remove some fields
[freq,~] = psr_remove_field(freq,'artifacts');
[freq,missing] = psr_remove_field(freq,'missing');
% Time-frequency analysis
try timefreq = ft_freqanalysis(cfg,freq); % FieldTrip function
catch ME
timefreq = [];
str = ME.message;
psr_show_warning({str});
return;
end
% Set artifacts to NaN
nTime = length(timefreq.time);
nChan = size(timefreq.powspctrm,2);
for iTrial = 1:length(missing)
for iChan = 1:nChan
I = missing{iTrial}(iChan,:);
N = size(I,2);
I = find(I);
i = unique(ceil((nTime/N)*I));
timefreq.powspctrm(iTrial,iChan,:,i) = NaN;
end
end
% Output results
if (~cfg.keepchans) % Average over probe channels
timefreq.powspctrm = nanmean(timefreq.powspctrm,2);
timefreq.label = timefreq.label{1};
end
timefreq.powspctrm = single(timefreq.powspctrm); % To avoid memory issues
timefreq = orderfields(timefreq);
end