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
|
tom@516
|
20 function CF = CARFAC_Design(fs, CF_filter_params, ...
|
tom@516
|
21 CF_AGC_params, ERB_break_freq, ERB_Q, CF_IHC_params)
|
tom@516
|
22 % function CF = CARFAC_Design(fs, CF_filter_params, ...
|
tom@516
|
23 % CF_AGC_params, ERB_break_freq, ERB_Q, CF_IHC_params)
|
tom@516
|
24 %
|
tom@516
|
25 % This function designs the CARFAC (Cascade of Asymmetric Resonators with
|
tom@516
|
26 % Fast-Acting Compression); that is, it take bundles of parameters and
|
tom@516
|
27 % computes all the filter coefficients needed to run it.
|
tom@516
|
28 %
|
tom@516
|
29 % fs is sample rate (per second)
|
tom@516
|
30 % CF_filter_params bundles all the pole-zero filter cascade parameters
|
tom@516
|
31 % CF_AGC_params bundles all the automatic gain control parameters
|
tom@516
|
32 % CF_IHC_params bundles all the inner hair cell parameters
|
tom@516
|
33 %
|
tom@516
|
34 % See other functions for designing and characterizing the CARFAC:
|
tom@516
|
35 % [naps, CF] = CARFAC_Run(CF, input_waves)
|
tom@516
|
36 % transfns = CARFAC_Transfer_Functions(CF, to_channels, from_channels)
|
tom@516
|
37 %
|
tom@516
|
38 % Defaults to Glasberg & Moore's ERB curve:
|
tom@516
|
39 % ERB_break_freq = 1000/4.37; % 228.833
|
tom@516
|
40 % ERB_Q = 1000/(24.7*4.37); % 9.2645
|
tom@516
|
41 %
|
tom@516
|
42 % All args are defaultable; for sample/default args see the code; they
|
tom@516
|
43 % make 96 channels at default fs = 22050, 114 channels at 44100.
|
tom@516
|
44
|
tom@516
|
45 if nargin < 6
|
tom@516
|
46 % HACK: these constant control the defaults
|
tom@516
|
47 one_cap = 0; % bool; 0 for new two-cap hack
|
tom@516
|
48 just_hwr = 0; % book; 0 for normal/fancy IHC; 1 for HWR
|
tom@516
|
49 if just_hwr
|
tom@516
|
50 CF_IHC_params = struct('just_hwr', 1); % just a simple HWR
|
tom@516
|
51 else
|
tom@516
|
52 if one_cap
|
tom@516
|
53 CF_IHC_params = struct( ...
|
dicklyon@523
|
54 'just_hwr', just_hwr, ... % not just a simple HWR
|
tom@516
|
55 'one_cap', one_cap, ... % bool; 0 for new two-cap hack
|
tom@516
|
56 'tau_lpf', 0.000080, ... % 80 microseconds smoothing twice
|
tom@516
|
57 'tau_out', 0.0005, ... % depletion tau is pretty fast
|
tom@516
|
58 'tau_in', 0.010 ); % recovery tau is slower
|
tom@516
|
59 else
|
tom@516
|
60 CF_IHC_params = struct( ...
|
dicklyon@523
|
61 'just_hwr', just_hwr, ... % not just a simple HWR
|
tom@516
|
62 'one_cap', one_cap, ... % bool; 0 for new two-cap hack
|
tom@516
|
63 'tau_lpf', 0.000080, ... % 80 microseconds smoothing twice
|
tom@516
|
64 'tau1_out', 0.020, ... % depletion tau is pretty fast
|
tom@516
|
65 'tau1_in', 0.020, ... % recovery tau is slower
|
tom@516
|
66 'tau2_out', 0.005, ... % depletion tau is pretty fast
|
tom@516
|
67 'tau2_in', 0.005 ); % recovery tau is slower
|
tom@516
|
68 end
|
tom@516
|
69 end
|
tom@516
|
70 end
|
tom@516
|
71
|
tom@516
|
72 if nargin < 5
|
tom@516
|
73 % Ref: Glasberg and Moore: Hearing Research, 47 (1990), 103-138
|
tom@516
|
74 % ERB = 24.7 * (1 + 4.37 * CF_Hz / 1000);
|
tom@516
|
75 ERB_Q = 1000/(24.7*4.37); % 9.2645
|
tom@516
|
76 if nargin < 4
|
tom@516
|
77 ERB_break_freq = 1000/4.37; % 228.833
|
tom@516
|
78 end
|
tom@516
|
79 end
|
tom@516
|
80
|
tom@516
|
81 if nargin < 3
|
tom@516
|
82 CF_AGC_params = struct( ...
|
tom@516
|
83 'n_stages', 4, ...
|
tom@516
|
84 'time_constants', [1, 4, 16, 64]*0.002, ...
|
tom@516
|
85 'AGC_stage_gain', 2, ... % gain from each stage to next slower stage
|
dicklyon@523
|
86 'decimation', [8, 2, 2, 2], ... % how often to update the AGC states
|
dicklyon@524
|
87 'AGC1_scales', [1, 2, 4, 6]*1, ... % in units of channels
|
dicklyon@524
|
88 'AGC2_scales', [1, 2, 4, 6]*1.5, ... % spread more toward base
|
tom@516
|
89 'detect_scale', 0.15, ... % the desired damping range
|
dicklyon@523
|
90 'AGC_mix_coeff', 0.5);
|
tom@516
|
91 end
|
tom@516
|
92
|
tom@516
|
93 if nargin < 2
|
tom@516
|
94 CF_filter_params = struct( ...
|
dicklyon@523
|
95 'velocity_scale', 0.2, ... % for the "cubic" velocity nonlinearity
|
dicklyon@523
|
96 'v_offset', 0.01, ... % offset gives a quadratic part
|
dicklyon@523
|
97 'v2_corner', 0.2, ... % corner for essential nonlin
|
dicklyon@523
|
98 'v_damp_max', 0.01, ... % damping delta damping from velocity nonlin
|
dicklyon@530
|
99 'min_zeta', 0.10, ...
|
dicklyon@528
|
100 'first_pole_theta', 0.85*pi, ...
|
dicklyon@528
|
101 'zero_ratio', sqrt(2), ... % how far zero is above pole
|
dicklyon@530
|
102 'high_f_damping_compression', 0.5, ... % 0 to 1 to compress zeta
|
dicklyon@528
|
103 'ERB_per_step', 0.5, ... % assume G&M's ERB formula
|
dicklyon@528
|
104 'min_pole_Hz', 30 );
|
tom@516
|
105 end
|
tom@516
|
106
|
tom@516
|
107 if nargin < 1
|
tom@516
|
108 fs = 22050;
|
tom@516
|
109 end
|
tom@516
|
110
|
tom@516
|
111 % first figure out how many filter stages (PZFC/CARFAC channels):
|
tom@516
|
112 pole_Hz = CF_filter_params.first_pole_theta * fs / (2*pi);
|
tom@516
|
113 n_ch = 0;
|
tom@516
|
114 while pole_Hz > CF_filter_params.min_pole_Hz
|
tom@516
|
115 n_ch = n_ch + 1;
|
tom@516
|
116 pole_Hz = pole_Hz - CF_filter_params.ERB_per_step * ...
|
tom@516
|
117 ERB_Hz(pole_Hz, ERB_break_freq, 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);
|
tom@516
|
122 pole_Hz = CF_filter_params.first_pole_theta * fs / (2*pi);
|
tom@516
|
123 for ch = 1:n_ch
|
tom@516
|
124 pole_freqs(ch) = pole_Hz;
|
tom@516
|
125 pole_Hz = pole_Hz - CF_filter_params.ERB_per_step * ...
|
tom@516
|
126 ERB_Hz(pole_Hz, ERB_break_freq, 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
|
tom@516
|
132 CF = struct( ...
|
tom@516
|
133 'fs', fs, ...
|
dicklyon@528
|
134 'max_channels_per_octave', max_channels_per_octave, ...
|
tom@516
|
135 'filter_params', CF_filter_params, ...
|
tom@516
|
136 'AGC_params', CF_AGC_params, ...
|
tom@516
|
137 'IHC_params', CF_IHC_params, ...
|
tom@516
|
138 'n_ch', n_ch, ...
|
tom@516
|
139 'pole_freqs', pole_freqs, ...
|
tom@516
|
140 'filter_coeffs', CARFAC_DesignFilters(CF_filter_params, fs, pole_freqs), ...
|
tom@516
|
141 'AGC_coeffs', CARFAC_DesignAGC(CF_AGC_params, fs), ...
|
tom@516
|
142 'IHC_coeffs', CARFAC_DesignIHC(CF_IHC_params, fs), ...
|
tom@516
|
143 'n_mics', 0 );
|
tom@516
|
144
|
tom@516
|
145 % adjust the AGC_coeffs to account for IHC saturation level to get right
|
tom@516
|
146 % damping change as specified in CF.AGC_params.detect_scale
|
tom@516
|
147 CF.AGC_coeffs.detect_scale = CF.AGC_params.detect_scale / ...
|
tom@516
|
148 (CF.IHC_coeffs.saturation_output * CF.AGC_coeffs.AGC_gain);
|
tom@516
|
149
|
tom@516
|
150 %% Design the filter coeffs:
|
tom@516
|
151 function filter_coeffs = CARFAC_DesignFilters(filter_params, fs, pole_freqs)
|
tom@516
|
152
|
tom@516
|
153 n_ch = length(pole_freqs);
|
tom@516
|
154
|
tom@516
|
155 % the filter design coeffs:
|
tom@516
|
156
|
dicklyon@523
|
157 filter_coeffs = struct('velocity_scale', filter_params.velocity_scale, ...
|
dicklyon@523
|
158 'v_offset', filter_params.v_offset, ...
|
dicklyon@523
|
159 'v2_corner', filter_params.v2_corner, ...
|
dicklyon@523
|
160 'v_damp_max', filter_params.v_damp_max ...
|
dicklyon@523
|
161 );
|
tom@516
|
162
|
dicklyon@530
|
163 filter_coeffs.r1_coeffs = zeros(n_ch, 1);
|
dicklyon@530
|
164 filter_coeffs.a0_coeffs = zeros(n_ch, 1);
|
dicklyon@530
|
165 filter_coeffs.c0_coeffs = zeros(n_ch, 1);
|
tom@516
|
166 filter_coeffs.h_coeffs = zeros(n_ch, 1);
|
dicklyon@530
|
167 filter_coeffs.g0_coeffs = zeros(n_ch, 1);
|
tom@516
|
168
|
tom@516
|
169 % zero_ratio comes in via h. In book's circuit D, zero_ratio is 1/sqrt(a),
|
tom@516
|
170 % and that a is here 1 / (1+f) where h = f*c.
|
tom@516
|
171 % solve for f: 1/zero_ratio^2 = 1 / (1+f)
|
tom@516
|
172 % zero_ratio^2 = 1+f => f = zero_ratio^2 - 1
|
tom@516
|
173 f = filter_params.zero_ratio^2 - 1; % nominally 1 for half-octave
|
tom@516
|
174
|
tom@516
|
175 % Make pole positions, s and c coeffs, h and g coeffs, etc.,
|
tom@516
|
176 % which mostly depend on the pole angle theta:
|
tom@516
|
177 theta = pole_freqs .* (2 * pi / fs);
|
tom@516
|
178
|
dicklyon@530
|
179 c0 = sin(theta);
|
dicklyon@530
|
180 a0 = cos(theta);
|
dicklyon@530
|
181
|
tom@516
|
182 % different possible interpretations for min-damping r:
|
tom@516
|
183 % r = exp(-theta * CF_filter_params.min_zeta).
|
dicklyon@530
|
184 % Compress theta to give somewhat higher Q at highest thetas:
|
dicklyon@530
|
185 ff = filter_params.high_f_damping_compression; % 0 to 1; typ. 0.5
|
dicklyon@530
|
186 x = theta/pi;
|
dicklyon@530
|
187 zr_coeffs = pi * (x - ff * x.^3); % when ff is 0, this is just theta,
|
dicklyon@530
|
188 % and when ff is 1 it goes to zero at theta = pi.
|
dicklyon@530
|
189 filter_coeffs.zr_coeffs = zr_coeffs; % how r relates to zeta
|
dicklyon@530
|
190
|
dicklyon@530
|
191 r = (1 - zr_coeffs * filter_params.min_zeta);
|
dicklyon@530
|
192 filter_coeffs.r1_coeffs = r;
|
tom@516
|
193
|
tom@516
|
194 % undamped coupled-form coefficients:
|
dicklyon@530
|
195 filter_coeffs.a0_coeffs = a0;
|
dicklyon@530
|
196 filter_coeffs.c0_coeffs = c0;
|
tom@516
|
197
|
tom@516
|
198 % the zeros follow via the h_coeffs
|
dicklyon@530
|
199 h = c0 .* f;
|
tom@516
|
200 filter_coeffs.h_coeffs = h;
|
tom@516
|
201
|
dicklyon@530
|
202 % for unity gain at min damping, radius r; only used in CARFAC_Init:
|
dicklyon@530
|
203 extra_damping = zeros(size(r));
|
dicklyon@530
|
204 % this function needs to take filter_coeffs even if we haven't finished
|
dicklyon@530
|
205 % constucting it by putting in the g0_coeffs:
|
dicklyon@530
|
206 filter_coeffs.g0_coeffs = CARFAC_Stage_g(filter_coeffs, extra_damping);
|
tom@516
|
207
|
tom@516
|
208
|
tom@516
|
209 %% the AGC design coeffs:
|
tom@516
|
210 function AGC_coeffs = CARFAC_DesignAGC(AGC_params, fs)
|
tom@516
|
211
|
dicklyon@523
|
212 AGC_coeffs = struct('AGC_stage_gain', AGC_params.AGC_stage_gain);
|
tom@516
|
213
|
tom@516
|
214 % AGC1 pass is smoothing from base toward apex;
|
tom@516
|
215 % AGC2 pass is back, which is done first now
|
tom@516
|
216 AGC1_scales = AGC_params.AGC1_scales;
|
tom@516
|
217 AGC2_scales = AGC_params.AGC2_scales;
|
tom@516
|
218
|
tom@516
|
219 n_AGC_stages = AGC_params.n_stages;
|
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@523
|
278 AGC_coeffs.AGC_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@525
|
294 % % print some results
|
dicklyon@525
|
295 % AGC_coeffs
|
dicklyon@525
|
296 % AGC_spatial_FIR = AGC_coeffs.AGC_spatial_FIR
|
dicklyon@525
|
297
|
dicklyon@525
|
298
|
dicklyon@525
|
299 %%
|
dicklyon@525
|
300 function [FIR, OK] = Design_FIR_coeffs(n_taps, var, mn, n_iter)
|
dicklyon@525
|
301 % function [FIR, OK] = Design_FIR_coeffs(n_taps, spread_sq, delay, n_iter)
|
dicklyon@525
|
302
|
dicklyon@525
|
303 % reduce mean and variance of smoothing distribution by n_iterations:
|
dicklyon@525
|
304 mn = mn / n_iter;
|
dicklyon@525
|
305 var = var / n_iter;
|
dicklyon@525
|
306 switch n_taps
|
dicklyon@525
|
307 case 3
|
dicklyon@525
|
308 % based on solving to match mean and variance of [a, 1-a-b, b]:
|
dicklyon@525
|
309 a = (var + mn*mn - mn) / 2;
|
dicklyon@525
|
310 b = (var + mn*mn + mn) / 2;
|
dicklyon@525
|
311 FIR = [a, 1 - a - b, b];
|
dicklyon@525
|
312 OK = FIR(2) >= 0.2;
|
dicklyon@525
|
313 case 5
|
dicklyon@525
|
314 % based on solving to match [a/2, a/2, 1-a-b, b/2, b/2]:
|
dicklyon@525
|
315 a = ((var + mn*mn)*2/5 - mn*2/3) / 2;
|
dicklyon@525
|
316 b = ((var + mn*mn)*2/5 + mn*2/3) / 2;
|
dicklyon@525
|
317 % first and last coeffs are implicitly duplicated to make 5-point FIR:
|
dicklyon@525
|
318 FIR = [a/2, 1 - a - b, b/2];
|
dicklyon@525
|
319 OK = FIR(2) >= 0.1;
|
dicklyon@525
|
320 otherwise
|
dicklyon@525
|
321 error('Bad n_taps in AGC_spatial_FIR');
|
dicklyon@525
|
322 end
|
dicklyon@523
|
323
|
tom@516
|
324
|
tom@516
|
325 %% the IHC design coeffs:
|
tom@516
|
326 function IHC_coeffs = CARFAC_DesignIHC(IHC_params, fs)
|
tom@516
|
327
|
tom@516
|
328 if IHC_params.just_hwr
|
tom@516
|
329 IHC_coeffs = struct('just_hwr', 1);
|
tom@516
|
330 IHC_coeffs.saturation_output = 10; % HACK: assume some max out
|
tom@516
|
331 else
|
tom@516
|
332 if IHC_params.one_cap
|
tom@516
|
333 IHC_coeffs = struct(...
|
tom@516
|
334 'just_hwr', 0, ...
|
tom@516
|
335 'lpf_coeff', 1 - exp(-1/(IHC_params.tau_lpf * fs)), ...
|
tom@516
|
336 'out_rate', 1 / (IHC_params.tau_out * fs), ...
|
tom@516
|
337 'in_rate', 1 / (IHC_params.tau_in * fs), ...
|
tom@516
|
338 'one_cap', IHC_params.one_cap);
|
tom@516
|
339 else
|
tom@516
|
340 IHC_coeffs = struct(...
|
tom@516
|
341 'just_hwr', 0, ...
|
tom@516
|
342 'lpf_coeff', 1 - exp(-1/(IHC_params.tau_lpf * fs)), ...
|
tom@516
|
343 'out1_rate', 1 / (IHC_params.tau1_out * fs), ...
|
tom@516
|
344 'in1_rate', 1 / (IHC_params.tau1_in * fs), ...
|
tom@516
|
345 'out2_rate', 1 / (IHC_params.tau2_out * fs), ...
|
tom@516
|
346 'in2_rate', 1 / (IHC_params.tau2_in * fs), ...
|
tom@516
|
347 'one_cap', IHC_params.one_cap);
|
tom@516
|
348 end
|
tom@516
|
349
|
tom@516
|
350 % run one channel to convergence to get rest state:
|
tom@516
|
351 IHC_coeffs.rest_output = 0;
|
tom@516
|
352 IHC_state = struct( ...
|
tom@516
|
353 'cap_voltage', 0, ...
|
tom@516
|
354 'cap1_voltage', 0, ...
|
tom@516
|
355 'cap2_voltage', 0, ...
|
tom@516
|
356 'lpf1_state', 0, ...
|
tom@516
|
357 'lpf2_state', 0, ...
|
tom@516
|
358 'ihc_accum', 0);
|
tom@516
|
359
|
tom@516
|
360 IHC_in = 0;
|
tom@516
|
361 for k = 1:30000
|
tom@516
|
362 [IHC_out, IHC_state] = CARFAC_IHCStep(IHC_in, IHC_coeffs, IHC_state);
|
tom@516
|
363 end
|
tom@516
|
364
|
tom@516
|
365 IHC_coeffs.rest_output = IHC_out;
|
tom@516
|
366 IHC_coeffs.rest_cap = IHC_state.cap_voltage;
|
tom@516
|
367 IHC_coeffs.rest_cap1 = IHC_state.cap1_voltage;
|
tom@516
|
368 IHC_coeffs.rest_cap2 = IHC_state.cap2_voltage;
|
tom@516
|
369
|
tom@516
|
370 LARGE = 2;
|
tom@516
|
371 IHC_in = LARGE; % "Large" saturating input to IHC; make it alternate
|
tom@516
|
372 for k = 1:30000
|
tom@516
|
373 [IHC_out, IHC_state] = CARFAC_IHCStep(IHC_in, IHC_coeffs, IHC_state);
|
tom@516
|
374 prev_IHC_out = IHC_out;
|
tom@516
|
375 IHC_in = -IHC_in;
|
tom@516
|
376 end
|
tom@516
|
377
|
tom@516
|
378 IHC_coeffs.saturation_output = (IHC_out + prev_IHC_out) / 2;
|
tom@516
|
379 end
|
tom@516
|
380
|
tom@516
|
381 %%
|
tom@516
|
382 % default design result, running this function with no args, should look
|
tom@516
|
383 % like this, before CARFAC_Init puts state storage into it:
|
tom@516
|
384 %
|
dicklyon@523
|
385 %
|
tom@516
|
386 % CF = CARFAC_Design
|
tom@516
|
387 % CF.filter_params
|
tom@516
|
388 % CF.AGC_params
|
tom@516
|
389 % CF.filter_coeffs
|
tom@516
|
390 % CF.AGC_coeffs
|
tom@516
|
391 % CF.IHC_coeffs
|
tom@516
|
392 %
|
dicklyon@530
|
393 % CF =
|
dicklyon@530
|
394 % fs: 22050
|
dicklyon@530
|
395 % max_channels_per_octave: 12.1873
|
dicklyon@530
|
396 % filter_params: [1x1 struct]
|
dicklyon@530
|
397 % AGC_params: [1x1 struct]
|
dicklyon@530
|
398 % IHC_params: [1x1 struct]
|
dicklyon@530
|
399 % n_ch: 66
|
dicklyon@530
|
400 % pole_freqs: [66x1 double]
|
dicklyon@530
|
401 % filter_coeffs: [1x1 struct]
|
dicklyon@530
|
402 % AGC_coeffs: [1x1 struct]
|
dicklyon@530
|
403 % IHC_coeffs: [1x1 struct]
|
dicklyon@530
|
404 % n_mics: 0
|
dicklyon@530
|
405 % ans =
|
dicklyon@530
|
406 % velocity_scale: 0.2000
|
dicklyon@530
|
407 % v_offset: 0.0100
|
dicklyon@530
|
408 % v2_corner: 0.2000
|
dicklyon@530
|
409 % v_damp_max: 0.0100
|
dicklyon@530
|
410 % min_zeta: 0.1200
|
dicklyon@530
|
411 % first_pole_theta: 2.6704
|
dicklyon@530
|
412 % zero_ratio: 1.4142
|
dicklyon@530
|
413 % high_f_damping_compression: 0.5000
|
dicklyon@530
|
414 % ERB_per_step: 0.5000
|
dicklyon@530
|
415 % min_pole_Hz: 30
|
dicklyon@530
|
416 % ans =
|
tom@516
|
417 % n_stages: 4
|
tom@516
|
418 % time_constants: [0.0020 0.0080 0.0320 0.1280]
|
tom@516
|
419 % AGC_stage_gain: 2
|
dicklyon@523
|
420 % decimation: [8 2 2 2]
|
dicklyon@530
|
421 % AGC1_scales: [1 2 4 6]
|
dicklyon@530
|
422 % AGC2_scales: [1.5000 3 6 9]
|
tom@516
|
423 % detect_scale: 0.1500
|
dicklyon@530
|
424 % AGC_mix_coeff: 0.5000
|
dicklyon@530
|
425 % ans =
|
tom@516
|
426 % velocity_scale: 0.2000
|
dicklyon@523
|
427 % v_offset: 0.0100
|
dicklyon@523
|
428 % v2_corner: 0.2000
|
dicklyon@523
|
429 % v_damp_max: 0.0100
|
dicklyon@530
|
430 % r1_coeffs: [66x1 double]
|
dicklyon@530
|
431 % a0_coeffs: [66x1 double]
|
dicklyon@530
|
432 % c0_coeffs: [66x1 double]
|
dicklyon@530
|
433 % h_coeffs: [66x1 double]
|
dicklyon@530
|
434 % g0_coeffs: [66x1 double]
|
dicklyon@530
|
435 % zr_coeffs: [66x1 double]
|
dicklyon@530
|
436 % ans =
|
dicklyon@523
|
437 % AGC_stage_gain: 2
|
dicklyon@523
|
438 % AGC_epsilon: [0.1659 0.0867 0.0443 0.0224]
|
dicklyon@523
|
439 % decimation: [8 2 2 2]
|
dicklyon@530
|
440 % AGC_polez1: [0.1627 0.2713 0.3944 0.4194]
|
dicklyon@530
|
441 % AGC_polez2: [0.2219 0.3165 0.4260 0.4414]
|
dicklyon@530
|
442 % AGC_spatial_iterations: [1 1 2 2]
|
dicklyon@523
|
443 % AGC_spatial_FIR: [3x4 double]
|
dicklyon@523
|
444 % AGC_n_taps: [3 5 5 5]
|
dicklyon@530
|
445 % AGC_mix_coeffs: [0 0.0454 0.0227 0.0113]
|
dicklyon@523
|
446 % AGC_gain: 15
|
dicklyon@523
|
447 % detect_scale: 0.0664
|
dicklyon@530
|
448 % ans =
|
dicklyon@523
|
449 % just_hwr: 0
|
tom@516
|
450 % lpf_coeff: 0.4327
|
tom@516
|
451 % out1_rate: 0.0023
|
tom@516
|
452 % in1_rate: 0.0023
|
tom@516
|
453 % out2_rate: 0.0091
|
tom@516
|
454 % in2_rate: 0.0091
|
tom@516
|
455 % one_cap: 0
|
tom@516
|
456 % rest_output: 0.0365
|
tom@516
|
457 % rest_cap: 0
|
tom@516
|
458 % rest_cap1: 0.9635
|
tom@516
|
459 % rest_cap2: 0.9269
|
dicklyon@523
|
460 % saturation_output: 0.1507
|
tom@516
|
461
|