annotate aim-mat/tools/findstrobes.m @ 4:537f939baef0 tip

various bug fixes and changed copyright message
author Stefan Bleeck <bleeck@gmail.com>
date Tue, 16 Aug 2011 14:37:17 +0100
parents 20ada0af3d7d
children
rev   line source
tomwalters@0 1 % tool for aim
tomwalters@0 2 %
tomwalters@0 3 % INPUT VALUES:
tomwalters@0 4 %
tomwalters@0 5 % RETURN VALUE:
tomwalters@0 6 %
tomwalters@0 7 %
bleeck@3 8 % This external file is included as part of the 'aim-mat' distribution package
bleeck@3 9 % (c) 2011, University of Southampton
bleeck@3 10 % Maintained by Stefan Bleeck (bleeck@gmail.com)
bleeck@3 11 % download of current version is on the soundsoftware site:
bleeck@3 12 % http://code.soundsoftware.ac.uk/projects/aimmat
bleeck@3 13 % documentation and everything is on http://www.acousticscale.org
tomwalters@0 14
tomwalters@0 15 function [strobe_points,threshold]=adaptivthreshold(sig,options)
tomwalters@0 16
tomwalters@0 17
tomwalters@0 18 sr=getsr(sig);
tomwalters@0 19 newsig=sig;
tomwalters@0 20 newsig=setname(newsig,sprintf('adaptive threshold of %s',getname(sig)));
tomwalters@0 21 threshold=sig;
tomwalters@0 22 tresval=getvalues(sig);
tomwalters@0 23
tomwalters@0 24 % for speed reasons (matlab cant accelerate classes) all signals are
tomwalters@0 25 % passed as their values
tomwalters@0 26 sigvals=getvalues(sig);
tomwalters@0 27 options.sr=sr;
tomwalters@0 28
tomwalters@0 29 switch options.strobe_criterion
tomwalters@0 30 case 'parabola'
tomwalters@0 31 [strobe_points,tresval]=do_parabola(sigvals,options);
tomwalters@0 32 case 'threshold'
tomwalters@0 33 case 'peak'
tomwalters@0 34 [strobe_points,tresval]=do_peak(sigvals,options);
tomwalters@0 35 case 'temporal_shadow_plus'
tomwalters@0 36 [strobe_points,tresval]=do_peakshadowplus(sigvals,options);
tomwalters@0 37 case 'local_maximum'
tomwalters@0 38 [strobe_points,tresval]=do_local_maximum(sigvals,options);
tomwalters@0 39 case 'constrained_local_maximum'
tomwalters@0 40 [strobe_points,tresval]=do_constrained_local_maximum(sigvals,options);
tomwalters@0 41 case 'temporal_shadow'
tomwalters@0 42 [strobe_points,tresval]=do_peakshadow(sigvals,options);
tomwalters@0 43 case 'delta_gamma'
tomwalters@0 44 case 'adaptive'
tomwalters@0 45 [strobe_points,tresval]=do_adaptive(sigvals,options);
tomwalters@0 46 case 'bunt'
tomwalters@0 47 [strobe_points,tresval]=do_bunt(sigvals,options);
tomwalters@0 48 otherwise
tomwalters@0 49 error(sprintf('findstrobes: Sorry, I dont know the strobe criterium %s',options.strobe_criterion));
tomwalters@0 50
tomwalters@0 51 end
tomwalters@0 52
tomwalters@0 53 strobe_points=bin2time(sig,strobe_points);
tomwalters@0 54
tomwalters@0 55 threshold=setvalues(threshold,tresval);
tomwalters@0 56
tomwalters@0 57 return
tomwalters@0 58
tomwalters@0 59
tomwalters@0 60
tomwalters@0 61 function [strobe_points,tresval]=do_parabola(sigvals,options)
tomwalters@0 62 % the threshold is calculated relativ to the hight of the last strobe
tomwalters@0 63 % the sample rate is needed for the calculation of the next thresholds
tomwalters@0 64 % for time_constant_alpha this is a linear decreasing function that goes
tomwalters@0 65 % from the maximum value to 0 in the time_constant
tomwalters@0 66
tomwalters@0 67 current_threshold=0;
tomwalters@0 68 sr=options.sr;
tomwalters@0 69 last_strobe=-inf;
tomwalters@0 70
tomwalters@0 71 tresval=zeros(size(sigvals));
tomwalters@0 72 newvals=zeros(size(sigvals));
tomwalters@0 73 nr=length(sigvals);
tomwalters@0 74 strobe_points=zeros(1000,1); % makes things faster
tomwalters@0 75
tomwalters@0 76 %when the last strobe occured
tomwalters@0 77 last_strobe_time=-inf;
tomwalters@0 78 last_threshold_value=0;
tomwalters@0 79 last_val=sigvals(1);
tomwalters@0 80
tomwalters@0 81 % copy options to save time
tomwalters@0 82 h=options.parabel_heigth;
tomwalters@0 83 wnull=options.parabel_width_in_cycles*1/options.current_cf;
tomwalters@0 84 w_variabel=wnull;
tomwalters@0 85
tomwalters@0 86 strobe_decay_time=options.strobe_decay_time;
tomwalters@0 87
tomwalters@0 88 counter=1;
tomwalters@0 89 for ii=2:nr-1
tomwalters@0 90 current_val=sigvals(ii);
tomwalters@0 91 current_time=ii/sr;
tomwalters@0 92 next_val=sigvals(ii+1);
tomwalters@0 93
tomwalters@0 94 if current_val>=current_threshold % above threshold -> criterium for strobe
tomwalters@0 95 current_threshold=current_val;
tomwalters@0 96 if current_val > last_val && current_val > next_val % only strobe on local maxima
tomwalters@0 97 % take this one as a new one
tomwalters@0 98 strobe_points(counter)=ii;
tomwalters@0 99 strobe_time(counter)=ii/sr;
tomwalters@0 100 counter=counter+1; % strobecounter
tomwalters@0 101
tomwalters@0 102 last_strobe_time=ii/sr; % anyhow, its a candidate
tomwalters@0 103 last_threshold_value=current_threshold;
tomwalters@0 104 a=4*(1-h)/(wnull*wnull);
tomwalters@0 105 b=-wnull/2;
tomwalters@0 106 w_variabel=wnull-(current_threshold-2*a*b)/(2*a);
tomwalters@0 107 end
tomwalters@0 108 end
tomwalters@0 109 tresval(ii)=current_threshold;
tomwalters@0 110
tomwalters@0 111 time_since_last_strobe=current_time-last_strobe_time;
tomwalters@0 112 if time_since_last_strobe > w_variabel % linear falling threshold
tomwalters@0 113 const_decay=last_threshold_value/sr/strobe_decay_time;
tomwalters@0 114 current_threshold=current_threshold-const_decay;
tomwalters@0 115 current_threshold=max(0,current_threshold);
tomwalters@0 116 else % parabel for the first time y=a(x+b)^2+c
tomwalters@0 117 a=4*(1-h)/(wnull*wnull);
tomwalters@0 118 b=-wnull/2;
tomwalters@0 119 c=h;
tomwalters@0 120 factor=a*(time_since_last_strobe + b) ^2+c;
tomwalters@0 121 current_threshold=last_threshold_value*factor;
tomwalters@0 122 end
tomwalters@0 123
tomwalters@0 124 current_threshold=max(0,current_threshold); % cant be smaller then 0
tomwalters@0 125 last_val=current_val;
tomwalters@0 126 end
tomwalters@0 127 % give back only the strobes, that really occured:
tomwalters@0 128 if counter>1
tomwalters@0 129 strobe_points=strobe_points(1:counter-1);
tomwalters@0 130 else
tomwalters@0 131 strobe_points=[];
tomwalters@0 132 end
tomwalters@0 133
tomwalters@0 134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 135 function [strobe_points,tresval]=do_peak(sigvals,options)
tomwalters@0 136 % finds every single local maximum
tomwalters@0 137 sr=options.sr;
tomwalters@0 138 tresval=zeros(size(sigvals));
tomwalters@0 139 newvals=zeros(size(sigvals));
tomwalters@0 140 sig=signal(sigvals);
tomwalters@0 141 sig=setsr(sig,sr);
tomwalters@0 142 [t,h]=getlocalmaxima(sig);
tomwalters@0 143 strobe_points=t*sr;
tomwalters@0 144
tomwalters@0 145
tomwalters@0 146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 147 function [strobe_points,tresval]=do_peakshadow(sigvals,options)
tomwalters@0 148 % finds every single peak and starts from that an falling threshold
tomwalters@0 149
tomwalters@0 150 current_threshold=0;last_threshold_value=0;last_strobe=-inf;counter=1;
tomwalters@0 151 sr=options.sr;
tomwalters@0 152 tresval=zeros(size(sigvals));
tomwalters@0 153 nr=length(sigvals);
tomwalters@0 154 strobe_points=zeros(1000,1);
tomwalters@0 155 strobe_decay_time=options.strobe_decay_time;
tomwalters@0 156 for ii=2:nr-1
tomwalters@0 157 current_val=sigvals(ii);
tomwalters@0 158 current_time=ii/sr;
tomwalters@0 159 if current_val>current_threshold
tomwalters@0 160 if sigvals(ii) > sigvals(ii-1) && sigvals(ii) > sigvals(ii+1)
tomwalters@0 161 new_val=current_val-current_threshold;
tomwalters@0 162 current_threshold=current_val;
tomwalters@0 163 strobe_points(counter)=ii;
tomwalters@0 164 counter=counter+1;
tomwalters@0 165 last_strobe=ii;
tomwalters@0 166 last_threshold_value=current_threshold;
tomwalters@0 167 end
tomwalters@0 168 end
tomwalters@0 169 const_decay=last_threshold_value/sr/strobe_decay_time;
tomwalters@0 170 current_threshold=current_threshold-const_decay;
tomwalters@0 171 current_threshold=max(0,current_threshold);
tomwalters@0 172 tresval(ii)=current_threshold;
tomwalters@0 173 end
tomwalters@0 174 % give back only the strobes, that really occured:
tomwalters@0 175 strobe_points=strobe_points(1:counter);
tomwalters@0 176
tomwalters@0 177
tomwalters@0 178
tomwalters@0 179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 181 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 182 function [strobe_points,tresval]=do_peakshadowplus(sigvals,options)
tomwalters@0 183 % finds each local maximum. The next peak must be further away then
tomwalters@0 184 % strobe_lag. But after timeout a strobe must occure
tomwalters@0 185
tomwalters@0 186 strobe_lag=options.strobe_lag;
tomwalters@0 187 timeout=options.timeout;
tomwalters@0 188
tomwalters@0 189 current_threshold=0;
tomwalters@0 190 sr=options.sr;
tomwalters@0 191 tresval=zeros(size(sigvals));
tomwalters@0 192 nr=length(sigvals);
tomwalters@0 193 strobe_points=zeros(1000,1);
tomwalters@0 194 strobe_decay_time=options.strobe_decay_time;
tomwalters@0 195 last_strobe=-inf;last_strobe_time=-inf;
tomwalters@0 196 counter=1;
tomwalters@0 197 last_threshold_value=0;
tomwalters@0 198
tomwalters@0 199 for ii=2:nr-1
tomwalters@0 200 current_val=sigvals(ii);
tomwalters@0 201 current_time=ii/sr;
tomwalters@0 202
tomwalters@0 203 if current_val>current_threshold
tomwalters@0 204 if sigvals(ii) > sigvals(ii-1) && sigvals(ii) > sigvals(ii+1)
tomwalters@0 205 if current_time > last_strobe_time+strobe_lag || ... % not in these 5 ms
tomwalters@0 206 current_time > last_strobe_time + timeout % but after 10 ms again
tomwalters@0 207 new_val=current_val-current_threshold;
tomwalters@0 208 current_threshold=current_val;
tomwalters@0 209 strobe_points(counter)=ii;
tomwalters@0 210 counter=counter+1;
tomwalters@0 211 last_strobe_time=ii/sr;
tomwalters@0 212 last_strobe=ii;
tomwalters@0 213 last_threshold_value=current_threshold;
tomwalters@0 214 end
tomwalters@0 215 end
tomwalters@0 216 end
tomwalters@0 217 const_decay=last_threshold_value/sr/strobe_decay_time;
tomwalters@0 218 current_threshold=current_threshold-const_decay;
tomwalters@0 219 current_threshold=max(0,current_threshold);
tomwalters@0 220 tresval(ii)=current_threshold;
tomwalters@0 221 end
tomwalters@0 222 % give back only the strobes, that really occured:
tomwalters@0 223 strobe_points=strobe_points(1:counter);
tomwalters@0 224
tomwalters@0 225 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 226 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 227 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 228 function [strobe_points,tresval]=do_constrained_local_maximum(sigvals,options)
tomwalters@0 229 % finds each local maximum. The next peak must be further away then
tomwalters@0 230 % strobe_lag. But after timeout a strobe must occur. This version has
tomwalters@0 231 % the added constraint that the timeout and decay constant are calculated
tomwalters@0 232 % on a per-channel basis.
tomwalters@0 233
tomwalters@0 234
tomwalters@0 235 % set strobe_lag to the rise time of the filter in this channel
tomwalters@0 236 % For now, this assumes a gammatone filterbank with standard parameters.
tomwalters@0 237 % Todo: update this.
tomwalters@0 238 n=4; b=1.019; %! hard-coded - fix!!
tomwalters@0 239 fc=options.current_cf;
tomwalters@0 240 strobe_lag=(n-1)./(2.*pi.*b.*(24.7+0.108.*fc));% value in seconds
tomwalters@0 241
tomwalters@0 242 % The decay time is set according to the channel's centre frequency
tomwalters@0 243 strobe_decay_time=1/options.current_cf; % value in seconds
tomwalters@0 244
tomwalters@0 245 current_threshold=0;
tomwalters@0 246 sr=options.sr;
tomwalters@0 247 tresval=zeros(size(sigvals));
tomwalters@0 248 nr=length(sigvals);
tomwalters@0 249 strobe_points=zeros(1000,1);
tomwalters@0 250
tomwalters@0 251 % last_strobe=-inf;
tomwalters@0 252 last_strobe_time=-inf;
tomwalters@0 253 counter=1;
tomwalters@0 254 last_threshold_value=0;
tomwalters@0 255
tomwalters@0 256 for ii=2:nr-1
tomwalters@0 257 current_val=sigvals(ii);
tomwalters@0 258 current_time=ii/sr;
tomwalters@0 259 if current_val>current_threshold
tomwalters@0 260 if sigvals(ii) > sigvals(ii-1) && sigvals(ii) > sigvals(ii+1)
tomwalters@0 261 current_threshold=current_val;
tomwalters@0 262 if current_time > last_strobe_time+strobe_lag || ... % not in these 5 ms
tomwalters@0 263 current_time > last_strobe_time + timeout % but after 10 ms again
tomwalters@0 264 strobe_points(counter)=ii;
tomwalters@0 265 counter=counter+1;
tomwalters@0 266 last_strobe_time=ii/sr;
tomwalters@0 267 % last_strobe=ii;
tomwalters@0 268 end
tomwalters@0 269 end
tomwalters@0 270 last_threshold_value=current_threshold;
tomwalters@0 271 end
tomwalters@0 272 const_decay=last_threshold_value/sr/strobe_decay_time;
tomwalters@0 273 current_threshold=current_threshold-const_decay;
tomwalters@0 274 current_threshold=max(0,current_threshold);
tomwalters@0 275 tresval(ii)=current_threshold;
tomwalters@0 276 end
tomwalters@0 277 % give back only the strobes, that really occured:
tomwalters@0 278 strobe_points=strobe_points(1:counter);
tomwalters@0 279
tomwalters@0 280
tomwalters@0 281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 282 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 284 function [strobe_points,tresval]=do_local_maximum(sigvals,options)
tomwalters@0 285 % finds each local maximum. The next peak must be further away then
tomwalters@0 286 % strobe_lag. But after timeout a strobe must occure
tomwalters@0 287
tomwalters@0 288 unit=options.unit;
tomwalters@0 289
tomwalters@0 290 if strcmp(unit,'cycles')
tomwalters@0 291 strobe_lag=1/options.current_cf*options.strobe_lag;
tomwalters@0 292 timeout=1/options.current_cf*options.timeout;
tomwalters@0 293 elseif strcmp(unit,'sec')
tomwalters@0 294 strobe_lag=options.strobe_lag;
tomwalters@0 295 timeout=options.timeout;
tomwalters@0 296 elseif strcmp(unit,'ms')
tomwalters@0 297 strobe_lag=options.strobe_lag/1000;
tomwalters@0 298 timeout=options.timeout/1000;
tomwalters@0 299 else
tomwalters@0 300 error(sprintf('findstobes: dont know unit %s',unit));
tomwalters@0 301 end
tomwalters@0 302
tomwalters@0 303 current_threshold=0;
tomwalters@0 304 sr=options.sr;
tomwalters@0 305 tresval=zeros(size(sigvals));
tomwalters@0 306 nr=length(sigvals);
tomwalters@0 307 strobe_points=zeros(1000,1);
tomwalters@0 308 strobe_decay_time=options.strobe_decay_time;
tomwalters@0 309 % last_strobe=-inf;
tomwalters@0 310 last_strobe_time=-inf;
tomwalters@0 311 counter=1;
tomwalters@0 312 last_threshold_value=0;
tomwalters@0 313
tomwalters@0 314 if options.current_cf>300
tomwalters@0 315 a=0;
tomwalters@0 316 end
tomwalters@0 317 for ii=2:nr-1
tomwalters@0 318 current_val=sigvals(ii);
tomwalters@0 319 current_time=ii/sr;
tomwalters@0 320 if current_val>current_threshold
tomwalters@0 321 if sigvals(ii) > sigvals(ii-1) && sigvals(ii) > sigvals(ii+1)
tomwalters@0 322 current_threshold=current_val;
tomwalters@0 323 if current_time > last_strobe_time+strobe_lag || ... % not in these 5 ms
tomwalters@0 324 current_time > last_strobe_time + timeout % but after 10 ms again
tomwalters@0 325 strobe_points(counter)=ii;
tomwalters@0 326 counter=counter+1;
tomwalters@0 327 last_strobe_time=ii/sr;
tomwalters@0 328 % last_strobe=ii;
tomwalters@0 329 end
tomwalters@0 330 end
tomwalters@0 331 last_threshold_value=current_threshold;
tomwalters@0 332 end
tomwalters@0 333 const_decay=last_threshold_value/sr/strobe_decay_time;
tomwalters@0 334 current_threshold=current_threshold-const_decay;
tomwalters@0 335 current_threshold=max(0,current_threshold);
tomwalters@0 336 tresval(ii)=current_threshold;
tomwalters@0 337 end
tomwalters@0 338 % give back only the strobes, that really occured:
tomwalters@0 339 strobe_points=strobe_points(1:counter);
tomwalters@0 340
tomwalters@0 341 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 342 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 344 function [strobe_points,tresval]=do_adaptive(sigvals,options)
tomwalters@0 345 % the threshold is calculated relativ to the hight of the last strobe
tomwalters@0 346 % the sample rate is needed for the calculation of the next thresholds
tomwalters@0 347 % for time_constant_alpha this is a linear decreasing function that goes
tomwalters@0 348 % from the maximum value to 0 in the time_constant
tomwalters@0 349
tomwalters@0 350 current_threshold=0;
tomwalters@0 351 sr=options.sr;
tomwalters@0 352 last_strobe=-inf;
tomwalters@0 353
tomwalters@0 354 tresval=zeros(size(sigvals));
tomwalters@0 355 newvals=zeros(size(sigvals));
tomwalters@0 356 nr=length(sigvals);
tomwalters@0 357 strobe_points=zeros(1000,1);
tomwalters@0 358
tomwalters@0 359 %when the last strobe occured
tomwalters@0 360 % last_strobe=-inf;
tomwalters@0 361 last_threshold_value=0;
tomwalters@0 362
tomwalters@0 363 % copy options to save time
tomwalters@0 364 strobe_decay_time=options.strobe_decay_time;
tomwalters@0 365
tomwalters@0 366 bunt=0.5;
tomwalters@0 367
tomwalters@0 368 % decay_time=options.strobe_decay_time;
tomwalters@0 369 % threshold_decay_constant=power(0.5,1./(options.strobe_decay_time*sr));
tomwalters@0 370
tomwalters@0 371 slope_coefficient=options.slope_coefficient;
tomwalters@0 372 slope_coefficient=0.0005;
tomwalters@0 373 minoffset=0.2;
tomwalters@0 374
tomwalters@0 375 threshold_decay_offset=-1/(options.strobe_decay_time*sr);
tomwalters@0 376 default_threshold_decay_offset=threshold_decay_offset;
tomwalters@0 377
tomwalters@0 378 counter=1;
tomwalters@0 379 for ii=1:nr
tomwalters@0 380 current_val=sigvals(ii);
tomwalters@0 381 current_time=ii/sr;
tomwalters@0 382
tomwalters@0 383 if current_val>current_threshold
tomwalters@0 384 new_val=current_val-current_threshold;
tomwalters@0 385 current_threshold=current_val;
tomwalters@0 386 strobe_points(counter)=ii;
tomwalters@0 387 counter=counter+1;
tomwalters@0 388 time_offset=ii-last_strobe; % time since last strobe
tomwalters@0 389 last_strobe=ii;
tomwalters@0 390
tomwalters@0 391 amplitude_offset=current_threshold-last_threshold_value;
tomwalters@0 392
tomwalters@0 393 last_threshold_value=current_threshold;
tomwalters@0 394
tomwalters@0 395 current_bunt=0;
tomwalters@0 396 % if amplitude_offset>0
tomwalters@0 397 % current_bunt=amplitude_offset/1;
tomwalters@0 398 % else
tomwalters@0 399 % current_bunt=0;
tomwalters@0 400 % end
tomwalters@0 401 current_threshold=current_threshold+current_bunt+minoffset;
tomwalters@0 402
tomwalters@0 403 offset=amplitude_offset/time_offset*slope_coefficient;
tomwalters@0 404
tomwalters@0 405 threshold_decay_offset=threshold_decay_offset+offset;
tomwalters@0 406 % threshold_decay_constant=power(0.5,1./(decay_time*sr));
tomwalters@0 407 else
tomwalters@0 408 new_val=0;
tomwalters@0 409 end
tomwalters@0 410 tresval(ii)=current_threshold;
tomwalters@0 411 time_since_last_strobe=(ii-last_strobe)/sr;
tomwalters@0 412
tomwalters@0 413
tomwalters@0 414 % current_threshold=current_threshold*threshold_decay_constant;
tomwalters@0 415 current_threshold=current_threshold+threshold_decay_offset;
tomwalters@0 416 current_threshold=max(current_threshold,0);
tomwalters@0 417
tomwalters@0 418 if time_since_last_strobe>0.035
tomwalters@0 419 current_threshold=0;
tomwalters@0 420 threshold_decay_offset=default_threshold_decay_offset;
tomwalters@0 421 end
tomwalters@0 422
tomwalters@0 423 newvals(ii)=new_val;
tomwalters@0 424 end
tomwalters@0 425 % give back only the strobes, that really occured:
tomwalters@0 426 strobe_points=strobe_points(1:counter);
tomwalters@0 427
tomwalters@0 428
tomwalters@0 429 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 430 %%%% BUNT
tomwalters@0 431 function [strobe_points,tresval]=do_bunt(sigvals,options)
tomwalters@0 432 % the bunt is relative to the last peak hight
tomwalters@0 433
tomwalters@0 434 current_threshold=0;
tomwalters@0 435 sr=options.sr;
tomwalters@0 436 last_strobe=-inf;
tomwalters@0 437
tomwalters@0 438 tresval=zeros(size(sigvals));
tomwalters@0 439 newvals=zeros(size(sigvals));
tomwalters@0 440 nr=length(sigvals);
tomwalters@0 441 strobe_points=zeros(1000,1);
tomwalters@0 442
tomwalters@0 443 %when the last strobe occured
tomwalters@0 444 last_strobe_time=-inf;
tomwalters@0 445 last_threshold_value=0;
tomwalters@0 446 % last_was_depressed_time=-inf; % time, when last strobe was thown out
tomwalters@0 447
tomwalters@0 448 % copy options to save time
tomwalters@0 449 strobe_decay_time=options.strobe_decay_time;
tomwalters@0 450
tomwalters@0 451 % wait that many cycles to confirm a strobe
tomwalters@0 452 wait_time=options.wait_cycles/options.current_cf;
tomwalters@0 453
tomwalters@0 454 % if waited for too long, then strobe anyhow after some passed candidates:
tomwalters@0 455 wait_timeout=options.wait_timeout_ms/1000;
tomwalters@0 456
tomwalters@0 457
tomwalters@0 458 bunt_height=options.bunt;
tomwalters@0 459
tomwalters@0 460 strobe_decay_time=options.strobe_decay_time;
tomwalters@0 461 last_val=sigvals(1);
tomwalters@0 462
tomwalters@0 463 counter=1;
tomwalters@0 464 for ii=2:nr-1
tomwalters@0 465 current_val=sigvals(ii);
tomwalters@0 466 next_val=sigvals(ii+1);
tomwalters@0 467 current_time=ii/sr;
tomwalters@0 468 if current_val>=current_threshold % above threshold -> criterium for strobe
tomwalters@0 469 current_threshold=current_val;
tomwalters@0 470 if current_val > last_val && current_val > next_val % only strobe on local maxima
tomwalters@0 471
tomwalters@0 472 % so far its a candidate, but is it a real strobe?
tomwalters@0 473 % look if there was a strobe in the past, that is deleted
tomwalters@0 474 if (current_time-last_strobe_time<wait_time && counter>1 )
tomwalters@0 475 % if its too long in the past, fire anyway
tomwalters@0 476 timediff=current_time-last_strobe_time;
tomwalters@0 477 prob=f2f(timediff,0,wait_timeout,0,1);
tomwalters@0 478
tomwalters@0 479 % if timediff>wait_timeout %&& current_time-last_was_depressed_time<wait_timeout
tomwalters@0 480 if prob>rand(1);
tomwalters@0 481 is_valid=1;
tomwalters@0 482 else % this one was not a good one,
tomwalters@0 483 counter=counter-1; % delete the last one
tomwalters@0 484 % last_was_depressed_time=current_time;
tomwalters@0 485 is_valid=0;
tomwalters@0 486 end
tomwalters@0 487 else
tomwalters@0 488 is_valid=1;
tomwalters@0 489 end
tomwalters@0 490
tomwalters@0 491 % take this one as a new one
tomwalters@0 492 strobe_points(counter)=ii;
tomwalters@0 493 strobe_time(counter)=ii/sr;
tomwalters@0 494 counter=counter+1; % strobecounter
tomwalters@0 495
tomwalters@0 496 % increase the threshold by an amount
tomwalters@0 497 current_threshold=current_threshold*bunt_height; %increase threshold
tomwalters@0 498 last_threshold_value=current_threshold;
tomwalters@0 499 % if is_valid==1
tomwalters@0 500 last_strobe_time=ii/sr; % anyhow, its a candidate
tomwalters@0 501 % end
tomwalters@0 502
tomwalters@0 503
tomwalters@0 504 end
tomwalters@0 505 end
tomwalters@0 506 tresval(ii)=current_threshold;
tomwalters@0 507
tomwalters@0 508 const_decay=last_threshold_value/sr/strobe_decay_time;
tomwalters@0 509 current_threshold=current_threshold-const_decay;
tomwalters@0 510
tomwalters@0 511 current_threshold=max(current_threshold,0);
tomwalters@0 512 last_val=current_val;
tomwalters@0 513 end
tomwalters@0 514 % give back only the strobes, that really occured:
tomwalters@0 515 strobe_points=strobe_points(1:counter);
tomwalters@0 516
tomwalters@0 517 % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 518 % %%%% BUNT
tomwalters@0 519 % function [strobe_points,tresval]=do_bunt(sigvals,options)
tomwalters@0 520 % % the bunt is relative to the last peak hight
tomwalters@0 521 %
tomwalters@0 522 % current_threshold=0;
tomwalters@0 523 % sr=options.sr;
tomwalters@0 524 % last_strobe=-inf;
tomwalters@0 525 %
tomwalters@0 526 % tresval=zeros(size(sigvals));
tomwalters@0 527 % newvals=zeros(size(sigvals));
tomwalters@0 528 % nr=length(sigvals);
tomwalters@0 529 % strobe_points=zeros(100,1);
tomwalters@0 530 %
tomwalters@0 531 % %when the last strobe occured
tomwalters@0 532 % last_strobe_time=-inf;
tomwalters@0 533 % last_threshold_value=0;
tomwalters@0 534 %
tomwalters@0 535 % % copy options to save time
tomwalters@0 536 % strobe_decay_time=options.strobe_decay_time;
tomwalters@0 537 %
tomwalters@0 538 % % wait that many cycles to confirm a strobe
tomwalters@0 539 % wait_time=options.wait_cycles/options.current_cf;
tomwalters@0 540 %
tomwalters@0 541 % % if waited for too long, then strobe anyhow after some passed candidates:
tomwalters@0 542 % wait_candidate_max=options.nr_strobe_candidates;
tomwalters@0 543 % current_jumped_candidates=1;
tomwalters@0 544 %
tomwalters@0 545 %
tomwalters@0 546 % bunt=options.bunt;
tomwalters@0 547 %
tomwalters@0 548 % strobe_decay_time=options.strobe_decay_time;
tomwalters@0 549 % last_val=sigvals(1);
tomwalters@0 550 %
tomwalters@0 551 % counter=1;
tomwalters@0 552 % for ii=2:nr-1
tomwalters@0 553 % current_val=sigvals(ii);
tomwalters@0 554 % next_val=sigvals(ii+1);
tomwalters@0 555 % current_time=ii/sr;
tomwalters@0 556 % if current_val>=current_threshold % above threshold -> criterium for strobe
tomwalters@0 557 % current_threshold=current_val;
tomwalters@0 558 % if current_val > last_val && current_val > next_val % only strobe on local maxima
tomwalters@0 559 %
tomwalters@0 560 % % so far its a candidate, but is it a real strobe?
tomwalters@0 561 % % look if there was a strobe in the past, that is deleted
tomwalters@0 562 % if (counter>1 && current_time-strobe_time(counter-1)<wait_time) %&& current_threshold >last_threshold_value
tomwalters@0 563 % % if its too long in the past, fire anyway
tomwalters@0 564 % if current_jumped_candidates > wait_candidate_max
tomwalters@0 565 % current_jumped_candidates=1; % reset counter
tomwalters@0 566 % else
tomwalters@0 567 % current_jumped_candidates=current_jumped_candidates+1;
tomwalters@0 568 % counter=counter-1; % delete the last one
tomwalters@0 569 % end
tomwalters@0 570 % else
tomwalters@0 571 % current_jumped_candidates=1;
tomwalters@0 572 % end
tomwalters@0 573 %
tomwalters@0 574 %
tomwalters@0 575 % % take this one as a new one
tomwalters@0 576 % strobe_points(counter)=ii;
tomwalters@0 577 % strobe_time(counter)=ii/sr;
tomwalters@0 578 % counter=counter+1; % strobecounter
tomwalters@0 579 % current_threshold=current_threshold*options.bunt; %increase threshold
tomwalters@0 580 %
tomwalters@0 581 % last_strobe_time=ii/sr; % anyhow, its a candidate
tomwalters@0 582 % last_threshold_value=current_threshold;
tomwalters@0 583 %
tomwalters@0 584 % end
tomwalters@0 585 % end
tomwalters@0 586 % tresval(ii)=current_threshold;
tomwalters@0 587 %
tomwalters@0 588 % const_decay=last_threshold_value/sr/strobe_decay_time;
tomwalters@0 589 % current_threshold=current_threshold-const_decay;
tomwalters@0 590 %
tomwalters@0 591 % current_threshold=max(current_threshold,0);
tomwalters@0 592 % last_val=current_val;
tomwalters@0 593 % end
tomwalters@0 594 %