a1_prepare_preprocessing.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. %% datadef
  20. cond_eeg = 'dynamic';
  21. %% define IDs for preprocessing
  22. % N = 33
  23. IDs = tdfread(fullfile(pn.eeg_BIDS, 'participants.tsv'));
  24. IDs = cellstr(IDs.participant_id);
  25. for id = 1:length(IDs)
  26. if ~exist(fullfile(pn.eeg_ft, [IDs{id}, '_task-xxxx_eeg_raw.mat']),'file')
  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. cfg_preproc.implicitref = 'REF'; % recover implicit reference
  38. % get all data first, then apply specific steps only for subsets
  39. data_eeg = ft_preprocessing(cfg_preproc);
  40. %% preprocessing
  41. cfg_preproc = [];
  42. cfg_preproc.channel = {'all'};
  43. cfg_preproc.continuous = 'yes';
  44. cfg_preproc.demean = 'yes';
  45. cfg_preproc.reref = 'yes';
  46. cfg_preproc.refchannel = {'M1', 'M2'};
  47. cfg_preproc.implicitref = 'A2';
  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
  70. save(fullfile(pn.history, [IDs{id}, '_task-xxxx_config.mat']),'config');
  71. % clear variables
  72. clear cfg_* config data_eeg
  73. end
  74. end; clear id;