-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMS_coh_matrix.m
More file actions
120 lines (115 loc) · 7.21 KB
/
Copy pathMS_coh_matrix.m
File metadata and controls
120 lines (115 loc) · 7.21 KB
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
function [phase_coh_out] = MS_coh_matrix(trials, ExpKeys)
%% MS_coh_matrix:
%
%
%
%
% OUTPUT MATRIX
%
% |OFC | NAC | PL | CG | Piri_OFC | Piri_NAc
% OFC | %
% NAC | % [High gamma]
% PL | %
% CG | %
%Piri_OFC | %
%Piri_NAc | [low gamma] %
%
%
%
%
%
pairs = ExpKeys.GoodPairs;
bands = {'low', 'high'};
phases = {'pre', 'ipsi', 'contra', 'post'};
types = {'_pot', '_trk'};
if isempty(pairs) || isempty(pairs{1})
for iPhase = 1:length(phases)
phase_coh_out.(phases{iPhase}).low = NaN*ones(5,5);
phase_coh_out.(phases{iPhase}).high = NaN*ones(5,5);
end
all_trials = [];
else
% parameters
f_bandpass = {[45 65]; [70 90]};
cfg = [];
cfg.Fs =2000;
cfg.wsize = 128;
% compute the phase difference between each channel and the reference using cross-spectral power desnity.
% a negative values represent lags, while positives are leads.v
Add_ft()
for iPairs = 1:length(pairs)
sites = strsplit(pairs{iPairs}, '_');
for iBand = 1:length(bands)
for iType = 1:length(types)
for iPhase = 1:length(phases)
if ~isfield(trials.([sites{iSite} types{iType}]).(phases{iPhase}), (bands{iBand}))
continue
else
% filter the entire signal
% keep = any(~cellfun('isempty',trials.([sites{iSite} types{iType}]).(phases{iPhase}).(bands{iBand}).evts), 1);
% trials.(phases{iPhase}).(pairs{iPairs}).(bands{iBand}) = trials.(phases{iPhase}).(pairs{iPairs}).(bands{iBand})(:,keep);
% loop over events and compare the phase of
for itrial = 1:length(trials.([sites{1} types{iType}]).(phases{iPhase}).(bands{iBand}).evts)
[COH,F] = mscohere(trials.([sites{1} types{iType}]).(phases{iPhase}).(bands{iBand}).evts{itrial}.data,trials.([sites{2} types{iType}]).(phases{iPhase}).(bands{iBand}).evts{itrial}.data,hanning(cfg.wsize/2),cfg.wsize/4,cfg.wsize,cfg.Fs);
all_coh_spec.(phases{iPhase}).(bands{iBand})(itrial) =nanmean(COH(nearest(F,f_bandpass{iBand}(1)):nearest(F,f_bandpass{iBand}(2))));
[COH,F] = mscohere(trials.(phases{iPhase}).(pairs{iPairs}).(bands{iBand}){itrial}(1,:),trials.(phases{iPhase}).(pairs{iPairs}).(bands{iBand}){itrial}(2,:),hanning(cfg.wsize/2),cfg.wsize/4,cfg.wsize,cfg.Fs);
all_coh_spec_21.(phases{iPhase}).(bands{iBand})(itrial) = nanmean(COH(nearest(F,f_bandpass{iBand}(1)):nearest(F,f_bandpass{iBand}(2))));
% all_trials.(Phases{iPhase}).(pairs{iPairs}).(type{itype}){itrial} = [data_in_1.data(cycle_idx(cycle.prev.idx(1):cycle.next.idx(2))); data_in_2.data(cycle_idx(cycle.prev.idx(1):cycle.next.idx(2)))];
end
all_coh_spec.(phases{iPhase}).(bands{iBand})(all_coh_spec.(phases{iPhase}).(bands{iBand})==0) =[];
all_coh_spec_21.(phases{iPhase}).(bands{iBand})(all_coh_spec_21.(phases{iPhase}).(bands{iBand})==0) =[];
if strcmp(pairs{iPairs}, 'OFC_NAc')
if isempty(all_coh_spec.(phases{iPhase}).(bands{iBand}))
phase_coh_out.(phases{iPhase}).(bands{iBand})(1,2) = NaN;
phase_coh_out.(phases{iPhase}).(bands{iBand})(2,1) = NaN;
else
phase_coh_out.(phases{iPhase}).(bands{iBand})(1,2) = rad2deg(circ_mean(all_coh_spec.(phases{iPhase}).(bands{iBand})'));
phase_coh_out.(phases{iPhase}).(bands{iBand})(2,1) = rad2deg(circ_mean(all_coh_spec_21.(phases{iPhase}).(bands{iBand})'));
end
elseif strcmp(pairs{iPairs}, 'OFC_CG')
if isempty(all_coh_spec.(phases{iPhase}).(bands{iBand}))
phase_coh_out.(phases{iPhase}).(bands{iBand})(1,3) = NaN;
phase_coh_out.(phases{iPhase}).(bands{iBand})(3,1) = NaN;
else
phase_coh_out.(phases{iPhase}).(bands{iBand})(1,3) = rad2deg(circ_mean(all_coh_spec.(phases{iPhase}).(bands{iBand})'));
phase_coh_out.(phases{iPhase}).(bands{iBand})(3,1) = rad2deg(circ_mean(all_coh_spec_21.(phases{iPhase}).(bands{iBand})'));
end
elseif strcmp(pairs{iPairs}, 'PL_OFC')
if isempty(all_coh_spec.(phases{iPhase}).(bands{iBand}))
phase_coh_out.(phases{iPhase}).(bands{iBand})(1,4) = NaN;
phase_coh_out.(phases{iPhase}).(bands{iBand})(4,1) = NaN;
else
phase_coh_out.(phases{iPhase}).(bands{iBand})(1,4) = rad2deg(circ_mean(all_coh_spec.(phases{iPhase}).(bands{iBand})'));
phase_coh_out.(phases{iPhase}).(bands{iBand})(4,1) = rad2deg(circ_mean(all_coh_spec_21.(phases{iPhase}).(bands{iBand})'));
end
elseif strcmp(pairs{iPairs}, 'NAc_CG')
if isempty(all_coh_spec.(phases{iPhase}).(bands{iBand}))
phase_coh_out.(phases{iPhase}).(bands{iBand})(2,3) = NaN;
phase_coh_out.(phases{iPhase}).(bands{iBand})(3,2) = NaN;
else
phase_coh_out.(phases{iPhase}).(bands{iBand})(2,3) = rad2deg(circ_mean(all_coh_spec.(phases{iPhase}).(bands{iBand})'));
phase_coh_out.(phases{iPhase}).(bands{iBand})(3,2) = rad2deg(circ_mean(all_coh_spec_21.(phases{iPhase}).(bands{iBand})'));
end
elseif strcmp(pairs{iPairs}, 'PL_NAc')
if isempty(all_coh_spec.(phases{iPhase}).(bands{iBand}))
phase_coh_out.(phases{iPhase}).(bands{iBand})(2,4) = NaN;
phase_coh_out.(phases{iPhase}).(bands{iBand})(4,2) = NaN;
else
phase_coh_out.(phases{iPhase}).(bands{iBand})(2,4) = rad2deg(circ_mean(all_coh_spec.(phases{iPhase}).(bands{iBand})'));
phase_coh_out.(phases{iPhase}).(bands{iBand})(4,2) = rad2deg(circ_mean(all_coh_spec_21.(phases{iPhase}).(bands{iBand})'));
end
elseif strcmp(pairs{iPairs}, 'PL_CG')
if isempty(all_coh_spec.(phases{iPhase}).(bands{iBand}))
phase_coh_out.(phases{iPhase}).(bands{iBand})(3,4) = NaN;
phase_coh_out.(phases{iPhase}).(bands{iBand})(4,3) = NaN;
else
phase_coh_out.(phases{iPhase}).(bands{iBand})(3,4) = rad2deg(circ_mean(all_coh_spec.(phases{iPhase}).(bands{iBand})'));
phase_coh_out.(phases{iPhase}).(bands{iBand})(4,3) = rad2deg(circ_mean(all_coh_spec_21.(phases{iPhase}).(bands{iBand})'));
end
end
end
end
end
end
Remove_ft()
end