abr_data_analysis.m 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. %% ABR analysis script: this script takes the raw recordings and extracts the results
  2. % raw recordings are available from last author upon request
  3. %% settings
  4. fs=19200; % sampling rate after 20-fold resampling
  5. bma200_input_gain=10000; % value gained during calibration
  6. abr_scale_factor=1/(7e-5*bma200_input_gain); % in mV
  7. nboot=500; % number of bootstraps for statistics
  8. % set if you want to save the results and bootstrap options (1=on, 0=off)
  9. saveon=0;
  10. booton=1;
  11. % set abr analysis window
  12. ana_wind_start=0.001; % offset of rms calculation window
  13. ana_wind_dur=0.007; % duration of rms calculation window
  14. ana_wind_starts=round(ana_wind_start*fs);
  15. ana_wind_stops=round((ana_wind_start+ana_wind_dur)*fs);
  16. % define (internal) bat ID, recording date and recording number for analyses
  17. dates = {'20170207','20170210','20170817','20191204',...
  18. '20170213','20170223','20170818','20191204',...
  19. '20170217','20170220','20170821','20191205',...
  20. '20170203','20170209','20170817','20191204',...
  21. '20170208','20170216','20170818','20191203',...
  22. '20170306','20170313','20170821','20191205'};
  23. reco = {'3','3','3','2','3','3','8','2','3','5','3','2',...
  24. '5','3','7','2','3','3','3','3','3','7','4','2'};
  25. animalname = {'3','3','3','3','5','5','5','5','8','8','8','8',...
  26. '2','2','2','2','4','4','4','4','7','7','7','7'};
  27. %% data extraction and bootstrap analysis
  28. for i=1:24
  29. % choose current bat, date, record number
  30. batid=char(animalname(i));
  31. date=dates(i);
  32. recn=reco(i);
  33. % location of folder with the raw recordings
  34. rootpath=['D:\Documents\Manuskripte\Vocal_development\ABR_recordings\B' batid '\' char(date) '\'];
  35. % circumvent naming error of B2 files
  36. if batid=='2'
  37. recname=['abr_tonepips_ed_b' batid '_m' char(recn) '_'];
  38. else
  39. recname=['abr_tonepips_vpl_b' batid '_m' char(recn) '_'];
  40. end
  41. % access the folder with the recordings
  42. d=dir(rootpath);
  43. filenames=char(d.name); % read in the names of the files in this folder
  44. recnum=0; % set recnum to zero
  45. for fn=3:size(filenames,1) % go through all files in this folder
  46. filename=deblank(filenames(fn,:));
  47. if 1-isempty(strfind(filename,recname))
  48. % extract info from filename
  49. li=strfind(filename,'_l');
  50. fi=strfind(filename,'_fc');
  51. xi=strfind(filename,'_');
  52. xi=xi(min(find(xi>fi)));
  53. if xi>fi+4 % if there is frequency information in the filename
  54. xi=max(xi);
  55. recnum=recnum+1;
  56. lstring=filename(li+2:fi-1);
  57. current_level=str2num(lstring);
  58. fstring=filename(fi+3:xi-1);
  59. current_freq=str2num(fstring);
  60. load([rootpath filename]); % load the file to be analysed
  61. end
  62. mrxin=squeeze(mean(rxin,2)).*abr_scale_factor;
  63. levels(recnum)=current_level;
  64. freqs(recnum)=current_freq/1e3;
  65. rec(recnum,:)=mrxin;
  66. %%% BOOTSTRAPPING ANALYSIS STARTS HERE
  67. if booton==1
  68. data=rxin(ana_wind_starts:ana_wind_stops,:);
  69. datalong=reshape(data,numel(data),1);
  70. % do a bootstrapping analysis
  71. samplesize=size(data,1);
  72. indices=1:(length(datalong)-samplesize);
  73. samplenumber=size(data,2);
  74. originalsignal=mean(data,2);
  75. originalrms=std(originalsignal);
  76. for bn=1:nboot
  77. for rep=1:size(data,2)
  78. bsdata(:,rep)=circshift(data(:,rep),ceil(rand*size(data,1)));
  79. end
  80. bootrms(bn)=std(mean(bsdata,2));
  81. end
  82. confidence=numel(find(originalrms>bootrms))/nboot;
  83. confidences(recnum)=confidence;
  84. end %%% BOOTSTRAP ANALYSIS ENDS HERE
  85. end
  86. end
  87. % organise data for bootstrap plots
  88. all_levels=sort(unique(levels));
  89. all_freqs=sort(unique(freqs));
  90. out=zeros(length(all_levels),length(all_freqs));
  91. for sn=1:length(levels)
  92. cl=levels(sn);
  93. cf=freqs(sn);
  94. li=find(all_levels==cl);
  95. fi=find(all_freqs==cf);
  96. out(li,fi)=std(rec(sn,ana_wind_starts:ana_wind_stops)); % calculate rms from sample 31 because of rec onset irregularities
  97. end
  98. in=[freqs' levels' confidences'];
  99. sort_in=sortrows(in,[1 2]);
  100. ufreqs=unique(freqs);
  101. ulevels=unique(levels);
  102. for fn=1:length(ufreqs)
  103. cfreq=ufreqs(fn);
  104. fconf=sort_in(find(sort_in(:,1)==cfreq),3);
  105. threshold_ind=max(find(fconf<0.95))+1;
  106. if threshold_ind<=length(ulevels) & 1-isempty(threshold_ind)
  107. bootstrap_thresholds(fn)=ulevels(threshold_ind);
  108. else
  109. bootstrap_thresholds(fn)=nan;
  110. end
  111. end
  112. % save results in structures
  113. resultTHRES(i)=struct('uniqueFrequencies',ufreqs','bootstrapThresholds',bootstrap_thresholds');
  114. allCONFI(i)=struct('frequencies',freqs,'levels',levels,'confidences',confidences);
  115. heatRES(i)=struct('all_freqs',all_freqs,'all_levels',all_levels,'out',out);
  116. end
  117. %% saving of the variables
  118. if saveon==1
  119. bats = strcat('b', animalname);
  120. save('abr_deafening_results.mat','dates','bats','resultTHRES','allCONFI');
  121. end