tom@516
|
1 % Copyright 2012, Google, Inc.
|
tom@516
|
2 % Author: Richard F. Lyon
|
tom@516
|
3 %
|
tom@516
|
4 % This Matlab file is part of an implementation of Lyon's cochlear model:
|
tom@516
|
5 % "Cascade of Asymmetric Resonators with Fast-Acting Compression"
|
tom@516
|
6 % to supplement Lyon's upcoming book "Human and Machine Hearing"
|
tom@516
|
7 %
|
tom@516
|
8 % Licensed under the Apache License, Version 2.0 (the "License");
|
tom@516
|
9 % you may not use this file except in compliance with the License.
|
tom@516
|
10 % You may obtain a copy of the License at
|
tom@516
|
11 %
|
tom@516
|
12 % http://www.apache.org/licenses/LICENSE-2.0
|
tom@516
|
13 %
|
tom@516
|
14 % Unless required by applicable law or agreed to in writing, software
|
tom@516
|
15 % distributed under the License is distributed on an "AS IS" BASIS,
|
tom@516
|
16 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
tom@516
|
17 % See the License for the specific language governing permissions and
|
tom@516
|
18 % limitations under the License.
|
tom@516
|
19
|
dicklyon@553
|
20 function CF = CARFAC_Design(fs, CF_CAR_params, CF_AGC_params, CF_IHC_params)
|
dicklyon@534
|
21 % function CF = CARFAC_Design(fs, CF_CAR_params, ...
|
tom@516
|
22 % CF_AGC_params, ERB_break_freq, ERB_Q, CF_IHC_params)
|
tom@516
|
23 %
|
tom@516
|
24 % This function designs the CARFAC (Cascade of Asymmetric Resonators with
|
tom@516
|
25 % Fast-Acting Compression); that is, it take bundles of parameters and
|
tom@516
|
26 % computes all the filter coefficients needed to run it.
|
tom@516
|
27 %
|
tom@516
|
28 % fs is sample rate (per second)
|
dicklyon@534
|
29 % CF_CAR_params bundles all the pole-zero filter cascade parameters
|
tom@516
|
30 % CF_AGC_params bundles all the automatic gain control parameters
|
tom@516
|
31 % CF_IHC_params bundles all the inner hair cell parameters
|
tom@516
|
32 %
|
tom@516
|
33 % See other functions for designing and characterizing the CARFAC:
|
tom@516
|
34 % [naps, CF] = CARFAC_Run(CF, input_waves)
|
tom@516
|
35 % transfns = CARFAC_Transfer_Functions(CF, to_channels, from_channels)
|
tom@516
|
36 %
|
tom@516
|
37 % Defaults to Glasberg & Moore's ERB curve:
|
tom@516
|
38 % ERB_break_freq = 1000/4.37; % 228.833
|
tom@516
|
39 % ERB_Q = 1000/(24.7*4.37); % 9.2645
|
tom@516
|
40 %
|
tom@516
|
41 % All args are defaultable; for sample/default args see the code; they
|
tom@516
|
42 % make 96 channels at default fs = 22050, 114 channels at 44100.
|
tom@516
|
43
|
dicklyon@553
|
44 if nargin < 4
|
tom@516
|
45 % HACK: these constant control the defaults
|
tom@516
|
46 one_cap = 0; % bool; 0 for new two-cap hack
|
tom@516
|
47 just_hwr = 0; % book; 0 for normal/fancy IHC; 1 for HWR
|
tom@516
|
48 if just_hwr
|
tom@516
|
49 CF_IHC_params = struct('just_hwr', 1); % just a simple HWR
|
tom@516
|
50 else
|
tom@516
|
51 if one_cap
|
tom@516
|
52 CF_IHC_params = struct( ...
|
dicklyon@523
|
53 'just_hwr', just_hwr, ... % not just a simple HWR
|
tom@516
|
54 'one_cap', one_cap, ... % bool; 0 for new two-cap hack
|
tom@516
|
55 'tau_lpf', 0.000080, ... % 80 microseconds smoothing twice
|
tom@516
|
56 'tau_out', 0.0005, ... % depletion tau is pretty fast
|
tom@516
|
57 'tau_in', 0.010 ); % recovery tau is slower
|
tom@516
|
58 else
|
tom@516
|
59 CF_IHC_params = struct( ...
|
dicklyon@523
|
60 'just_hwr', just_hwr, ... % not just a simple HWR
|
tom@516
|
61 'one_cap', one_cap, ... % bool; 0 for new two-cap hack
|
tom@516
|
62 'tau_lpf', 0.000080, ... % 80 microseconds smoothing twice
|
dicklyon@556
|
63 'tau1_out', 0.010, ... % depletion tau is pretty fast
|
tom@516
|
64 'tau1_in', 0.020, ... % recovery tau is slower
|
dicklyon@556
|
65 'tau2_out', 0.0025, ... % depletion tau is pretty fast
|
tom@516
|
66 'tau2_in', 0.005 ); % recovery tau is slower
|
tom@516
|
67 end
|
tom@516
|
68 end
|
tom@516
|
69 end
|
tom@516
|
70
|
tom@516
|
71 if nargin < 3
|
tom@516
|
72 CF_AGC_params = struct( ...
|
tom@516
|
73 'n_stages', 4, ...
|
tom@516
|
74 'time_constants', [1, 4, 16, 64]*0.002, ...
|
tom@516
|
75 'AGC_stage_gain', 2, ... % gain from each stage to next slower stage
|
dicklyon@523
|
76 'decimation', [8, 2, 2, 2], ... % how often to update the AGC states
|
dicklyon@536
|
77 'AGC1_scales', [1.0, 1.4, 2.0, 2.8], ... % in units of channels
|
dicklyon@536
|
78 'AGC2_scales', [1.6, 2.25, 3.2, 4.5], ... % spread more toward base
|
dicklyon@536
|
79 'detect_scale', 0.25, ... % the desired damping range
|
dicklyon@523
|
80 'AGC_mix_coeff', 0.5);
|
tom@516
|
81 end
|
tom@516
|
82
|
tom@516
|
83 if nargin < 2
|
dicklyon@534
|
84 CF_CAR_params = struct( ...
|
dicklyon@523
|
85 'velocity_scale', 0.2, ... % for the "cubic" velocity nonlinearity
|
dicklyon@523
|
86 'v_offset', 0.01, ... % offset gives a quadratic part
|
dicklyon@523
|
87 'v2_corner', 0.2, ... % corner for essential nonlin
|
dicklyon@523
|
88 'v_damp_max', 0.01, ... % damping delta damping from velocity nonlin
|
dicklyon@533
|
89 'min_zeta', 0.10, ... % minimum damping factor in mid-freq channels
|
dicklyon@528
|
90 'first_pole_theta', 0.85*pi, ...
|
dicklyon@528
|
91 'zero_ratio', sqrt(2), ... % how far zero is above pole
|
dicklyon@530
|
92 'high_f_damping_compression', 0.5, ... % 0 to 1 to compress zeta
|
dicklyon@528
|
93 'ERB_per_step', 0.5, ... % assume G&M's ERB formula
|
dicklyon@553
|
94 'min_pole_Hz', 30, ...
|
dicklyon@553
|
95 'ERB_break_freq', 165.3, ... % Greenwood map's break freq.
|
dicklyon@553
|
96 'ERB_Q', 1000/(24.7*4.37)); % Glasberg and Moore's high-cf ratio
|
tom@516
|
97 end
|
tom@516
|
98
|
tom@516
|
99 if nargin < 1
|
tom@516
|
100 fs = 22050;
|
tom@516
|
101 end
|
tom@516
|
102
|
tom@516
|
103 % first figure out how many filter stages (PZFC/CARFAC channels):
|
dicklyon@534
|
104 pole_Hz = CF_CAR_params.first_pole_theta * fs / (2*pi);
|
tom@516
|
105 n_ch = 0;
|
dicklyon@534
|
106 while pole_Hz > CF_CAR_params.min_pole_Hz
|
tom@516
|
107 n_ch = n_ch + 1;
|
dicklyon@534
|
108 pole_Hz = pole_Hz - CF_CAR_params.ERB_per_step * ...
|
dicklyon@553
|
109 ERB_Hz(pole_Hz, CF_CAR_params.ERB_break_freq, CF_CAR_params.ERB_Q);
|
tom@516
|
110 end
|
tom@516
|
111 % Now we have n_ch, the number of channels, so can make the array
|
tom@516
|
112 % and compute all the frequencies again to put into it:
|
tom@516
|
113 pole_freqs = zeros(n_ch, 1);
|
dicklyon@534
|
114 pole_Hz = CF_CAR_params.first_pole_theta * fs / (2*pi);
|
tom@516
|
115 for ch = 1:n_ch
|
tom@516
|
116 pole_freqs(ch) = pole_Hz;
|
dicklyon@534
|
117 pole_Hz = pole_Hz - CF_CAR_params.ERB_per_step * ...
|
dicklyon@553
|
118 ERB_Hz(pole_Hz, CF_CAR_params.ERB_break_freq, CF_CAR_params.ERB_Q);
|
tom@516
|
119 end
|
tom@516
|
120 % now we have n_ch, the number of channels, and pole_freqs array
|
tom@516
|
121
|
dicklyon@528
|
122 max_channels_per_octave = log(2) / log(pole_freqs(1)/pole_freqs(2));
|
dicklyon@528
|
123
|
tom@516
|
124 CF = struct( ...
|
tom@516
|
125 'fs', fs, ...
|
dicklyon@528
|
126 'max_channels_per_octave', max_channels_per_octave, ...
|
dicklyon@534
|
127 'CAR_params', CF_CAR_params, ...
|
tom@516
|
128 'AGC_params', CF_AGC_params, ...
|
tom@516
|
129 'IHC_params', CF_IHC_params, ...
|
tom@516
|
130 'n_ch', n_ch, ...
|
tom@516
|
131 'pole_freqs', pole_freqs, ...
|
dicklyon@534
|
132 'CAR_coeffs', CARFAC_DesignFilters(CF_CAR_params, fs, pole_freqs), ...
|
dicklyon@534
|
133 'AGC_coeffs', CARFAC_DesignAGC(CF_AGC_params, fs, n_ch), ...
|
dicklyon@534
|
134 'IHC_coeffs', CARFAC_DesignIHC(CF_IHC_params, fs, n_ch), ...
|
dicklyon@534
|
135 'n_ears', 0 );
|
tom@516
|
136
|
tom@516
|
137
|
dicklyon@534
|
138
|
tom@516
|
139 %% Design the filter coeffs:
|
dicklyon@534
|
140 function CAR_coeffs = CARFAC_DesignFilters(CAR_params, fs, pole_freqs)
|
tom@516
|
141
|
tom@516
|
142 n_ch = length(pole_freqs);
|
tom@516
|
143
|
tom@516
|
144 % the filter design coeffs:
|
tom@516
|
145
|
dicklyon@534
|
146 CAR_coeffs = struct( ...
|
dicklyon@534
|
147 'n_ch', n_ch, ...
|
dicklyon@534
|
148 'velocity_scale', CAR_params.velocity_scale, ...
|
dicklyon@534
|
149 'v_offset', CAR_params.v_offset, ...
|
dicklyon@534
|
150 'v2_corner', CAR_params.v2_corner, ...
|
dicklyon@534
|
151 'v_damp_max', CAR_params.v_damp_max ...
|
dicklyon@523
|
152 );
|
tom@516
|
153
|
dicklyon@534
|
154 CAR_coeffs.r1_coeffs = zeros(n_ch, 1);
|
dicklyon@534
|
155 CAR_coeffs.a0_coeffs = zeros(n_ch, 1);
|
dicklyon@534
|
156 CAR_coeffs.c0_coeffs = zeros(n_ch, 1);
|
dicklyon@534
|
157 CAR_coeffs.h_coeffs = zeros(n_ch, 1);
|
dicklyon@534
|
158 CAR_coeffs.g0_coeffs = zeros(n_ch, 1);
|
tom@516
|
159
|
tom@516
|
160 % zero_ratio comes in via h. In book's circuit D, zero_ratio is 1/sqrt(a),
|
tom@516
|
161 % and that a is here 1 / (1+f) where h = f*c.
|
tom@516
|
162 % solve for f: 1/zero_ratio^2 = 1 / (1+f)
|
tom@516
|
163 % zero_ratio^2 = 1+f => f = zero_ratio^2 - 1
|
dicklyon@534
|
164 f = CAR_params.zero_ratio^2 - 1; % nominally 1 for half-octave
|
tom@516
|
165
|
tom@516
|
166 % Make pole positions, s and c coeffs, h and g coeffs, etc.,
|
tom@516
|
167 % which mostly depend on the pole angle theta:
|
tom@516
|
168 theta = pole_freqs .* (2 * pi / fs);
|
tom@516
|
169
|
dicklyon@530
|
170 c0 = sin(theta);
|
dicklyon@530
|
171 a0 = cos(theta);
|
dicklyon@530
|
172
|
tom@516
|
173 % different possible interpretations for min-damping r:
|
dicklyon@534
|
174 % r = exp(-theta * CF_CAR_params.min_zeta).
|
dicklyon@530
|
175 % Compress theta to give somewhat higher Q at highest thetas:
|
dicklyon@534
|
176 ff = CAR_params.high_f_damping_compression; % 0 to 1; typ. 0.5
|
dicklyon@530
|
177 x = theta/pi;
|
dicklyon@530
|
178 zr_coeffs = pi * (x - ff * x.^3); % when ff is 0, this is just theta,
|
dicklyon@530
|
179 % and when ff is 1 it goes to zero at theta = pi.
|
dicklyon@534
|
180 CAR_coeffs.zr_coeffs = zr_coeffs; % how r relates to zeta
|
dicklyon@530
|
181
|
dicklyon@534
|
182 min_zeta = CAR_params.min_zeta;
|
dicklyon@533
|
183 % increase the min damping where channels are spaced out more:
|
dicklyon@553
|
184
|
dicklyon@553
|
185 min_zeta = min_zeta + 0.25*(ERB_Hz(pole_freqs, ...
|
dicklyon@553
|
186 CAR_params.ERB_break_freq, CAR_params.ERB_Q) ./ pole_freqs - min_zeta);
|
dicklyon@533
|
187 r1 = (1 - zr_coeffs .* min_zeta); % "1" for the min-damping condition
|
dicklyon@533
|
188
|
dicklyon@534
|
189 CAR_coeffs.r1_coeffs = r1;
|
tom@516
|
190
|
tom@516
|
191 % undamped coupled-form coefficients:
|
dicklyon@534
|
192 CAR_coeffs.a0_coeffs = a0;
|
dicklyon@534
|
193 CAR_coeffs.c0_coeffs = c0;
|
tom@516
|
194
|
tom@516
|
195 % the zeros follow via the h_coeffs
|
dicklyon@530
|
196 h = c0 .* f;
|
dicklyon@534
|
197 CAR_coeffs.h_coeffs = h;
|
tom@516
|
198
|
dicklyon@530
|
199 % for unity gain at min damping, radius r; only used in CARFAC_Init:
|
dicklyon@533
|
200 extra_damping = zeros(size(r1));
|
dicklyon@534
|
201 % this function needs to take CAR_coeffs even if we haven't finished
|
dicklyon@530
|
202 % constucting it by putting in the g0_coeffs:
|
dicklyon@534
|
203 CAR_coeffs.g0_coeffs = CARFAC_Stage_g(CAR_coeffs, extra_damping);
|
tom@516
|
204
|
tom@516
|
205
|
tom@516
|
206 %% the AGC design coeffs:
|
dicklyon@534
|
207 function AGC_coeffs = CARFAC_DesignAGC(AGC_params, fs, n_ch)
|
tom@516
|
208
|
dicklyon@534
|
209 n_AGC_stages = AGC_params.n_stages;
|
dicklyon@534
|
210 AGC_coeffs = struct( ...
|
dicklyon@534
|
211 'n_ch', n_ch, ...
|
dicklyon@534
|
212 'n_AGC_stages', n_AGC_stages, ...
|
dicklyon@534
|
213 'AGC_stage_gain', AGC_params.AGC_stage_gain);
|
tom@516
|
214
|
tom@516
|
215 % AGC1 pass is smoothing from base toward apex;
|
tom@516
|
216 % AGC2 pass is back, which is done first now
|
tom@516
|
217 AGC1_scales = AGC_params.AGC1_scales;
|
tom@516
|
218 AGC2_scales = AGC_params.AGC2_scales;
|
tom@516
|
219
|
tom@516
|
220 AGC_coeffs.AGC_epsilon = zeros(1, n_AGC_stages); % the 1/(tau*fs) roughly
|
dicklyon@523
|
221 decim = 1;
|
dicklyon@523
|
222 AGC_coeffs.decimation = AGC_params.decimation;
|
dicklyon@523
|
223
|
dicklyon@523
|
224 total_DC_gain = 0;
|
tom@516
|
225 for stage = 1:n_AGC_stages
|
dicklyon@525
|
226 tau = AGC_params.time_constants(stage); % time constant in seconds
|
dicklyon@525
|
227 decim = decim * AGC_params.decimation(stage); % net decim to this stage
|
tom@516
|
228 % epsilon is how much new input to take at each update step:
|
tom@516
|
229 AGC_coeffs.AGC_epsilon(stage) = 1 - exp(-decim / (tau * fs));
|
dicklyon@523
|
230 % effective number of smoothings in a time constant:
|
dicklyon@525
|
231 ntimes = tau * (fs / decim); % typically 5 to 50
|
dicklyon@524
|
232
|
dicklyon@524
|
233 % decide on target spread (variance) and delay (mean) of impulse
|
dicklyon@524
|
234 % response as a distribution to be convolved ntimes:
|
dicklyon@525
|
235 % TODO (dicklyon): specify spread and delay instead of scales???
|
dicklyon@524
|
236 delay = (AGC2_scales(stage) - AGC1_scales(stage)) / ntimes;
|
dicklyon@524
|
237 spread_sq = (AGC1_scales(stage)^2 + AGC2_scales(stage)^2) / ntimes;
|
dicklyon@524
|
238
|
dicklyon@525
|
239 % get pole positions to better match intended spread and delay of
|
dicklyon@525
|
240 % [[geometric distribution]] in each direction (see wikipedia)
|
dicklyon@524
|
241 u = 1 + 1 / spread_sq; % these are based on off-line algebra hacking.
|
dicklyon@524
|
242 p = u - sqrt(u^2 - 1); % pole that would give spread if used twice.
|
dicklyon@524
|
243 dp = delay * (1 - 2*p +p^2)/2;
|
dicklyon@524
|
244 polez1 = p - dp;
|
dicklyon@524
|
245 polez2 = p + dp;
|
dicklyon@523
|
246 AGC_coeffs.AGC_polez1(stage) = polez1;
|
dicklyon@523
|
247 AGC_coeffs.AGC_polez2(stage) = polez2;
|
dicklyon@523
|
248
|
dicklyon@525
|
249 % try a 3- or 5-tap FIR as an alternative to the double exponential:
|
dicklyon@525
|
250 n_taps = 0;
|
dicklyon@525
|
251 FIR_OK = 0;
|
dicklyon@525
|
252 n_iterations = 1;
|
dicklyon@525
|
253 while ~FIR_OK
|
dicklyon@525
|
254 switch n_taps
|
dicklyon@525
|
255 case 0
|
dicklyon@525
|
256 % first attempt a 3-point FIR to apply once:
|
dicklyon@525
|
257 n_taps = 3;
|
dicklyon@525
|
258 case 3
|
dicklyon@525
|
259 % second time through, go wider but stick to 1 iteration
|
dicklyon@525
|
260 n_taps = 5;
|
dicklyon@525
|
261 case 5
|
dicklyon@525
|
262 % apply FIR multiple times instead of going wider:
|
dicklyon@525
|
263 n_iterations = n_iterations + 1;
|
dicklyon@525
|
264 if n_iterations > 16
|
dicklyon@525
|
265 error('Too many n_iterations in CARFAC_DesignAGC');
|
dicklyon@525
|
266 end
|
dicklyon@525
|
267 otherwise
|
dicklyon@525
|
268 % to do other n_taps would need changes in CARFAC_Spatial_Smooth
|
dicklyon@525
|
269 % and in Design_FIR_coeffs
|
dicklyon@525
|
270 error('Bad n_taps in CARFAC_DesignAGC');
|
dicklyon@523
|
271 end
|
dicklyon@525
|
272 [AGC_spatial_FIR, FIR_OK] = Design_FIR_coeffs( ...
|
dicklyon@525
|
273 n_taps, spread_sq, delay, n_iterations);
|
dicklyon@523
|
274 end
|
dicklyon@525
|
275 % when FIR_OK, store the resulting FIR design in coeffs:
|
dicklyon@523
|
276 AGC_coeffs.AGC_spatial_iterations(stage) = n_iterations;
|
dicklyon@523
|
277 AGC_coeffs.AGC_spatial_FIR(:,stage) = AGC_spatial_FIR;
|
dicklyon@536
|
278 AGC_coeffs.AGC_spatial_n_taps(stage) = n_taps;
|
dicklyon@523
|
279
|
dicklyon@525
|
280 % accumulate DC gains from all the stages, accounting for stage_gain:
|
dicklyon@523
|
281 total_DC_gain = total_DC_gain + AGC_params.AGC_stage_gain^(stage-1);
|
dicklyon@523
|
282
|
dicklyon@525
|
283 % TODO (dicklyon) -- is this the best binaural mixing plan?
|
dicklyon@523
|
284 if stage == 1
|
dicklyon@523
|
285 AGC_coeffs.AGC_mix_coeffs(stage) = 0;
|
dicklyon@523
|
286 else
|
dicklyon@523
|
287 AGC_coeffs.AGC_mix_coeffs(stage) = AGC_params.AGC_mix_coeff / ...
|
dicklyon@523
|
288 (tau * (fs / decim));
|
dicklyon@523
|
289 end
|
tom@516
|
290 end
|
tom@516
|
291
|
dicklyon@524
|
292 AGC_coeffs.AGC_gain = total_DC_gain;
|
dicklyon@523
|
293
|
dicklyon@556
|
294 % adjust the detect_scale by the total DC gain of the AGC filters:
|
dicklyon@556
|
295 AGC_coeffs.detect_scale = AGC_params.detect_scale / total_DC_gain;
|
dicklyon@556
|
296
|
dicklyon@556
|
297
|
dicklyon@525
|
298 % % print some results
|
dicklyon@536
|
299 AGC_coeffs
|
dicklyon@536
|
300 AGC_spatial_FIR = AGC_coeffs.AGC_spatial_FIR
|
dicklyon@536
|
301 AGC_spatial_iterations = AGC_coeffs.AGC_spatial_iterations
|
dicklyon@536
|
302 AGC_spatial_n_taps = AGC_coeffs.AGC_spatial_n_taps
|
dicklyon@525
|
303
|
dicklyon@525
|
304
|
dicklyon@525
|
305 %%
|
dicklyon@525
|
306 function [FIR, OK] = Design_FIR_coeffs(n_taps, var, mn, n_iter)
|
dicklyon@525
|
307 % function [FIR, OK] = Design_FIR_coeffs(n_taps, spread_sq, delay, n_iter)
|
dicklyon@525
|
308
|
dicklyon@525
|
309 % reduce mean and variance of smoothing distribution by n_iterations:
|
dicklyon@525
|
310 mn = mn / n_iter;
|
dicklyon@525
|
311 var = var / n_iter;
|
dicklyon@525
|
312 switch n_taps
|
dicklyon@525
|
313 case 3
|
dicklyon@525
|
314 % based on solving to match mean and variance of [a, 1-a-b, b]:
|
dicklyon@525
|
315 a = (var + mn*mn - mn) / 2;
|
dicklyon@525
|
316 b = (var + mn*mn + mn) / 2;
|
dicklyon@525
|
317 FIR = [a, 1 - a - b, b];
|
dicklyon@525
|
318 OK = FIR(2) >= 0.2;
|
dicklyon@525
|
319 case 5
|
dicklyon@525
|
320 % based on solving to match [a/2, a/2, 1-a-b, b/2, b/2]:
|
dicklyon@525
|
321 a = ((var + mn*mn)*2/5 - mn*2/3) / 2;
|
dicklyon@525
|
322 b = ((var + mn*mn)*2/5 + mn*2/3) / 2;
|
dicklyon@525
|
323 % first and last coeffs are implicitly duplicated to make 5-point FIR:
|
dicklyon@525
|
324 FIR = [a/2, 1 - a - b, b/2];
|
dicklyon@525
|
325 OK = FIR(2) >= 0.1;
|
dicklyon@525
|
326 otherwise
|
dicklyon@525
|
327 error('Bad n_taps in AGC_spatial_FIR');
|
dicklyon@525
|
328 end
|
dicklyon@523
|
329
|
tom@516
|
330
|
tom@516
|
331 %% the IHC design coeffs:
|
dicklyon@534
|
332 function IHC_coeffs = CARFAC_DesignIHC(IHC_params, fs, n_ch)
|
tom@516
|
333
|
tom@516
|
334 if IHC_params.just_hwr
|
tom@516
|
335 IHC_coeffs = struct('just_hwr', 1);
|
dicklyon@556
|
336 saturation_output = 10; % HACK: assume some max out
|
tom@516
|
337 else
|
tom@516
|
338 if IHC_params.one_cap
|
dicklyon@556
|
339 ro = 1 / CARFAC_Detect(2); % output resistance
|
dicklyon@556
|
340 c = IHC_params.tau_out / ro;
|
dicklyon@556
|
341 ri = IHC_params.tau_in / c;
|
dicklyon@556
|
342 % to get steady-state average, double ro for 50% duty cycle
|
dicklyon@556
|
343 saturation_output = 1 / (2*ro + ri);
|
dicklyon@556
|
344 % also consider the zero-signal equilibrium:
|
dicklyon@556
|
345 r0 = 1 / CARFAC_Detect(0);
|
dicklyon@556
|
346 current = 1 / (ri + r0);
|
dicklyon@556
|
347 cap_voltage = 1 - current * ri;
|
dicklyon@556
|
348 IHC_coeffs.rest_output = IHC_out;
|
dicklyon@534
|
349 IHC_coeffs = struct( ...
|
dicklyon@534
|
350 'n_ch', n_ch, ...
|
tom@516
|
351 'just_hwr', 0, ...
|
tom@516
|
352 'lpf_coeff', 1 - exp(-1/(IHC_params.tau_lpf * fs)), ...
|
dicklyon@556
|
353 'out_rate', ro / (IHC_params.tau_out * fs), ...
|
tom@516
|
354 'in_rate', 1 / (IHC_params.tau_in * fs), ...
|
dicklyon@556
|
355 'one_cap', IHC_params.one_cap, ...
|
dicklyon@556
|
356 'output_gain', 1/ (saturation_output - current), ...
|
dicklyon@556
|
357 'rest_output', current / (saturation_output - current), ...
|
dicklyon@556
|
358 'rest_cap', cap_voltage);
|
dicklyon@556
|
359 % one-channel state for testing/verification:
|
dicklyon@556
|
360 IHC_state = struct( ...
|
dicklyon@556
|
361 'cap_voltage', IHC_coeffs.rest_cap, ...
|
dicklyon@556
|
362 'lpf1_state', 0, ...
|
dicklyon@556
|
363 'lpf2_state', 0, ...
|
dicklyon@556
|
364 'ihc_accum', 0); else
|
dicklyon@556
|
365 ro = 1 / CARFAC_Detect(2); % output resistance
|
dicklyon@556
|
366 c2 = IHC_params.tau2_out / ro;
|
dicklyon@556
|
367 r2 = IHC_params.tau2_in / c2;
|
dicklyon@556
|
368 c1 = IHC_params.tau1_out / r2;
|
dicklyon@556
|
369 r1 = IHC_params.tau1_in / c1;
|
dicklyon@556
|
370 % to get steady-state average, double ro for 50% duty cycle
|
dicklyon@556
|
371 saturation_output = 1 / (2*ro + r2 + r1);
|
dicklyon@556
|
372 % also consider the zero-signal equilibrium:
|
dicklyon@556
|
373 r0 = 1 / CARFAC_Detect(0);
|
dicklyon@556
|
374 current = 1 / (r1 + r2 + r0);
|
dicklyon@556
|
375 cap1_voltage = 1 - current * r1;
|
dicklyon@556
|
376 cap2_voltage = cap1_voltage - current * r2;
|
tom@516
|
377 IHC_coeffs = struct(...
|
dicklyon@534
|
378 'n_ch', n_ch, ...
|
tom@516
|
379 'just_hwr', 0, ...
|
tom@516
|
380 'lpf_coeff', 1 - exp(-1/(IHC_params.tau_lpf * fs)), ...
|
tom@516
|
381 'out1_rate', 1 / (IHC_params.tau1_out * fs), ...
|
tom@516
|
382 'in1_rate', 1 / (IHC_params.tau1_in * fs), ...
|
dicklyon@556
|
383 'out2_rate', ro / (IHC_params.tau2_out * fs), ...
|
tom@516
|
384 'in2_rate', 1 / (IHC_params.tau2_in * fs), ...
|
dicklyon@556
|
385 'one_cap', IHC_params.one_cap, ...
|
dicklyon@556
|
386 'output_gain', 1/ (saturation_output - current), ...
|
dicklyon@556
|
387 'rest_output', current / (saturation_output - current), ...
|
dicklyon@556
|
388 'rest_cap2', cap2_voltage, ...
|
dicklyon@556
|
389 'rest_cap1', cap1_voltage);
|
dicklyon@556
|
390 % one-channel state for testing/verification:
|
dicklyon@556
|
391 IHC_state = struct( ...
|
dicklyon@556
|
392 'cap1_voltage', IHC_coeffs.rest_cap1, ...
|
dicklyon@556
|
393 'cap2_voltage', IHC_coeffs.rest_cap2, ...
|
dicklyon@556
|
394 'lpf1_state', 0, ...
|
dicklyon@556
|
395 'lpf2_state', 0, ...
|
dicklyon@556
|
396 'ihc_accum', 0);
|
tom@516
|
397 end
|
tom@516
|
398 end
|
tom@516
|
399
|
tom@516
|
400 %%
|
tom@516
|
401 % default design result, running this function with no args, should look
|
tom@516
|
402 % like this, before CARFAC_Init puts state storage into it:
|
tom@516
|
403 %
|
dicklyon@523
|
404 %
|
tom@516
|
405 % CF = CARFAC_Design
|
dicklyon@534
|
406 % CF.CAR_params
|
tom@516
|
407 % CF.AGC_params
|
dicklyon@534
|
408 % CF.CAR_coeffs
|
tom@516
|
409 % CF.AGC_coeffs
|
tom@516
|
410 % CF.IHC_coeffs
|
dicklyon@530
|
411 % CF =
|
dicklyon@530
|
412 % fs: 22050
|
dicklyon@556
|
413 % max_channels_per_octave: 12.2709
|
dicklyon@556
|
414 % CAR_params: [1x1 struct]
|
dicklyon@530
|
415 % AGC_params: [1x1 struct]
|
dicklyon@530
|
416 % IHC_params: [1x1 struct]
|
dicklyon@556
|
417 % n_ch: 71
|
dicklyon@556
|
418 % pole_freqs: [71x1 double]
|
dicklyon@556
|
419 % CAR_coeffs: [1x1 struct]
|
dicklyon@530
|
420 % AGC_coeffs: [1x1 struct]
|
dicklyon@530
|
421 % IHC_coeffs: [1x1 struct]
|
dicklyon@534
|
422 % n_ears: 0
|
dicklyon@530
|
423 % ans =
|
dicklyon@530
|
424 % velocity_scale: 0.2000
|
dicklyon@530
|
425 % v_offset: 0.0100
|
dicklyon@530
|
426 % v2_corner: 0.2000
|
dicklyon@530
|
427 % v_damp_max: 0.0100
|
dicklyon@533
|
428 % min_zeta: 0.1000
|
dicklyon@530
|
429 % first_pole_theta: 2.6704
|
dicklyon@530
|
430 % zero_ratio: 1.4142
|
dicklyon@530
|
431 % high_f_damping_compression: 0.5000
|
dicklyon@530
|
432 % ERB_per_step: 0.5000
|
dicklyon@530
|
433 % min_pole_Hz: 30
|
dicklyon@556
|
434 % ERB_break_freq: 165.3000
|
dicklyon@556
|
435 % ERB_Q: 9.2645
|
dicklyon@530
|
436 % ans =
|
tom@516
|
437 % n_stages: 4
|
tom@516
|
438 % time_constants: [0.0020 0.0080 0.0320 0.1280]
|
tom@516
|
439 % AGC_stage_gain: 2
|
dicklyon@523
|
440 % decimation: [8 2 2 2]
|
dicklyon@556
|
441 % AGC1_scales: [1 1.4000 2 2.8000]
|
dicklyon@556
|
442 % AGC2_scales: [1.6000 2.2500 3.2000 4.5000]
|
dicklyon@556
|
443 % detect_scale: 0.2500
|
dicklyon@530
|
444 % AGC_mix_coeff: 0.5000
|
dicklyon@530
|
445 % ans =
|
dicklyon@556
|
446 % n_ch: 71
|
tom@516
|
447 % velocity_scale: 0.2000
|
dicklyon@523
|
448 % v_offset: 0.0100
|
dicklyon@523
|
449 % v2_corner: 0.2000
|
dicklyon@523
|
450 % v_damp_max: 0.0100
|
dicklyon@556
|
451 % r1_coeffs: [71x1 double]
|
dicklyon@556
|
452 % a0_coeffs: [71x1 double]
|
dicklyon@556
|
453 % c0_coeffs: [71x1 double]
|
dicklyon@556
|
454 % h_coeffs: [71x1 double]
|
dicklyon@556
|
455 % g0_coeffs: [71x1 double]
|
dicklyon@556
|
456 % zr_coeffs: [71x1 double]
|
dicklyon@530
|
457 % ans =
|
dicklyon@556
|
458 % n_ch: 71
|
dicklyon@556
|
459 % n_AGC_stages: 4
|
dicklyon@523
|
460 % AGC_stage_gain: 2
|
dicklyon@523
|
461 % AGC_epsilon: [0.1659 0.0867 0.0443 0.0224]
|
dicklyon@523
|
462 % decimation: [8 2 2 2]
|
dicklyon@556
|
463 % AGC_polez1: [0.1699 0.1780 0.1872 0.1903]
|
dicklyon@556
|
464 % AGC_polez2: [0.2388 0.2271 0.2216 0.2148]
|
dicklyon@556
|
465 % AGC_spatial_iterations: [1 1 1 1]
|
dicklyon@523
|
466 % AGC_spatial_FIR: [3x4 double]
|
dicklyon@556
|
467 % AGC_spatial_n_taps: [3 3 3 3]
|
dicklyon@530
|
468 % AGC_mix_coeffs: [0 0.0454 0.0227 0.0113]
|
dicklyon@523
|
469 % AGC_gain: 15
|
dicklyon@556
|
470 % detect_scale: 0.0167
|
dicklyon@530
|
471 % ans =
|
dicklyon@556
|
472 % n_ch: 71
|
dicklyon@556
|
473 % just_hwr: 0
|
dicklyon@556
|
474 % lpf_coeff: 0.4327
|
dicklyon@556
|
475 % out1_rate: 0.0045
|
dicklyon@556
|
476 % in1_rate: 0.0023
|
dicklyon@556
|
477 % out2_rate: 0.0267
|
dicklyon@556
|
478 % in2_rate: 0.0091
|
dicklyon@556
|
479 % one_cap: 0
|
dicklyon@556
|
480 % output_gain: 17.9162
|
dicklyon@556
|
481 % rest_output: 0.5240
|
dicklyon@556
|
482 % rest_cap2: 0.7421
|
dicklyon@556
|
483 % rest_cap1: 0.8281
|