annotate aim-mat/modules/nap/twodat2003/gen_twoDat2003.m @ 0:74dedb26614d

Initial checkin of AIM-MAT version 1.5 (6.4.2011).
author tomwalters
date Fri, 20 May 2011 12:32:31 +0100
parents
children 20ada0af3d7d
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
tomwalters@0 9 % Maintained and written by Stefan Bleeck (bleec@gmail.com)
tomwalters@0 10 % http://www.soton.ac.uk/aim
tomwalters@0 11
tomwalters@0 12
tomwalters@0 13
tomwalters@0 14
tomwalters@0 15 function ret=gen_twoDat2003(bmm,options)
tomwalters@0 16 % two D adaptive thresholding
tomwalters@0 17
tomwalters@0 18 % first do the normal nap:
tomwalters@0 19 nap=gen_hcl(bmm,options.nap);
tomwalters@0 20
tomwalters@0 21 if nargin < 2
tomwalters@0 22 options=[];
tomwalters@0 23 end
tomwalters@0 24
tomwalters@0 25
tomwalters@0 26 vals=getvalues(nap);
tomwalters@0 27
tomwalters@0 28
tomwalters@0 29
tomwalters@0 30 %%%normalise to 0-60dB range
tomwalters@0 31 %max_nap=max(max(vals));
tomwalters@0 32 %nap_norm=60./max_nap;
tomwalters@0 33 %vals=vals*nap_norm;
tomwalters@0 34 %save vals.mat vals;
tomwalters@0 35
tomwalters@0 36 new_vals=zeros(size(vals));
tomwalters@0 37 nr_chan=size(vals,1);
tomwalters@0 38 nr_dots=size(vals,2);
tomwalters@0 39 sr=getsr(nap);
tomwalters@0 40 cfs=getcf(nap);
tomwalters@0 41 %load bmm.mat;
tomwalters@0 42
tomwalters@0 43
tomwalters@0 44 % the delays can be set by an option
tomwalters@0 45 if isfield(options,'time_constant_factor') % this is multiplied to the threshold_time_const
tomwalters@0 46 time_constant_factor=options.time_constant_factor;
tomwalters@0 47 else
tomwalters@0 48 time_constant_factor=0.8;
tomwalters@0 49 end
tomwalters@0 50
tomwalters@0 51 if isfield(options,'frequency_constant_factor') % the influence of the left and the right channel
tomwalters@0 52 frequency_constant_factor=options.frequency_constant_factor;
tomwalters@0 53 else
tomwalters@0 54 frequency_constant_factor=0.9;
tomwalters@0 55 end
tomwalters@0 56
tomwalters@0 57
tomwalters@0 58 if isfield(options,'threshold_rise_constant') % the rate at which the threshold may rise
tomwalters@0 59 threshold_rise_constant=options.threshold_rise_constant;
tomwalters@0 60 else
tomwalters@0 61 threshold_rise_constant=1;
tomwalters@0 62 end
tomwalters@0 63
tomwalters@0 64
tomwalters@0 65
tomwalters@0 66
tomwalters@0 67 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 68 %the following calculates the envelope of nap response
tomwalters@0 69 %out_li=zeros(nr_chan,nr_dots);
tomwalters@0 70 %for chan=1:nr_chan
tomwalters@0 71 %out_li(chan,:)=standard_leaky_integrator(vals(chan,:),0.003,1,16000);
tomwalters@0 72 %out_li_scale=max(vals(chan,:))./max(out_li(chan,:));
tomwalters@0 73 %out_li(chan,:)=out_li(chan,:).*out_li_scale;
tomwalters@0 74 %end
tomwalters@0 75 %[ERB_no, dummy] = Freq2ERB(cfs);
tomwalters@0 76 %for chan=1:nr_chan
tomwalters@0 77 %out_li(chan,:)=standard_leaky_integrator(vals(chan,:),0.003,1,16000);
tomwalters@0 78 %out_li_scale=max(vals(chan,:))./max(out_li(chan,:));
tomwalters@0 79 %out_li(chan,:)=out_li(chan,:).*out_li_scale;
tomwalters@0 80 %end
tomwalters@0 81 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 82
tomwalters@0 83
tomwalters@0 84 %save cfs.mat cfs;
tomwalters@0 85 order = 4;
tomwalters@0 86 [rate ERB] = Freq2ERB(cfs);
tomwalters@0 87 B=options.b *ERB;
tomwalters@0 88
tomwalters@0 89
tomwalters@0 90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 91 %note: decay of gammatone is described by: exp(-2*pi*B*t)
tomwalters@0 92 % time to drop to 1/2 (6dB) --in seconds
tomwalters@0 93 %i.e. exp(-2*pi*B*t)=0.5
tomwalters@0 94 drop6dbtime=-log(0.5)./(2*pi.*B);
tomwalters@0 95
tomwalters@0 96 % this means so many per samplepoint
tomwalters@0 97 threshold_decay_constant=-log10(2)*20./(drop6dbtime*sr);
tomwalters@0 98 threshold_decay_constant=threshold_decay_constant./time_constant_factor;
tomwalters@0 99
tomwalters@0 100
tomwalters@0 101 %threshold_rise_constant=.5;
tomwalters@0 102
tomwalters@0 103 threshold_rise=(100.*B.^0.9)./sr;
tomwalters@0 104 threshold_rise=threshold_rise.*threshold_rise_constant;
tomwalters@0 105 %threshold_rise=zeros(nr_chan);
tomwalters@0 106
tomwalters@0 107
tomwalters@0 108
tomwalters@0 109
tomwalters@0 110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tomwalters@0 112
tomwalters@0 113 %calc frequency slope values
tomwalters@0 114 if nr_chan>1
tomwalters@0 115 %frequency_slope_c=calcfreqslope(cfs(2),cfs(1));
tomwalters@0 116 %frequency_slope_a=calcfreqslope(cfs(1),cfs(2));
tomwalters@0 117
tomwalters@0 118 %c is effect from channel above but we must input
tomwalters@0 119 %the higher frequency (2) as the adjacent band and
tomwalters@0 120 %the channel in which we want the level (1)as
tomwalters@0 121 %the centre freq
tomwalters@0 122 frequency_slope_c=calcfreqslope(cfs(1),cfs(2),options.b);
tomwalters@0 123
tomwalters@0 124 %a is effect from channel below but we must input
tomwalters@0 125 %the lower frequency (1) as the adjacent band and
tomwalters@0 126 %the channel in which we want the level (2) as
tomwalters@0 127 %the centre freq
tomwalters@0 128 frequency_slope_a=calcfreqslope(cfs(2),cfs(1),options.b);
tomwalters@0 129
tomwalters@0 130 else
tomwalters@0 131 frequency_slope_c=0;
tomwalters@0 132 frequency_slope_a=0;
tomwalters@0 133 end
tomwalters@0 134
tomwalters@0 135 %we divide by the frequency_constant_factor because we want a
tomwalters@0 136 %frequency_constant_factor of 1 to mean we use the actual
tomwalters@0 137 %frequency slope and a frequency_constant_factor of 0.5
tomwalters@0 138 %to mean that the frequency slope falls away at twice the rate
tomwalters@0 139 %i.e. less effect
tomwalters@0 140 frequency_slope_c=frequency_slope_c./frequency_constant_factor;
tomwalters@0 141 frequency_slope_a=frequency_slope_a./frequency_constant_factor;
tomwalters@0 142
tomwalters@0 143
tomwalters@0 144
tomwalters@0 145
tomwalters@0 146 oldfigure=gcf;
tomwalters@0 147 if nr_chan==1
tomwalters@0 148 onechannelfigure=figure(3); % assuming, the other one is 1
tomwalters@0 149 end
tomwalters@0 150
tomwalters@0 151 % channel_select=1:nr_chan;
tomwalters@0 152 times_per_ms=round(sr*0.01);
tomwalters@0 153 if nr_chan>1
tomwalters@0 154 waithand=waitbar(0,'generating 2dat');
tomwalters@0 155 end
tomwalters@0 156
tomwalters@0 157
tomwalters@0 158
tomwalters@0 159 %val_e=zeros(nr_chan,nr_dots);
tomwalters@0 160 %val_inp=zeros(nr_chan,nr_dots);
tomwalters@0 161 %val_thres=zeros(nr_chan,nr_dots);
tomwalters@0 162
tomwalters@0 163
tomwalters@0 164
tomwalters@0 165
tomwalters@0 166 % a is the decayed working variable from the channel above
tomwalters@0 167 % b is the range limit
tomwalters@0 168 % c is the decayed working variable from the channel below
tomwalters@0 169 % d is the delayed and decayed nap signal from on-channel
tomwalters@0 170 % e is the maximum of [a b c d]
tomwalters@0 171 % threshold is the working variable
tomwalters@0 172 % thresholdlast is the working variable in the last round
tomwalters@0 173 % inp is the current input
tomwalters@0 174
tomwalters@0 175
tomwalters@0 176 thresholdlast=zeros(nr_chan,1);
tomwalters@0 177 threshold=zeros(nr_chan,1);
tomwalters@0 178 a=0;b=1;c=0;d=0;e=0;
tomwalters@0 179
tomwalters@0 180
tomwalters@0 181
tomwalters@0 182 for ii=1:nr_dots % through the time
tomwalters@0 183 if nr_chan>1
tomwalters@0 184 if mod(ii,times_per_ms)==0
tomwalters@0 185 waitbar(ii/nr_dots);
tomwalters@0 186 end
tomwalters@0 187 end
tomwalters@0 188
tomwalters@0 189 for jj=1:nr_chan % through all channels: prepare working variable
tomwalters@0 190 inp=vals(jj,ii);% current input
tomwalters@0 191
tomwalters@0 192 if jj< nr_chan
tomwalters@0 193 chan_above=thresholdlast(jj+1);
tomwalters@0 194 a=chan_above+frequency_slope_a;
tomwalters@0 195 else
tomwalters@0 196 a=0;
tomwalters@0 197 end
tomwalters@0 198 if jj>1
tomwalters@0 199 chan_below=thresholdlast(jj-1);
tomwalters@0 200 c=chan_below+frequency_slope_c;
tomwalters@0 201 else
tomwalters@0 202 c=0;
tomwalters@0 203 end
tomwalters@0 204
tomwalters@0 205 %a=0;
tomwalters@0 206 %c=0;
tomwalters@0 207
tomwalters@0 208
tomwalters@0 209
tomwalters@0 210
tomwalters@0 211 d=max(thresholdlast(jj)+threshold_decay_constant(jj),0);
tomwalters@0 212
tomwalters@0 213
tomwalters@0 214 e1=max(a,b);
tomwalters@0 215 e2=max(c,d);
tomwalters@0 216 e=max(e1,e2);
tomwalters@0 217
tomwalters@0 218
tomwalters@0 219
tomwalters@0 220
tomwalters@0 221 if inp>e %threshold is exceeded
tomwalters@0 222
tomwalters@0 223 threshold(jj)=min(thresholdlast(jj)+threshold_rise(jj),inp);
tomwalters@0 224 %threshold(jj)=inp;
tomwalters@0 225
tomwalters@0 226 %new_vals(jj,ii)=threshold(jj)-e;
tomwalters@0 227 new_vals(jj,ii)=inp-threshold(jj);
tomwalters@0 228 % % else threshold decays
tomwalters@0 229 else
tomwalters@0 230
tomwalters@0 231 threshold(jj)=e;
tomwalters@0 232 new_vals(jj,ii)=0;
tomwalters@0 233 end
tomwalters@0 234
tomwalters@0 235
tomwalters@0 236
tomwalters@0 237 %val_e(jj,ii)=e;
tomwalters@0 238 %val_inp(jj,ii)=inp;
tomwalters@0 239 %val_thres(jj,ii)=threshold(jj);
tomwalters@0 240
tomwalters@0 241
tomwalters@0 242 end
tomwalters@0 243
tomwalters@0 244
tomwalters@0 245
tomwalters@0 246
tomwalters@0 247
tomwalters@0 248 % prepare next round
tomwalters@0 249 if ii< nr_dots
tomwalters@0 250 thresholdlast=threshold;
tomwalters@0 251 end
tomwalters@0 252
tomwalters@0 253 if nr_chan==1
tomwalters@0 254 tvals(ii)=thresholdlast;
tomwalters@0 255 end
tomwalters@0 256
tomwalters@0 257
tomwalters@0 258 end
tomwalters@0 259
tomwalters@0 260
tomwalters@0 261
tomwalters@0 262
tomwalters@0 263
tomwalters@0 264 if nr_chan==1
tomwalters@0 265 figure(onechannelfigure);
tomwalters@0 266 plot(nap);
tomwalters@0 267 hold on
tomwalters@0 268 a=plot(tvals,'r');
tomwalters@0 269 set(a,'LineWidth',1.5);
tomwalters@0 270 end
tomwalters@0 271
tomwalters@0 272
tomwalters@0 273 nap=setvalues(nap,new_vals);
tomwalters@0 274 %save nap.mat nap;
tomwalters@0 275
tomwalters@0 276 vals=getvalues(nap);
tomwalters@0 277 nap=setallmaxvalue(nap,max(max(vals))*2);
tomwalters@0 278 nap=setallminvalue(nap,min(min(vals)));
tomwalters@0 279
tomwalters@0 280
tomwalters@0 281
tomwalters@0 282 %save val_e.mat val_e;
tomwalters@0 283 %save new_vals.mat new_vals;
tomwalters@0 284 %save val_inp.mat val_inp;
tomwalters@0 285 %save val_thres.mat val_thres;
tomwalters@0 286
tomwalters@0 287
tomwalters@0 288
tomwalters@0 289
tomwalters@0 290
tomwalters@0 291
tomwalters@0 292
tomwalters@0 293 ret=nap;
tomwalters@0 294 if nr_chan>1
tomwalters@0 295 close(waithand);
tomwalters@0 296 end
tomwalters@0 297
tomwalters@0 298 figure(oldfigure);