a_eegmp_calculate_erp_tfr.m 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. data_eeg = ft_scalpcurrentdensity(csd_cfg, data_eeg);
  28. %% time-frequency transform using wavelets
  29. freq_cfg = [];
  30. freq_cfg.channel = 'all';
  31. freq_cfg.method = 'wavelet';
  32. freq_cfg.width = 5;
  33. freq_cfg.keeptrials = 'yes';
  34. freq_cfg.output = 'pow';
  35. freq_cfg.foi = 1:1:40;
  36. freq_cfg.toi = -1:0.05:2;
  37. tfr = ft_freqanalysis(freq_cfg, data_eeg);
  38. % single-trial log10-transform
  39. tfr.powspctrm = log10(tfr.powspctrm);
  40. %% single-trial baseline-correction for ERPs
  41. time = data_eeg.time{1};
  42. data_eeg_bl = data_eeg;
  43. for indTrial = 1:numel(data_eeg_bl.trial)
  44. curbl = squeeze(nanmean(data_eeg_bl.trial{indTrial}(:,time>-.2 & time<0),2));
  45. data_eeg_bl.trial{indTrial} = data_eeg_bl.trial{indTrial}-repmat(curbl,1,numel(time));
  46. end
  47. %% split tfr and time series data by condition, average across trials
  48. parameter = {'scene_category'; 'old'; 'behavior'; 'subsequent_memory'};
  49. for ind_param = 1:numel(parameter)
  50. conds.(parameter{ind_param}) = unique(events.(parameter{ind_param}));
  51. for ind_cond = 1:numel(conds.(parameter{ind_param}))
  52. cfg = [];
  53. cfg.trials = ismember(events.(parameter{ind_param}), ...
  54. conds.(parameter{ind_param}){ind_cond});
  55. tfravg.(parameter{ind_param}){ind_cond} = ft_freqdescriptives(cfg, tfr);
  56. erp.(parameter{ind_param}){ind_cond} = ft_timelockanalysis(cfg, data_eeg);
  57. erp_bl.(parameter{ind_param}){ind_cond} = ft_timelockanalysis(cfg, data_eeg_bl);
  58. end
  59. end
  60. erf = tfravg; clear tfravg;
  61. %% save data
  62. save(fullfile(pn.data_erp, [id, '_erp.mat']), 'erp', 'conds');
  63. save(fullfile(pn.data_erp, [id, '_erp_bl.mat']), 'erp_bl', 'conds');
  64. save(fullfile(pn.data_erf, [id, '_erf.mat']), 'erf', 'conds');