tom@455
|
1 % Copyright 2012, Google, Inc.
|
tom@455
|
2 % Author: Richard F. Lyon
|
tom@455
|
3 %
|
tom@455
|
4 % This Matlab file is part of an implementation of Lyon's cochlear model:
|
tom@455
|
5 % "Cascade of Asymmetric Resonators with Fast-Acting Compression"
|
tom@455
|
6 % to supplement Lyon's upcoming book "Human and Machine Hearing"
|
tom@455
|
7 %
|
tom@455
|
8 % Licensed under the Apache License, Version 2.0 (the "License");
|
tom@455
|
9 % you may not use this file except in compliance with the License.
|
tom@455
|
10 % You may obtain a copy of the License at
|
tom@455
|
11 %
|
tom@455
|
12 % http://www.apache.org/licenses/LICENSE-2.0
|
tom@455
|
13 %
|
tom@455
|
14 % Unless required by applicable law or agreed to in writing, software
|
tom@455
|
15 % distributed under the License is distributed on an "AS IS" BASIS,
|
tom@455
|
16 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
tom@455
|
17 % See the License for the specific language governing permissions and
|
tom@455
|
18 % limitations under the License.
|
tom@455
|
19
|
tom@455
|
20 function CF = CARFAC_Design(fs, CF_filter_params, ...
|
tom@455
|
21 CF_AGC_params, ERB_break_freq, ERB_Q, CF_IHC_params)
|
tom@455
|
22 % function CF = CARFAC_Design(fs, CF_filter_params, ...
|
tom@455
|
23 % CF_AGC_params, ERB_break_freq, ERB_Q, CF_IHC_params)
|
tom@455
|
24 %
|
tom@455
|
25 % This function designs the CARFAC (Cascade of Asymmetric Resonators with
|
tom@455
|
26 % Fast-Acting Compression); that is, it take bundles of parameters and
|
tom@455
|
27 % computes all the filter coefficients needed to run it.
|
tom@455
|
28 %
|
tom@455
|
29 % fs is sample rate (per second)
|
tom@455
|
30 % CF_filter_params bundles all the pole-zero filter cascade parameters
|
tom@455
|
31 % CF_AGC_params bundles all the automatic gain control parameters
|
tom@455
|
32 % CF_IHC_params bundles all the inner hair cell parameters
|
tom@455
|
33 %
|
tom@455
|
34 % See other functions for designing and characterizing the CARFAC:
|
tom@455
|
35 % [naps, CF] = CARFAC_Run(CF, input_waves)
|
tom@455
|
36 % transfns = CARFAC_Transfer_Functions(CF, to_channels, from_channels)
|
tom@455
|
37 %
|
tom@455
|
38 % Defaults to Glasberg & Moore's ERB curve:
|
tom@455
|
39 % ERB_break_freq = 1000/4.37; % 228.833
|
tom@455
|
40 % ERB_Q = 1000/(24.7*4.37); % 9.2645
|
tom@455
|
41 %
|
tom@455
|
42 % All args are defaultable; for sample/default args see the code; they
|
tom@455
|
43 % make 96 channels at default fs = 22050, 114 channels at 44100.
|
tom@455
|
44
|
tom@455
|
45 if nargin < 6
|
tom@455
|
46 % HACK: these constant control the defaults
|
tom@455
|
47 one_cap = 0; % bool; 0 for new two-cap hack
|
tom@455
|
48 just_hwr = 0; % book; 0 for normal/fancy IHC; 1 for HWR
|
tom@455
|
49 if just_hwr
|
tom@455
|
50 CF_IHC_params = struct('just_hwr', 1); % just a simple HWR
|
tom@455
|
51 else
|
tom@455
|
52 if one_cap
|
tom@455
|
53 CF_IHC_params = struct( ...
|
tom@455
|
54 'just_hwr', 0, ... % not just a simple HWR
|
tom@455
|
55 'one_cap', one_cap, ... % bool; 0 for new two-cap hack
|
tom@455
|
56 'tau_lpf', 0.000080, ... % 80 microseconds smoothing twice
|
tom@455
|
57 'tau_out', 0.0005, ... % depletion tau is pretty fast
|
tom@455
|
58 'tau_in', 0.010 ); % recovery tau is slower
|
tom@455
|
59 else
|
tom@455
|
60 CF_IHC_params = struct( ...
|
tom@455
|
61 'just_hwr', 0, ... % not just a simple HWR
|
tom@455
|
62 'one_cap', one_cap, ... % bool; 0 for new two-cap hack
|
tom@455
|
63 'tau_lpf', 0.000080, ... % 80 microseconds smoothing twice
|
tom@455
|
64 'tau1_out', 0.020, ... % depletion tau is pretty fast
|
tom@455
|
65 'tau1_in', 0.020, ... % recovery tau is slower
|
tom@455
|
66 'tau2_out', 0.005, ... % depletion tau is pretty fast
|
tom@455
|
67 'tau2_in', 0.005 ); % recovery tau is slower
|
tom@455
|
68 end
|
tom@455
|
69 end
|
tom@455
|
70 end
|
tom@455
|
71
|
tom@455
|
72 if nargin < 5
|
tom@455
|
73 % Ref: Glasberg and Moore: Hearing Research, 47 (1990), 103-138
|
tom@455
|
74 % ERB = 24.7 * (1 + 4.37 * CF_Hz / 1000);
|
tom@455
|
75 ERB_Q = 1000/(24.7*4.37); % 9.2645
|
tom@455
|
76 if nargin < 4
|
tom@455
|
77 ERB_break_freq = 1000/4.37; % 228.833
|
tom@455
|
78 end
|
tom@455
|
79 end
|
tom@455
|
80
|
tom@455
|
81 if nargin < 3
|
tom@455
|
82 CF_AGC_params = struct( ...
|
tom@455
|
83 'n_stages', 4, ...
|
tom@455
|
84 'time_constants', [1, 4, 16, 64]*0.002, ...
|
tom@455
|
85 'AGC_stage_gain', 2, ... % gain from each stage to next slower stage
|
tom@455
|
86 'decimation', 16, ... % how often to update the AGC states
|
tom@455
|
87 'AGC1_scales', [1, 2, 3, 4]*1, ... % in units of channels
|
tom@455
|
88 'AGC2_scales', [1, 2, 3, 4]*1.25, ... % spread more toward base
|
tom@455
|
89 'detect_scale', 0.15, ... % the desired damping range
|
tom@455
|
90 'AGC_mix_coeff', 0.25);
|
tom@455
|
91 end
|
tom@455
|
92
|
tom@455
|
93 if nargin < 2
|
tom@455
|
94 CF_filter_params = struct( ...
|
tom@455
|
95 'velocity_scale', 0.2, ... % for the cubic nonlinearity
|
tom@455
|
96 'min_zeta', 0.12, ...
|
tom@455
|
97 'first_pole_theta', 0.78*pi, ...
|
tom@455
|
98 'zero_ratio', sqrt(2), ...
|
tom@455
|
99 'ERB_per_step', 0.3333, ... % assume G&M's ERB formula
|
tom@455
|
100 'min_pole_Hz', 40 );
|
tom@455
|
101 end
|
tom@455
|
102
|
tom@455
|
103 if nargin < 1
|
tom@455
|
104 fs = 22050;
|
tom@455
|
105 end
|
tom@455
|
106
|
tom@455
|
107 % first figure out how many filter stages (PZFC/CARFAC channels):
|
tom@455
|
108 pole_Hz = CF_filter_params.first_pole_theta * fs / (2*pi);
|
tom@455
|
109 n_ch = 0;
|
tom@455
|
110 while pole_Hz > CF_filter_params.min_pole_Hz
|
tom@455
|
111 n_ch = n_ch + 1;
|
tom@455
|
112 pole_Hz = pole_Hz - CF_filter_params.ERB_per_step * ...
|
tom@455
|
113 ERB_Hz(pole_Hz, ERB_break_freq, ERB_Q);
|
tom@455
|
114 end
|
tom@455
|
115 % Now we have n_ch, the number of channels, so can make the array
|
tom@455
|
116 % and compute all the frequencies again to put into it:
|
tom@455
|
117 pole_freqs = zeros(n_ch, 1);
|
tom@455
|
118 pole_Hz = CF_filter_params.first_pole_theta * fs / (2*pi);
|
tom@455
|
119 for ch = 1:n_ch
|
tom@455
|
120 pole_freqs(ch) = pole_Hz;
|
tom@455
|
121 pole_Hz = pole_Hz - CF_filter_params.ERB_per_step * ...
|
tom@455
|
122 ERB_Hz(pole_Hz, ERB_break_freq, ERB_Q);
|
tom@455
|
123 end
|
tom@455
|
124 % now we have n_ch, the number of channels, and pole_freqs array
|
tom@455
|
125
|
tom@455
|
126 CF = struct( ...
|
tom@455
|
127 'fs', fs, ...
|
tom@455
|
128 'filter_params', CF_filter_params, ...
|
tom@455
|
129 'AGC_params', CF_AGC_params, ...
|
tom@455
|
130 'IHC_params', CF_IHC_params, ...
|
tom@455
|
131 'n_ch', n_ch, ...
|
tom@455
|
132 'pole_freqs', pole_freqs, ...
|
tom@455
|
133 'filter_coeffs', CARFAC_DesignFilters(CF_filter_params, fs, pole_freqs), ...
|
tom@455
|
134 'AGC_coeffs', CARFAC_DesignAGC(CF_AGC_params, fs), ...
|
tom@455
|
135 'IHC_coeffs', CARFAC_DesignIHC(CF_IHC_params, fs), ...
|
tom@455
|
136 'n_mics', 0 );
|
tom@455
|
137
|
tom@455
|
138 % adjust the AGC_coeffs to account for IHC saturation level to get right
|
tom@455
|
139 % damping change as specified in CF.AGC_params.detect_scale
|
tom@455
|
140 CF.AGC_coeffs.detect_scale = CF.AGC_params.detect_scale / ...
|
tom@455
|
141 (CF.IHC_coeffs.saturation_output * CF.AGC_coeffs.AGC_gain);
|
tom@455
|
142
|
tom@455
|
143 %% Design the filter coeffs:
|
tom@455
|
144 function filter_coeffs = CARFAC_DesignFilters(filter_params, fs, pole_freqs)
|
tom@455
|
145
|
tom@455
|
146 n_ch = length(pole_freqs);
|
tom@455
|
147
|
tom@455
|
148 % the filter design coeffs:
|
tom@455
|
149
|
tom@455
|
150 filter_coeffs = struct('velocity_scale', filter_params.velocity_scale);
|
tom@455
|
151
|
tom@455
|
152 filter_coeffs.r_coeffs = zeros(n_ch, 1);
|
tom@455
|
153 filter_coeffs.a_coeffs = zeros(n_ch, 1);
|
tom@455
|
154 filter_coeffs.c_coeffs = zeros(n_ch, 1);
|
tom@455
|
155 filter_coeffs.h_coeffs = zeros(n_ch, 1);
|
tom@455
|
156 filter_coeffs.g_coeffs = zeros(n_ch, 1);
|
tom@455
|
157
|
tom@455
|
158 % zero_ratio comes in via h. In book's circuit D, zero_ratio is 1/sqrt(a),
|
tom@455
|
159 % and that a is here 1 / (1+f) where h = f*c.
|
tom@455
|
160 % solve for f: 1/zero_ratio^2 = 1 / (1+f)
|
tom@455
|
161 % zero_ratio^2 = 1+f => f = zero_ratio^2 - 1
|
tom@455
|
162 f = filter_params.zero_ratio^2 - 1; % nominally 1 for half-octave
|
tom@455
|
163
|
tom@455
|
164 % Make pole positions, s and c coeffs, h and g coeffs, etc.,
|
tom@455
|
165 % which mostly depend on the pole angle theta:
|
tom@455
|
166 theta = pole_freqs .* (2 * pi / fs);
|
tom@455
|
167
|
tom@455
|
168 % different possible interpretations for min-damping r:
|
tom@455
|
169 % r = exp(-theta * CF_filter_params.min_zeta).
|
tom@455
|
170 % Using sin gives somewhat higher Q at highest thetas.
|
tom@455
|
171 r = (1 - sin(theta) * filter_params.min_zeta);
|
tom@455
|
172 filter_coeffs.r_coeffs = r;
|
tom@455
|
173
|
tom@455
|
174 % undamped coupled-form coefficients:
|
tom@455
|
175 filter_coeffs.a_coeffs = cos(theta);
|
tom@455
|
176 filter_coeffs.c_coeffs = sin(theta);
|
tom@455
|
177
|
tom@455
|
178 % the zeros follow via the h_coeffs
|
tom@455
|
179 h = sin(theta) .* f;
|
tom@455
|
180 filter_coeffs.h_coeffs = h;
|
tom@455
|
181
|
tom@455
|
182 r2 = r; % aim for unity DC gain at min damping, here; or could try r^2
|
tom@455
|
183 filter_coeffs.g_coeffs = 1 ./ (1 + h .* r2 .* sin(theta) ./ ...
|
tom@455
|
184 (1 - 2 * r2 .* cos(theta) + r2 .^ 2));
|
tom@455
|
185
|
tom@455
|
186
|
tom@455
|
187 %% the AGC design coeffs:
|
tom@455
|
188 function AGC_coeffs = CARFAC_DesignAGC(AGC_params, fs)
|
tom@455
|
189
|
tom@455
|
190 AGC_coeffs = struct('AGC_stage_gain', AGC_params.AGC_stage_gain, ...
|
tom@455
|
191 'AGC_mix_coeff', AGC_params.AGC_mix_coeff);
|
tom@455
|
192
|
tom@455
|
193
|
tom@455
|
194 % AGC1 pass is smoothing from base toward apex;
|
tom@455
|
195 % AGC2 pass is back, which is done first now
|
tom@455
|
196 AGC1_scales = AGC_params.AGC1_scales;
|
tom@455
|
197 AGC2_scales = AGC_params.AGC2_scales;
|
tom@455
|
198
|
tom@455
|
199 n_AGC_stages = AGC_params.n_stages;
|
tom@455
|
200 AGC_coeffs.AGC_epsilon = zeros(1, n_AGC_stages); % the 1/(tau*fs) roughly
|
tom@455
|
201 decim = AGC_params.decimation;
|
tom@455
|
202 gain = 0;
|
tom@455
|
203 for stage = 1:n_AGC_stages
|
tom@455
|
204 tau = AGC_params.time_constants(stage);
|
tom@455
|
205 % epsilon is how much new input to take at each update step:
|
tom@455
|
206 AGC_coeffs.AGC_epsilon(stage) = 1 - exp(-decim / (tau * fs));
|
tom@455
|
207 % and these are the smoothing scales and poles for decimated rate:
|
tom@455
|
208 ntimes = tau * (fs / decim); % effective number of smoothings
|
tom@455
|
209 % divide the spatial variance by effective number of smoothings:
|
tom@455
|
210 t = (AGC1_scales(stage)^2) / ntimes; % adjust scale for diffusion
|
tom@455
|
211 AGC_coeffs.AGC1_polez(stage) = 1 + 1/t - sqrt((1+1/t)^2 - 1);
|
tom@455
|
212 t = (AGC2_scales(stage)^2) / ntimes; % adjust scale for diffusion
|
tom@455
|
213 AGC_coeffs.AGC2_polez(stage) = 1 + 1/t - sqrt((1+1/t)^2 - 1);
|
tom@455
|
214 gain = gain + AGC_params.AGC_stage_gain^(stage-1);
|
tom@455
|
215 end
|
tom@455
|
216
|
tom@455
|
217 AGC_coeffs.AGC_gain = gain;
|
tom@455
|
218
|
tom@455
|
219 %% the IHC design coeffs:
|
tom@455
|
220 function IHC_coeffs = CARFAC_DesignIHC(IHC_params, fs)
|
tom@455
|
221
|
tom@455
|
222 if IHC_params.just_hwr
|
tom@455
|
223 IHC_coeffs = struct('just_hwr', 1);
|
tom@455
|
224 IHC_coeffs.saturation_output = 10; % HACK: assume some max out
|
tom@455
|
225 else
|
tom@455
|
226 if IHC_params.one_cap
|
tom@455
|
227 IHC_coeffs = struct(...
|
tom@455
|
228 'just_hwr', 0, ...
|
tom@455
|
229 'lpf_coeff', 1 - exp(-1/(IHC_params.tau_lpf * fs)), ...
|
tom@455
|
230 'out_rate', 1 / (IHC_params.tau_out * fs), ...
|
tom@455
|
231 'in_rate', 1 / (IHC_params.tau_in * fs), ...
|
tom@455
|
232 'one_cap', IHC_params.one_cap);
|
tom@455
|
233 else
|
tom@455
|
234 IHC_coeffs = struct(...
|
tom@455
|
235 'just_hwr', 0, ...
|
tom@455
|
236 'lpf_coeff', 1 - exp(-1/(IHC_params.tau_lpf * fs)), ...
|
tom@455
|
237 'out1_rate', 1 / (IHC_params.tau1_out * fs), ...
|
tom@455
|
238 'in1_rate', 1 / (IHC_params.tau1_in * fs), ...
|
tom@455
|
239 'out2_rate', 1 / (IHC_params.tau2_out * fs), ...
|
tom@455
|
240 'in2_rate', 1 / (IHC_params.tau2_in * fs), ...
|
tom@455
|
241 'one_cap', IHC_params.one_cap);
|
tom@455
|
242 end
|
tom@455
|
243
|
tom@455
|
244 % run one channel to convergence to get rest state:
|
tom@455
|
245 IHC_coeffs.rest_output = 0;
|
tom@455
|
246 IHC_state = struct( ...
|
tom@455
|
247 'cap_voltage', 0, ...
|
tom@455
|
248 'cap1_voltage', 0, ...
|
tom@455
|
249 'cap2_voltage', 0, ...
|
tom@455
|
250 'lpf1_state', 0, ...
|
tom@455
|
251 'lpf2_state', 0, ...
|
tom@455
|
252 'ihc_accum', 0);
|
tom@455
|
253
|
tom@455
|
254 IHC_in = 0;
|
tom@455
|
255 for k = 1:30000
|
tom@455
|
256 [IHC_out, IHC_state] = CARFAC_IHCStep(IHC_in, IHC_coeffs, IHC_state);
|
tom@455
|
257 end
|
tom@455
|
258
|
tom@455
|
259 IHC_coeffs.rest_output = IHC_out;
|
tom@455
|
260 IHC_coeffs.rest_cap = IHC_state.cap_voltage;
|
tom@455
|
261 IHC_coeffs.rest_cap1 = IHC_state.cap1_voltage;
|
tom@455
|
262 IHC_coeffs.rest_cap2 = IHC_state.cap2_voltage;
|
tom@455
|
263
|
tom@455
|
264 LARGE = 2;
|
tom@455
|
265 IHC_in = LARGE; % "Large" saturating input to IHC; make it alternate
|
tom@455
|
266 for k = 1:30000
|
tom@455
|
267 [IHC_out, IHC_state] = CARFAC_IHCStep(IHC_in, IHC_coeffs, IHC_state);
|
tom@455
|
268 prev_IHC_out = IHC_out;
|
tom@455
|
269 IHC_in = -IHC_in;
|
tom@455
|
270 end
|
tom@455
|
271
|
tom@455
|
272 IHC_coeffs.saturation_output = (IHC_out + prev_IHC_out) / 2;
|
tom@455
|
273 end
|
tom@455
|
274
|
tom@455
|
275 %%
|
tom@455
|
276 % default design result, running this function with no args, should look
|
tom@455
|
277 % like this, before CARFAC_Init puts state storage into it:
|
tom@455
|
278 %
|
tom@455
|
279 % CF = CARFAC_Design
|
tom@455
|
280 % CF.filter_params
|
tom@455
|
281 % CF.AGC_params
|
tom@455
|
282 % CF.filter_coeffs
|
tom@455
|
283 % CF.AGC_coeffs
|
tom@455
|
284 % CF.IHC_coeffs
|
tom@455
|
285 %
|
tom@455
|
286 % CF =
|
tom@455
|
287 % fs: 22050
|
tom@455
|
288 % filter_params: [1x1 struct]
|
tom@455
|
289 % AGC_params: [1x1 struct]
|
tom@455
|
290 % IHC_params: [1x1 struct]
|
tom@455
|
291 % n_ch: 96
|
tom@455
|
292 % pole_freqs: [96x1 double]
|
tom@455
|
293 % filter_coeffs: [1x1 struct]
|
tom@455
|
294 % AGC_coeffs: [1x1 struct]
|
tom@455
|
295 % IHC_coeffs: [1x1 struct]
|
tom@455
|
296 % n_mics: 0
|
tom@455
|
297 % ans =
|
tom@455
|
298 % velocity_scale: 0.2000
|
tom@455
|
299 % min_zeta: 0.1200
|
tom@455
|
300 % first_pole_theta: 2.4504
|
tom@455
|
301 % zero_ratio: 1.4142
|
tom@455
|
302 % ERB_per_step: 0.3333
|
tom@455
|
303 % min_pole_Hz: 40
|
tom@455
|
304 % ans =
|
tom@455
|
305 % n_stages: 4
|
tom@455
|
306 % time_constants: [0.0020 0.0080 0.0320 0.1280]
|
tom@455
|
307 % AGC_stage_gain: 2
|
tom@455
|
308 % decimation: 16
|
tom@455
|
309 % AGC1_scales: [1 2 3 4]
|
tom@455
|
310 % AGC2_scales: [1.2500 2.5000 3.7500 5]
|
tom@455
|
311 % detect_scale: 0.1500
|
tom@455
|
312 % AGC_mix_coeff: 0.2500
|
tom@455
|
313 % ans =
|
tom@455
|
314 % velocity_scale: 0.2000
|
tom@455
|
315 % r_coeffs: [96x1 double]
|
tom@455
|
316 % a_coeffs: [96x1 double]
|
tom@455
|
317 % c_coeffs: [96x1 double]
|
tom@455
|
318 % h_coeffs: [96x1 double]
|
tom@455
|
319 % g_coeffs: [96x1 double]
|
tom@455
|
320 % ans =
|
tom@455
|
321 % AGC_stage_gain: 2
|
tom@455
|
322 % AGC_mix_coeff: 0.2500
|
tom@455
|
323 % AGC_epsilon: [0.3043 0.0867 0.0224 0.0057]
|
tom@455
|
324 % AGC1_polez: [0.1356 0.1356 0.0854 0.0417]
|
tom@455
|
325 % AGC2_polez: [0.1872 0.1872 0.1227 0.0623]
|
tom@455
|
326 % AGC_gain: 15
|
tom@455
|
327 % detect_scale: 0.0630
|
tom@455
|
328 % ans =
|
tom@455
|
329 % lpf_coeff: 0.4327
|
tom@455
|
330 % out1_rate: 0.0023
|
tom@455
|
331 % in1_rate: 0.0023
|
tom@455
|
332 % out2_rate: 0.0091
|
tom@455
|
333 % in2_rate: 0.0091
|
tom@455
|
334 % one_cap: 0
|
tom@455
|
335 % rest_output: 0.0365
|
tom@455
|
336 % rest_cap: 0
|
tom@455
|
337 % rest_cap1: 0.9635
|
tom@455
|
338 % rest_cap2: 0.9269
|
tom@455
|
339 % saturation_output: 0.1587
|
tom@455
|
340
|
tom@455
|
341
|
tom@455
|
342
|