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: dicklyon@534: function CF_struct = CARFAC_Init(CF_struct, n_ears) dicklyon@534: % function CF_struct = CARFAC_Init(CF_struct, n_ears) tom@516: % dicklyon@534: % Initialize state for n_ears 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 dicklyon@536: n_ears = 1; % monaural default tom@516: end tom@516: tom@516: % % this is probably what I'd do in the C++ version: dicklyon@534: % if CF_struct.n_ears ~= n_ears; tom@516: % % free the state and make new number of channels dicklyon@534: % % make a struct arrray, one element per ear channel, numbered: dicklyon@534: % for k = 1:n_ears dicklyon@534: % CF_struct.state(k) = struct('ear_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: dicklyon@534: % For now I don't ever free anything if n_ears is reduced; dicklyon@534: % so be sure to respect n_ears, not the size of the state struct array. tom@516: dicklyon@534: CF_struct.n_ears = n_ears; tom@516: dicklyon@534: % These inits grow the struct arrays as needed: dicklyon@534: for ear = 1:n_ears dicklyon@534: % for now there's only one coeffs, not one per ear dicklyon@534: CF_struct.CAR_state(ear) = CAR_Init_State(CF_struct.CAR_coeffs); dicklyon@534: CF_struct.IHC_state(ear) = IHC_Init_State(CF_struct.IHC_coeffs); dicklyon@534: CF_struct.AGC_state(ear) = AGC_Init_State(CF_struct.AGC_coeffs); dicklyon@534: end dicklyon@523: dicklyon@534: dicklyon@534: function state = CAR_Init_State(coeffs) dicklyon@534: n_ch = coeffs.n_ch; dicklyon@534: state = struct( ... dicklyon@534: 'z1_memory', zeros(n_ch, 1), ... dicklyon@534: 'z2_memory', zeros(n_ch, 1), ... dicklyon@534: 'zA_memory', zeros(n_ch, 1), ... dicklyon@534: 'zB_memory', zeros(n_ch, 1), ... dicklyon@534: 'dzB_memory', zeros(n_ch, 1), ... dicklyon@534: 'zY_memory', zeros(n_ch, 1), ... dicklyon@534: 'g_memory', coeffs.g0_coeffs, ... dicklyon@534: 'dg_memory', zeros(n_ch, 1) ... dicklyon@534: ); dicklyon@534: dicklyon@534: dicklyon@534: function state = AGC_Init_State(coeffs) dicklyon@534: n_ch = coeffs.n_ch; dicklyon@534: n_AGC_stages = coeffs.n_AGC_stages; dicklyon@534: state = struct( ... dicklyon@534: 'AGC_memory', zeros(n_ch, n_AGC_stages), ... dicklyon@534: 'input_accum', zeros(n_ch, n_AGC_stages), ... dicklyon@534: 'decim_phase', zeros(n_AGC_stages, 1) ... % integer decimator phase dicklyon@534: ); dicklyon@534: dicklyon@534: dicklyon@534: function state = IHC_Init_State(coeffs) dicklyon@534: n_ch = coeffs.n_ch; dicklyon@556: if coeffs.just_hwr dicklyon@556: state = struct('ihc_accum', zeros(n_ch, 1)); dicklyon@556: else dicklyon@556: if coeffs.one_cap dicklyon@556: state = struct( ... dicklyon@556: 'ihc_accum', zeros(n_ch, 1), ... dicklyon@556: 'cap_voltage', coeffs.rest_cap * ones(n_ch, 1), ... dicklyon@556: 'lpf1_state', coeffs.rest_output * ones(n_ch, 1), ... dicklyon@556: 'lpf2_state', coeffs.rest_output * ones(n_ch, 1) ... dicklyon@556: ); dicklyon@556: else dicklyon@556: state = struct( ... dicklyon@556: 'ihc_accum', zeros(n_ch, 1), ... dicklyon@556: 'cap1_voltage', coeffs.rest_cap1 * ones(n_ch, 1), ... dicklyon@556: 'cap2_voltage', coeffs.rest_cap2* ones(n_ch, 1), ... dicklyon@556: 'lpf1_state', coeffs.rest_output * ones(n_ch, 1), ... dicklyon@556: 'lpf2_state', coeffs.rest_output * ones(n_ch, 1) ... dicklyon@556: ); dicklyon@556: end dicklyon@556: end dicklyon@534: dicklyon@556: dicklyon@556: