spm_DEM_R.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. function [R,V] = spm_DEM_R(n,s,form)
  2. % returns the precision of the temporal derivatives of a Gaussian process
  3. % FORMAT [R,V] = spm_DEM_R(n,s,form)
  4. %__________________________________________________________________________
  5. % n - truncation order
  6. % s - temporal smoothness - s.d. of kernel {bins}
  7. % form - 'Gaussian', '1/f' [default: 'Gaussian']
  8. %
  9. % e[:] <- E*e(0)
  10. % e(0) -> D*e[:]
  11. % <e[:]*e[:]'> = R
  12. % = <E*e(0)*e(0)'*E'>
  13. % = E*V*E'
  14. %
  15. % R - (n x n) E*V*E: precision of n derivatives
  16. % V - (n x n) V: covariance of n derivatives
  17. %__________________________________________________________________________
  18. % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
  19. % Karl Friston
  20. % $Id: spm_DEM_R.m 4278 2011-03-31 11:48:00Z karl $
  21. % if no serial dependencies
  22. %--------------------------------------------------------------------------
  23. if ~n, R = sparse(0,0); V = R; return, end
  24. if isempty(s), R = sparse(n,n); V = R; return, end
  25. if ~s, s = exp(-8); end
  26. % temporal correlations (assuming known form) - V
  27. %--------------------------------------------------------------------------
  28. try, form; catch, form = 'Gaussian'; end
  29. switch form
  30. case{'Gaussian'} % curvature: D^k(r) = cumprod(1 - k)/(sqrt(2)*s)^k
  31. %------------------------------------------------------------------
  32. k = 0:(n - 1);
  33. x = sqrt(2)*s;
  34. r(1 + 2*k) = cumprod(1 - 2*k)./(x.^(2*k));
  35. case{'1/f'} % g(w) = exp(-x*w) => r(t) = sqrt(2/pi)*x/(x^2 + w^2)
  36. %------------------------------------------------------------------
  37. k = [0:(n - 1)];
  38. x = 8*s^2;
  39. r(1 + 2*k) = (-1).^k.*gamma(2*k + 1)./(x.^(2*k));
  40. otherwise
  41. errordlg('unknown autocorrelation')
  42. end
  43. % create covariance matrix in generalised coordinates
  44. %==========================================================================
  45. V = [];
  46. for i = 1:n;
  47. V = [V; r([1:n] + i - 1)];
  48. r = -r;
  49. end
  50. % and precision - R
  51. %--------------------------------------------------------------------------
  52. sw = warning('off','MATLAB:nearlySingularMatrix');
  53. R = inv(V);
  54. warning(sw);
  55. if nargout, return, end
  56. % Inverse embedding operator (D): c.f., a Taylor expansion Y(t) <- D*y[:]
  57. %--------------------------------------------------------------------------
  58. dt = 1;
  59. x = fix((n + 1)/2);
  60. t = ([1:n] - x)*dt;
  61. for i = 1:n
  62. for j = 1:n
  63. D(i,j) = ((i - x)*dt)^(j - 1)/prod(1:(j - 1));
  64. end
  65. end
  66. % graphics
  67. %==========================================================================
  68. subplot(2,2,1)
  69. imagesc(R)
  70. axis square
  71. title({'precision';'derivatives'})
  72. subplot(2,2,2)
  73. imagesc(t,t,spm_inv(D*V*D'))
  74. axis square
  75. title({'precision';'time (secs)'})
  76. return
  77. % NB: temporal correlations (assuming stationary Gaussian form)
  78. %--------------------------------------------------------------------------
  79. t = ((1:n) - 1)*dt;
  80. v = 2*(s^2);
  81. V = exp(-t.^2/(2*v));
  82. V = toeplitz(V);