RS_PlottingIntro.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. %% define some variables
  2. filt = 6; % 1: non, 2: 0.1 Hz, 3: 10 Hz, 4: 40 Hz, 5: 45 Hz, 6: 50 Hz
  3. cutTime = 1; % plot only certain time window? (0: no, 1: short, 2: long)
  4. avType = 1; % plot grand average (1) or individual averages (2)
  5. repRateI = 2; % which rep rate to plot (1: 25 ms, 2: 50 ms)
  6. fixedY = 1; % use dynamic (0) or fixed (1) y-axis?
  7. plotSpecI = 0; % plot spectrogram of the stimulus?
  8. plotOsciI = 0; % plot oscillogram of the stimulus?
  9. plotRespI = 1; % plot responses?
  10. saveI = 1; % save figure?
  11. chirpPeak = [0.00648,0.00842];
  12. %% load processed data
  13. switch repRateI
  14. case 1
  15. load('RS_avData_25ms.mat') % load data
  16. case 2
  17. load('RS_avData_50ms.mat') % load data
  18. end
  19. col = 'k';
  20. %% do some additional calculations
  21. % define y-label name
  22. switch zsNormI
  23. case 0
  24. ylabelName = 'Voltage [µV]';
  25. case 1
  26. ylabelName = 'z-norm. voltage [a.u.]';
  27. end
  28. % define x-Axis
  29. switch cutTime
  30. case 0
  31. xtickSteps = 100; % ms-steps on x-axis that should be plotted
  32. case 1
  33. xtickSteps = 5; % ms-steps on x-axis that should be plotted
  34. switch filt
  35. case {1,2,3}
  36. timeStart = -3; % starting time of the plot (relative to stim onset)
  37. timeEnd = 10; % ending time of the plot (relative to stim onset)
  38. case {4,5,6}
  39. timeStart = -3; % starting time of the plot (relative to stim onset)
  40. timeEnd = 12; % ending time of the plot (relative to stim onset)
  41. end
  42. case 2
  43. xtickSteps = 50; % ms-steps on x-axis that should be plotted
  44. timeStart = 0; % starting time of the plot (relative to stim onset)
  45. timeEnd = 350; % ending time of the plot (relative to stim onset)
  46. end
  47. % define some names
  48. combNameS = split(combName,","); % split comb names at comma
  49. if filt==1
  50. filtName = 'noF';
  51. else
  52. filtName = num2str(lowc(filt-1));
  53. end
  54. % define y-axis depending on filter setting and plotted time frame
  55. switch filt
  56. case 1
  57. switch cutTime
  58. case 0
  59. y_lim = [-0.8,1];
  60. case 1
  61. y_lim = [-0.8,1];
  62. case 2
  63. y_lim = [-0.8,1];
  64. end
  65. case 2
  66. switch cutTime
  67. case 0
  68. y_lim = [-0.8,1];
  69. case 1
  70. y_lim = [-0.8,1];
  71. case 2
  72. y_lim = [-0.8,1];
  73. end
  74. case 3
  75. switch cutTime
  76. case 0
  77. y_lim = [-0.9,1];
  78. case 1
  79. y_lim = [-0.3,0.55];
  80. case 2
  81. y_lim = [-0.9,1];
  82. end
  83. case 4
  84. switch cutTime
  85. case 0
  86. y_lim = [-0.9,1];
  87. case 1
  88. y_lim = [-0.3,0.55];
  89. case 2
  90. y_lim = [-0.9,1];
  91. end
  92. case 5
  93. switch cutTime
  94. case 0
  95. y_lim = [-0.9,1];
  96. case 1
  97. y_lim = [-0.3,0.55];
  98. case 2
  99. y_lim = [-0.9,1];
  100. end
  101. case 6
  102. switch cutTime
  103. case 0
  104. y_lim = [-1,1];
  105. case 1
  106. y_lim = [-0.35,0.5];
  107. case 2
  108. y_lim = [-0.9,1];
  109. end
  110. end
  111. % do some prior calculations for colormap
  112. rgb1 = [ones(1,100),ones(1,100),(1:-0.01:0.01)]';
  113. rgb2 = [ones(1,100),(1:-0.01:0.01),zeros(1,100)]';
  114. rgb3 = [(1:-0.01:0.01),zeros(1,100),zeros(1,100)]';
  115. cMap = [rgb1,rgb2,rgb3];
  116. % do some prior calculations for tiled plots
  117. yMax = 11; % y-axis maximum in kHz for spectrogram
  118. % number of rows and colums of the tiled figure
  119. nCol = 2;
  120. nRow = 0; % depending on what is plotted, number of rows will change
  121. % add rows depending on what is plotted
  122. if plotOsciI==1
  123. nRow = nRow+1;
  124. end
  125. if plotSpecI==1
  126. nRow = nRow+1;
  127. end
  128. if plotRespI==1
  129. nRow = nRow+1;
  130. end
  131. %% plotting: stimuli and responses
  132. for f = 1:2 % once for each figure (block-stim and o1st-stim)
  133. % figure('NumberTitle','off','Name',[num2str(f),'_',SOAName,],'Position',[0,0,800,1300])
  134. figure('NumberTitle','off','Name',[num2str(f),'_',SOAName,],'Position',[0,0,1600,800])
  135. set(gca,'FontSize',20,'Linewidth',2,'FontName','Arial')
  136. t = tiledlayout(nRow,nCol);
  137. tileCount = 0; % counter of plotted tiles, important for later
  138. switch f
  139. case 1
  140. nComb = (1:2);
  141. case 2
  142. nComb = (3:4);
  143. end
  144. for s = 1:2 % run once for each stimulus combination
  145. c = (f-1)*2+s;
  146. % extract data corresponding to current stimulation-condition from
  147. % cell-arrays
  148. dataAv = dataAv_cell{c};
  149. dataGrAv = dataGrAv_cell{c};
  150. dataSe = dataSe_cell{c};
  151. filenames = filenames_stim_cell{c};
  152. recID = recID_cell{c};
  153. bsPos = cell2mat(strfind(filenames,'\')); % find positions of back slashes in filename
  154. startPos = bsPos(:,end); % use last back slash position as starting point
  155. filenames = extractAfter(filenames,startPos);
  156. timetr = round(timetrUnit_cell{c}*1000,4);
  157. stimDur = stimDur_cell{c}(1)*1000;
  158. stimDelay = stimDelay_cell{c}(1)*1000;
  159. stimWin = [stimDelay,stimDelay+stimDur];
  160. nFiles = nFiles_cell{c};
  161. fs_stim = fs_stim_cell{c};
  162. fs = fs_cell{c};
  163. stimCat = stimCat_cell{c};
  164. pts2begin = pts2begin_cell{c};
  165. % process timetrace
  166. switch cutTime
  167. case 0
  168. timeWin = floor([timetr(1),timetr(end)]); % data in this window will be plotted (relative to recording onset)
  169. timeStart = timeWin(1)-(stimDelay);
  170. timeEnd = timeWin(2)-(stimDelay);
  171. timeCut = (1:size(timetr,2));
  172. case {1,2}
  173. timeWin = [round((stimDelay)+timeStart+chirpPeak(s)*1000,4),...
  174. round((stimDelay)+timeEnd+chirpPeak(s)*1000,4)]; % data in this window will be plotted (relative to recording onset)
  175. timeCut = round(timeWin(1)/1000*fs:timeWin(2)/1000*fs+1); % "+1" to reach the proper length, otherwise the index will be 1 point too short since timetr starts at 0 ms
  176. end
  177. nexttile(s)
  178. % spectrogram
  179. if plotSpecI==1
  180. spectrogram(stimCat,56,52,[],fs_stim,'yaxis',"MinThreshold",-75)
  181. colormap(cMap)
  182. xLimAuto = xlim;
  183. xlim([xLimAuto(1),timeEnd])
  184. xticks(0:xtickSteps:timeEnd)
  185. ylim([0,yMax])
  186. colorbar('off')
  187. if s==1||s==3||s==6
  188. ylabel('Frequency [kHz]','FontWeight','bold')
  189. else
  190. ylabel([])
  191. end
  192. xlabel([])
  193. box on
  194. set(gca,'FontSize',20,'Linewidth',2,'FontName','Arial')
  195. tileCount = tileCount+1;
  196. end
  197. hold on
  198. title(combNameS(s))
  199. switch nRow
  200. case 2
  201. switch tileCount
  202. case {1,3}
  203. nexttile(s+nCol) % open new tile
  204. end
  205. case 3
  206. switch tileCount
  207. case {1,4}
  208. nexttile(s+nCol) % open new tile
  209. end
  210. end
  211. % oscillogram
  212. if plotOsciI==1
  213. box on
  214. xs = (0:length(stimCat)-1)/fs_stim*1000;
  215. plot(xs,stimCat,'k','Linewidth',2)
  216. xlim([0,timeEnd])
  217. xticks(0:xtickSteps:timeEnd)
  218. if s==1||s==3||s==nCol+1
  219. ylabel('SPL [a.u.]','FontWeight','bold')
  220. end
  221. set(gca,'FontSize',20,'Linewidth',2,'FontName','Arial')
  222. tileCount = tileCount+1;
  223. end
  224. switch nRow
  225. case 2
  226. switch tileCount
  227. case {1,3}
  228. nexttile(s+nCol) % open new tile
  229. end
  230. case 3
  231. switch tileCount
  232. case {1,4}
  233. nexttile(s+nCol) % open new tile
  234. case {2,5}
  235. nexttile(s+nCol+2) % open new tile
  236. end
  237. end
  238. % responses
  239. if plotRespI==1
  240. hold on
  241. box on
  242. % plot standard error
  243. seHiCtrl = dataGrAv(timeCut,filt)+dataSe(timeCut,filt);
  244. seLoCtrl = dataGrAv(timeCut,filt)-dataSe(timeCut,filt);
  245. seX = [timetr(timeCut),fliplr(timetr(timeCut))];
  246. seY = [seHiCtrl',fliplr(seLoCtrl')];
  247. patch(seX,seY,col,'FaceAlpha',0.2,'EdgeColor','none')
  248. % plot data
  249. plotGrAv = plot(timetr(timeCut),dataGrAv(timeCut,filt),col,'Linewidth',2);
  250. xticks(timeWin(1):xtickSteps:timeWin(2))
  251. set(gca,'XTickLabel',timeStart:xtickSteps:timeEnd)
  252. xlabel('Time [ms]','FontWeight','bold','FontSize',20)
  253. if s==1||s==3||s==nCol+1
  254. ylabel(ylabelName,'FontWeight','bold','FontSize',20)
  255. end
  256. set(gca,'FontSize',20,'Linewidth',2,'FontName','Arial')
  257. switch fixedY
  258. case 0
  259. y_limAuto = ylim;
  260. y_min = y_limAuto(1);
  261. y_max = y_limAuto(2);
  262. case 1
  263. y_min = y_lim(1);
  264. y_max = y_lim(2);
  265. ylim(y_lim)
  266. end
  267. xlim([timeWin(1),timeWin(2)])
  268. % if plotStatsI==1
  269. % y_temp = [y_min;y_min;y_max;y_max];
  270. % y = repmat(y_temp,1,uWin(end));
  271. % for r = startResp:nResp
  272. % for w = uWin
  273. % switch daSe
  274. % case {1,2,3}
  275. % switch plotDaSeI
  276. % case 1
  277. % statsV = pV(c,filt,r,w);
  278. % markWin = patch('XData',x(1,w,:),'YData',y(:,w),'EdgeColor','black','EdgeAlpha',1,'FaceColor','none','Linewidth',2,'LineStyle','--'); % window for first response, to indicate area all other responses were compared to
  279. % uistack(markWin,'bottom')
  280. % case 2
  281. % statsV = multcompV{c,filt,w}.pValue(2);
  282. % end
  283. % case 4 % comparison between 1st and second ABR (16 ms del)
  284. % statsV = pV(c,filt);
  285. % markWin = patch('XData',x(r,4,:),'YData',y(:,4),'EdgeColor','black','EdgeAlpha',1,'FaceColor','none','Linewidth',2,'LineStyle','--'); % window for first response, to indicate area 2nd responses was compared to
  286. % uistack(markWin,'bottom')
  287. % end
  288. % if statsV<0.05
  289. % sigWin = patch('XData',x(r,w,:),'YData',y(:,w),'EdgeColor','black','EdgeAlpha',1,'FaceColor','black','FaceAlpha',.2,'Linewidth',2);
  290. % else
  291. % sigWin = patch('XData',x(r,w,:),'YData',y(:,w),'EdgeColor','black','EdgeAlpha',1,'FaceColor','none','Linewidth',2);
  292. % end
  293. % uistack(sigWin,'bottom')
  294. % end
  295. % end
  296. % end
  297. tileCount = tileCount+1;
  298. end
  299. xticks(timeWin(1):3:timeWin(2))
  300. set(gca,'XTickLabel',timeStart:3:timeEnd)
  301. end
  302. if saveI==1
  303. saveas(gcf,[num2str(f),'_',filtName,'_',SOAName,'_cutTime',num2str(cutTime),'_to',num2str(timeEnd),'.jpg'])
  304. saveas(gcf,[num2str(f),'_',filtName,'_',SOAName,'_cutTime',num2str(cutTime),'_to',num2str(timeEnd),'.svg'])
  305. saveas(gcf,[num2str(f),'_',filtName,'_',SOAName,'_cutTime',num2str(cutTime),'_to',num2str(timeEnd),'.fig'])
  306. end
  307. end
  308. bla = 1;