FDCalc.m 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. function [FDts,Stat]=FDCalc(MP,varargin)
  2. % [FDts,SS]=FDCalc(MP,drflag,radius)
  3. % Calculates the Frame-Wise Displacement
  4. %
  5. %%%INPUTS:
  6. %
  7. % MP : Movement Parameters (regressors). Should be a matrix of
  8. % size TxP. Where P is number of params, and T is number of volumes.
  9. % drflag : Rotation and Disp indicator. if a param is for rotation set
  10. % the index to 1 otherwise to 0. e.g. drflag=[0 0 0 1 1 1] for three
  11. % disp param and three rotation param.
  12. %
  13. %%%OUTPUTS:
  14. %
  15. % FDts : Frame-Wise Displacement
  16. % Stat.SS : Sum of square of the movements.
  17. % Stat.FD_0{2,5}_Idx : Index of scans exceeding the 0.2/0.5mm threshold
  18. % Stat.FD_0{2,5}_p : % of the scans explained above
  19. % Stat.AbsRot : Absolute sum of rotation dip
  20. % Stat.AbsTrans : Absolute sum of translational disp
  21. % Stat.AbsRot & Stat.AbsRot : Absolute sum of one-lag difference
  22. %
  23. %%%NOTES:
  24. % 1) Rotation params should be in degree
  25. % 2) Only first six params takes into account
  26. %
  27. %_________________________________________________________________________
  28. % Soroosh Afyouni, NISOx.org, 2017
  29. % srafyouni@gmail.com
  30. fnnf=mfilename; if ~nargin; help(fnnf); return; end; clear fnnf;
  31. %_________________________________________________________________________
  32. if isempty(varargin)
  33. drflag = [0 0 0 1 1 1]; radius = 50; verbose = 1;
  34. elseif nargin==2
  35. drflag = varargin{1}; radius = 50; verbose = 1;
  36. elseif nargin==3
  37. drflag = varargin{1}; radius = varargin{2}; verbose = 1;
  38. elseif nargin==4
  39. drflag = varargin{1}; radius = varargin{2}; verbose = varargin{3};
  40. else
  41. error('FDCalc :: Too many inputs.')
  42. end
  43. if size(MP,2) > size(MP,1)
  44. error('FDCalc :: Check dim of params. It should be TxP. Use MP=transpose(MP) if necessary.')
  45. end
  46. if size(MP,2) > 6 && verbose
  47. warning('-Only first six colums is taken to acount as mov par.')
  48. end
  49. r_Idx = find(drflag);
  50. t_idx = find(~drflag);
  51. if verbose
  52. disp(['-Rotation Index:' num2str(r_Idx) ', Dsplcmnt Index: ' num2str(t_idx)])
  53. end
  54. MP = MP(:,1:6);
  55. MP(:,r_Idx) = (2*radius*pi/360)*MP(:,r_Idx); %Oi! Degree Degree!
  56. dMP = diff(MP);
  57. FDts = sum(abs(dMP),2);
  58. SS = sqrt(sum(dMP.^2,2));
  59. Stat.SS = SS;
  60. Stat.FD_02_Idx = find(FDts>0.2);
  61. Stat.FD_05_Idx = find(FDts>0.5);
  62. Stat.FD_02_p = length(Stat.FD_02_Idx)./length(FDts)*100;
  63. Stat.FD_05_p = length(Stat.FD_05_Idx)./length(FDts)*100;
  64. Stat.AbsRot = sum(abs(MP(:,r_Idx)),2);
  65. Stat.AbsTrans = sum(abs(MP(:,t_idx)),2);
  66. Stat.AbsDiffRot = sum(abs(diff(MP(:,r_Idx))),2);
  67. Stat.AbsDiffTrans = sum(abs(diff(MP(:,t_idx))),2);