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@561
|
20 function CF = CARFAC_Design(n_ears, 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@561
|
44 if nargin < 1
|
dicklyon@561
|
45 n_ears = 1; % if more than 1, make them identical channels;
|
dicklyon@561
|
46 % then modify the design if necessary for different reasons
|
dicklyon@561
|
47 end
|
dicklyon@561
|
48
|
dicklyon@561
|
49 if nargin < 2
|
dicklyon@561
|
50 fs = 22050;
|
dicklyon@561
|
51 end
|
dicklyon@561
|
52
|
dicklyon@561
|
53 if nargin < 3
|
dicklyon@561
|
54 CF_CAR_params = struct( ...
|
dicklyon@567
|
55 'velocity_scale', 0.1, ... % for the velocity nonlinearity
|
dicklyon@563
|
56 'v_offset', 0.04, ... % offset gives a quadratic part
|
dicklyon@561
|
57 'min_zeta', 0.10, ... % minimum damping factor in mid-freq channels
|
dicklyon@565
|
58 'max_zeta', 0.35, ... % maximum damping factor in mid-freq channels
|
dicklyon@561
|
59 'first_pole_theta', 0.85*pi, ...
|
dicklyon@561
|
60 'zero_ratio', sqrt(2), ... % how far zero is above pole
|
dicklyon@561
|
61 'high_f_damping_compression', 0.5, ... % 0 to 1 to compress zeta
|
dicklyon@561
|
62 'ERB_per_step', 0.5, ... % assume G&M's ERB formula
|
dicklyon@561
|
63 'min_pole_Hz', 30, ...
|
dicklyon@561
|
64 'ERB_break_freq', 165.3, ... % Greenwood map's break freq.
|
dicklyon@561
|
65 'ERB_Q', 1000/(24.7*4.37)); % Glasberg and Moore's high-cf ratio
|
dicklyon@561
|
66 end
|
dicklyon@561
|
67
|
dicklyon@553
|
68 if nargin < 4
|
dicklyon@561
|
69 CF_AGC_params = struct( ...
|
dicklyon@561
|
70 'n_stages', 4, ...
|
dicklyon@561
|
71 'time_constants', [1, 4, 16, 64]*0.002, ...
|
dicklyon@561
|
72 'AGC_stage_gain', 2, ... % gain from each stage to next slower stage
|
dicklyon@565
|
73 'decimation', [8, 2, 2, 2], ... % how often to update the AGC states
|
dicklyon@561
|
74 'AGC1_scales', [1.0, 1.4, 2.0, 2.8], ... % in units of channels
|
dicklyon@561
|
75 'AGC2_scales', [1.6, 2.25, 3.2, 4.5], ... % spread more toward base
|
dicklyon@561
|
76 'AGC_mix_coeff', 0.5);
|
dicklyon@561
|
77 end
|
dicklyon@561
|
78
|
dicklyon@561
|
79 if nargin < 5
|
tom@516
|
80 % HACK: these constant control the defaults
|
tom@516
|
81 one_cap = 0; % bool; 0 for new two-cap hack
|
tom@516
|
82 just_hwr = 0; % book; 0 for normal/fancy IHC; 1 for HWR
|
tom@516
|
83 if just_hwr
|
dicklyon@565
|
84 CF_IHC_params = struct('just_hwr', 1, ... % just a simple HWR
|
dicklyon@565
|
85 'ac_corner_Hz', 20);
|
tom@516
|
86 else
|
tom@516
|
87 if one_cap
|
tom@516
|
88 CF_IHC_params = struct( ...
|
dicklyon@523
|
89 'just_hwr', just_hwr, ... % not just a simple HWR
|
tom@516
|
90 'one_cap', one_cap, ... % bool; 0 for new two-cap hack
|
tom@516
|
91 'tau_lpf', 0.000080, ... % 80 microseconds smoothing twice
|
tom@516
|
92 'tau_out', 0.0005, ... % depletion tau is pretty fast
|
dicklyon@565
|
93 'tau_in', 0.010, ... % recovery tau is slower
|
dicklyon@565
|
94 'ac_corner_Hz', 20);
|
tom@516
|
95 else
|
tom@516
|
96 CF_IHC_params = struct( ...
|
dicklyon@523
|
97 'just_hwr', just_hwr, ... % not just a simple HWR
|
tom@516
|
98 'one_cap', one_cap, ... % bool; 0 for new two-cap hack
|
tom@516
|
99 'tau_lpf', 0.000080, ... % 80 microseconds smoothing twice
|
dicklyon@556
|
100 'tau1_out', 0.010, ... % depletion tau is pretty fast
|
tom@516
|
101 'tau1_in', 0.020, ... % recovery tau is slower
|
dicklyon@556
|
102 'tau2_out', 0.0025, ... % depletion tau is pretty fast
|
dicklyon@565
|
103 'tau2_in', 0.005, ... % recovery tau is slower
|
dicklyon@565
|
104 'ac_corner_Hz', 20);
|
tom@516
|
105 end
|
tom@516
|
106 end
|
tom@516
|
107 end
|
tom@516
|
108
|
tom@516
|
109
|
tom@516
|
110
|
tom@516
|
111 % first figure out how many filter stages (PZFC/CARFAC channels):
|
dicklyon@534
|
112 pole_Hz = CF_CAR_params.first_pole_theta * fs / (2*pi);
|
tom@516
|
113 n_ch = 0;
|
dicklyon@534
|
114 while pole_Hz > CF_CAR_params.min_pole_Hz
|
tom@516
|
115 n_ch = n_ch + 1;
|
dicklyon@534
|
116 pole_Hz = pole_Hz - CF_CAR_params.ERB_per_step * ...
|
dicklyon@553
|
117 ERB_Hz(pole_Hz, CF_CAR_params.ERB_break_freq, CF_CAR_params.ERB_Q);
|
tom@516
|
118 end
|
tom@516
|
119 % Now we have n_ch, the number of channels, so can make the array
|
tom@516
|
120 % and compute all the frequencies again to put into it:
|
tom@516
|
121 pole_freqs = zeros(n_ch, 1);
|
dicklyon@534
|
122 pole_Hz = CF_CAR_params.first_pole_theta * fs / (2*pi);
|
tom@516
|
123 for ch = 1:n_ch
|
tom@516
|
124 pole_freqs(ch) = pole_Hz;
|
dicklyon@534
|
125 pole_Hz = pole_Hz - CF_CAR_params.ERB_per_step * ...
|
dicklyon@553
|
126 ERB_Hz(pole_Hz, CF_CAR_params.ERB_break_freq, CF_CAR_params.ERB_Q);
|
tom@516
|
127 end
|
tom@516
|
128 % now we have n_ch, the number of channels, and pole_freqs array
|
tom@516
|
129
|
dicklyon@528
|
130 max_channels_per_octave = log(2) / log(pole_freqs(1)/pole_freqs(2));
|
dicklyon@528
|
131
|
dicklyon@561
|
132 % convert to include an ear_array, each w coeffs and state...
|
dicklyon@561
|
133 CAR_coeffs = CARFAC_DesignFilters(CF_CAR_params, fs, pole_freqs);
|
dicklyon@561
|
134 AGC_coeffs = CARFAC_DesignAGC(CF_AGC_params, fs, n_ch);
|
dicklyon@561
|
135 IHC_coeffs = CARFAC_DesignIHC(CF_IHC_params, fs, n_ch);
|
dicklyon@561
|
136 % copy same designed coeffs into each ear (can do differently in the
|
dicklyon@561
|
137 % future:
|
dicklyon@561
|
138 for ear = 1:n_ears
|
dicklyon@561
|
139 ears(ear).CAR_coeffs = CAR_coeffs;
|
dicklyon@561
|
140 ears(ear).AGC_coeffs = AGC_coeffs;
|
dicklyon@561
|
141 ears(ear).IHC_coeffs = IHC_coeffs;
|
dicklyon@561
|
142 end
|
dicklyon@561
|
143
|
tom@516
|
144 CF = struct( ...
|
tom@516
|
145 'fs', fs, ...
|
dicklyon@528
|
146 'max_channels_per_octave', max_channels_per_octave, ...
|
dicklyon@534
|
147 'CAR_params', CF_CAR_params, ...
|
tom@516
|
148 'AGC_params', CF_AGC_params, ...
|
tom@516
|
149 'IHC_params', CF_IHC_params, ...
|
tom@516
|
150 'n_ch', n_ch, ...
|
tom@516
|
151 'pole_freqs', pole_freqs, ...
|
dicklyon@561
|
152 'ears', ears, ...
|
dicklyon@561
|
153 'n_ears', n_ears );
|
tom@516
|
154
|
tom@516
|
155
|
dicklyon@534
|
156
|
tom@516
|
157 %% Design the filter coeffs:
|
dicklyon@534
|
158 function CAR_coeffs = CARFAC_DesignFilters(CAR_params, fs, pole_freqs)
|
tom@516
|
159
|
tom@516
|
160 n_ch = length(pole_freqs);
|
tom@516
|
161
|
tom@516
|
162 % the filter design coeffs:
|
dicklyon@567
|
163 % scalars first:
|
dicklyon@534
|
164 CAR_coeffs = struct( ...
|
dicklyon@534
|
165 'n_ch', n_ch, ...
|
dicklyon@534
|
166 'velocity_scale', CAR_params.velocity_scale, ...
|
dicklyon@567
|
167 'v_offset', CAR_params.v_offset ...
|
dicklyon@523
|
168 );
|
tom@516
|
169
|
dicklyon@559
|
170 % don't really need these zero arrays, but it's a clue to what fields
|
dicklyon@559
|
171 % and types are need in ohter language implementations:
|
dicklyon@534
|
172 CAR_coeffs.r1_coeffs = zeros(n_ch, 1);
|
dicklyon@534
|
173 CAR_coeffs.a0_coeffs = zeros(n_ch, 1);
|
dicklyon@534
|
174 CAR_coeffs.c0_coeffs = zeros(n_ch, 1);
|
dicklyon@534
|
175 CAR_coeffs.h_coeffs = zeros(n_ch, 1);
|
dicklyon@534
|
176 CAR_coeffs.g0_coeffs = zeros(n_ch, 1);
|
tom@516
|
177
|
tom@516
|
178 % zero_ratio comes in via h. In book's circuit D, zero_ratio is 1/sqrt(a),
|
tom@516
|
179 % and that a is here 1 / (1+f) where h = f*c.
|
tom@516
|
180 % solve for f: 1/zero_ratio^2 = 1 / (1+f)
|
tom@516
|
181 % zero_ratio^2 = 1+f => f = zero_ratio^2 - 1
|
dicklyon@534
|
182 f = CAR_params.zero_ratio^2 - 1; % nominally 1 for half-octave
|
tom@516
|
183
|
tom@516
|
184 % Make pole positions, s and c coeffs, h and g coeffs, etc.,
|
tom@516
|
185 % which mostly depend on the pole angle theta:
|
tom@516
|
186 theta = pole_freqs .* (2 * pi / fs);
|
tom@516
|
187
|
dicklyon@530
|
188 c0 = sin(theta);
|
dicklyon@530
|
189 a0 = cos(theta);
|
dicklyon@530
|
190
|
tom@516
|
191 % different possible interpretations for min-damping r:
|
dicklyon@534
|
192 % r = exp(-theta * CF_CAR_params.min_zeta).
|
dicklyon@530
|
193 % Compress theta to give somewhat higher Q at highest thetas:
|
dicklyon@534
|
194 ff = CAR_params.high_f_damping_compression; % 0 to 1; typ. 0.5
|
dicklyon@530
|
195 x = theta/pi;
|
dicklyon@565
|
196
|
dicklyon@530
|
197 zr_coeffs = pi * (x - ff * x.^3); % when ff is 0, this is just theta,
|
dicklyon@530
|
198 % and when ff is 1 it goes to zero at theta = pi.
|
dicklyon@565
|
199 max_zeta = CAR_params.max_zeta;
|
dicklyon@565
|
200 CAR_coeffs.r1_coeffs = (1 - zr_coeffs .* max_zeta); % "r1" for the max-damping condition
|
dicklyon@530
|
201
|
dicklyon@534
|
202 min_zeta = CAR_params.min_zeta;
|
dicklyon@565
|
203 % Increase the min damping where channels are spaced out more, by pulling
|
dicklyon@565
|
204 % 25% of the way toward ERB_Hz/pole_freqs (close to 0.1 at high f)
|
dicklyon@565
|
205 min_zetas = min_zeta + 0.25*(ERB_Hz(pole_freqs, ...
|
dicklyon@553
|
206 CAR_params.ERB_break_freq, CAR_params.ERB_Q) ./ pole_freqs - min_zeta);
|
dicklyon@565
|
207 CAR_coeffs.zr_coeffs = zr_coeffs .* ...
|
dicklyon@565
|
208 (max_zeta - min_zetas); % how r relates to undamping
|
tom@516
|
209
|
tom@516
|
210 % undamped coupled-form coefficients:
|
dicklyon@534
|
211 CAR_coeffs.a0_coeffs = a0;
|
dicklyon@534
|
212 CAR_coeffs.c0_coeffs = c0;
|
tom@516
|
213
|
tom@516
|
214 % the zeros follow via the h_coeffs
|
dicklyon@530
|
215 h = c0 .* f;
|
dicklyon@534
|
216 CAR_coeffs.h_coeffs = h;
|
tom@516
|
217
|
dicklyon@530
|
218 % for unity gain at min damping, radius r; only used in CARFAC_Init:
|
dicklyon@565
|
219 relative_undamping = ones(n_ch, 1); % max undamping to start
|
dicklyon@534
|
220 % this function needs to take CAR_coeffs even if we haven't finished
|
dicklyon@530
|
221 % constucting it by putting in the g0_coeffs:
|
dicklyon@565
|
222 CAR_coeffs.g0_coeffs = CARFAC_Stage_g(CAR_coeffs, relative_undamping);
|
tom@516
|
223
|
tom@516
|
224
|
tom@516
|
225 %% the AGC design coeffs:
|
dicklyon@534
|
226 function AGC_coeffs = CARFAC_DesignAGC(AGC_params, fs, n_ch)
|
tom@516
|
227
|
dicklyon@534
|
228 n_AGC_stages = AGC_params.n_stages;
|
dicklyon@534
|
229 AGC_coeffs = struct( ...
|
dicklyon@534
|
230 'n_ch', n_ch, ...
|
dicklyon@534
|
231 'n_AGC_stages', n_AGC_stages, ...
|
dicklyon@534
|
232 'AGC_stage_gain', AGC_params.AGC_stage_gain);
|
tom@516
|
233
|
tom@516
|
234 % AGC1 pass is smoothing from base toward apex;
|
dicklyon@559
|
235 % AGC2 pass is back, which is done first now (in double exp. version)
|
tom@516
|
236 AGC1_scales = AGC_params.AGC1_scales;
|
tom@516
|
237 AGC2_scales = AGC_params.AGC2_scales;
|
tom@516
|
238
|
tom@516
|
239 AGC_coeffs.AGC_epsilon = zeros(1, n_AGC_stages); % the 1/(tau*fs) roughly
|
dicklyon@523
|
240 decim = 1;
|
dicklyon@523
|
241 AGC_coeffs.decimation = AGC_params.decimation;
|
dicklyon@523
|
242
|
dicklyon@523
|
243 total_DC_gain = 0;
|
tom@516
|
244 for stage = 1:n_AGC_stages
|
dicklyon@525
|
245 tau = AGC_params.time_constants(stage); % time constant in seconds
|
dicklyon@525
|
246 decim = decim * AGC_params.decimation(stage); % net decim to this stage
|
tom@516
|
247 % epsilon is how much new input to take at each update step:
|
tom@516
|
248 AGC_coeffs.AGC_epsilon(stage) = 1 - exp(-decim / (tau * fs));
|
dicklyon@523
|
249 % effective number of smoothings in a time constant:
|
dicklyon@525
|
250 ntimes = tau * (fs / decim); % typically 5 to 50
|
dicklyon@524
|
251
|
dicklyon@524
|
252 % decide on target spread (variance) and delay (mean) of impulse
|
dicklyon@524
|
253 % response as a distribution to be convolved ntimes:
|
dicklyon@525
|
254 % TODO (dicklyon): specify spread and delay instead of scales???
|
dicklyon@524
|
255 delay = (AGC2_scales(stage) - AGC1_scales(stage)) / ntimes;
|
dicklyon@524
|
256 spread_sq = (AGC1_scales(stage)^2 + AGC2_scales(stage)^2) / ntimes;
|
dicklyon@524
|
257
|
dicklyon@561
|
258 % get pole positions to better match intended spread and delay of
|
dicklyon@525
|
259 % [[geometric distribution]] in each direction (see wikipedia)
|
dicklyon@524
|
260 u = 1 + 1 / spread_sq; % these are based on off-line algebra hacking.
|
dicklyon@524
|
261 p = u - sqrt(u^2 - 1); % pole that would give spread if used twice.
|
dicklyon@524
|
262 dp = delay * (1 - 2*p +p^2)/2;
|
dicklyon@524
|
263 polez1 = p - dp;
|
dicklyon@524
|
264 polez2 = p + dp;
|
dicklyon@523
|
265 AGC_coeffs.AGC_polez1(stage) = polez1;
|
dicklyon@523
|
266 AGC_coeffs.AGC_polez2(stage) = polez2;
|
dicklyon@523
|
267
|
dicklyon@525
|
268 % try a 3- or 5-tap FIR as an alternative to the double exponential:
|
dicklyon@525
|
269 n_taps = 0;
|
dicklyon@525
|
270 FIR_OK = 0;
|
dicklyon@525
|
271 n_iterations = 1;
|
dicklyon@525
|
272 while ~FIR_OK
|
dicklyon@525
|
273 switch n_taps
|
dicklyon@525
|
274 case 0
|
dicklyon@525
|
275 % first attempt a 3-point FIR to apply once:
|
dicklyon@525
|
276 n_taps = 3;
|
dicklyon@525
|
277 case 3
|
dicklyon@525
|
278 % second time through, go wider but stick to 1 iteration
|
dicklyon@525
|
279 n_taps = 5;
|
dicklyon@525
|
280 case 5
|
dicklyon@525
|
281 % apply FIR multiple times instead of going wider:
|
dicklyon@525
|
282 n_iterations = n_iterations + 1;
|
dicklyon@525
|
283 if n_iterations > 16
|
dicklyon@525
|
284 error('Too many n_iterations in CARFAC_DesignAGC');
|
dicklyon@525
|
285 end
|
dicklyon@525
|
286 otherwise
|
dicklyon@525
|
287 % to do other n_taps would need changes in CARFAC_Spatial_Smooth
|
dicklyon@525
|
288 % and in Design_FIR_coeffs
|
dicklyon@525
|
289 error('Bad n_taps in CARFAC_DesignAGC');
|
dicklyon@523
|
290 end
|
dicklyon@525
|
291 [AGC_spatial_FIR, FIR_OK] = Design_FIR_coeffs( ...
|
dicklyon@525
|
292 n_taps, spread_sq, delay, n_iterations);
|
dicklyon@523
|
293 end
|
dicklyon@525
|
294 % when FIR_OK, store the resulting FIR design in coeffs:
|
dicklyon@523
|
295 AGC_coeffs.AGC_spatial_iterations(stage) = n_iterations;
|
dicklyon@523
|
296 AGC_coeffs.AGC_spatial_FIR(:,stage) = AGC_spatial_FIR;
|
dicklyon@536
|
297 AGC_coeffs.AGC_spatial_n_taps(stage) = n_taps;
|
dicklyon@523
|
298
|
dicklyon@525
|
299 % accumulate DC gains from all the stages, accounting for stage_gain:
|
dicklyon@523
|
300 total_DC_gain = total_DC_gain + AGC_params.AGC_stage_gain^(stage-1);
|
dicklyon@523
|
301
|
dicklyon@525
|
302 % TODO (dicklyon) -- is this the best binaural mixing plan?
|
dicklyon@523
|
303 if stage == 1
|
dicklyon@523
|
304 AGC_coeffs.AGC_mix_coeffs(stage) = 0;
|
dicklyon@523
|
305 else
|
dicklyon@523
|
306 AGC_coeffs.AGC_mix_coeffs(stage) = AGC_params.AGC_mix_coeff / ...
|
dicklyon@523
|
307 (tau * (fs / decim));
|
dicklyon@523
|
308 end
|
tom@516
|
309 end
|
tom@516
|
310
|
dicklyon@524
|
311 AGC_coeffs.AGC_gain = total_DC_gain;
|
dicklyon@523
|
312
|
dicklyon@565
|
313 % adjust the detect_scale to be the reciprocal DC gain of the AGC filters:
|
dicklyon@565
|
314 AGC_coeffs.detect_scale = 1 / total_DC_gain;
|
dicklyon@525
|
315
|
dicklyon@525
|
316
|
dicklyon@525
|
317 %%
|
dicklyon@525
|
318 function [FIR, OK] = Design_FIR_coeffs(n_taps, var, mn, n_iter)
|
dicklyon@525
|
319 % function [FIR, OK] = Design_FIR_coeffs(n_taps, spread_sq, delay, n_iter)
|
dicklyon@525
|
320
|
dicklyon@525
|
321 % reduce mean and variance of smoothing distribution by n_iterations:
|
dicklyon@525
|
322 mn = mn / n_iter;
|
dicklyon@525
|
323 var = var / n_iter;
|
dicklyon@525
|
324 switch n_taps
|
dicklyon@525
|
325 case 3
|
dicklyon@525
|
326 % based on solving to match mean and variance of [a, 1-a-b, b]:
|
dicklyon@525
|
327 a = (var + mn*mn - mn) / 2;
|
dicklyon@525
|
328 b = (var + mn*mn + mn) / 2;
|
dicklyon@525
|
329 FIR = [a, 1 - a - b, b];
|
dicklyon@525
|
330 OK = FIR(2) >= 0.2;
|
dicklyon@525
|
331 case 5
|
dicklyon@525
|
332 % based on solving to match [a/2, a/2, 1-a-b, b/2, b/2]:
|
dicklyon@525
|
333 a = ((var + mn*mn)*2/5 - mn*2/3) / 2;
|
dicklyon@525
|
334 b = ((var + mn*mn)*2/5 + mn*2/3) / 2;
|
dicklyon@525
|
335 % first and last coeffs are implicitly duplicated to make 5-point FIR:
|
dicklyon@525
|
336 FIR = [a/2, 1 - a - b, b/2];
|
dicklyon@525
|
337 OK = FIR(2) >= 0.1;
|
dicklyon@525
|
338 otherwise
|
dicklyon@525
|
339 error('Bad n_taps in AGC_spatial_FIR');
|
dicklyon@525
|
340 end
|
dicklyon@523
|
341
|
tom@516
|
342
|
tom@516
|
343 %% the IHC design coeffs:
|
dicklyon@534
|
344 function IHC_coeffs = CARFAC_DesignIHC(IHC_params, fs, n_ch)
|
tom@516
|
345
|
tom@516
|
346 if IHC_params.just_hwr
|
dicklyon@561
|
347 IHC_coeffs = struct( ...
|
dicklyon@561
|
348 'n_ch', n_ch, ...
|
dicklyon@561
|
349 'just_hwr', 1);
|
tom@516
|
350 else
|
tom@516
|
351 if IHC_params.one_cap
|
dicklyon@565
|
352 ro = 1 / CARFAC_Detect(10); % output resistance at a very high level
|
dicklyon@556
|
353 c = IHC_params.tau_out / ro;
|
dicklyon@556
|
354 ri = IHC_params.tau_in / c;
|
dicklyon@556
|
355 % to get steady-state average, double ro for 50% duty cycle
|
dicklyon@556
|
356 saturation_output = 1 / (2*ro + ri);
|
dicklyon@556
|
357 % also consider the zero-signal equilibrium:
|
dicklyon@556
|
358 r0 = 1 / CARFAC_Detect(0);
|
dicklyon@556
|
359 current = 1 / (ri + r0);
|
dicklyon@556
|
360 cap_voltage = 1 - current * ri;
|
dicklyon@534
|
361 IHC_coeffs = struct( ...
|
dicklyon@534
|
362 'n_ch', n_ch, ...
|
tom@516
|
363 'just_hwr', 0, ...
|
tom@516
|
364 'lpf_coeff', 1 - exp(-1/(IHC_params.tau_lpf * fs)), ...
|
dicklyon@556
|
365 'out_rate', ro / (IHC_params.tau_out * fs), ...
|
tom@516
|
366 'in_rate', 1 / (IHC_params.tau_in * fs), ...
|
dicklyon@556
|
367 'one_cap', IHC_params.one_cap, ...
|
dicklyon@556
|
368 'output_gain', 1/ (saturation_output - current), ...
|
dicklyon@556
|
369 'rest_output', current / (saturation_output - current), ...
|
dicklyon@556
|
370 'rest_cap', cap_voltage);
|
dicklyon@556
|
371 % one-channel state for testing/verification:
|
dicklyon@556
|
372 IHC_state = struct( ...
|
dicklyon@556
|
373 'cap_voltage', IHC_coeffs.rest_cap, ...
|
dicklyon@556
|
374 'lpf1_state', 0, ...
|
dicklyon@556
|
375 'lpf2_state', 0, ...
|
dicklyon@561
|
376 'ihc_accum', 0);
|
dicklyon@560
|
377 else
|
dicklyon@565
|
378 ro = 1 / CARFAC_Detect(10); % output resistance at a very high level
|
dicklyon@556
|
379 c2 = IHC_params.tau2_out / ro;
|
dicklyon@556
|
380 r2 = IHC_params.tau2_in / c2;
|
dicklyon@556
|
381 c1 = IHC_params.tau1_out / r2;
|
dicklyon@556
|
382 r1 = IHC_params.tau1_in / c1;
|
dicklyon@556
|
383 % to get steady-state average, double ro for 50% duty cycle
|
dicklyon@556
|
384 saturation_output = 1 / (2*ro + r2 + r1);
|
dicklyon@556
|
385 % also consider the zero-signal equilibrium:
|
dicklyon@556
|
386 r0 = 1 / CARFAC_Detect(0);
|
dicklyon@556
|
387 current = 1 / (r1 + r2 + r0);
|
dicklyon@556
|
388 cap1_voltage = 1 - current * r1;
|
dicklyon@556
|
389 cap2_voltage = cap1_voltage - current * r2;
|
tom@516
|
390 IHC_coeffs = struct(...
|
dicklyon@534
|
391 'n_ch', n_ch, ...
|
tom@516
|
392 'just_hwr', 0, ...
|
tom@516
|
393 'lpf_coeff', 1 - exp(-1/(IHC_params.tau_lpf * fs)), ...
|
tom@516
|
394 'out1_rate', 1 / (IHC_params.tau1_out * fs), ...
|
tom@516
|
395 'in1_rate', 1 / (IHC_params.tau1_in * fs), ...
|
dicklyon@556
|
396 'out2_rate', ro / (IHC_params.tau2_out * fs), ...
|
tom@516
|
397 'in2_rate', 1 / (IHC_params.tau2_in * fs), ...
|
dicklyon@556
|
398 'one_cap', IHC_params.one_cap, ...
|
dicklyon@556
|
399 'output_gain', 1/ (saturation_output - current), ...
|
dicklyon@556
|
400 'rest_output', current / (saturation_output - current), ...
|
dicklyon@556
|
401 'rest_cap2', cap2_voltage, ...
|
dicklyon@556
|
402 'rest_cap1', cap1_voltage);
|
dicklyon@556
|
403 % one-channel state for testing/verification:
|
dicklyon@556
|
404 IHC_state = struct( ...
|
dicklyon@556
|
405 'cap1_voltage', IHC_coeffs.rest_cap1, ...
|
dicklyon@556
|
406 'cap2_voltage', IHC_coeffs.rest_cap2, ...
|
dicklyon@556
|
407 'lpf1_state', 0, ...
|
dicklyon@556
|
408 'lpf2_state', 0, ...
|
dicklyon@556
|
409 'ihc_accum', 0);
|
tom@516
|
410 end
|
tom@516
|
411 end
|
dicklyon@565
|
412 % one more late addition that applies to all cases:
|
dicklyon@565
|
413 IHC_coeffs.ac_coeff = 2 * pi * IHC_params.ac_corner_Hz / fs;
|
tom@516
|
414
|
tom@516
|
415 %%
|
tom@516
|
416 % default design result, running this function with no args, should look
|
tom@516
|
417 % like this, before CARFAC_Init puts state storage into it:
|
tom@516
|
418 %
|
dicklyon@523
|
419 %
|
tom@516
|
420 % CF = CARFAC_Design
|
dicklyon@565
|
421 % CAR_params = CF.CAR_params
|
dicklyon@565
|
422 % AGC_params = CF.AGC_params
|
dicklyon@565
|
423 % IHC_params = CF.IHC_params
|
dicklyon@565
|
424 % CAR_coeffs = CF.ears(1).CAR_coeffs
|
dicklyon@565
|
425 % AGC_coeffs = CF.ears(1).AGC_coeffs
|
dicklyon@565
|
426 % AGC_spatial_FIR = AGC_coeffs.AGC_spatial_FIR
|
dicklyon@565
|
427 % IHC_coeffs = CF.ears(1).IHC_coeffs
|
dicklyon@565
|
428
|
dicklyon@565
|
429 % CF =
|
dicklyon@530
|
430 % fs: 22050
|
dicklyon@556
|
431 % max_channels_per_octave: 12.2709
|
dicklyon@556
|
432 % CAR_params: [1x1 struct]
|
dicklyon@530
|
433 % AGC_params: [1x1 struct]
|
dicklyon@530
|
434 % IHC_params: [1x1 struct]
|
dicklyon@556
|
435 % n_ch: 71
|
dicklyon@556
|
436 % pole_freqs: [71x1 double]
|
dicklyon@565
|
437 % ears: [1x1 struct]
|
dicklyon@565
|
438 % n_ears: 1
|
dicklyon@565
|
439 % CAR_params =
|
dicklyon@565
|
440 % velocity_scale: 0.0500
|
dicklyon@565
|
441 % v_offset: 0.0400
|
dicklyon@530
|
442 % v2_corner: 0.2000
|
dicklyon@533
|
443 % min_zeta: 0.1000
|
dicklyon@565
|
444 % max_zeta: 0.3500
|
dicklyon@530
|
445 % first_pole_theta: 2.6704
|
dicklyon@530
|
446 % zero_ratio: 1.4142
|
dicklyon@530
|
447 % high_f_damping_compression: 0.5000
|
dicklyon@530
|
448 % ERB_per_step: 0.5000
|
dicklyon@530
|
449 % min_pole_Hz: 30
|
dicklyon@556
|
450 % ERB_break_freq: 165.3000
|
dicklyon@556
|
451 % ERB_Q: 9.2645
|
dicklyon@565
|
452 % AGC_params =
|
tom@516
|
453 % n_stages: 4
|
tom@516
|
454 % time_constants: [0.0020 0.0080 0.0320 0.1280]
|
tom@516
|
455 % AGC_stage_gain: 2
|
dicklyon@523
|
456 % decimation: [8 2 2 2]
|
dicklyon@556
|
457 % AGC1_scales: [1 1.4000 2 2.8000]
|
dicklyon@556
|
458 % AGC2_scales: [1.6000 2.2500 3.2000 4.5000]
|
dicklyon@530
|
459 % AGC_mix_coeff: 0.5000
|
dicklyon@565
|
460 % IHC_params =
|
dicklyon@565
|
461 % just_hwr: 0
|
dicklyon@565
|
462 % one_cap: 0
|
dicklyon@565
|
463 % tau_lpf: 8.0000e-05
|
dicklyon@565
|
464 % tau1_out: 0.0100
|
dicklyon@565
|
465 % tau1_in: 0.0200
|
dicklyon@565
|
466 % tau2_out: 0.0025
|
dicklyon@565
|
467 % tau2_in: 0.0050
|
dicklyon@565
|
468 % ac_corner_Hz: 20
|
dicklyon@565
|
469 % CAR_coeffs =
|
dicklyon@556
|
470 % n_ch: 71
|
dicklyon@565
|
471 % velocity_scale: 0.0500
|
dicklyon@565
|
472 % v_offset: 0.0400
|
dicklyon@523
|
473 % v2_corner: 0.2000
|
dicklyon@556
|
474 % r1_coeffs: [71x1 double]
|
dicklyon@556
|
475 % a0_coeffs: [71x1 double]
|
dicklyon@556
|
476 % c0_coeffs: [71x1 double]
|
dicklyon@556
|
477 % h_coeffs: [71x1 double]
|
dicklyon@556
|
478 % g0_coeffs: [71x1 double]
|
dicklyon@556
|
479 % zr_coeffs: [71x1 double]
|
dicklyon@565
|
480 % AGC_coeffs =
|
dicklyon@556
|
481 % n_ch: 71
|
dicklyon@556
|
482 % n_AGC_stages: 4
|
dicklyon@523
|
483 % AGC_stage_gain: 2
|
dicklyon@523
|
484 % AGC_epsilon: [0.1659 0.0867 0.0443 0.0224]
|
dicklyon@523
|
485 % decimation: [8 2 2 2]
|
dicklyon@556
|
486 % AGC_polez1: [0.1699 0.1780 0.1872 0.1903]
|
dicklyon@556
|
487 % AGC_polez2: [0.2388 0.2271 0.2216 0.2148]
|
dicklyon@556
|
488 % AGC_spatial_iterations: [1 1 1 1]
|
dicklyon@523
|
489 % AGC_spatial_FIR: [3x4 double]
|
dicklyon@556
|
490 % AGC_spatial_n_taps: [3 3 3 3]
|
dicklyon@530
|
491 % AGC_mix_coeffs: [0 0.0454 0.0227 0.0113]
|
dicklyon@523
|
492 % AGC_gain: 15
|
dicklyon@565
|
493 % detect_scale: 0.0667
|
dicklyon@565
|
494 % AGC_spatial_FIR =
|
dicklyon@565
|
495 % 0.2744 0.2829 0.2972 0.2999
|
dicklyon@565
|
496 % 0.3423 0.3571 0.3512 0.3616
|
dicklyon@565
|
497 % 0.3832 0.3600 0.3516 0.3385
|
dicklyon@565
|
498 % IHC_coeffs =
|
dicklyon@556
|
499 % n_ch: 71
|
dicklyon@556
|
500 % just_hwr: 0
|
dicklyon@556
|
501 % lpf_coeff: 0.4327
|
dicklyon@556
|
502 % out1_rate: 0.0045
|
dicklyon@556
|
503 % in1_rate: 0.0023
|
dicklyon@565
|
504 % out2_rate: 0.0199
|
dicklyon@556
|
505 % in2_rate: 0.0091
|
dicklyon@556
|
506 % one_cap: 0
|
dicklyon@565
|
507 % output_gain: 12.1185
|
dicklyon@565
|
508 % rest_output: 0.3791
|
dicklyon@565
|
509 % rest_cap2: 0.7938
|
dicklyon@565
|
510 % rest_cap1: 0.8625
|
dicklyon@565
|
511 % ac_coeff: 0.0057
|
dicklyon@565
|
512
|
dicklyon@565
|
513
|
dicklyon@565
|
514
|