spm_get_def.m 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. function def = spm_get_def(Bx,By,Bz,beta)
  2. % Calculating deformation field from coefficient vector.
  3. %
  4. % FORMAT [def] = get_def(Bx,By,Bz,beta);
  5. % Bx, By, Bz - Separable basis sets such that B=kron(Bz,kron(By,Bx));
  6. % beta - Coefficient vector for basis set.
  7. % or
  8. % FORMAT [def] = get_def(dim,order,beta);
  9. % dim - Dimensionality of image in x,y and z directions.
  10. % order - Order of DCT set in x,y and z directions.
  11. % beta - Coefficient vector for basis set.
  12. %_______________________________________________________________________
  13. %
  14. % Calculates the equivalent to kron(Bz,kron(By,Bx))*beta
  15. % in a faster and more efficient way. Note that this routine
  16. % can be used to calculate AtY efficiently as well since
  17. % A'*y = (diag(dfdy)*kron(Bz,kron(By,Bx)))'*y =
  18. % kron(Bz',kron(By',Bx'))*(diag(dfdy)*y) = get_def(Bx',By',Bz',dfdy.*y)
  19. %
  20. %_______________________________________________________________________
  21. % Copyright (C) 2008 Wellcome Trust Centre for Neuroimaging
  22. % Jesper Andersson
  23. % $Id: spm_get_def.m 1143 2008-02-07 19:33:33Z spm $
  24. if nargin == 4
  25. [nx,mx] = size(Bx);
  26. [ny,my] = size(By);
  27. [nz,mz] = size(Bz);
  28. if mx*my*mz ~= size(beta,1)
  29. warning('get_def: Size mismatch between beta and basis-set');
  30. return
  31. end
  32. elseif nargin == 3
  33. if numel(Bx) ~= 3 || numel(By) ~= 3
  34. warning('get_def: Wrong dimensionality on input');
  35. elseif prod(By) ~= size(Bz,1)
  36. warning('get_def: Size mismatch between beta and basis-set');
  37. return
  38. end
  39. nx = Bx(1); ny = Bx(2); nz = Bx(3);
  40. mx = By(1); my = By(2); mz = By(3);
  41. beta = Bz;
  42. Bx = spm_dctmtx(nx,mx);
  43. By = spm_dctmtx(ny,my);
  44. Bz = spm_dctmtx(nz,mz);
  45. end
  46. def = zeros(nx*ny*nz,1);
  47. for bf = 1:mz
  48. mbeta = reshape(beta((bf-1)*my*mx+1:bf*my*mx),mx,my);
  49. tmp = reshape(Bx*mbeta*By',nx*ny,1);
  50. for sl = 1:nz
  51. def((sl-1)*ny*nx+1:sl*ny*nx) = def((sl-1)*ny*nx+1:sl*ny*nx) + Bz(sl,bf)*tmp;
  52. end
  53. end
  54. return