Shadron_Pena_Fig5.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. %% Does the actual reliability calculations. Ruff removed HRTF's have less
  2. % directions, so downsample normal files. Makes things go faster too.
  3. % Currently at 10 iterations
  4. clear
  5. cd('F:\Shadron_Pena_2022\')
  6. %cd('C:\Users\penalab2\Documents\MATLAB\Brian_Reliability')
  7. addpath('F:\Shadron_Pena_2022')
  8. % % Load HRIRs
  9. owl_folder{1}='Merry';
  10. owl_folder{2}='Owl19';
  11. owl_folder{3}='Owl39';
  12. owl_folder{4}='Ugly';
  13. owl_folder{5}='Vespucci';
  14. haircut_id{1}='normal';
  15. haircut_id{2}='ohne'; % maximum cut
  16. date_folder = '10-24-22 Data'; %change to current date
  17. %% This anlaysis is similar to Figure 1 analysis, but no maskers are used.
  18. for o=1:5
  19. for h=1:2
  20. clearvars -except h haircut_id o owl_folder date_folder
  21. load(['HRTF_Wagner_Lab\results\' owl_folder{o} '\' owl_folder{o} '__' haircut_id{h} '__hrir_result.mat'])
  22. newazimuths = -160:20:160;
  23. newelevations = -60:20:80;
  24. dir=NaN(2,length(azimuths)*length(elevations));
  25. TF1=NaN(length(azimuths)*length(elevations),size(hrir_l,3));
  26. TF2=NaN(length(azimuths)*length(elevations),size(hrir_l,3));
  27. count=0;
  28. for a=1:length(azimuths)
  29. for e=1:length(elevations)
  30. count=count+1;
  31. dir(:,count)=[elevations(e);azimuths(a)];
  32. TF1(count,:)=hrir_l(a,e,:);
  33. TF2(count,:)=hrir_r(a,e,:);
  34. end
  35. end
  36. newdir=NaN(2,length(newazimuths)*length(newelevations));
  37. count=0;
  38. for a=1:length(newazimuths)
  39. for e=1:length(newelevations)
  40. count=count+1;
  41. newdir(:,count)=[newelevations(e);newazimuths(a)];
  42. end
  43. end
  44. dir = dir';
  45. newdir = newdir';
  46. downsample = ismember(dir, newdir, 'rows');
  47. TF1 = TF1(downsample, :);
  48. TF2 = TF2(downsample, :);
  49. dir = newdir';
  50. azimuth = newazimuths;
  51. elevation = newelevations;
  52. clear newdir newazimuth newelevation
  53. % % Analysis parameters
  54. %center frequencies of cochlear filters (Hz)
  55. cF = 400:200:10000;
  56. %cF = linspace(1000,9000, 9);
  57. n_cF=length(cF);
  58. %Sampling frequency for HRTFs (Hz)
  59. Fs = samplingrate;
  60. %Factor by which you will upsample
  61. Factor = 5;
  62. %Number of trials for each condition
  63. Nt=10;
  64. % % Get cochlear filters
  65. %Upsampled frequency
  66. FS=Fs*Factor;
  67. %Get filters
  68. fcoefs=getFilterCoefs(cF,FS);
  69. % % Number of directions and initialize
  70. n_dir = size(TF1,1);
  71. IPDm_all = zeros(n_dir, n_cF, Nt);
  72. IPDs_all = zeros(n_dir, n_cF, Nt);
  73. % ITDm=zeros(n_dir,n_cF);
  74. % ITDs=zeros(n_dir,n_cF);
  75. ILDm_all = zeros(n_dir, n_cF, Nt);
  76. ILDs_all = zeros(n_dir, n_cF, Nt);
  77. gainr_all = zeros(n_dir, n_cF, Nt);
  78. gainl_all = zeros(n_dir, n_cF, Nt);
  79. % %
  80. tic
  81. TF1_res = resample(TF1',Factor,1)';
  82. TF2_res = resample(TF2',Factor,1)';
  83. parfor idir = 1:n_dir % TODO parfor
  84. %Resample the HRIRs
  85. hL=TF1_res(idir,:);
  86. hR=TF2_res(idir,:);
  87. %Run over trials
  88. for tt=1:Nt
  89. %Generate a target sound
  90. s=genSignal(100,1/30,1,2,2*pi*12);
  91. s1=resample(s,Factor,1);
  92. %Convolve target with HRIR
  93. sL=conv(hL,s1);
  94. sR=conv(hR,s1);
  95. ITDp=zeros(n_cF);
  96. %cochlear (gammmatone) filterbank
  97. vL=ERBFilterBank(sL,fcoefs);
  98. vR=ERBFilterBank(sR,fcoefs);
  99. %Run over frequency and compute cross correlation
  100. for icF = 1:n_cF
  101. [itd,l]=xcorr(vL(icF,:),vR(icF,:));
  102. [~,j]=max(itd);
  103. ITDp(icF)=l(j)*1/Fs*1e6/Factor;
  104. end
  105. %Compute ILD
  106. R=rms(vR,2);
  107. L=rms(vL,2);
  108. Z=20*log10(R./L);
  109. GR=20*log10(R);
  110. GL=20*log10(L);
  111. %Run over frequency and compute mean and SD
  112. for n=1:n_cF
  113. IPD_all(idir,n,tt) =ITDp(n)*cF(n)/1e6*2*pi; %#ok<PFBNS>
  114. end
  115. %compute mean and SD for ILD.
  116. ILD_all(idir,:,tt) = Z;
  117. %compute gain
  118. gainr_all(idir,:,tt) =GR;
  119. gainl_all(idir,:,tt) =GL;
  120. end
  121. end
  122. IPDs = std(IPD_all,0,3);
  123. IPDm = mean(IPD_all, 3);
  124. ITDs = (1e6 * IPDs) ./ (2 * pi * cF);
  125. ITDm = (1e6 * IPDm) ./ (2 * pi * cF);
  126. ILDs = std(ILD_all,0,3);
  127. ILDm = sum(ILD_all, 3)/Nt;
  128. gainl = sum(gainl_all, 3)/Nt;
  129. gainr = sum(gainr_all, 3)/Nt;
  130. toc
  131. save(['Shadron_Pena_2022\', date_folder, '\', owl_folder{o} '__' haircut_id{h} '__hrtf_fig5.mat'])
  132. end
  133. end
  134. %% Some minor editing
  135. for o = 1:5
  136. for h = 1:2
  137. clearvars -except h haircut_id o owl_folder date_folder
  138. load(['Shadron_Pena_2022\', date_folder, '\', owl_folder{o} '__' haircut_id{h} '__hrtf_fig5.mat'])
  139. %have to wrap ITD's to make things make sense
  140. %Earlier had to convert 17x8x563 to a 136x563, so now have to revert
  141. %back to 17x8x20.
  142. itd = size(hrir_l, 3);
  143. y = size(cF, 2);
  144. IxD.IPDm = zeros(17,8,y);
  145. IxD.IPDs = zeros(17,8,y);
  146. IxD.ITDm = zeros(17,8,y);
  147. IxD.ITDs = zeros(17,8,y);
  148. IxD.ILDm = zeros(17,8,y);
  149. IxD.ILDs = zeros(17,8,y);
  150. IxD.gainl = zeros(17,8,y);
  151. IxD.gainr = zeros(17,8,y);
  152. for n = 1:y
  153. for a = 1:17
  154. for e = 1:8
  155. counter = (a-1)*8 + e;
  156. IxD.IPDm(a,e,n) = IPDm(counter,n);
  157. IxD.IPDs(a,e,n) = IPDs(counter,n);
  158. IxD.ITDm(a,e,n) = ITDm(counter,n);
  159. IxD.ITDs(a,e,n) = ITDs(counter,n);
  160. IxD.ILDm(a,e,n) = ILDm(counter,n);
  161. IxD.ILDs(a,e,n) = ILDs(counter,n);
  162. IxD.gainl(a,e,n) = gainl(counter,n);
  163. IxD.gainr(a,e,n) = gainr(counter,n);
  164. end
  165. end
  166. end
  167. clear n a e count Factor Fs FS idir n n_cF n_dir Nt tt itd ILDm ILDs IPDm IPDp IPDs ITDp ITDs ITDm
  168. clear samplingrate s1 s2 sL1 sR1 TF1 TF2 Z ax counter fcoef dir s fcoefs hL hR hrir_l hrir_r
  169. if size(IxD.IPDm,2) > 8 %Loop occurs if spatial locations are > 17 azimuth and > 9 elevation positions
  170. %Resamples to have locations that are
  171. %-160:20:160 in azimuth and -60:20:80 in elevation
  172. for n = 1:y
  173. for a = 1:2:34
  174. for e = 2:2:16
  175. IxD2.IPDm((a+1)/2, e/2, n) = IxD.IPDm(a,e,n);
  176. IxD2.IPDs((a+1)/2, e/2, n) = IxD.IPDs(a,e,n);
  177. IxD2.ITDm((a+1)/2, e/2, n) = IxD.ITDm(a,e,n);
  178. IxD2.ITDs((a+1)/2, e/2, n) = IxD.ITDs(a,e,n);
  179. IxD2.ILDm((a+1)/2, e/2, n) = IxD.ILDm(a,e,n);
  180. IxD2.ILDs((a+1)/2, e/2, n) = IxD.ILDs(a,e,n);
  181. end
  182. end
  183. end
  184. clear IxD
  185. IxD = IxD2;
  186. clear IxD2
  187. azimuths = -160:20:160;
  188. elevations = -60:20:80;
  189. end
  190. save(['Shadron_Pena_2022\', date_folder, '\', owl_folder{o} '__' haircut_id{h} '__stats_fig5.mat'])
  191. end
  192. end
  193. %% Compile all data into one struct
  194. clearvars -except h haircut_id o owl_folder date_folder
  195. condition = [];
  196. for o = [1 2 3 4 5]
  197. for h = 1:2
  198. clearvars -except h haircut_id o owl_folder condition date_folder
  199. load(['Shadron_Pena_2022\', date_folder, '\', owl_folder{o} '__' haircut_id{h} '__stats_fig5.mat'])
  200. condition.(owl_folder{o}).(haircut_id{h}) = IxD;
  201. end
  202. end
  203. clear IxD name
  204. %average the owls
  205. for h=1:2
  206. for o = [1 2 3 4 5]
  207. if o == 1
  208. condition.(haircut_id{h}).avg.IPDm = condition.(owl_folder{o}).(haircut_id{h}).IPDm;
  209. condition.(haircut_id{h}).avg.IPDs = condition.(owl_folder{o}).(haircut_id{h}).IPDs;
  210. condition.(haircut_id{h}).avg.ITDm = condition.(owl_folder{o}).(haircut_id{h}).ITDm;
  211. condition.(haircut_id{h}).avg.ITDs = condition.(owl_folder{o}).(haircut_id{h}).ITDs;
  212. condition.(haircut_id{h}).avg.ILDm = condition.(owl_folder{o}).(haircut_id{h}).ILDm;
  213. condition.(haircut_id{h}).avg.ILDs = condition.(owl_folder{o}).(haircut_id{h}).ILDs;
  214. condition.(haircut_id{h}).avg.gainl = condition.(owl_folder{o}).(haircut_id{h}).gainl;
  215. condition.(haircut_id{h}).avg.gainr = condition.(owl_folder{o}).(haircut_id{h}).gainr;
  216. else
  217. condition.(haircut_id{h}).avg.IPDs = condition.(haircut_id{h}).avg.IPDs + condition.(owl_folder{o}).(haircut_id{h}).IPDs;
  218. condition.(haircut_id{h}).avg.ITDm = condition.(haircut_id{h}).avg.ITDm + condition.(owl_folder{o}).(haircut_id{h}).ITDm;
  219. condition.(haircut_id{h}).avg.ITDs = condition.(haircut_id{h}).avg.ITDs + condition.(owl_folder{o}).(haircut_id{h}).ITDs;
  220. condition.(haircut_id{h}).avg.ILDm = condition.(haircut_id{h}).avg.ILDm + condition.(owl_folder{o}).(haircut_id{h}).ILDm;
  221. condition.(haircut_id{h}).avg.ILDs = condition.(haircut_id{h}).avg.ILDs + condition.(owl_folder{o}).(haircut_id{h}).ILDs;
  222. condition.(haircut_id{h}).avg.gainl = condition.(haircut_id{h}).avg.gainl + condition.(owl_folder{o}).(haircut_id{h}).gainl;
  223. condition.(haircut_id{h}).avg.gainr = condition.(haircut_id{h}).avg.gainr + condition.(owl_folder{o}).(haircut_id{h}).gainr;
  224. end
  225. end
  226. condition.(haircut_id{h}).avg.IPDm = condition.(haircut_id{h}).avg.IPDm / 5;
  227. condition.(haircut_id{h}).avg.IPDs = condition.(haircut_id{h}).avg.IPDs / 5;
  228. condition.(haircut_id{h}).avg.ITDm = condition.(haircut_id{h}).avg.ITDm / 5;
  229. condition.(haircut_id{h}).avg.ITDs = condition.(haircut_id{h}).avg.ITDs / 5;
  230. condition.(haircut_id{h}).avg.ILDm = condition.(haircut_id{h}).avg.ILDm / 5;
  231. condition.(haircut_id{h}).avg.ILDs = condition.(haircut_id{h}).avg.ILDs / 5;
  232. condition.(haircut_id{h}).avg.gainl = condition.(haircut_id{h}).avg.gainl/5;
  233. condition.(haircut_id{h}).avg.gainr = condition.(haircut_id{h}).avg.gainr/5;
  234. end
  235. %% Code for constructing Figure 5, model neurons
  236. %model neuron input parameters
  237. freq = 2000:1000:7000;
  238. width = length(freq);
  239. sigma = length(freq);
  240. itd = -800:800;
  241. ild = -20:0.01:20;
  242. %tuning for model neurons
  243. itd_peak = 0;
  244. ild_peak = 0;
  245. %max spike count
  246. A = 10;
  247. cF_ind = 9:5:34;
  248. normalITD = zeros(length(azimuths), length(elevations), length(freq));
  249. normalILD = zeros(length(azimuths), length(elevations), length(freq));
  250. ruffcutITD = zeros(length(azimuths), length(elevations), length(freq));
  251. ruffcutILD = zeros(length(azimuths), length(elevations), length(freq));
  252. for p = 1:length(freq)
  253. %model ITD curve
  254. itd_y = A* (exp(cos( (2*pi*freq(p)/1000000) *(itd - itd_peak))) - exp(-1) ) / (exp(1) - exp(-1));
  255. %model ILD curve
  256. ild_y = 10* exp( -(8)^-1* (ild - ild_peak).^2);
  257. %determine spike count given the ITD from HRTFs
  258. for q = 1:numel(condition.normal.avg.ITDm(:,:,p))
  259. [row, col] = ind2sub([17,8], q);
  260. normalITD(row,col,p) = itd_y(itd == round(condition.normal.avg.ITDm(row,col, cF_ind(p) ) ) );
  261. normalILD(row,col,p) = ild_y(round(ild,2) == round(condition.normal.avg.ILDm(row,col,cF_ind(p) ),2));
  262. ruffcutITD(row,col,p) = itd_y(itd == round(condition.ohne.avg.ITDm(row,col, cF_ind(p) ) ) );
  263. ruffcutILD(row,col,p) = ild_y(round(ild,2) == round(condition.ohne.avg.ILDm(row,col,cF_ind(p) ),2));
  264. end
  265. end
  266. normaltun = (normalITD.*normalILD)*.01;
  267. ruffcuttun = (ruffcutITD.*ruffcutILD)*.01;
  268. %% model neuron tuning across itds and freqs, normal HRTF
  269. normalmap = figure;
  270. set(gcf,'Position',[100 100 900 300]);
  271. sgtitle('Normal', FontSize=20)
  272. for plotID = 2:length(freq)
  273. subplot(1,length(freq), plotID-1)
  274. imagesc(azimuths(6:12), elevations(2:6), normaltun(6:12,2:6,plotID)'); axis xy; caxis([0 1]); colormap jet;
  275. title([num2str(freq(plotID)/1000), ' kHz'], FontSize=16);
  276. xticks([azimuths(6:3:12)])
  277. yticks([elevations(2:2:6)])
  278. ax = gca;
  279. ax.FontSize = 16;
  280. if plotID == 2
  281. ylabel('Elevation (°)', FontSize=16)
  282. e = text(-200,90, 'b');
  283. e.FontSize = 20;
  284. end
  285. if plotID == 2
  286. xlabel('Azimuth (°)', FontSize=16);
  287. end
  288. end
  289. subplot(1,length(freq), length(freq))
  290. cc = imagesc(azimuths, elevations, normaltun(:,:,plotID)'); axis xy; caxis([0 1]); colormap jet;
  291. c = colorbar('manual', 'Position', [.82 0.25 0.02, 0.5]);
  292. c.FontSize = 16;
  293. d = text(0, -90,'Norm. Spike Count');
  294. d.FontSize = 16; d.Rotation = 90;
  295. set(cc,'Visible', 'off')
  296. set(get(cc,'Children'),'Visible','off');
  297. axis off
  298. %saveas(normalmap, 'F:\Various Files\My Papers\Shadron and Pena 2022\eLife Figures\normalmap.png')
  299. %% model neuron tuning across itds and freqs, ruff-removed HRTF
  300. ruffcutmap = figure;
  301. set(gcf,'Position',[100 100 900 300]);
  302. sgtitle('Ruff-Removed', FontSize=20)
  303. for plotID = 2:length(freq)
  304. subplot(1,length(freq), plotID-1)
  305. imagesc(azimuths(6:12), elevations(2:6), ruffcuttun(6:12,2:6,plotID)'); axis xy; caxis([0 1]); colormap jet;
  306. title([num2str(freq(plotID)/1000), ' kHz'], FontSize=16);
  307. xticks([azimuths(6:3:12)])
  308. yticks([elevations(2:2:6)])
  309. ax = gca;
  310. ax.FontSize = 16;
  311. if plotID == 2
  312. ylabel('Elevation (°)', FontSize=16)
  313. e = text(-200,90, 'c');
  314. e.FontSize = 20;
  315. end
  316. if plotID == 2
  317. xlabel('Azimuth (°)', FontSize=16);
  318. end
  319. end
  320. subplot(1,length(freq), length(freq))
  321. cc = imagesc(azimuths, elevations, ruffcuttun(:,:,plotID)'); axis xy; caxis([0 1]); colormap jet;
  322. c = colorbar('manual', 'Position', [.82 0.25 0.02, 0.5]);
  323. c.FontSize = 16;
  324. d = text(0, -90,'Norm. Spike Count');
  325. d.FontSize = 16; d.Rotation = 90;
  326. set(cc,'Visible', 'off')
  327. set(get(cc,'Children'),'Visible','off');
  328. axis off
  329. %saveas(ruffcutmap, 'F:\Various Files\My Papers\Shadron and Pena 2022\eLife Figures\ruffcutmap.png')
  330. %% Other subplots for Figure 5
  331. plotID = 5;
  332. n = 29;
  333. ITDmap = figure;
  334. set(gcf,'Position',[100 100 400 300]);
  335. imagesc(azimuths(6:12), elevations(2:6), condition.normal.avg.ITDm((6:12),(2:6),n)'); axis xy; caxis([-150 150]);
  336. title(['Frequency: ', num2str(cF(n)/1000), ' kHz'], FontSize=16);
  337. c = colorbar('XTick',[-150 0 150]); c.FontSize = 12; c.Label.FontSize = 18; colormap jet;
  338. ax = gca; xticks(azimuths(6:12)); yticks(elevation(2:6)); ax.FontSize = 14; ylabel('Elevation (°)'); xlabel('Azimuth (°)')
  339. set(c.XLabel,{'String','Rotation','Position'},{'ITD (μs)',90,[2 0]})
  340. %saveas(ITDmap, 'F:\Various Files\My Papers\Shadron and Pena 2022\Matlab\ITDmap.png')
  341. ILDmap = figure;
  342. set(gcf,'Position',[100 100 400 300]);
  343. imagesc(azimuths(6:12), elevations(2:6), condition.normal.avg.ILDm((6:12),(2:6),n)'); axis xy; caxis([-16 16]);
  344. title(['Frequency: ', num2str(cF(n)/1000), ' kHz'], FontSize=16);
  345. c = colorbar('XTick',[-15 0 15]); c.FontSize = 14; c.Label.FontSize = 18; colormap jet;
  346. ax = gca; xticks(azimuths(6:12)); yticks(elevation(2:6)); ax.FontSize = 14; ylabel('Elevation (°)'); xlabel('Azimuth (°)');
  347. set(c.XLabel,{'String','Rotation','Position'},{'ILD (dB)',90,[2 0]})
  348. %saveas(ILDmap, 'F:\Various Files\My Papers\Shadron and Pena 2022\Matlab\ILDmap.png')
  349. ITDcurve = figure;
  350. set(gcf,'Position',[100 100 200 150]);
  351. plot(itd, itd_y, LineWidth=3, Color = 'black');
  352. xlim([-200,200]); ax = gca; ax.FontSize = 12; ylabel('Spike Count'); xlabel('ITD (μs)')
  353. %saveas(ITDcurve, 'F:\Various Files\My Papers\Shadron and Pena 2022\Matlab\ITDcurve.png')
  354. ILDcurve = figure;
  355. set(gcf,'Position',[100 100 200 150]);
  356. plot(ild, ild_y, LineWidth=3, Color = 'black');
  357. xlim([-16,16]); ax = gca; ax.FontSize = 12; ylabel('Spike Count'); xlabel('ILD (dB)');
  358. %saveas(ILDcurve, 'F:\Various Files\My Papers\Shadron and Pena 2022\Matlab\ILDcurve.png')
  359. sixkhz = figure;
  360. set(gcf,'Position',[100 100 400 300]);
  361. imagesc(azimuths(6:12), elevations(2:6), normaltun(6:12,2:6,plotID)'); axis xy; caxis([0 1]); colormap jet;
  362. title('Spatial Tuning', FontSize=16); ax = gca; ax.FontSize = 16;
  363. c = colorbar; c.Label.FontSize = 16;
  364. ylabel('Elevation (°)'); xlabel('Azimuth (°)'); xticks(azimuths(6:12)); yticks(elevation(2:6));
  365. set(c.XLabel,{'String','Rotation','Position'},{'Norm. Spike Count',90,[2.5 .5]})
  366. %saveas(sixkhz, 'F:\Various Files\My Papers\Shadron and Pena 2022\Matlab\spatialtun.png')