connectivity 搞了两周  从图论到各种

x(t) = 0.8*x(t-1) – 0.5*x(t-2)

y(t) = 0.9*y(t-1) + 0.5*z(t-1) – 0.8*y(t-2)

z(t) = 0.5*z(t-1) + 0.4*x(t-1) – 0.2*z(t-2)

t-1    t-2 为两个时间点

```cfg             = [];
cfg.ntrials     = 500;
cfg.triallength = 1;
cfg.fsample     = 200;
cfg.nsignal     = 3;
cfg.method      = 'ar';

cfg.params(:,:,1) = [ 0.8    0    0 ;
0  0.9  0.5 ;
0.4    0  0.5];

cfg.params(:,:,2) = [-0.5    0    0 ;
0 -0.8    0 ;
0    0 -0.2];

cfg.noisecov      = [ 0.3    0    0 ;
0    1    0 ;
0    0  0.2];

data              = ft_connectivitysimulation(cfg);

三个信号如上图所示

```cfg         = [];
cfg.order   = 5;
cfg.toolbox = 'bsmart';
mdata       = ft_mvaranalysis(cfg, data);

For the actual computation of the autoregressive coefficients FieldTrip makes use of
an implementation from third party toolboxes. At present ft_mvaranalysis supports
the biosig and bsmart toolboxes for these computations.

```
```mdata =
dimord: 'chan_chan_lag'
label: {3x1 cell}
coeffs: [3x3x5 double]
noisecov: [3x3 double]
dof: 500
fsampleorig: 200
cfg: [1x1 struct]

val(:,:,1) =

0.7939 0.0008 -0.0013
0.0047 0.8988 0.4905
0.4011 -0.0016 0.4983

val(:,:,2) =

-0.4992 -0.0004 0.0017
-0.0026 -0.8023 -0.0036
0.0015 -0.0003 -0.2064

val(:,:,3) =

-0.0025 -0.0001 -0.0015
0.0050 -0.0023 0.0019
0.0026 -0.0016 0.0013

val(:,:,4) =

-0.0046 0.0012 0.0048
-0.0096 0.0008 -0.0054
-0.0017 0.0003 -0.0021

val(:,:,5) =

-0.0042 -0.0013 -0.0101
0.0063 -0.0092 0.0060
0.0056 -0.0008 -0.0029```

transfer matrix  转移矩阵

```cfg        = [];
cfg.method = 'mvar';
mfreq      = ft_freqanalysis(cfg, mdata);```

Filedtrip自带freqanalysis 里面可以用mvar的方式进行频谱计算

```'mvar', does a fourier transform on the coefficients
of an estimated multivariate autoregressive model,
obtained with FT_MVARANALYSIS. In this case, the
output will contain a spectral transfer matrix,
the cross-spectral density matrix, and the
covariance matrix of the innovatio noise.```

```mfreq =
label: {3x1 cell}
freq: [1x101 double]
dimord: 'chan_chan_freq'
transfer: [3x3x101 double]
noisecov: [3x3 double]
dof: 500
cfg: [1x1 struct]
```

### Non-parametric computation of the cross-spectral density matrix

```cfg           = [];
cfg.method    = 'mtmfft';
cfg.taper     = 'dpss';
cfg.output    = 'fourier';
cfg.tapsmofrq = 2;
freq          = ft_freqanalysis(cfg, data);

```
```cfg           = [];
cfg.method    = 'coh';
coh           = ft_connectivityanalysis(cfg, freq);
cohm          = ft_connectivityanalysis(cfg, mfreq);

```
```上图可以看出，coh不平滑，cohm很平滑，当然两者都可以逼近的很好

The coherence measure is a symmetric measure, which means that it does not provide
information regarding the direction of information flow between any pair of signals.

In order to analyze directionality in interactions, measures based on the concept
of granger causality can be computed. These measures are based on an estimate of
the spectral transfer matrix, which can be computed in a straightforward way from
the multivariate autoregressive model fitted to the data.```

cfg=[];

cfg.method=’granger';

granger=ft_conectivityanalysis(cfg,mfreq)

singal1 和 2 3没有因果上的关系 （singal1 的变量中没有 y z  只有x ）

singal2 和 1 3 都有关系 （singal2 里面有y  和 z，没有x）

singal3 和 1有关系 （singal2 里面有x和 z   没有y）

partial directed coherence (pdc),

```cfg           = [];
cfg.method    = 'pdc';
pdc       = ft_connectivityanalysis(cfg, mfreq);
```

directed transfer function (dtf),

```cfg           = [];
cfg.method    = 'dtf';
dtf       = ft_connectivityanalysis(cfg, mfreq);
```

phase slope index (psi). (Note that psi will require specifying cfg.bandwidth.

```cfg           = [];
cfg.method    = 'psi';
cfg.bandwidth=[8 14];
psi       = ft_connectivityanalysis(cfg, mfreq);```