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@561: function CF = CARFAC_Init(CF) dicklyon@561: % function CF = CARFAC_Init(CF) tom@516: % dicklyon@561: % Initialize state for one or more ears of CF. dicklyon@561: % This allocates and zeros all the state vector storage in the CF struct. tom@516: dicklyon@561: n_ears = CF.n_ears; tom@516: dicklyon@534: for ear = 1:n_ears dicklyon@534: % for now there's only one coeffs, not one per ear dicklyon@561: CF.ears(ear).CAR_state = CAR_Init_State(CF.ears(ear).CAR_coeffs); dicklyon@561: CF.ears(ear).IHC_state = IHC_Init_State(CF.ears(ear).IHC_coeffs); dicklyon@561: CF.ears(ear).AGC_state = AGC_Init_State(CF.ears(ear).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@617: 'zB_memory', coeffs.zr_coeffs, ... 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@665: n_ch = coeffs(1).n_ch; dicklyon@665: n_AGC_stages = coeffs(1).n_AGC_stages; dicklyon@665: state = struct([]); dicklyon@665: for stage = 1:n_AGC_stages dicklyon@665: % Initialize state recursively... dicklyon@665: state(stage).AGC_memory = zeros(n_ch, 1); dicklyon@665: state(stage).input_accum = zeros(n_ch, 1); dicklyon@665: state(stage).decim_phase = 0; % integer decimator phase dicklyon@665: end 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@565: 'lpf2_state', coeffs.rest_output * ones(n_ch, 1), ... dicklyon@565: 'ac_coupler', zeros(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@565: 'lpf2_state', coeffs.rest_output * ones(n_ch, 1), ... dicklyon@565: 'ac_coupler', zeros(n_ch, 1) ... dicklyon@556: ); dicklyon@556: end dicklyon@556: end dicklyon@534: dicklyon@556: dicklyon@556: