calculateFD.m 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. function FD_measures = calculateFD(MP, r, FD_threshold)
  2. % Function to calculate framewise displacement (FD) and related measures.
  3. %
  4. % INPUT:
  5. % MP - movement parameter matrix (Nt x 6)
  6. % r - radius to use for displacement derived from
  7. % rotational movement parameters
  8. % FD_threshold - threshold (in mm) that defines outlier volumes
  9. %
  10. % OUTPUT:
  11. % FD_measures - structure with filenames and data
  12. %__________________________________________________________________________
  13. % Copyright (C) Stephan Heunis 2018
  14. % Define variables
  15. FD_measures = struct;
  16. [Nt, p] = size(MP);
  17. % First demean and detrend the movement parameters
  18. MP2 = MP - repmat(mean(MP, 1), Nt,1);
  19. X = (1:Nt)';
  20. X2 = X - mean(X);
  21. X3 = [ones(Nt,1) X2];
  22. b = X3\MP2;
  23. MP_corrected = MP2 - X3(:, 2)*b(2,:);
  24. % Then transform rotational parameters to linear translations (small angle assumption)
  25. MP_mm = MP_corrected;
  26. MP_mm(:,4:6) = MP_mm(:,4:6)*r; % 50mm from Power 2017; 80 mm from QAP
  27. % Calculate FD and related measures
  28. MP_diff = [zeros(1, 6); diff(MP_mm)];
  29. FD_measures.FD = sum(abs(MP_diff),2);
  30. FD_measures.FD_outliers_regr = FD_measures.FD>=FD_threshold;
  31. FD_measures.FD_outliers_ind = find(FD_measures.FD_outliers_regr);
  32. FD_measures.FD_sum = sum(FD_measures.FD);
  33. FD_measures.FD_mean = FD_measures.FD_sum/Nt;