annotate aim-mat/modules/strobes/sf2003/gen_sf2003.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 % generating function for 'aim-mat'
tomwalters@0 2 %
tomwalters@0 3 % INPUT VALUES:
tomwalters@0 4 %
tomwalters@0 5 % RETURN VALUE:
tomwalters@0 6 %
tomwalters@0 7 %
tomwalters@0 8 % (c) 2011, University of Southampton
bleeck@3 9 % Maintained by Stefan Bleeck (bleeck@gmail.com)
bleeck@3 10 % download of current version is on the soundsoftware site:
bleeck@3 11 % http://code.soundsoftware.ac.uk/projects/aimmat
bleeck@3 12 % documentation and everything is on http://www.acousticscale.org
bleeck@3 13
tomwalters@0 14
tomwalters@0 15
tomwalters@0 16 function [allstrobeprocesses,allthresholds]=gen_sf2003(nap,strobeoptions)
tomwalters@0 17
tomwalters@0 18 % find in each channel each strobe and gives it back as an list of
tomwalters@0 19 % structures of strobes
tomwalters@0 20
tomwalters@0 21 % possible values of strobe_criterion:
tomwalters@0 22 % switch strobeoptions.strobe_criterion
tomwalters@0 23 % case 'threshold'
tomwalters@0 24 % case 'peak'
tomwalters@0 25 % case 'temporal_shadow'
tomwalters@0 26 % case 'local_maximum'
tomwalters@0 27 % case 'delta_gamma'
tomwalters@0 28 % case 'parabola'
tomwalters@0 29 % case 'bunt'
tomwalters@0 30 % case 'adaptive'
tomwalters@0 31 % end
tomwalters@0 32
tomwalters@0 33 if strcmp(strobeoptions.strobe_criterion,'interparabola')
tomwalters@0 34 [allstrobeprocesses,allthresholds]=do_interparabola(nap,strobeoptions);
tomwalters@0 35 return
tomwalters@0 36 end
tomwalters@0 37
tomwalters@0 38 % non interchannel methods:
tomwalters@0 39 % buffer for all thrsholds afterwards. Exact copy of the signal:
tomwalters@0 40 allthresholds=nap;
tomwalters@0 41 cfs=getcf(nap);
tomwalters@0 42
tomwalters@0 43 waithand=waitbar(0,'generating strobes');
tomwalters@0 44 nr_channels=getnrchannels(nap);
tomwalters@0 45 for ii=1:nr_channels
tomwalters@0 46 waitbar(ii/nr_channels);
tomwalters@0 47 single_channel=getsinglechannel(nap,ii);
tomwalters@0 48 current_cf=cfs(ii);
tomwalters@0 49 strobeoptions.current_cf=current_cf;
tomwalters@0 50
tomwalters@0 51 % here they are calculated:
tomwalters@0 52 [strobe_points,threshold]=findstrobes(single_channel,strobeoptions);
tomwalters@0 53
tomwalters@0 54 % return values: strobes in sec
tomwalters@0 55 % threshold= signal indicating the adaptive threshold
tomwalters@0 56
tomwalters@0 57 strobe_points=strobe_points(find(strobe_points>0));
tomwalters@0 58 strobe_vals=gettimevalue(single_channel,strobe_points);
tomwalters@0 59
tomwalters@0 60 thresvals=getvalues(threshold);
tomwalters@0 61
tomwalters@0 62 % make shure, they are in one column
tomwalters@0 63 if size(strobe_points,1) > size(strobe_points,2)
tomwalters@0 64 strobe_points=strobe_points';
tomwalters@0 65 strobe_vals=strobe_vals';
tomwalters@0 66 end
tomwalters@0 67 allstrobeprocesses{ii}.strobes=strobe_points;
tomwalters@0 68 allstrobeprocesses{ii}.strobe_vals=strobe_vals;
tomwalters@0 69 allthresholds=setsinglechannel(allthresholds,ii,thresvals);
tomwalters@0 70 end
tomwalters@0 71 close(waithand);
tomwalters@0 72 return
tomwalters@0 73
tomwalters@0 74
tomwalters@0 75 % interchannel methods:
tomwalters@0 76 function [all_processes,thresholds]=do_interparabola(nap,options)
tomwalters@0 77 % the threshold is calculated relativ to the hight of the last strobe
tomwalters@0 78 % the sample rate is needed for the calculation of the next thresholds
tomwalters@0 79 % for time_constant_alpha this is a linear decreasing function that goes
tomwalters@0 80 % from the maximum value to 0 in the time_constant
tomwalters@0 81 %
tomwalters@0 82 % with interchannel interaction: A strobe in one channel reduces the
tomwalters@0 83 % threshold in the neighbouring channels
tomwalters@0 84
tomwalters@0 85 nr_channels=getnrchannels(nap);
tomwalters@0 86
tomwalters@0 87 current_threshold=zeros(nr_channels,1);
tomwalters@0 88 sr=getsr(nap);
tomwalters@0 89 last_strobe=ones(nr_channels,1)* -inf;
tomwalters@0 90
tomwalters@0 91 napvals=getvalues(nap);
tomwalters@0 92 tresval=zeros(size(napvals));
tomwalters@0 93 nr_dots=length(napvals);
tomwalters@0 94
tomwalters@0 95 strobe_points=zeros(nr_channels,1000); % makes things faster
tomwalters@0 96 strobe_times=zeros(size(strobe_points)); % makes things faster
tomwalters@0 97
tomwalters@0 98 %when the last strobe occured
tomwalters@0 99 last_strobe_time=ones(nr_channels,1)* -inf;
tomwalters@0 100 last_threshold_value=zeros(nr_channels,1);
tomwalters@0 101 last_val=napvals(:,1);
tomwalters@0 102
tomwalters@0 103 cfs=getcf(nap);
tomwalters@0 104 % copy options to save time
tomwalters@0 105 h=options.parabel_heigth;
tomwalters@0 106 wnull=options.parabel_width_in_cycles*1./cfs;
tomwalters@0 107 w_variabel=wnull;
tomwalters@0 108
tomwalters@0 109 strobe_decay_time=options.strobe_decay_time;
tomwalters@0 110 times_per_ms=round(sr*0.005); % how often the bar should be updated
tomwalters@0 111 counter=ones(nr_channels,1);
tomwalters@0 112
tomwalters@0 113 waithand=waitbar(0,'generating Interchannel Strobes');
tomwalters@0 114
tomwalters@0 115 for ii=2:nr_dots-1
tomwalters@0 116 current_time=ii/sr;
tomwalters@0 117 if mod(ii,times_per_ms)==0
tomwalters@0 118 waitbar(ii/nr_dots);
tomwalters@0 119 end
tomwalters@0 120
tomwalters@0 121 for jj=1:nr_channels
tomwalters@0 122 current_val=napvals(jj,ii);
tomwalters@0 123 next_val=napvals(jj,ii+1);
tomwalters@0 124
tomwalters@0 125 if current_val>=current_threshold(jj) % above threshold -> criterium for strobe
tomwalters@0 126 current_threshold(jj)=current_val;
tomwalters@0 127 if current_val > last_val(jj) && current_val > next_val % only strobe on local maxima
tomwalters@0 128 % take this one as a new one
tomwalters@0 129 strobe_points(jj,counter(jj))=ii;
tomwalters@0 130 strobe_time(jj,counter(jj))=ii/sr;
tomwalters@0 131 counter(jj)=counter(jj)+1; % strobecounter
tomwalters@0 132
tomwalters@0 133 last_strobe_time(jj)=ii/sr; % anyhow, its a candidate
tomwalters@0 134 last_threshold_value(jj)=current_threshold(jj);
tomwalters@0 135 a=4*(1-h)/(wnull(jj)*wnull(jj));
tomwalters@0 136 b=-wnull(jj)/2;
tomwalters@0 137 w_variabel(jj)=wnull(jj)-(current_threshold(jj)-2*a*b)/(2*a);
tomwalters@0 138
tomwalters@0 139 % the interchannel action: reduce the threshold in adjacent channel
tomwalters@0 140 if jj>1
tomwalters@0 141 current_threshold(jj-1)=current_threshold(jj-1)/1.2;
tomwalters@0 142 last_threshold_value(jj-1)=last_threshold_value(jj-1)/1.2;
tomwalters@0 143 end
tomwalters@0 144
tomwalters@0 145 end
tomwalters@0 146 end
tomwalters@0 147 tresval(jj,ii)=current_threshold(jj);
tomwalters@0 148
tomwalters@0 149 time_since_last_strobe(jj)=current_time-last_strobe_time(jj);
tomwalters@0 150 if time_since_last_strobe(jj) > w_variabel(jj) % linear falling threshold
tomwalters@0 151 const_decay=last_threshold_value(jj)/sr/strobe_decay_time;
tomwalters@0 152 current_threshold(jj)=current_threshold(jj)-const_decay;
tomwalters@0 153 current_threshold(jj)=max(0,current_threshold(jj));
tomwalters@0 154 else % parabel for the first time y=a(x+b)^2+c
tomwalters@0 155 a=4*(1-h)/(wnull(jj)*wnull(jj));
tomwalters@0 156 b=-wnull(jj)/2;
tomwalters@0 157 c=h;
tomwalters@0 158 factor=a*(time_since_last_strobe(jj) + b) ^2+c;
tomwalters@0 159 current_threshold(jj)=last_threshold_value(jj)*factor;
tomwalters@0 160 end
tomwalters@0 161
tomwalters@0 162 current_threshold(jj)=max(0,current_threshold(jj)); % cant be smaller then 0
tomwalters@0 163 last_val(jj)=current_val;
tomwalters@0 164 end
tomwalters@0 165 end
tomwalters@0 166
tomwalters@0 167 % give back only the strobes, that really occured:
tomwalters@0 168 for jj=1:nr_channels
tomwalters@0 169 if counter(jj)>1
tomwalters@0 170 real_strobe_points=strobe_points(jj,1:counter(jj)-1);
tomwalters@0 171 strobe_vals=napvals(jj,real_strobe_points);
tomwalters@0 172 all_processes{jj}.strobe_vals=strobe_vals;
tomwalters@0 173 real_strobe_points=bin2time(nap,real_strobe_points);
tomwalters@0 174 all_processes{jj}.strobes=real_strobe_points;
tomwalters@0 175 else
tomwalters@0 176 all_processes{jj}.strobe_vals=[];
tomwalters@0 177 all_processes{jj}.strobes=[];
tomwalters@0 178 end
tomwalters@0 179 end
tomwalters@0 180
tomwalters@0 181 thresholds=nap;
tomwalters@0 182 thresholds=setvalues(thresholds,tresval);
tomwalters@0 183
tomwalters@0 184 close(waithand);
tomwalters@0 185 return