annotate matlab/bmm/carfac/CARFAC_Init.m @ 626:586b0677aae8

Fourth revision of Alex Brandmeyer's C++ implementation. Fixed more style issues, changed AGC structures to vectors, replaced FloatArray2d with vector<FloatArray>, implemented first tests using GTest to verify coefficients and monaural output against Matlab values (stored in aimc/carfac/test_data/). To run tests, change the path stored in carfac_test.h in TEST_SRC_DIR. Added CARFAC_GenerateTestData to the Matlab branch, fixed stage indexing in CARFAC_Cross_Couple.m to reflect changes in AGCCoeffs and AGCState structs.
author alexbrandmeyer
date Wed, 22 May 2013 21:30:02 +0000
parents b3118c9ed67f
children
rev   line source
tom@455 1 % Copyright 2012, Google, Inc.
tom@455 2 % Author: Richard F. Lyon
tom@455 3 %
tom@455 4 % This Matlab file is part of an implementation of Lyon's cochlear model:
tom@455 5 % "Cascade of Asymmetric Resonators with Fast-Acting Compression"
tom@455 6 % to supplement Lyon's upcoming book "Human and Machine Hearing"
tom@455 7 %
tom@455 8 % Licensed under the Apache License, Version 2.0 (the "License");
tom@455 9 % you may not use this file except in compliance with the License.
tom@455 10 % You may obtain a copy of the License at
tom@455 11 %
tom@455 12 % http://www.apache.org/licenses/LICENSE-2.0
tom@455 13 %
tom@455 14 % Unless required by applicable law or agreed to in writing, software
tom@455 15 % distributed under the License is distributed on an "AS IS" BASIS,
tom@455 16 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tom@455 17 % See the License for the specific language governing permissions and
tom@455 18 % limitations under the License.
tom@455 19
dicklyon@500 20 function CF = CARFAC_Init(CF)
dicklyon@500 21 % function CF = CARFAC_Init(CF)
tom@455 22 %
dicklyon@500 23 % Initialize state for one or more ears of CF.
dicklyon@500 24 % This allocates and zeros all the state vector storage in the CF struct.
tom@455 25
dicklyon@500 26 n_ears = CF.n_ears;
tom@455 27
dicklyon@473 28 for ear = 1:n_ears
dicklyon@473 29 % for now there's only one coeffs, not one per ear
dicklyon@500 30 CF.ears(ear).CAR_state = CAR_Init_State(CF.ears(ear).CAR_coeffs);
dicklyon@500 31 CF.ears(ear).IHC_state = IHC_Init_State(CF.ears(ear).IHC_coeffs);
dicklyon@500 32 CF.ears(ear).AGC_state = AGC_Init_State(CF.ears(ear).AGC_coeffs);
dicklyon@473 33 end
dicklyon@462 34
dicklyon@473 35
dicklyon@473 36 function state = CAR_Init_State(coeffs)
dicklyon@473 37 n_ch = coeffs.n_ch;
dicklyon@473 38 state = struct( ...
dicklyon@473 39 'z1_memory', zeros(n_ch, 1), ...
dicklyon@473 40 'z2_memory', zeros(n_ch, 1), ...
dicklyon@473 41 'zA_memory', zeros(n_ch, 1), ...
dicklyon@606 42 'zB_memory', coeffs.zr_coeffs, ...
dicklyon@473 43 'dzB_memory', zeros(n_ch, 1), ...
dicklyon@473 44 'zY_memory', zeros(n_ch, 1), ...
dicklyon@473 45 'g_memory', coeffs.g0_coeffs, ...
dicklyon@473 46 'dg_memory', zeros(n_ch, 1) ...
dicklyon@473 47 );
dicklyon@473 48
dicklyon@473 49
dicklyon@473 50 function state = AGC_Init_State(coeffs)
dicklyon@623 51 n_ch = coeffs(1).n_ch;
dicklyon@623 52 n_AGC_stages = coeffs(1).n_AGC_stages;
dicklyon@623 53 state = struct([]);
dicklyon@623 54 for stage = 1:n_AGC_stages
dicklyon@623 55 % Initialize state recursively...
dicklyon@623 56 state(stage).AGC_memory = zeros(n_ch, 1);
dicklyon@623 57 state(stage).input_accum = zeros(n_ch, 1);
dicklyon@623 58 state(stage).decim_phase = 0; % integer decimator phase
dicklyon@623 59 end
dicklyon@473 60
dicklyon@473 61
dicklyon@473 62 function state = IHC_Init_State(coeffs)
dicklyon@473 63 n_ch = coeffs.n_ch;
dicklyon@495 64 if coeffs.just_hwr
dicklyon@495 65 state = struct('ihc_accum', zeros(n_ch, 1));
dicklyon@495 66 else
dicklyon@495 67 if coeffs.one_cap
dicklyon@495 68 state = struct( ...
dicklyon@495 69 'ihc_accum', zeros(n_ch, 1), ...
dicklyon@495 70 'cap_voltage', coeffs.rest_cap * ones(n_ch, 1), ...
dicklyon@495 71 'lpf1_state', coeffs.rest_output * ones(n_ch, 1), ...
dicklyon@504 72 'lpf2_state', coeffs.rest_output * ones(n_ch, 1), ...
dicklyon@504 73 'ac_coupler', zeros(n_ch, 1) ...
dicklyon@495 74 );
dicklyon@495 75 else
dicklyon@495 76 state = struct( ...
dicklyon@495 77 'ihc_accum', zeros(n_ch, 1), ...
dicklyon@495 78 'cap1_voltage', coeffs.rest_cap1 * ones(n_ch, 1), ...
dicklyon@495 79 'cap2_voltage', coeffs.rest_cap2* ones(n_ch, 1), ...
dicklyon@495 80 'lpf1_state', coeffs.rest_output * ones(n_ch, 1), ...
dicklyon@504 81 'lpf2_state', coeffs.rest_output * ones(n_ch, 1), ...
dicklyon@504 82 'ac_coupler', zeros(n_ch, 1) ...
dicklyon@495 83 );
dicklyon@495 84 end
dicklyon@495 85 end
dicklyon@473 86
dicklyon@495 87
dicklyon@495 88