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