spm_DEM_z.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. function [z,w] = spm_DEM_z(M,N)
  2. % creates hierarchical innovations for generating data
  3. % FORMAT [z w] = spm_DEM_z(M,N)
  4. % M - model structure
  5. % N - length of data sequence
  6. %
  7. % z{i} - innovations for level i (N.B. z{end} corresponds to causes)
  8. % w{i} - innovations for level i (state noise)
  9. %
  10. % If there is no fixed or hyper parameterized precision, then unit noise is
  11. % created. It is assumed that this will be later modulated by state
  12. % dependent terms, specified by M.ph and M.pg in spm_DEM_int
  13. %__________________________________________________________________________
  14. % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
  15. % Karl Friston
  16. % $Id: spm_DEM_z.m 5047 2012-11-09 20:48:20Z karl $
  17. % temporal convolution matrix (with unit variance)
  18. %--------------------------------------------------------------------------
  19. s = M(1).E.s + exp(-16);
  20. dt = M(1).E.dt;
  21. t = ((1:N) - 1)*dt;
  22. K = toeplitz(exp(-t.^2/(2*s^2)));
  23. K = diag(1./sqrt(diag(K*K')))*K;
  24. % create innovations z{i} and w{i}
  25. %--------------------------------------------------------------------------
  26. for i = 1:length(M)
  27. % precision of causes
  28. %======================================================================
  29. P = M(i).V;
  30. % plus prior expectations
  31. %----------------------------------------------------------------------
  32. try
  33. for j = 1:length(M(i).Q)
  34. P = P + M(i).Q{j}*exp(M(i).hE(j));
  35. end
  36. end
  37. % create causes: assume i.i.d. if precision is zero
  38. %----------------------------------------------------------------------
  39. if norm(P,1) == 0;
  40. z{i} = randn(M(i).l,N)*K;
  41. elseif norm(P,1) >= exp(16)
  42. z{i} = sparse(M(i).l,N);
  43. else
  44. z{i} = spm_sqrtm(inv(P))*randn(M(i).l,N)*K;
  45. end
  46. % precision of states
  47. %======================================================================
  48. P = M(i).W;
  49. % plus prior expectations
  50. %----------------------------------------------------------------------
  51. try
  52. for j = 1:length(M(i).R)
  53. P = P + M(i).R{j}*exp(M(i).gE(j));
  54. end
  55. end
  56. % create states: assume i.i.d. if precision (P) is zero
  57. %----------------------------------------------------------------------
  58. if ~isempty(P)
  59. if norm(P,1) == 0;
  60. w{i} = randn(M(i).n,N)*K*dt;
  61. elseif norm(P,1) >= exp(16)
  62. w{i} = sparse(M(i).n,N);
  63. else
  64. w{i} = spm_sqrtm(inv(P))*randn(M(i).n,N)*K*dt;
  65. end
  66. else
  67. w{i} = sparse(0,0);
  68. end
  69. end