a_eegmp_calculate_erp_tfr.m 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. function a_eegmp_calculate_erp_tfr(rootpath, id)
  2. %% paths
  3. if ismac
  4. currentFile = mfilename('fullpath');
  5. [pathstr,~,~] = fileparts(currentFile);
  6. cd(fullfile(pathstr,'..'))
  7. rootpath = pwd;
  8. id = 'sub-011';
  9. end
  10. pn.data_eeg = fullfile(rootpath, '..', 'eegmp_preproc', 'data', 'outputs', 'eeg');
  11. pn.data_erp = fullfile(rootpath, 'data', 'erp'); mkdir(pn.data_erp);
  12. pn.data_erf = fullfile(rootpath, 'data', 'erf'); mkdir(pn.data_erf);
  13. pn.tools = fullfile(rootpath, '..', 'eegmp_preproc', 'tools');
  14. addpath(fullfile(pn.tools, 'fieldtrip')); ft_defaults
  15. %% load data_eeg
  16. % load preprocessed eeg data_eeg
  17. load(fullfile(pn.data_eeg, [id,'_task-xxxx_eeg_art.mat']), 'data_eeg', 'events');
  18. %% further preprocessing
  19. % apply notch filter
  20. cfg = [];
  21. cfg.dftfilter = 'yes';
  22. data_eeg = ft_preprocessing(cfg, data_eeg);
  23. %% CSD transform
  24. csd_cfg = [];
  25. csd_cfg.elecfile = 'standard_1005.elc';
  26. csd_cfg.method = 'spline';
  27. csd_cfg.degree = 14;
  28. data_eeg = ft_scalpcurrentdensity(csd_cfg, data_eeg);
  29. %% time-frequency transform using superlets
  30. freq_cfg = [];
  31. freq_cfg.channel = 'all';
  32. freq_cfg.method = 'superlet';
  33. freq_cfg.width = 3;
  34. freq_cfg.keeptrials = 'yes';
  35. freq_cfg.output = 'pow';
  36. freq_cfg.foi = 1:1:40;
  37. freq_cfg.toi = -1:0.025:2;
  38. tfr = ft_freqanalysis(freq_cfg, data_eeg);
  39. % single-trial log10-transform
  40. tfr.powspctrm = log10(tfr.powspctrm);
  41. %% single-trial baseline-correction for ERPs
  42. time = data_eeg.time{1};
  43. data_eeg_bl = data_eeg;
  44. for indTrial = 1:numel(data_eeg_bl.trial)
  45. curbl = squeeze(nanmean(data_eeg_bl.trial{indTrial}(:,time>-.2 & time<0),2));
  46. data_eeg_bl.trial{indTrial} = data_eeg_bl.trial{indTrial}-repmat(curbl,1,numel(time));
  47. end
  48. %% split tfr and time series data by condition, average across trials
  49. parameter = {'scene_category'; 'old'; 'behavior'; 'subsequent_memory'};
  50. for ind_param = 1:numel(parameter)
  51. conds.(parameter{ind_param}) = unique(events.(parameter{ind_param}));
  52. for ind_cond = 1:numel(conds.(parameter{ind_param}))
  53. cfg = [];
  54. cfg.trials = ismember(events.(parameter{ind_param}), ...
  55. conds.(parameter{ind_param}){ind_cond});
  56. tfravg.(parameter{ind_param}){ind_cond} = ft_freqdescriptives(cfg, tfr);
  57. erp.(parameter{ind_param}){ind_cond} = ft_timelockanalysis(cfg, data_eeg);
  58. erp_bl.(parameter{ind_param}){ind_cond} = ft_timelockanalysis(cfg, data_eeg_bl);
  59. end
  60. end
  61. erf = tfravg; clear tfravg;
  62. %% save data
  63. save(fullfile(pn.data_erp, [id, '_erp.mat']), 'erp', 'conds');
  64. save(fullfile(pn.data_erp, [id, '_erp_bl.mat']), 'erp_bl', 'conds');
  65. save(fullfile(pn.data_erf, [id, '_erf.mat']), 'erf', 'conds');