-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathphase_synchronization_measures.m
202 lines (164 loc) · 5.57 KB
/
phase_synchronization_measures.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
%% Script to calculate the phase-synchronization measures corrected with the surrogates
%This script calculates phase synchronization measures to study brain
%dynamics. The goal here is to describe the synchornization dynamics based
%on phase statistics. The present script computes four basic measures;
%integration, segregation, phase-interaction fluctuations and FCD.
%These measures are applied mainly in fMRI BOLD signals.
%BASIC PHASE SYNCHRONIZATION MEASURES
%------------------------------
%Integration
%
%Segregation
%
%Phase-interaction fluctuations
%
%FCD
%
%
%Ane López-González 25/01/2021
clc
close all
clear all
%% Loading data and basic parameters:
load('data_UWS_all1') %timeseries of each group Nsubjects*nodes*time
ts=ts_all;
NSUB = size(ts,1);
N = size(ts,2);
%Basic filtering parameters
delta= 2; %TR
flp = .04; % lowpass frequency of filter
fhi = .07; % highpass
k = 2; % 2nd order butterworth filter
fnq = 1/(2*delta); % Nyquist frequency
Wn = [flp/fnq fhi/fnq]; % butterworth bandpass non-dimensional frequency
[bfilt2,afilt2] = butter(k,Wn); % construct the filter
%% Surrogate analysis
%account for the phase-effects that can be removed by the surrogates.
%In this part of the code, we create surrogates of the BOLD timeseries and
%from them, the mean surrogate Phase-Interaction matrix, that will be used
%in the following section to clean the empirical matrices.
for nsub = 1:NSUB
xs = squeeze(ts(nsub,:,:));
Tmax = size(xs,2); %%%The time is calculated inside the subjects loop
%%%in case any subject has different time duration.
T = 1:Tmax;
timeseriedata = zeros(N,Tmax);%length(xs)
for surr=1:100 %we used 100 surrogates as default
for seed = 1:N
x = demean(detrend(xs(seed,:)));
x_surr=surrogates(x); % Create the phase-randomized copy of x
timeseriedata(seed,:) = filtfilt(bfilt2,afilt2,x_surr); % zero phase filter the data
Xanalytic = hilbert(demean(timeseriedata(seed,:))); %Hilbert transformation
Phases(seed,:) = angle(Xanalytic); %%% calculating phase of each ROI for each signal
%%% which will use to compute the
%%% phase synchronization measures
end
% Phase-interaction matrix: At each time point, the phase difference between two regions was calculated:
for t = T
for i = 1:N
for j = 1:N
dM_surr(i,j,t) = cos(adif(Phases(i,t),Phases(j,t)));
end
end
%mPLM(nsub,t)=mean(mean(dM_surr(:,:,t)));
end
mdM_surr=mean(dM_surr,3);
mdM(surr,:,:)=mdM_surr; %Mean phase-interaction matrix across time
surr
end
save(sprintf('surrogate_UWS_%02d',nsub),'mdM') %save the surrogate analysis
end
clear Phases x xs timeseriedata Xanalytic
%% Calculate the phase-synchronization measured from the
%% real BOLD timeseries and then corrected with the surrogates
for nsub = 1:NSUB
xs = squeeze(ts(nsub,:,:));
Tmax = size(xs,2); %%%The time is calculated inside the subjects loop
%%%in case any subject has different time duration.
T = 1:Tmax;
timeseriedata = zeros(N,Tmax);%length(xs)
for seed = 1:N
x = demean(detrend(xs(seed,:)));
timeseriedata(seed,:) = filtfilt(bfilt2,afilt2,x); % zero phase filter the data
Xanalytic = hilbert(demean(timeseriedata(seed,:))); %Hilbert transformation
Phases(seed,:) = angle(Xanalytic); %%% calculating phase of each ROI for each signal
%%% which will use to compute the
%%% phase synchronization measures
end
T = 1:Tmax;
sync = zeros(1, Tmax);
for t = T
ku = sum(complex(cos(Phases(:,t)),sin(Phases(:,t))))/N;
sync(t) = abs(ku);
end
fluct(nsub) = std(sync(:));
sync_all (nsub,:) =sync;
%Phase-interaction matrix:
%At each time point, the phase difference between two regions was calculated:
for t = T
for i = 1:N
for j = 1:N
dM(i,j,t) = cos(adif(Phases(i,t),Phases(j,t))); % computes dynamic matrix/ dFC
end
end
end
fluct_data(nsub,:)=std(sync);
load(sprintf('surrogate_UWS_%02d',nsub))
mmdM=squeeze(mean(mdM,1));
%Integration:
cc = mean(dM,3)-mmdM; % Correct with the mean matrices calculated with the surrogates
%cc = mean(dM,3);
cc = cc-eye(N);
pp = 1;
PR = 0:0.01:0.99;
cs=zeros(1,length(PR));
for p = PR
A = (cc)>p;
[~, csize] = get_components(A);
cs(pp) = max(csize);
pp = pp+1;
end
integ(nsub) = sum(cs)*0.01/N;
% The modularity (as a measure of segregation) is calculated in the mean matrix and corrected with the
% bined matrix given by the surrogate and imposing a threhsold of the
% 99% percentile
meandM=mean(dM,3);
for i=1:N
for j=1:N
Y=prctile(mdM(:,i,j),99);
if meandM(i,j)>Y
bin_M(i,j)=1;
else
bin_M(i,j)=0;
end
end
end
[~, Q(nsub)] = community_louvain((bin_M));
%FCD (phases)
Isubdiag = find(tril(ones(N),-1));
for t=T
for i=1:N
for j=1:N
dM(i,j,t) = cos(adif(Phases(i,t),Phases(j,t))); % computes dynamic matrix/ dFC
end
end
patt=dM(:,:,t);
pattern(t,:)=patt(Isubdiag);
end
npattmax=size(pattern,1);
kk3=1;
for t=1:npattmax-30
p1=mean(pattern(t:t+30,:));
for t2=t+1:npattmax-30
p2=mean(pattern(t2:t2+30,:));
phfcddata(kk3)=dot(p1,p2)/norm(p1)/norm(p2);
matrixcdc(t,t2)=dot(p1,p2)/norm(p1)/norm(p2);
matrixcdc(t2,t)=dot(p1,p2)/norm(p1)/norm(p2);
kk3=kk3+1;
end
end
CDC(nsub,:)=phfcddata;
matrix_CDC(nsub,:,:)=matrixcdc;
display(nsub);
end
save('phase_synchronization_UWS','integ','Q','fluct','CDC','matrix_CDC')