z_eegmp_old_new_ERP_trace.m 4.6 KB

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