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