annotate matlab/bmm/carfac/CARFAC_Design.m @ 476:b084aed83e87

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