annotate trunk/matlab/bmm/carfac/CARFAC_Design.m @ 696:d8a404fbc4df

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