a1_prepare_preprocessing.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. function a1_prepare_preprocessing(id, rootpath)
  2. % prepare (i.e. filter + downsample data) for ICA1
  3. if ismac % run if function is not pre-compiled
  4. id = '1'; % test for example subject
  5. currentFile = mfilename('fullpath');
  6. [pathstr,~,~] = fileparts(currentFile);
  7. cd(fullfile(pathstr,'..', '..'))
  8. rootpath = pwd;
  9. end
  10. % inputs
  11. pn.eeg_BIDS = fullfile(rootpath, 'data', 'inputs', 'rawdata', 'eeg_BIDS');
  12. pn.channel_locations = fullfile(rootpath, 'data', 'inputs', 'rawdata', 'channel_locations');
  13. pn.events = fullfile(rootpath, 'data', 'inputs', 'rawdata', 'events');
  14. pn.tools = fullfile(rootpath, 'tools');
  15. % outputs
  16. pn.eeg_ft = fullfile(rootpath, 'data', 'outputs', 'eeg');
  17. pn.history = fullfile(rootpath, 'data', 'outputs', 'history');
  18. if ismac % run if function is not pre-compiled
  19. addpath(fullfile(pn.tools, 'fieldtrip')); ft_defaults;
  20. addpath(fullfile(pn.tools, 'helpers'));
  21. end
  22. %% define IDs for visual screening
  23. % N = 33
  24. IDs = tdfread(fullfile(pn.eeg_BIDS, 'participants.tsv'));
  25. IDs = cellstr(IDs.participant_id);
  26. id = str2num(id);
  27. % load data
  28. cfg_preproc = [];
  29. cfg_preproc.datafile = ...
  30. fullfile(pn.eeg_BIDS, IDs{id}, 'eeg', [IDs{id}, '_task-xxxx_eeg.eeg']);
  31. % load header & event information
  32. config = ft_read_header(cfg_preproc.datafile);
  33. config.data_file = cfg_preproc.datafile;
  34. config.mrk = ft_read_event(cfg_preproc.datafile);
  35. % define reading & preprocessing parameters
  36. cfg_preproc.channel = {'all'};
  37. % get all data first, then apply specific steps only for subsets
  38. data_eeg = ft_preprocessing(cfg_preproc);
  39. %% preprocessing
  40. cfg_preproc = [];
  41. cfg_preproc.channel = {'all'};
  42. cfg_preproc.continuous = 'yes';
  43. cfg_preproc.demean = 'yes';
  44. cfg_preproc.reref = 'yes';
  45. cfg_preproc.refmethod = 'avg';
  46. cfg_preproc.refchannel = {'M1', 'M2'};
  47. cfg_preproc.implicitref = 'POz';
  48. cfg_preproc.hpfilter = 'yes';
  49. cfg_preproc.hpfreq = 1;
  50. cfg_preproc.hpfiltord = 4;
  51. cfg_preproc.hpfilttype = 'but';
  52. cfg_preproc.lpfilter = 'yes';
  53. cfg_preproc.lpfreq = 100;
  54. cfg_preproc.lpfiltord = 4;
  55. cfg_preproc.lpfilttype = 'but';
  56. data_eeg = ft_preprocessing(cfg_preproc, data_eeg);
  57. % define settings for resampling
  58. cfg_resample.resamplefs = 250;
  59. cfg_resample.detrend = 'no';
  60. cfg_resample.feedback = 'no';
  61. cfg_resample.trials = 'all';
  62. data_eeg = ft_resampledata(cfg_resample,data_eeg);
  63. % change data precision to single
  64. for t = 1:length(data_eeg.trial)
  65. data_eeg.trial{t} = single(data_eeg.trial{t});
  66. end; clear t
  67. %% save outputs
  68. save(fullfile(pn.eeg_ft, [IDs{id}, '_task-xxxx_eeg_raw.mat']),'data_eeg');
  69. % save config if it does not exist yet (otherwise we risk overwriting manual segs)
  70. if ~exist(fullfile(pn.history, [IDs{id}, '_task-xxxx_config.mat']),'file')
  71. save(fullfile(pn.history, [IDs{id}, '_task-xxxx_config.mat']),'config');
  72. end
  73. % clear variables
  74. clear cfg_* config data_eeg
  75. end