forked from fpellegrini/FCsim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfp_cs2sctrgcmim.m
78 lines (64 loc) · 2.64 KB
/
fp_cs2sctrgcmim.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
function conn = fp_cs2sctrgcmim(CS, fres, nlags, inds, output)
% Copyright (c) 2022 Franziska Pellegrini and Stefan Haufe
%%
%set parameters
ninds = length(inds);
freqs = linspace(0, 1, fres+1);
freqs = freqs(1:fres+1);
z = exp(-i*pi*freqs);
if ~isempty(intersect(output, {'MIM', 'MIC', 'COH'}))
clear COH
for ifreq = 1:fres+1
clear pow
pow = real(diag(CS(:,:,ifreq)));
COH(:,:,ifreq) = CS(:,:,ifreq)./ sqrt(pow*pow');
end
end
if ~isempty(intersect(output, {'GC', 'TRGC'}))
G = cpsd_to_autocov(CS, nlags);
end
for iind = 1:ninds
if ~isequal(inds{iind}{1}, inds{iind}{2})
%ind configuration
subset = [inds{iind}{1} inds{iind}{2}];
nsubsetvars = length(subset);
subinds = {1:length(inds{iind}{1}), length(inds{iind}{1}) + (1:length(inds{iind}{2}))};
if ~isempty(intersect(output, {'MIM', 'MIC'}))
%MIC and MIM
[MIC(:, iind) , MIM(:, iind)] = roi_mim2(COH(subset, subset, :), subinds{1}, subinds{2});
end
if ~isempty(intersect(output, {'GC', 'TRGC'}))
% autocovariance to full forward VAR model
[A, SIG] = autocov_to_var3(G(subset, subset, :));
% forward VAR model to state space VARMA models
[eA2, eC2, eK2, eV2, eVy2] = varma2iss(reshape(A, nsubsetvars, []), [], SIG, eye(nsubsetvars));
% GC and TRGC computation
GC(:, iind, 1) = iss_SGC(eA2, eC2, eK2, eV2, z, subinds{2}, subinds{1});
GC(:, iind, 2) = iss_SGC(eA2, eC2, eK2, eV2, z, subinds{1}, subinds{2});
if ~isempty(intersect(output, {'TRGC'}))
% backward autocovariance to full backward VAR model
[AR, SIGR] = autocov_to_var3(permute(G(subset, subset, :), [2 1 3]));
% backward VAR to VARMA
[eA2R, eC2R, eK2R, eV2R, eVy2R] = varma2iss(reshape(AR, nsubsetvars, []), [], SIGR, eye(nsubsetvars));
GCR = iss_SGC(eA2R, eC2R, eK2R, eV2R, z, subinds{2}, subinds{1});
TRGC(:, iind, 1) = GC(:, iind, 1) - GCR';
GCR = iss_SGC(eA2R, eC2R, eK2R, eV2R, z, subinds{1}, subinds{2});
TRGC(:, iind, 2) = GC(:, iind, 2) - GCR';
end
end
else
if ~isempty(intersect(output, {'GC', 'TRGC'}))
GC(:, iind, 1:2) = 0;
TRGC(:, iind, 1:2) = 0;
end
end
end
if ~isempty(intersect(output, {'COH'}))
COH = permute(COH, [3 1 2 4]);
end
clear out
for iout = 1:length(output)
eval(['conn.' output{iout} ' = ' output{iout} ';'])
end
conn.nlags = nlags;
conn.inds = inds;