annotate trunk/matlab/bmm/carfac/CARFAC_Init.m @ 523:2b96cb7ea4f7

Major AGC improvements mostly
author dicklyon@google.com
date Thu, 01 Mar 2012 19:49:24 +0000
parents aa282a2b61bb
children fb60ea429bb8
rev   line source
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_struct = CARFAC_Init(CF_struct, n_mics)
tom@516 21 % function CF_struct = CARFAC_Init(CF_struct, n_mics)
tom@516 22 %
tom@516 23 % Initialize state for n_mics channels (default 1).
tom@516 24 % This allocates and zeros all the state vector storage in the CF_struct.
tom@516 25
tom@516 26 % TODO (dicklyon): Review whether storing state in the same struct as
tom@516 27 % the design is a good thing, or whether we want another
tom@516 28 % level of object. I like fewer structs and class types.
tom@516 29
tom@516 30 if nargin < 2
tom@516 31 n_mics = 1; % monaural
tom@516 32 end
tom@516 33
tom@516 34 % % this is probably what I'd do in the C++ version:
tom@516 35 % if CF_struct.n_mics ~= n_mics;
tom@516 36 % % free the state and make new number of channels
tom@516 37 % % make a struct arrray, one element per mic channel, numbered:
tom@516 38 % for k = 1:n_mics
tom@516 39 % CF_struct.state(k) = struct('mic_number', k);
tom@516 40 % end
tom@516 41 % end
tom@516 42 % But this code doesn't work because I don't understand struct arrays.
tom@516 43
tom@516 44 % For now I don't ever free anything if n_mics is reduced;
tom@516 45 % so be sure to respect n_mics, not the size of the state struct array.
tom@516 46
tom@516 47 AGC_time_constants = CF_struct.AGC_params.time_constants;
tom@516 48 n_AGC_stages = length(AGC_time_constants);
tom@516 49
tom@516 50 CF_struct.n_mics = n_mics;
tom@516 51 CF_struct.k_mod_decim = 0; % time index phase, cumulative over segments
dicklyon@523 52 n_ch = CF_struct.n_ch;
dicklyon@523 53
dicklyon@523 54 % keep all the decimator phase info in mic 1 state only:
dicklyon@523 55 CF_struct.AGC_state(1).decim_phase = zeros(n_AGC_stages, 1); % ints
tom@516 56
tom@516 57 % This zeroing grows the struct array as needed:
tom@516 58 for mic = 1:n_mics
dicklyon@523 59 CF_struct.filter_state(mic).z1_memory = zeros(n_ch, 1);
dicklyon@523 60 CF_struct.filter_state(mic).z2_memory = zeros(n_ch, 1);
dicklyon@523 61 CF_struct.filter_state(mic).zA_memory = zeros(n_ch, 1); % cubic loop
dicklyon@523 62 CF_struct.filter_state(mic).zB_memory = zeros(n_ch, 1); % AGC interp
dicklyon@523 63 CF_struct.filter_state(mic).dzB_memory = zeros(n_ch, 1); % AGC incr
dicklyon@523 64 CF_struct.filter_state(mic).zY_memory = zeros(n_ch, 1);
dicklyon@523 65 CF_struct.filter_state(mic).detect_accum = zeros(n_ch, 1);
tom@516 66 % AGC loop filters' state:
dicklyon@523 67 CF_struct.AGC_state(mic).AGC_memory = zeros(n_ch, n_AGC_stages); % HACK init
dicklyon@523 68 CF_struct.AGC_state(mic).input_accum = zeros(n_ch, n_AGC_stages); % HACK init
tom@516 69 % IHC state:
tom@516 70 if CF_struct.IHC_coeffs.just_hwr
dicklyon@523 71 CF_struct.IHC_state(mic).ihc_accum = zeros(n_ch, 1);
tom@516 72 else
tom@516 73 CF_struct.IHC_state(mic).cap_voltage = ...
dicklyon@523 74 CF_struct.IHC_coeffs.rest_cap * ones(n_ch, 1);
tom@516 75 CF_struct.IHC_state(mic).cap1_voltage = ...
dicklyon@523 76 CF_struct.IHC_coeffs.rest_cap1 * ones(n_ch, 1);
tom@516 77 CF_struct.IHC_state(mic).cap2_voltage = ...
dicklyon@523 78 CF_struct.IHC_coeffs.rest_cap2 * ones(n_ch, 1);
tom@516 79 CF_struct.IHC_state(mic).lpf1_state = ...
dicklyon@523 80 CF_struct.IHC_coeffs.rest_output * zeros(n_ch, 1);
tom@516 81 CF_struct.IHC_state(mic).lpf2_state = ...
dicklyon@523 82 CF_struct.IHC_coeffs.rest_output * zeros(n_ch, 1);
dicklyon@523 83 CF_struct.IHC_state(mic).ihc_accum = zeros(n_ch, 1);
tom@516 84 end
tom@516 85 end