z_eegmp_old_new_ERP_trace.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. currentFile = mfilename('fullpath');
  2. [pathstr,~,~] = fileparts(currentFile);
  3. cd(fullfile(pathstr,'..'))
  4. rootpath = pwd;
  5. pn.data_eeg = fullfile(rootpath, '..', 'eegmp_preproc', 'data', 'outputs', 'eeg');
  6. pn.data_erp = fullfile(rootpath, 'data', 'test');
  7. pn.data_erf = fullfile(rootpath, 'data', 'erf');
  8. pn.tools = fullfile(rootpath, 'tools');
  9. addpath(fullfile(rootpath, '..', 'eegmp_preproc', 'tools', 'fieldtrip')); ft_defaults
  10. addpath(fullfile(pn.tools, 'BrewerMap'));
  11. addpath(fullfile(pn.tools, 'shadedErrorBar'));
  12. % set custom colormap
  13. cBrew = brewermap(500,'RdBu');
  14. cBrew = flipud(cBrew);
  15. colormap(cBrew)
  16. %% load erp
  17. for ind_id = 1:33
  18. id = sprintf('sub-%03d', ind_id);
  19. load(fullfile(pn.data_erp, [id,'_erp_bl.mat']));
  20. if ind_id == 1
  21. erpgroup.old.old = erp_bl.old;
  22. erpgroup.old.old = ...
  23. rmfield(erpgroup.old.old, {'avg', 'var', 'dof'});
  24. erpgroup.old.old.dimord = 'sub_chan_time';
  25. erpgroup.old.new = erp_bl.new;
  26. erpgroup.old.new = ...
  27. rmfield(erpgroup.old.new, {'avg', 'var', 'dof'});
  28. erpgroup.old.new.dimord = 'sub_chan_time';
  29. end
  30. erpgroup.old.old.avg(ind_id,:,:) = erp_bl.old.avg;
  31. erpgroup.old.new.avg(ind_id,:,:) = erp_bl.new.avg;
  32. end
  33. time = erpgroup.old.old.time;
  34. elec = erpgroup.old.old.elec;
  35. channels = erpgroup.old.old.label;
  36. mergeddata = cat(4, erpgroup.old.old.avg, ...
  37. erpgroup.old.new.avg);
  38. %% visualize frontal ERP
  39. idx_chans = [38];
  40. % avg across channels and conditions
  41. condAvg = squeeze(nanmean(nanmean(mergeddata(:,idx_chans,:,1:2),2),4));
  42. h = figure('units','centimeters','position',[0 0 10 8]);
  43. cla; hold on;
  44. % highlight relevant phase in background
  45. patches.timeVec = [0.3 0.5];
  46. patches.colorVec = [1 .95 .8];
  47. for indP = 1:size(patches.timeVec,2)-1
  48. YLim = [-8 2]*10^-4;
  49. p = patch([patches.timeVec(indP) patches.timeVec(indP+1) patches.timeVec(indP+1) patches.timeVec(indP)], ...
  50. [YLim(1) YLim(1) YLim(2), YLim(2)], patches.colorVec(indP,:));
  51. p.EdgeColor = 'none';
  52. end
  53. % new value = old value ? subject average + grand average
  54. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,1),2));
  55. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  56. standError = nanstd(curData,1)./sqrt(size(curData,1));
  57. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  58. curData = squeeze(nanmean(mergeddata(:,idx_chans,:,2),2));
  59. curData = curData-condAvg+repmat(nanmean(condAvg,1),size(condAvg,1),1);
  60. standError = nanstd(curData,1)./sqrt(size(curData,1));
  61. l2 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'r','linewidth', 2}, 'patchSaturation', .1);
  62. % ax = gca; ax.YDir = 'reverse';
  63. legend([l1.mainLine, l2.mainLine],{'old', 'new'}, ...
  64. 'location', 'southwest'); legend('boxoff')
  65. xlabel('Time (s) from stim onset')
  66. xlim([-.05 .7]); %ylim([-.03 .18])
  67. ylabel({'ERP';'(microVolts)'});
  68. xlabel({'Time (s)'});
  69. set(findall(gcf,'-property','FontSize'),'FontSize',14)
  70. %% plot difference
  71. idx_chans = [38];
  72. h = figure('units','centimeters','position',[0 0 10 8]);
  73. cla; hold on;
  74. % highlight relevant phase in background
  75. patches.timeVec = [0.3 0.5];
  76. patches.colorVec = [1 .95 .8];
  77. for indP = 1:size(patches.timeVec,2)-1
  78. YLim = [-10 5]*10^-5;
  79. p = patch([patches.timeVec(indP) patches.timeVec(indP+1) patches.timeVec(indP+1) patches.timeVec(indP)], ...
  80. [YLim(1) YLim(1) YLim(2), YLim(2)], patches.colorVec(indP,:));
  81. p.EdgeColor = 'none';
  82. end
  83. % new value = old value ? subject average + grand average
  84. curData = squeeze(mergeddata(:,idx_chans,:,2)-mergeddata(:,idx_chans,:,1));
  85. standError = nanstd(curData,1)./sqrt(size(curData,1));
  86. l1 = shadedErrorBar(time,nanmean(curData,1),standError, 'lineprops', {'color', 'k','linewidth', 2}, 'patchSaturation', .1);
  87. legend([l1.mainLine],{'new-old'}, ...
  88. 'location', 'southwest'); legend('boxoff')
  89. xlabel('Time (s) from stim onset')
  90. xlim([-.05 .7]); %ylim([-.03 .18])
  91. ylabel({'ERP';'(microVolts)'});
  92. xlabel({'Time (s)'});
  93. set(findall(gcf,'-property','FontSize'),'FontSize',14)
  94. %% plot difference between conditions
  95. h = figure('units','centimeters','position',[0 0 10 10]);
  96. set(gcf,'renderer','Painters')
  97. cfg = [];
  98. cfg.layout = 'biosemi64.lay';
  99. cfg.parameter = 'powspctrm';
  100. cfg.comment = 'no';
  101. cfg.colormap = cBrew;
  102. cfg.colorbar = 'EastOutside';
  103. plotData = [];
  104. plotData.label = elec.label(1:64); % {1 x N}
  105. plotData.dimord = 'chan';
  106. oldminnew = mergeddata(:,:,:,1)-mergeddata(:,:,:,2);
  107. plotData.powspctrm = squeeze(nanmean(nanmean(oldminnew(:,1:64,time>=.3 & time <=.5),3),1))';
  108. cfg.figure = h;
  109. ft_topoplotER(cfg,plotData);
  110. cb = colorbar('location', 'EastOutside'); set(get(cb,'ylabel'),'string','Amplitude');