tom@516: % Copyright 2012, Google, Inc. tom@516: % Author: Richard F. Lyon tom@516: % tom@516: % This Matlab file is part of an implementation of Lyon's cochlear model: tom@516: % "Cascade of Asymmetric Resonators with Fast-Acting Compression" tom@516: % to supplement Lyon's upcoming book "Human and Machine Hearing" tom@516: % tom@516: % Licensed under the Apache License, Version 2.0 (the "License"); tom@516: % you may not use this file except in compliance with the License. tom@516: % You may obtain a copy of the License at tom@516: % tom@516: % http://www.apache.org/licenses/LICENSE-2.0 tom@516: % tom@516: % Unless required by applicable law or agreed to in writing, software tom@516: % distributed under the License is distributed on an "AS IS" BASIS, tom@516: % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. tom@516: % See the License for the specific language governing permissions and tom@516: % limitations under the License. tom@516: tom@516: function CF_struct = CARFAC_Init(CF_struct, n_mics) tom@516: % function CF_struct = CARFAC_Init(CF_struct, n_mics) tom@516: % tom@516: % Initialize state for n_mics channels (default 1). tom@516: % This allocates and zeros all the state vector storage in the CF_struct. tom@516: tom@516: % TODO (dicklyon): Review whether storing state in the same struct as tom@516: % the design is a good thing, or whether we want another tom@516: % level of object. I like fewer structs and class types. tom@516: tom@516: if nargin < 2 tom@516: n_mics = 1; % monaural tom@516: end tom@516: tom@516: % % this is probably what I'd do in the C++ version: tom@516: % if CF_struct.n_mics ~= n_mics; tom@516: % % free the state and make new number of channels tom@516: % % make a struct arrray, one element per mic channel, numbered: tom@516: % for k = 1:n_mics tom@516: % CF_struct.state(k) = struct('mic_number', k); tom@516: % end tom@516: % end tom@516: % But this code doesn't work because I don't understand struct arrays. tom@516: tom@516: % For now I don't ever free anything if n_mics is reduced; tom@516: % so be sure to respect n_mics, not the size of the state struct array. tom@516: tom@516: AGC_time_constants = CF_struct.AGC_params.time_constants; tom@516: n_AGC_stages = length(AGC_time_constants); tom@516: tom@516: CF_struct.n_mics = n_mics; tom@516: CF_struct.k_mod_decim = 0; % time index phase, cumulative over segments tom@516: tom@516: % This zeroing grows the struct array as needed: tom@516: for mic = 1:n_mics tom@516: CF_struct.filter_state(mic).z1_memory = zeros(CF_struct.n_ch, 1); tom@516: CF_struct.filter_state(mic).z2_memory = zeros(CF_struct.n_ch, 1); tom@516: % cubic loop tom@516: CF_struct.filter_state(mic).zA_memory = zeros(CF_struct.n_ch, 1); tom@516: % AGC interp tom@516: CF_struct.filter_state(mic).zB_memory = zeros(CF_struct.n_ch, 1); tom@516: % AGC incr tom@516: CF_struct.filter_state(mic).dzB_memory = zeros(CF_struct.n_ch, 1); tom@516: CF_struct.filter_state(mic).zY_memory = zeros(CF_struct.n_ch, 1); tom@516: CF_struct.filter_state(mic).detect_accum = zeros(CF_struct.n_ch, 1); tom@516: % AGC loop filters' state: tom@516: % HACK init tom@516: CF_struct.AGC_state(mic).AGC_memory = zeros(CF_struct.n_ch, n_AGC_stages); tom@516: CF_struct.AGC_state(mic).AGC_sum = zeros(CF_struct.n_ch, 1); tom@516: % IHC state: tom@516: if CF_struct.IHC_coeffs.just_hwr tom@516: CF_struct.IHC_state(mic).ihc_accum = zeros(CF_struct.n_ch, 1); tom@516: else tom@516: CF_struct.IHC_state(mic).cap_voltage = ... tom@516: CF_struct.IHC_coeffs(mic).rest_cap * ones(CF_struct.n_ch, 1); tom@516: CF_struct.IHC_state(mic).cap1_voltage = ... tom@516: CF_struct.IHC_coeffs(mic).rest_cap1 * ones(CF_struct.n_ch, 1); tom@516: CF_struct.IHC_state(mic).cap2_voltage = ... tom@516: CF_struct.IHC_coeffs(mic).rest_cap2 * ones(CF_struct.n_ch, 1); tom@516: CF_struct.IHC_state(mic).lpf1_state = ... tom@516: CF_struct.IHC_coeffs(mic).rest_output * zeros(CF_struct.n_ch, 1); tom@516: CF_struct.IHC_state(mic).lpf2_state = ... tom@516: CF_struct.IHC_coeffs(mic).rest_output * zeros(CF_struct.n_ch, 1); tom@516: CF_struct.IHC_state(mic).ihc_accum = zeros(CF_struct.n_ch, 1); tom@516: end tom@516: end tom@516: tom@516: