spm_ssm2s.m 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. function [s,u] = spm_ssm2s(P,M,TOL)
  2. % Converts state-space (M) representation to eigenspectrum
  3. % FORMAT [s,u] = spm_ssm2s(P,M)
  4. %
  5. % P - model parameters
  6. % M - model (with flow M.f and expansion point M.x and M.u)
  7. % TOL - optional upper bound for principality exponent (default -4)
  8. %
  9. % S - (sorted) eigenspectrum or Lyapunov exponents
  10. % V - associated eigenvectors
  11. %
  12. % csd - cross spectral density
  13. %__________________________________________________________________________
  14. % Copyright (C) 2012 Wellcome Trust Centre for Neuroimaging
  15. % Karl Friston
  16. % $Id: spm_ssm2s.m 6233 2014-10-12 09:43:50Z karl $
  17. % preliminaries
  18. %--------------------------------------------------------------------------
  19. if nargin < 3, TOL = -4; end
  20. % Steady state solution
  21. %--------------------------------------------------------------------------
  22. M.x = spm_dcm_neural_x(P,M);
  23. try
  24. M.u;
  25. catch
  26. M.u = zeros(M.l,1);
  27. end
  28. % Jacobian and delay operator - if not specified already
  29. %--------------------------------------------------------------------------
  30. if nargout(M.f) >= 3
  31. [f,dfdx,D] = feval(M.f,M.x,M.u,P,M);
  32. elseif nargout(M.f) == 2
  33. [f,dfdx] = feval(M.f,M.x,M.u,P,M);
  34. D = 1;
  35. else
  36. dfdx = spm_diff(M.f,M.x,M.u,P,M,1);
  37. D = 1;
  38. end
  39. dfdx = D*dfdx;
  40. dfdu = D*spm_diff(M.f,M.x,M.u,P,M,2);
  41. [u,s] = eig(full(dfdx),'nobalance');
  42. s = diag(s);
  43. % eigenvectors
  44. %--------------------------------------------------------------------------
  45. u = u*diag(pinv(u)*dfdu);
  46. % condition slow eigenmodes
  47. %--------------------------------------------------------------------------
  48. s = 1j*imag(s) + min(real(s),TOL);
  49. % principal eigenmodes (highest imaginary value)
  50. %--------------------------------------------------------------------------
  51. [d,i] = sort(imag(s),'descend');
  52. u = u(:,i);
  53. s = s(i);
  54. % principal eigenmodes (highest real value)
  55. %--------------------------------------------------------------------------
  56. j = find(~imag(s));
  57. [d,i] = sort(real(s(j)),'descend');
  58. u(:,j) = u(:,j(i));
  59. s(j) = s(j(i));