bleeck@3: % This external file is included as part of the 'aim-mat' distribution package bleeck@3: % (c) 2011, University of Southampton bleeck@3: % Maintained by Stefan Bleeck (bleeck@gmail.com) bleeck@3: % download of current version is on the soundsoftware site: bleeck@3: % http://code.soundsoftware.ac.uk/projects/aimmat bleeck@3: % documentation and everything is on http://www.acousticscale.org tomwalters@0: tomwalters@0: function [lowpasssig,maxinfo,mininfo]=peak_picker(sig,options); tomwalters@0: % sophisticated peak picker tomwalters@0: % the signal is first lowpassfilterd with the frequency given in options tomwalters@0: % options comes with: tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: % frequency with witch the psth is filtered tomwalters@0: % options.lowpassfrequency=500; tomwalters@0: % search for peaks in the range from to: tomwalters@0: % options.search_peak_start_search=0.001; % seach also before the offical latency tomwalters@0: % options.search_peak_stop_search=0.025; % seach this time for peaks tomwalters@0: tomwalters@0: tomwalters@0: % every distinct peak must comply the following conditions: tomwalters@0: % a certain activity during the duration of the maximum (a spike count per tomwalters@0: % presentation) tomwalters@0: if ~isfield(options,'min_count_per_peak') tomwalters@0: options.min_count_per_peak=-inf;% default: no certain activity: all peaks are significant tomwalters@0: end tomwalters@0: % a certain height of the maximum peak tomwalters@0: if ~isfield(options,'min_height_of_heighest_peak') tomwalters@0: options.min_height_of_heighest_peak=0; % default: no certain height: all peaks are significant tomwalters@0: end tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: % return values are the filtered signal and information about the found tomwalters@0: % peaks: tomwalters@0: % maxinfo.values % the hights of the found maxima tomwalters@0: % maxinfo.times % where are the found maxima tomwalters@0: % maxinfo.contrast % the contrast is the relation between the maximum and the surrounding minima tomwalters@0: % maxinfo.qvalue % how wide the maximum is at half height. tomwalters@0: % maxinfo.activity % sum of activity between the adjacent minima tomwalters@0: tomwalters@0: % every found maxima (except the first and the last) is surrounded by two tomwalters@0: % minima. Same is true for the found minimas tomwalters@0: tomwalters@0: % do we want some grafical output (for debugging) tomwalters@0: grafix=options.grafix; tomwalters@0: % define the return values tomwalters@0: maxinfo=[]; tomwalters@0: mininfo=[]; tomwalters@0: tomwalters@0: tomwalters@0: %do the lowpassfiltering tomwalters@0: lowpasssig=lowpass(sig,options.lowpassfrequency); tomwalters@0: [atmax,ahmax]=getlocalmaxima(lowpasssig); tomwalters@0: [atmin,ahmin]=getlocalminima(lowpasssig); tomwalters@0: tomwalters@0: % restrict to the requiered range tomwalters@0: indeces1=find(atmax>options.search_peak_start_search); tomwalters@0: indeces2=find(atmaxoptions.search_peak_start_search); tomwalters@0: indeces2=find(atmin=options.min_contrast_for_distinct_peak tomwalters@0: % and it must have a certain height tomwalters@0: if maxinfo.values(i)>=height_criterium tomwalters@0: if maxinfo.activity(i)>=count_criterium tomwalters@0: maxinfo.distinct_max(count).contrast=maxinfo.contrast(i); tomwalters@0: maxinfo.distinct_max(count).qvalue=maxinfo.qvalue(i); tomwalters@0: maxinfo.distinct_max(count).activity=maxinfo.activity(i); tomwalters@0: maxinfo.distinct_max(count).hmax=hmax(i); tomwalters@0: maxinfo.distinct_max(count).tmax=tmax(i); tomwalters@0: count=count+1; tomwalters@0: end tomwalters@0: end tomwalters@0: end tomwalters@0: end tomwalters@0: tomwalters@0: tomwalters@0: if grafix tomwalters@0: nr_all_max=length(atmax); tomwalters@0: for i=1:nr_all_max tomwalters@0: plot(atmax(i)*1000,ahmax(i),'o','markersize',2,'Markerfacecolor','r','Markeredgecolor','r'); tomwalters@0: end tomwalters@0: nr_all_min=length(atmin); tomwalters@0: for i=1:nr_all_min tomwalters@0: plot(atmin(i)*1000,ahmin(i),'o','markersize',2,'Markerfacecolor','g','Markeredgecolor','g'); tomwalters@0: end tomwalters@0: tomwalters@0: % plot a dot for every disticnt maximum found tomwalters@0: for i=1:nr_max tomwalters@0: testmaxpos=maxinfo.times(i); tomwalters@0: testmaxval=maxinfo.values(i); tomwalters@0: contrast=maxinfo.contrast(i); tomwalters@0: count=maxinfo.activity(i); tomwalters@0: plot(testmaxpos*1000,testmaxval,'o','markersize',8,'Markerfacecolor','r','Markeredgecolor','r'); tomwalters@0: end tomwalters@0: % plot a dot for every minimum found tomwalters@0: for i=1:length(maxinfo.distinct_max); tomwalters@0: testmaxpos=maxinfo.distinct_max(i).tmax; tomwalters@0: testmaxval=maxinfo.distinct_max(i).hmax; tomwalters@0: contrast=maxinfo.distinct_max(i).contrast; tomwalters@0: count=maxinfo.distinct_max(i).activity; tomwalters@0: text(testmaxpos*1000+1,testmaxval,sprintf('contr %3.3f',contrast),'fontsize',6,'ver','bottom'); tomwalters@0: text(testmaxpos*1000+1,testmaxval,sprintf('count %3.3f',count),'fontsize',6,'ver','top'); tomwalters@0: end tomwalters@0: for i=1:nr_min tomwalters@0: testminpos=mininfo.times(i); tomwalters@0: testminval=mininfo.values(i); tomwalters@0: plot(testminpos*1000,testminval,'o','markersize',4,'Markerfacecolor','g','Markeredgecolor','g'); tomwalters@0: end tomwalters@0: end tomwalters@0: return tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: tomwalters@0: function plotall(fignum,lowpasssig,tmax,tmin,hmax,hmin); tomwalters@0: figure(fignum); tomwalters@0: clf tomwalters@0: hold on tomwalters@0: fill(lowpasssig,'b'); tomwalters@0: set(gca,'xlim',[0,60]); tomwalters@0: for i=1:length(tmax) tomwalters@0: plot(tmax(i)*1000,hmax(i),'Marker','o','MarkerSize',6,'MarkerFaceColor','r','MarkerEdgeColor','r') tomwalters@0: end tomwalters@0: for i=1:length(tmin) tomwalters@0: % plot(time2bin(lowpasssig,tmin(i)),hmin(i),'Marker','o','MarkerSize',6,'MarkerFaceColor','g','MarkerEdgeColor','g') tomwalters@0: end tomwalters@0: xlabel('time (ms)'); tomwalters@0: ylabel('spikes / sweep / bin'); tomwalters@0: title(' PSTH plus found maxima'); tomwalters@0: % movegui(nfig,'northwest'); tomwalters@0: return tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: tomwalters@0: function [tmaxnew,tminnew,hmaxnew,hminnew]=try_reduce_maxima(tmax,tmin,hmax,hmin,options) tomwalters@0: % throw out the peak with the smallest contrast tomwalters@0: % if removed, the minimum to the right of it is removed too tomwalters@0: tomwalters@0: nr_max=length(tmax); tomwalters@0: tomwalters@0: % find the maximum with the neighbour that is closest in height and throw tomwalters@0: % it out! tomwalters@0: for i=1:nr_max tomwalters@0: contrastr(i)=getrightcontrast(i,tmax,tmin,hmax,hmin); tomwalters@0: end tomwalters@0: for i=1:nr_max tomwalters@0: contrastl(i)=getleftcontrast(i,tmax,tmin,hmax,hmin); tomwalters@0: end tomwalters@0: [mincontrastr,minconindexr]=min(abs(contrastr)); % thats the smallest right tomwalters@0: [mincontrastl,minconindexl]=min(abs(contrastl)); % thats the smallest left tomwalters@0: tomwalters@0: if mincontrastr0 && indexmin