annotate matlab/bmm/carfac/CARFAC_Design.m @ 498:056df17e0898

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