perturb_response_plot_psth_vF.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. function perturb_response_plot_psth_vF(spikeTimes, alignmentTimes, unitData, timeWindow, micData, thisType, pert_time, syll_lens, songIdx, figNum)
  2. %%%%
  3. % this plots a figure showing unit response to different perturbations
  4. %%%%
  5. % spike times is a simple vector of times when spike occurs (sec)
  6. % alignment Times is cell array where each entry contains a vector of start
  7. % times for all perturbations of a given type
  8. % unitData is unit #
  9. % window is time (sec) before and after 0 for each plot
  10. % micData is microphone data, used to generate spectrogram
  11. %%%%
  12. % function generates single figure window:
  13. % - A DIFFERENT COLUMN FOR EACH PERTURBATION TYPE! THIS COULD BE AS MANY AS
  14. % 19 COLUMNS! (but only 13 for first try because we messed up
  15. % perturbations)
  16. % - each column has spectrogram at top, raster in middle, psth at bottom
  17. %%%%
  18. fs = 40000;
  19. % do stuff based on type
  20. if contains(thisType, 'W')
  21. pert_time = pert_time;% - 0.05; %0.05 commented out
  22. pert_lens = syll_lens + 0.2;
  23. elseif contains(thisType, 'I')
  24. pert_time = pert_time + 0.05;
  25. pert_lens = 0.05 + 0.2;
  26. end
  27. %% first generate spectrograms
  28. % find relevant times in mic data and compute average
  29. for pertIdx = 1:length(alignmentTimes)
  30. these_ATs = alignmentTimes{pertIdx};
  31. spec_ATs = these_ATs(1:25); % to avoid blending
  32. micRows = zeros(length(spec_ATs),round((timeWindow(2)-timeWindow(1))*fs)+1);
  33. for trialIdx = 1:length(spec_ATs)
  34. thisIdxRange = round((these_ATs(trialIdx)+timeWindow)*fs);
  35. micRows(trialIdx,:) = micData(thisIdxRange(1):thisIdxRange(2))';
  36. end
  37. avgMic = mean(micRows,1);
  38. winLen = round(.005*fs); % 1ms window on spec
  39. freqs = 1:10:8001; % step to 8kHz in 10Hz bins
  40. figure(figNum);
  41. subplot(4,length(alignmentTimes),pertIdx)
  42. spectrogram(avgMic, winLen, floor(winLen/2), freqs, fs, 'yaxis');
  43. % this stuff to be inserted with actual plotting
  44. lim = caxis;
  45. span = diff(caxis);
  46. newspan = span/2;
  47. newlim1 = lim(2) - newspan;
  48. caxis([newlim1 lim(2)])
  49. set(gca, 'Xticklabel', []);
  50. set(gca, 'Yticklabel', []);
  51. title(['Unit ' num2str(unitData)])
  52. xlabel([])
  53. ylabel([])
  54. colorbar('off')
  55. %% now do PSTHs and ssa idx
  56. [~, bins, ~, ~, ~, ba] = psthAndBA(spikeTimes, these_ATs, timeWindow, 0.001); %1ms bin size
  57. [tr,b] = find(ba);
  58. [rasterX,yy] = rasterize(bins(b));
  59. rasterY = yy+reshape(repmat(tr',3,1),1,length(tr)*3); % yy is of the form [0 1 NaN 0 1 NaN...] so just need to add trial number to everything
  60. % scale the raster ticks
  61. rasterY(2:3:end) = rasterY(2:3:end)+floor(numel(these_ATs)/100);
  62. % PSTH smoothing filter
  63. % gw = gausswin(round(*6),3); % 5 ms gaussian window
  64. % smWin = gw./sum(gw);
  65. %
  66. % % smooth ba
  67. % baSm = conv2(smWin,1,ba', 'same')'./.001;
  68. psthSm = mean(ba); % mean(baSm) when smoothing
  69. if pertIdx == 1
  70. std_bins = bins;
  71. std_psth = psthSm;
  72. end
  73. figure(figNum);
  74. subplot(4,length(alignmentTimes),...
  75. [pertIdx+length(alignmentTimes), pertIdx+2*length(alignmentTimes),...
  76. pertIdx+3*length(alignmentTimes)])
  77. plot(rasterX, rasterY, 'k', 'MarkerSize', 1);
  78. xlim(timeWindow);
  79. xlabel('Time (s)')
  80. ylim([0 length(these_ATs)+1]);
  81. % makepretty;
  82. % if pertIdx ~= 1
  83. pTime = pert_time;
  84. pLen = pert_lens;
  85. hold on;
  86. lim_of_y = ylim;
  87. if songIdx == 1
  88. yl = [lim_of_y(1) 75+1];
  89. else
  90. yl = [lim_of_y(1) 50+1];
  91. end
  92. v1 = [pTime yl(1); pTime yl(2); pTime+pLen yl(1); pTime+pLen yl(2)];
  93. f1 = [1 2 4 3];
  94. patch('Faces', f1, 'Vertices', v1, 'FaceColor', 'k', 'FaceAlpha', 0.25);
  95. h1 = refline(0,25.5);
  96. h1.Color = 'r';
  97. if songIdx == 1
  98. h3 = refline(0,50.5);
  99. h3.Color = 'r';
  100. end
  101. hold off;
  102. % end
  103. if pertIdx == 1
  104. ylabel(['event number'])
  105. end
  106. xlabel(['Time (mot. on, s)'])
  107. % if pertIdx == 1
  108. % % ylabel(['Avg. Firing Rate (Hz)'])
  109. % end
  110. xlim(timeWindow)
  111. end