a1_prepare_preprocessing.m 3.0 KB

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