a5_segmentation_raw_data.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. function a5_segmentation_raw_data(id, rootpath)
  2. if ismac % run if function is not pre-compiled
  3. id = '1'; % test for example subject
  4. currentFile = mfilename('fullpath');
  5. [pathstr,~,~] = fileparts(currentFile);
  6. cd(fullfile(pathstr,'..', '..'))
  7. rootpath = pwd;
  8. end
  9. % inputs
  10. pn.eeg_BIDS = fullfile(rootpath, 'data', 'inputs', 'rawdata', 'eeg_BIDS');
  11. pn.channel_locations = fullfile(rootpath, 'data', 'inputs', 'rawdata', 'channel_locations');
  12. pn.events = fullfile(rootpath, 'data', 'inputs', 'rawdata', 'events');
  13. pn.tools = fullfile(rootpath, 'tools');
  14. % outputs
  15. pn.eeg_ft = fullfile(rootpath, 'data', 'outputs', 'eeg');
  16. pn.history = fullfile(rootpath, 'data', 'outputs', 'history');
  17. if ismac % run if function is not pre-compiled
  18. addpath(fullfile(pn.tools, 'fieldtrip')); ft_defaults;
  19. addpath(fullfile(pn.tools, 'helpers'));
  20. end
  21. % N = 33
  22. IDs = tdfread(fullfile(pn.eeg_BIDS, 'participants.tsv'));
  23. IDs = cellstr(IDs.participant_id);
  24. id = str2num(id);
  25. display(['processing ID ' num2str(IDs{id})]);
  26. %% load raw data
  27. if ~exist(fullfile(pn.eeg_ft, [IDs{id}, '_task-xxxx_eeg_seg.mat']),'file')
  28. % load config
  29. config = [];
  30. load(fullfile(pn.history, [IDs{id}, '_task-xxxx_config.mat']),'config');
  31. %% load events file
  32. events = readtable(fullfile(pn.events, ['EMP',IDs{id}(end-1:end),'_events.csv']));
  33. %% generate segmentation structure
  34. % load marker file
  35. mrk = config.mrk;
  36. % generate trial structure
  37. mrk_val = {mrk(1,:).value};
  38. indOnset = find(strcmp(mrk_val(:),'boundary')) + 1;
  39. mrk_val = mrk_val(indOnset:end);
  40. % Stim trials
  41. fsample = 512;
  42. trl = zeros(length(mrk_val),3);
  43. TOI1 = 1*fsample; % segmentation before trigger (s x fsample)
  44. TOI2 = 2*fsample; % segmentation following trigger (s x fsample)
  45. for j = 1:size(trl,1)
  46. trl(j,1) = mrk(1,indOnset+j-1).sample - TOI1;
  47. trl(j,2) = mrk(1,indOnset+j-1).sample + TOI2;
  48. trl(j,3) = -TOI1; % offset
  49. end; clear j
  50. % add trial structure to config
  51. config.trl = trl;
  52. %% load, filter, and re-reference raw data
  53. % define reading & preprocessing parameters
  54. % read in all data first
  55. % load data
  56. cfg = [];
  57. cfg.datafile = ...
  58. fullfile(pn.eeg_BIDS, IDs{id}, 'eeg', [IDs{id}, '_task-xxxx_eeg.eeg']);
  59. cfg.trl = config.trl;
  60. cfg.channel = {'all'};
  61. data_eeg = ft_preprocessing(cfg);
  62. %% reref & filter EEG data
  63. cfg = [];
  64. cfg.continuous = 'yes';
  65. cfg.demean = 'yes';
  66. cfg.reref = 'yes';
  67. cfg.refmethod = 'avg';
  68. cfg.refchannel = {'M1', 'M2'};
  69. cfg.implicitref = 'POz';
  70. cfg.hpfilter = 'yes';
  71. cfg.hpfreq = .2;
  72. cfg.hpfiltord = 4;
  73. cfg.hpfilttype = 'but';
  74. cfg.lpfilter = 'yes';
  75. cfg.lpfreq = 125;
  76. cfg.lpfiltord = 4;
  77. cfg.lpfilttype = 'but';
  78. % get data
  79. data_eeg = ft_preprocessing(cfg, data_eeg);
  80. flt = cfg;
  81. % clear cfg structure
  82. clear cfg
  83. %% resample to 500 Hz
  84. % define settings for resampling
  85. cfg.resamplefs = 500;
  86. cfg.detrend = 'no';
  87. cfg.feedback = 'no';
  88. cfg.trials = 'all';
  89. data_eeg = ft_resampledata(cfg,data_eeg);
  90. resample = cfg;
  91. % clear variables
  92. clear cfg
  93. % update config
  94. config.seg.flt = flt;
  95. config.seg.resample = resample;
  96. %% save eeg data and history
  97. save(fullfile(pn.eeg_ft, [IDs{id}, '_task-xxxx_eeg_seg.mat']),'data_eeg', 'events');
  98. save(fullfile(pn.history, [IDs{id}, '_task-xxxx_config.mat']),'config');
  99. end % exist