tomwalters@0: % tool for aim tomwalters@0: % tomwalters@0: % INPUT VALUES: tomwalters@0: % tomwalters@0: % RETURN VALUE: tomwalters@0: % tomwalters@0: % 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 [strobe_points,threshold]=adaptivthreshold(sig,options) tomwalters@0: tomwalters@0: tomwalters@0: sr=getsr(sig); tomwalters@0: newsig=sig; tomwalters@0: newsig=setname(newsig,sprintf('adaptive threshold of %s',getname(sig))); tomwalters@0: threshold=sig; tomwalters@0: tresval=getvalues(sig); tomwalters@0: tomwalters@0: % for speed reasons (matlab cant accelerate classes) all signals are tomwalters@0: % passed as their values tomwalters@0: sigvals=getvalues(sig); tomwalters@0: options.sr=sr; tomwalters@0: tomwalters@0: switch options.strobe_criterion tomwalters@0: case 'parabola' tomwalters@0: [strobe_points,tresval]=do_parabola(sigvals,options); tomwalters@0: case 'threshold' tomwalters@0: case 'peak' tomwalters@0: [strobe_points,tresval]=do_peak(sigvals,options); tomwalters@0: case 'temporal_shadow_plus' tomwalters@0: [strobe_points,tresval]=do_peakshadowplus(sigvals,options); tomwalters@0: case 'local_maximum' tomwalters@0: [strobe_points,tresval]=do_local_maximum(sigvals,options); tomwalters@0: case 'constrained_local_maximum' tomwalters@0: [strobe_points,tresval]=do_constrained_local_maximum(sigvals,options); tomwalters@0: case 'temporal_shadow' tomwalters@0: [strobe_points,tresval]=do_peakshadow(sigvals,options); tomwalters@0: case 'delta_gamma' tomwalters@0: case 'adaptive' tomwalters@0: [strobe_points,tresval]=do_adaptive(sigvals,options); tomwalters@0: case 'bunt' tomwalters@0: [strobe_points,tresval]=do_bunt(sigvals,options); tomwalters@0: otherwise tomwalters@0: error(sprintf('findstrobes: Sorry, I dont know the strobe criterium %s',options.strobe_criterion)); tomwalters@0: tomwalters@0: end tomwalters@0: tomwalters@0: strobe_points=bin2time(sig,strobe_points); tomwalters@0: tomwalters@0: threshold=setvalues(threshold,tresval); tomwalters@0: tomwalters@0: return tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: function [strobe_points,tresval]=do_parabola(sigvals,options) tomwalters@0: % the threshold is calculated relativ to the hight of the last strobe tomwalters@0: % the sample rate is needed for the calculation of the next thresholds tomwalters@0: % for time_constant_alpha this is a linear decreasing function that goes tomwalters@0: % from the maximum value to 0 in the time_constant tomwalters@0: tomwalters@0: current_threshold=0; tomwalters@0: sr=options.sr; tomwalters@0: last_strobe=-inf; tomwalters@0: tomwalters@0: tresval=zeros(size(sigvals)); tomwalters@0: newvals=zeros(size(sigvals)); tomwalters@0: nr=length(sigvals); tomwalters@0: strobe_points=zeros(1000,1); % makes things faster tomwalters@0: tomwalters@0: %when the last strobe occured tomwalters@0: last_strobe_time=-inf; tomwalters@0: last_threshold_value=0; tomwalters@0: last_val=sigvals(1); tomwalters@0: tomwalters@0: % copy options to save time tomwalters@0: h=options.parabel_heigth; tomwalters@0: wnull=options.parabel_width_in_cycles*1/options.current_cf; tomwalters@0: w_variabel=wnull; tomwalters@0: tomwalters@0: strobe_decay_time=options.strobe_decay_time; tomwalters@0: tomwalters@0: counter=1; tomwalters@0: for ii=2:nr-1 tomwalters@0: current_val=sigvals(ii); tomwalters@0: current_time=ii/sr; tomwalters@0: next_val=sigvals(ii+1); tomwalters@0: tomwalters@0: if current_val>=current_threshold % above threshold -> criterium for strobe tomwalters@0: current_threshold=current_val; tomwalters@0: if current_val > last_val && current_val > next_val % only strobe on local maxima tomwalters@0: % take this one as a new one tomwalters@0: strobe_points(counter)=ii; tomwalters@0: strobe_time(counter)=ii/sr; tomwalters@0: counter=counter+1; % strobecounter tomwalters@0: tomwalters@0: last_strobe_time=ii/sr; % anyhow, its a candidate tomwalters@0: last_threshold_value=current_threshold; tomwalters@0: a=4*(1-h)/(wnull*wnull); tomwalters@0: b=-wnull/2; tomwalters@0: w_variabel=wnull-(current_threshold-2*a*b)/(2*a); tomwalters@0: end tomwalters@0: end tomwalters@0: tresval(ii)=current_threshold; tomwalters@0: tomwalters@0: time_since_last_strobe=current_time-last_strobe_time; tomwalters@0: if time_since_last_strobe > w_variabel % linear falling threshold tomwalters@0: const_decay=last_threshold_value/sr/strobe_decay_time; tomwalters@0: current_threshold=current_threshold-const_decay; tomwalters@0: current_threshold=max(0,current_threshold); tomwalters@0: else % parabel for the first time y=a(x+b)^2+c tomwalters@0: a=4*(1-h)/(wnull*wnull); tomwalters@0: b=-wnull/2; tomwalters@0: c=h; tomwalters@0: factor=a*(time_since_last_strobe + b) ^2+c; tomwalters@0: current_threshold=last_threshold_value*factor; tomwalters@0: end tomwalters@0: tomwalters@0: current_threshold=max(0,current_threshold); % cant be smaller then 0 tomwalters@0: last_val=current_val; tomwalters@0: end tomwalters@0: % give back only the strobes, that really occured: tomwalters@0: if counter>1 tomwalters@0: strobe_points=strobe_points(1:counter-1); tomwalters@0: else tomwalters@0: strobe_points=[]; tomwalters@0: end tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: function [strobe_points,tresval]=do_peak(sigvals,options) tomwalters@0: % finds every single local maximum tomwalters@0: sr=options.sr; tomwalters@0: tresval=zeros(size(sigvals)); tomwalters@0: newvals=zeros(size(sigvals)); tomwalters@0: sig=signal(sigvals); tomwalters@0: sig=setsr(sig,sr); tomwalters@0: [t,h]=getlocalmaxima(sig); tomwalters@0: strobe_points=t*sr; tomwalters@0: tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: function [strobe_points,tresval]=do_peakshadow(sigvals,options) tomwalters@0: % finds every single peak and starts from that an falling threshold tomwalters@0: tomwalters@0: current_threshold=0;last_threshold_value=0;last_strobe=-inf;counter=1; tomwalters@0: sr=options.sr; tomwalters@0: tresval=zeros(size(sigvals)); tomwalters@0: nr=length(sigvals); tomwalters@0: strobe_points=zeros(1000,1); tomwalters@0: strobe_decay_time=options.strobe_decay_time; tomwalters@0: for ii=2:nr-1 tomwalters@0: current_val=sigvals(ii); tomwalters@0: current_time=ii/sr; tomwalters@0: if current_val>current_threshold tomwalters@0: if sigvals(ii) > sigvals(ii-1) && sigvals(ii) > sigvals(ii+1) tomwalters@0: new_val=current_val-current_threshold; tomwalters@0: current_threshold=current_val; tomwalters@0: strobe_points(counter)=ii; tomwalters@0: counter=counter+1; tomwalters@0: last_strobe=ii; tomwalters@0: last_threshold_value=current_threshold; tomwalters@0: end tomwalters@0: end tomwalters@0: const_decay=last_threshold_value/sr/strobe_decay_time; tomwalters@0: current_threshold=current_threshold-const_decay; tomwalters@0: current_threshold=max(0,current_threshold); tomwalters@0: tresval(ii)=current_threshold; tomwalters@0: end tomwalters@0: % give back only the strobes, that really occured: tomwalters@0: strobe_points=strobe_points(1:counter); tomwalters@0: tomwalters@0: tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: function [strobe_points,tresval]=do_peakshadowplus(sigvals,options) tomwalters@0: % finds each local maximum. The next peak must be further away then tomwalters@0: % strobe_lag. But after timeout a strobe must occure tomwalters@0: tomwalters@0: strobe_lag=options.strobe_lag; tomwalters@0: timeout=options.timeout; tomwalters@0: tomwalters@0: current_threshold=0; tomwalters@0: sr=options.sr; tomwalters@0: tresval=zeros(size(sigvals)); tomwalters@0: nr=length(sigvals); tomwalters@0: strobe_points=zeros(1000,1); tomwalters@0: strobe_decay_time=options.strobe_decay_time; tomwalters@0: last_strobe=-inf;last_strobe_time=-inf; tomwalters@0: counter=1; tomwalters@0: last_threshold_value=0; tomwalters@0: tomwalters@0: for ii=2:nr-1 tomwalters@0: current_val=sigvals(ii); tomwalters@0: current_time=ii/sr; tomwalters@0: tomwalters@0: if current_val>current_threshold tomwalters@0: if sigvals(ii) > sigvals(ii-1) && sigvals(ii) > sigvals(ii+1) tomwalters@0: if current_time > last_strobe_time+strobe_lag || ... % not in these 5 ms tomwalters@0: current_time > last_strobe_time + timeout % but after 10 ms again tomwalters@0: new_val=current_val-current_threshold; tomwalters@0: current_threshold=current_val; tomwalters@0: strobe_points(counter)=ii; tomwalters@0: counter=counter+1; tomwalters@0: last_strobe_time=ii/sr; tomwalters@0: last_strobe=ii; tomwalters@0: last_threshold_value=current_threshold; tomwalters@0: end tomwalters@0: end tomwalters@0: end tomwalters@0: const_decay=last_threshold_value/sr/strobe_decay_time; tomwalters@0: current_threshold=current_threshold-const_decay; tomwalters@0: current_threshold=max(0,current_threshold); tomwalters@0: tresval(ii)=current_threshold; tomwalters@0: end tomwalters@0: % give back only the strobes, that really occured: tomwalters@0: strobe_points=strobe_points(1:counter); tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: function [strobe_points,tresval]=do_constrained_local_maximum(sigvals,options) tomwalters@0: % finds each local maximum. The next peak must be further away then tomwalters@0: % strobe_lag. But after timeout a strobe must occur. This version has tomwalters@0: % the added constraint that the timeout and decay constant are calculated tomwalters@0: % on a per-channel basis. tomwalters@0: tomwalters@0: tomwalters@0: % set strobe_lag to the rise time of the filter in this channel tomwalters@0: % For now, this assumes a gammatone filterbank with standard parameters. tomwalters@0: % Todo: update this. tomwalters@0: n=4; b=1.019; %! hard-coded - fix!! tomwalters@0: fc=options.current_cf; tomwalters@0: strobe_lag=(n-1)./(2.*pi.*b.*(24.7+0.108.*fc));% value in seconds tomwalters@0: tomwalters@0: % The decay time is set according to the channel's centre frequency tomwalters@0: strobe_decay_time=1/options.current_cf; % value in seconds tomwalters@0: tomwalters@0: current_threshold=0; tomwalters@0: sr=options.sr; tomwalters@0: tresval=zeros(size(sigvals)); tomwalters@0: nr=length(sigvals); tomwalters@0: strobe_points=zeros(1000,1); tomwalters@0: tomwalters@0: % last_strobe=-inf; tomwalters@0: last_strobe_time=-inf; tomwalters@0: counter=1; tomwalters@0: last_threshold_value=0; tomwalters@0: tomwalters@0: for ii=2:nr-1 tomwalters@0: current_val=sigvals(ii); tomwalters@0: current_time=ii/sr; tomwalters@0: if current_val>current_threshold tomwalters@0: if sigvals(ii) > sigvals(ii-1) && sigvals(ii) > sigvals(ii+1) tomwalters@0: current_threshold=current_val; tomwalters@0: if current_time > last_strobe_time+strobe_lag || ... % not in these 5 ms tomwalters@0: current_time > last_strobe_time + timeout % but after 10 ms again tomwalters@0: strobe_points(counter)=ii; tomwalters@0: counter=counter+1; tomwalters@0: last_strobe_time=ii/sr; tomwalters@0: % last_strobe=ii; tomwalters@0: end tomwalters@0: end tomwalters@0: last_threshold_value=current_threshold; tomwalters@0: end tomwalters@0: const_decay=last_threshold_value/sr/strobe_decay_time; tomwalters@0: current_threshold=current_threshold-const_decay; tomwalters@0: current_threshold=max(0,current_threshold); tomwalters@0: tresval(ii)=current_threshold; tomwalters@0: end tomwalters@0: % give back only the strobes, that really occured: tomwalters@0: strobe_points=strobe_points(1:counter); tomwalters@0: tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: function [strobe_points,tresval]=do_local_maximum(sigvals,options) tomwalters@0: % finds each local maximum. The next peak must be further away then tomwalters@0: % strobe_lag. But after timeout a strobe must occure tomwalters@0: tomwalters@0: unit=options.unit; tomwalters@0: tomwalters@0: if strcmp(unit,'cycles') tomwalters@0: strobe_lag=1/options.current_cf*options.strobe_lag; tomwalters@0: timeout=1/options.current_cf*options.timeout; tomwalters@0: elseif strcmp(unit,'sec') tomwalters@0: strobe_lag=options.strobe_lag; tomwalters@0: timeout=options.timeout; tomwalters@0: elseif strcmp(unit,'ms') tomwalters@0: strobe_lag=options.strobe_lag/1000; tomwalters@0: timeout=options.timeout/1000; tomwalters@0: else tomwalters@0: error(sprintf('findstobes: dont know unit %s',unit)); tomwalters@0: end tomwalters@0: tomwalters@0: current_threshold=0; tomwalters@0: sr=options.sr; tomwalters@0: tresval=zeros(size(sigvals)); tomwalters@0: nr=length(sigvals); tomwalters@0: strobe_points=zeros(1000,1); tomwalters@0: strobe_decay_time=options.strobe_decay_time; tomwalters@0: % last_strobe=-inf; tomwalters@0: last_strobe_time=-inf; tomwalters@0: counter=1; tomwalters@0: last_threshold_value=0; tomwalters@0: tomwalters@0: if options.current_cf>300 tomwalters@0: a=0; tomwalters@0: end tomwalters@0: for ii=2:nr-1 tomwalters@0: current_val=sigvals(ii); tomwalters@0: current_time=ii/sr; tomwalters@0: if current_val>current_threshold tomwalters@0: if sigvals(ii) > sigvals(ii-1) && sigvals(ii) > sigvals(ii+1) tomwalters@0: current_threshold=current_val; tomwalters@0: if current_time > last_strobe_time+strobe_lag || ... % not in these 5 ms tomwalters@0: current_time > last_strobe_time + timeout % but after 10 ms again tomwalters@0: strobe_points(counter)=ii; tomwalters@0: counter=counter+1; tomwalters@0: last_strobe_time=ii/sr; tomwalters@0: % last_strobe=ii; tomwalters@0: end tomwalters@0: end tomwalters@0: last_threshold_value=current_threshold; tomwalters@0: end tomwalters@0: const_decay=last_threshold_value/sr/strobe_decay_time; tomwalters@0: current_threshold=current_threshold-const_decay; tomwalters@0: current_threshold=max(0,current_threshold); tomwalters@0: tresval(ii)=current_threshold; tomwalters@0: end tomwalters@0: % give back only the strobes, that really occured: tomwalters@0: strobe_points=strobe_points(1:counter); tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: function [strobe_points,tresval]=do_adaptive(sigvals,options) tomwalters@0: % the threshold is calculated relativ to the hight of the last strobe tomwalters@0: % the sample rate is needed for the calculation of the next thresholds tomwalters@0: % for time_constant_alpha this is a linear decreasing function that goes tomwalters@0: % from the maximum value to 0 in the time_constant tomwalters@0: tomwalters@0: current_threshold=0; tomwalters@0: sr=options.sr; tomwalters@0: last_strobe=-inf; tomwalters@0: tomwalters@0: tresval=zeros(size(sigvals)); tomwalters@0: newvals=zeros(size(sigvals)); tomwalters@0: nr=length(sigvals); tomwalters@0: strobe_points=zeros(1000,1); tomwalters@0: tomwalters@0: %when the last strobe occured tomwalters@0: % last_strobe=-inf; tomwalters@0: last_threshold_value=0; tomwalters@0: tomwalters@0: % copy options to save time tomwalters@0: strobe_decay_time=options.strobe_decay_time; tomwalters@0: tomwalters@0: bunt=0.5; tomwalters@0: tomwalters@0: % decay_time=options.strobe_decay_time; tomwalters@0: % threshold_decay_constant=power(0.5,1./(options.strobe_decay_time*sr)); tomwalters@0: tomwalters@0: slope_coefficient=options.slope_coefficient; tomwalters@0: slope_coefficient=0.0005; tomwalters@0: minoffset=0.2; tomwalters@0: tomwalters@0: threshold_decay_offset=-1/(options.strobe_decay_time*sr); tomwalters@0: default_threshold_decay_offset=threshold_decay_offset; tomwalters@0: tomwalters@0: counter=1; tomwalters@0: for ii=1:nr tomwalters@0: current_val=sigvals(ii); tomwalters@0: current_time=ii/sr; tomwalters@0: tomwalters@0: if current_val>current_threshold tomwalters@0: new_val=current_val-current_threshold; tomwalters@0: current_threshold=current_val; tomwalters@0: strobe_points(counter)=ii; tomwalters@0: counter=counter+1; tomwalters@0: time_offset=ii-last_strobe; % time since last strobe tomwalters@0: last_strobe=ii; tomwalters@0: tomwalters@0: amplitude_offset=current_threshold-last_threshold_value; tomwalters@0: tomwalters@0: last_threshold_value=current_threshold; tomwalters@0: tomwalters@0: current_bunt=0; tomwalters@0: % if amplitude_offset>0 tomwalters@0: % current_bunt=amplitude_offset/1; tomwalters@0: % else tomwalters@0: % current_bunt=0; tomwalters@0: % end tomwalters@0: current_threshold=current_threshold+current_bunt+minoffset; tomwalters@0: tomwalters@0: offset=amplitude_offset/time_offset*slope_coefficient; tomwalters@0: tomwalters@0: threshold_decay_offset=threshold_decay_offset+offset; tomwalters@0: % threshold_decay_constant=power(0.5,1./(decay_time*sr)); tomwalters@0: else tomwalters@0: new_val=0; tomwalters@0: end tomwalters@0: tresval(ii)=current_threshold; tomwalters@0: time_since_last_strobe=(ii-last_strobe)/sr; tomwalters@0: tomwalters@0: tomwalters@0: % current_threshold=current_threshold*threshold_decay_constant; tomwalters@0: current_threshold=current_threshold+threshold_decay_offset; tomwalters@0: current_threshold=max(current_threshold,0); tomwalters@0: tomwalters@0: if time_since_last_strobe>0.035 tomwalters@0: current_threshold=0; tomwalters@0: threshold_decay_offset=default_threshold_decay_offset; tomwalters@0: end tomwalters@0: tomwalters@0: newvals(ii)=new_val; tomwalters@0: end tomwalters@0: % give back only the strobes, that really occured: tomwalters@0: strobe_points=strobe_points(1:counter); tomwalters@0: tomwalters@0: tomwalters@0: %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: %%%% BUNT tomwalters@0: function [strobe_points,tresval]=do_bunt(sigvals,options) tomwalters@0: % the bunt is relative to the last peak hight tomwalters@0: tomwalters@0: current_threshold=0; tomwalters@0: sr=options.sr; tomwalters@0: last_strobe=-inf; tomwalters@0: tomwalters@0: tresval=zeros(size(sigvals)); tomwalters@0: newvals=zeros(size(sigvals)); tomwalters@0: nr=length(sigvals); tomwalters@0: strobe_points=zeros(1000,1); tomwalters@0: tomwalters@0: %when the last strobe occured tomwalters@0: last_strobe_time=-inf; tomwalters@0: last_threshold_value=0; tomwalters@0: % last_was_depressed_time=-inf; % time, when last strobe was thown out tomwalters@0: tomwalters@0: % copy options to save time tomwalters@0: strobe_decay_time=options.strobe_decay_time; tomwalters@0: tomwalters@0: % wait that many cycles to confirm a strobe tomwalters@0: wait_time=options.wait_cycles/options.current_cf; tomwalters@0: tomwalters@0: % if waited for too long, then strobe anyhow after some passed candidates: tomwalters@0: wait_timeout=options.wait_timeout_ms/1000; tomwalters@0: tomwalters@0: tomwalters@0: bunt_height=options.bunt; tomwalters@0: tomwalters@0: strobe_decay_time=options.strobe_decay_time; tomwalters@0: last_val=sigvals(1); tomwalters@0: tomwalters@0: counter=1; tomwalters@0: for ii=2:nr-1 tomwalters@0: current_val=sigvals(ii); tomwalters@0: next_val=sigvals(ii+1); tomwalters@0: current_time=ii/sr; tomwalters@0: if current_val>=current_threshold % above threshold -> criterium for strobe tomwalters@0: current_threshold=current_val; tomwalters@0: if current_val > last_val && current_val > next_val % only strobe on local maxima tomwalters@0: tomwalters@0: % so far its a candidate, but is it a real strobe? tomwalters@0: % look if there was a strobe in the past, that is deleted tomwalters@0: if (current_time-last_strobe_time1 ) tomwalters@0: % if its too long in the past, fire anyway tomwalters@0: timediff=current_time-last_strobe_time; tomwalters@0: prob=f2f(timediff,0,wait_timeout,0,1); tomwalters@0: tomwalters@0: % if timediff>wait_timeout %&& current_time-last_was_depressed_timerand(1); tomwalters@0: is_valid=1; tomwalters@0: else % this one was not a good one, tomwalters@0: counter=counter-1; % delete the last one tomwalters@0: % last_was_depressed_time=current_time; tomwalters@0: is_valid=0; tomwalters@0: end tomwalters@0: else tomwalters@0: is_valid=1; tomwalters@0: end tomwalters@0: tomwalters@0: % take this one as a new one tomwalters@0: strobe_points(counter)=ii; tomwalters@0: strobe_time(counter)=ii/sr; tomwalters@0: counter=counter+1; % strobecounter tomwalters@0: tomwalters@0: % increase the threshold by an amount tomwalters@0: current_threshold=current_threshold*bunt_height; %increase threshold tomwalters@0: last_threshold_value=current_threshold; tomwalters@0: % if is_valid==1 tomwalters@0: last_strobe_time=ii/sr; % anyhow, its a candidate tomwalters@0: % end tomwalters@0: tomwalters@0: tomwalters@0: end tomwalters@0: end tomwalters@0: tresval(ii)=current_threshold; tomwalters@0: tomwalters@0: const_decay=last_threshold_value/sr/strobe_decay_time; tomwalters@0: current_threshold=current_threshold-const_decay; tomwalters@0: tomwalters@0: current_threshold=max(current_threshold,0); tomwalters@0: last_val=current_val; tomwalters@0: end tomwalters@0: % give back only the strobes, that really occured: tomwalters@0: strobe_points=strobe_points(1:counter); tomwalters@0: tomwalters@0: % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% tomwalters@0: % %%%% BUNT tomwalters@0: % function [strobe_points,tresval]=do_bunt(sigvals,options) tomwalters@0: % % the bunt is relative to the last peak hight tomwalters@0: % tomwalters@0: % current_threshold=0; tomwalters@0: % sr=options.sr; tomwalters@0: % last_strobe=-inf; tomwalters@0: % tomwalters@0: % tresval=zeros(size(sigvals)); tomwalters@0: % newvals=zeros(size(sigvals)); tomwalters@0: % nr=length(sigvals); tomwalters@0: % strobe_points=zeros(100,1); tomwalters@0: % tomwalters@0: % %when the last strobe occured tomwalters@0: % last_strobe_time=-inf; tomwalters@0: % last_threshold_value=0; tomwalters@0: % tomwalters@0: % % copy options to save time tomwalters@0: % strobe_decay_time=options.strobe_decay_time; tomwalters@0: % tomwalters@0: % % wait that many cycles to confirm a strobe tomwalters@0: % wait_time=options.wait_cycles/options.current_cf; tomwalters@0: % tomwalters@0: % % if waited for too long, then strobe anyhow after some passed candidates: tomwalters@0: % wait_candidate_max=options.nr_strobe_candidates; tomwalters@0: % current_jumped_candidates=1; tomwalters@0: % tomwalters@0: % tomwalters@0: % bunt=options.bunt; tomwalters@0: % tomwalters@0: % strobe_decay_time=options.strobe_decay_time; tomwalters@0: % last_val=sigvals(1); tomwalters@0: % tomwalters@0: % counter=1; tomwalters@0: % for ii=2:nr-1 tomwalters@0: % current_val=sigvals(ii); tomwalters@0: % next_val=sigvals(ii+1); tomwalters@0: % current_time=ii/sr; tomwalters@0: % if current_val>=current_threshold % above threshold -> criterium for strobe tomwalters@0: % current_threshold=current_val; tomwalters@0: % if current_val > last_val && current_val > next_val % only strobe on local maxima tomwalters@0: % tomwalters@0: % % so far its a candidate, but is it a real strobe? tomwalters@0: % % look if there was a strobe in the past, that is deleted tomwalters@0: % if (counter>1 && current_time-strobe_time(counter-1)last_threshold_value tomwalters@0: % % if its too long in the past, fire anyway tomwalters@0: % if current_jumped_candidates > wait_candidate_max tomwalters@0: % current_jumped_candidates=1; % reset counter tomwalters@0: % else tomwalters@0: % current_jumped_candidates=current_jumped_candidates+1; tomwalters@0: % counter=counter-1; % delete the last one tomwalters@0: % end tomwalters@0: % else tomwalters@0: % current_jumped_candidates=1; tomwalters@0: % end tomwalters@0: % tomwalters@0: % tomwalters@0: % % take this one as a new one tomwalters@0: % strobe_points(counter)=ii; tomwalters@0: % strobe_time(counter)=ii/sr; tomwalters@0: % counter=counter+1; % strobecounter tomwalters@0: % current_threshold=current_threshold*options.bunt; %increase threshold tomwalters@0: % tomwalters@0: % last_strobe_time=ii/sr; % anyhow, its a candidate tomwalters@0: % last_threshold_value=current_threshold; tomwalters@0: % tomwalters@0: % end tomwalters@0: % end tomwalters@0: % tresval(ii)=current_threshold; tomwalters@0: % tomwalters@0: % const_decay=last_threshold_value/sr/strobe_decay_time; tomwalters@0: % current_threshold=current_threshold-const_decay; tomwalters@0: % tomwalters@0: % current_threshold=max(current_threshold,0); tomwalters@0: % last_val=current_val; tomwalters@0: % end tomwalters@0: %