-
Notifications
You must be signed in to change notification settings - Fork 8
/
spm_DEM_R.m
109 lines (82 loc) · 3.07 KB
/
spm_DEM_R.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
function [R,V] = spm_DEM_R(n,s,form)
% returns the precision of the temporal derivatives of a Gaussian process
% FORMAT [R,V] = spm_DEM_R(n,s,form)
%__________________________________________________________________________
% n - truncation order
% s - temporal smoothness - s.d. of kernel {bins}
% form - 'Gaussian', '1/f' [default: 'Gaussian']
%
% e[:] <- E*e(0)
% e(0) -> D*e[:]
% <e[:]*e[:]'> = R
% = <E*e(0)*e(0)'*E'>
% = E*V*E'
%
% R - (n x n) E*V*E: precision of n derivatives
% V - (n x n) V: covariance of n derivatives
%__________________________________________________________________________
% Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
% Karl Friston
% $Id: spm_DEM_R.m 4278 2011-03-31 11:48:00Z karl $
% if no serial dependencies
%--------------------------------------------------------------------------
if ~n, R = sparse(0,0); V = R; return, end
if isempty(s), R = sparse(n,n); V = R; return, end
if ~s, s = exp(-8); end
% temporal correlations (assuming known form) - V
%--------------------------------------------------------------------------
try, form; catch, form = 'Gaussian'; end
switch form
case{'Gaussian'} % curvature: D^k(r) = cumprod(1 - k)/(sqrt(2)*s)^k
%------------------------------------------------------------------
k = 0:(n - 1);
x = sqrt(2)*s;
r(1 + 2*k) = cumprod(1 - 2*k)./(x.^(2*k));
case{'1/f'} % g(w) = exp(-x*w) => r(t) = sqrt(2/pi)*x/(x^2 + w^2)
%------------------------------------------------------------------
k = [0:(n - 1)];
x = 8*s^2;
r(1 + 2*k) = (-1).^k.*gamma(2*k + 1)./(x.^(2*k));
otherwise
errordlg('unknown autocorrelation')
end
% create covariance matrix in generalised coordinates
%==========================================================================
V = [];
for i = 1:n;
V = [V; r([1:n] + i - 1)];
r = -r;
end
% and precision - R
%--------------------------------------------------------------------------
sw = warning('off','MATLAB:nearlySingularMatrix');
R = inv(V);
warning(sw);
if nargout, return, end
% Inverse embedding operator (D): c.f., a Taylor expansion Y(t) <- D*y[:]
%--------------------------------------------------------------------------
dt = 1;
x = fix((n + 1)/2);
t = ([1:n] - x)*dt;
for i = 1:n
for j = 1:n
D(i,j) = ((i - x)*dt)^(j - 1)/prod(1:(j - 1));
end
end
% graphics
%==========================================================================
subplot(2,2,1)
imagesc(R)
axis square
title({'precision';'derivatives'})
subplot(2,2,2)
imagesc(t,t,spm_inv(D*V*D'))
axis square
title({'precision';'time (secs)'})
return
% NB: temporal correlations (assuming stationary Gaussian form)
%--------------------------------------------------------------------------
t = ((1:n) - 1)*dt;
v = 2*(s^2);
V = exp(-t.^2/(2*v));
V = toeplitz(V);