MotionAddition_rsfmri.m 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. % Input folder path containing fMRI data
  2. input_folder = "C:\Users\aswen\Desktop\TestingData\Aswendt_qc_data\proc_data";
  3. % Get all rsfMRI files in the input folder
  4. searchpath = fullfile(input_folder, '**', 'func', '*EPI.nii.gz');
  5. rsfmri_files = dir(searchpath);
  6. rsfmri_files = fullfile({rsfmri_files.folder}, {rsfmri_files.name});
  7. % Parameters for motion addition
  8. translation_amount_initial = 7; % Initial translation amount in pixels
  9. translation_amount_growth = 1; % Amount by which translation increases at each time point
  10. translation_amount_max = 30; % Maximum translation amount in pixels
  11. timepoints = size(Im, 4); % Number of time points
  12. % Loop through each rsfMRI file for motion addition
  13. for i = 1:numel(rsfmri_files)
  14. % Read file
  15. file = rsfmri_files{i};
  16. % Load the 4D rsfMRI image and its info
  17. Im = niftiread(file);
  18. info = niftiinfo(file);
  19. % Initialize translation amount
  20. translation_amount = translation_amount_initial;
  21. % Add motion by shifting each volume in the time series
  22. for t = 1:timepoints
  23. % Apply translation to simulate motion
  24. Im(:,:,:,t) = imtranslate(Im(:,:,:,t), [translation_amount, 0, 0]);
  25. % Update translation amount for the next time point
  26. translation_amount = min(translation_amount + translation_amount_growth, translation_amount_max);
  27. end
  28. % Save the motion-added 4D rsfMRI image
  29. [filepath, filename, ext] = fileparts(file);
  30. output_filename = [filename '_motion_added' ext]; % Appending '_motion_added' to the filename
  31. output_path = fullfile(filepath, output_filename);
  32. niftiwrite(Im, output_path, info, 'Compressed', true);
  33. end