annotate trunk/matlab/bmm/carfac/CARFAC_Init.m @ 534:95a11cca4619

Add CARFAC_Design_Doc.txt, CARFAC_Run_Segment.m, and some renames; rename various variables to be more parallel; clean up init code and such.
author dicklyon@google.com
date Fri, 16 Mar 2012 04:19:24 +0000
parents fb60ea429bb8
children 2964a3b4a00a
rev   line source
tom@516 1 % Copyright 2012, Google, Inc.
tom@516 2 % Author: Richard F. Lyon
tom@516 3 %
tom@516 4 % This Matlab file is part of an implementation of Lyon's cochlear model:
tom@516 5 % "Cascade of Asymmetric Resonators with Fast-Acting Compression"
tom@516 6 % to supplement Lyon's upcoming book "Human and Machine Hearing"
tom@516 7 %
tom@516 8 % Licensed under the Apache License, Version 2.0 (the "License");
tom@516 9 % you may not use this file except in compliance with the License.
tom@516 10 % You may obtain a copy of the License at
tom@516 11 %
tom@516 12 % http://www.apache.org/licenses/LICENSE-2.0
tom@516 13 %
tom@516 14 % Unless required by applicable law or agreed to in writing, software
tom@516 15 % distributed under the License is distributed on an "AS IS" BASIS,
tom@516 16 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tom@516 17 % See the License for the specific language governing permissions and
tom@516 18 % limitations under the License.
tom@516 19
dicklyon@534 20 function CF_struct = CARFAC_Init(CF_struct, n_ears)
dicklyon@534 21 % function CF_struct = CARFAC_Init(CF_struct, n_ears)
tom@516 22 %
dicklyon@534 23 % Initialize state for n_ears channels (default 1).
tom@516 24 % This allocates and zeros all the state vector storage in the CF_struct.
tom@516 25
tom@516 26 % TODO (dicklyon): Review whether storing state in the same struct as
tom@516 27 % the design is a good thing, or whether we want another
tom@516 28 % level of object. I like fewer structs and class types.
tom@516 29
tom@516 30 if nargin < 2
dicklyon@534 31 n_ears = 1; % monaural
tom@516 32 end
tom@516 33
tom@516 34 % % this is probably what I'd do in the C++ version:
dicklyon@534 35 % if CF_struct.n_ears ~= n_ears;
tom@516 36 % % free the state and make new number of channels
dicklyon@534 37 % % make a struct arrray, one element per ear channel, numbered:
dicklyon@534 38 % for k = 1:n_ears
dicklyon@534 39 % CF_struct.state(k) = struct('ear_number', k);
tom@516 40 % end
tom@516 41 % end
tom@516 42 % But this code doesn't work because I don't understand struct arrays.
tom@516 43
dicklyon@534 44 % For now I don't ever free anything if n_ears is reduced;
dicklyon@534 45 % so be sure to respect n_ears, not the size of the state struct array.
tom@516 46
dicklyon@534 47 CF_struct.n_ears = n_ears;
tom@516 48
dicklyon@534 49 % These inits grow the struct arrays as needed:
dicklyon@534 50 for ear = 1:n_ears
dicklyon@534 51 % for now there's only one coeffs, not one per ear
dicklyon@534 52 CF_struct.CAR_state(ear) = CAR_Init_State(CF_struct.CAR_coeffs);
dicklyon@534 53 CF_struct.IHC_state(ear) = IHC_Init_State(CF_struct.IHC_coeffs);
dicklyon@534 54 CF_struct.AGC_state(ear) = AGC_Init_State(CF_struct.AGC_coeffs);
dicklyon@534 55 end
dicklyon@523 56
dicklyon@534 57 % for ear = 1:n_ears
dicklyon@534 58 % CF_struct.CAR_state(ear).z1_memory = zeros(n_ch, 1);
dicklyon@534 59 % CF_struct.CAR_state(ear).z2_memory = zeros(n_ch, 1);
dicklyon@534 60 % CF_struct.CAR_state(ear).zA_memory = zeros(n_ch, 1); % cubic loop
dicklyon@534 61 % CF_struct.CAR_state(ear).zB_memory = zeros(n_ch, 1); % AGC interp
dicklyon@534 62 % CF_struct.CAR_state(ear).dzB_memory = zeros(n_ch, 1); % AGC incr
dicklyon@534 63 % CF_struct.CAR_state(ear).zY_memory = zeros(n_ch, 1);
dicklyon@534 64 % CF_struct.CAR_state(ear).detect_accum = zeros(n_ch, 1);
dicklyon@534 65 % CF_struct.CAR_state(ear).g_memory = ...
dicklyon@534 66 % CF_struct.CAR_coeffs(ear).g0_coeffs; % initial g for min_zeta
dicklyon@534 67 % CF_struct.CAR_state(ear).dg_memory = zeros(n_ch, 1); % g interp
dicklyon@534 68 % % AGC loop filters' state:
dicklyon@534 69 % CF_struct.AGC_state(ear).AGC_memory = zeros(n_ch, n_AGC_stages); % HACK init
dicklyon@534 70 % CF_struct.AGC_state(ear).input_accum = zeros(n_ch, n_AGC_stages); % HACK init
dicklyon@534 71 % % IHC state:
dicklyon@534 72 % if CF_struct.IHC_coeffs.just_hwr
dicklyon@534 73 % CF_struct.IHC_state(ear).ihc_accum = zeros(n_ch, 1);
dicklyon@534 74 % else
dicklyon@534 75 % CF_struct.IHC_state(ear).cap_voltage = ...
dicklyon@534 76 % CF_struct.IHC_coeffs.rest_cap * ones(n_ch, 1);
dicklyon@534 77 % CF_struct.IHC_state(ear).cap1_voltage = ...
dicklyon@534 78 % CF_struct.IHC_coeffs.rest_cap1 * ones(n_ch, 1);
dicklyon@534 79 % CF_struct.IHC_state(ear).cap2_voltage = ...
dicklyon@534 80 % CF_struct.IHC_coeffs.rest_cap2 * ones(n_ch, 1);
dicklyon@534 81 % CF_struct.IHC_state(ear).lpf1_state = ...
dicklyon@534 82 % CF_struct.IHC_coeffs.rest_output * zeros(n_ch, 1);
dicklyon@534 83 % CF_struct.IHC_state(ear).lpf2_state = ...
dicklyon@534 84 % CF_struct.IHC_coeffs.rest_output * zeros(n_ch, 1);
dicklyon@534 85 % CF_struct.IHC_state(ear).ihc_accum = zeros(n_ch, 1);
dicklyon@534 86 % end
dicklyon@534 87 % end
tom@516 88
dicklyon@534 89
dicklyon@534 90 function state = CAR_Init_State(coeffs)
dicklyon@534 91 n_ch = coeffs.n_ch;
dicklyon@534 92 state = struct( ...
dicklyon@534 93 'z1_memory', zeros(n_ch, 1), ...
dicklyon@534 94 'z2_memory', zeros(n_ch, 1), ...
dicklyon@534 95 'zA_memory', zeros(n_ch, 1), ...
dicklyon@534 96 'zB_memory', zeros(n_ch, 1), ...
dicklyon@534 97 'dzB_memory', zeros(n_ch, 1), ...
dicklyon@534 98 'zY_memory', zeros(n_ch, 1), ...
dicklyon@534 99 'detect_accum', zeros(n_ch, 1), ...
dicklyon@534 100 'g_memory', coeffs.g0_coeffs, ...
dicklyon@534 101 'dg_memory', zeros(n_ch, 1) ...
dicklyon@534 102 );
dicklyon@534 103
dicklyon@534 104
dicklyon@534 105 function state = AGC_Init_State(coeffs)
dicklyon@534 106 n_ch = coeffs.n_ch;
dicklyon@534 107 n_AGC_stages = coeffs.n_AGC_stages;
dicklyon@534 108 state = struct( ...
dicklyon@534 109 'AGC_memory', zeros(n_ch, n_AGC_stages), ...
dicklyon@534 110 'input_accum', zeros(n_ch, n_AGC_stages), ...
dicklyon@534 111 'decim_phase', zeros(n_AGC_stages, 1) ... % integer decimator phase
dicklyon@534 112 );
dicklyon@534 113
dicklyon@534 114
dicklyon@534 115 function state = IHC_Init_State(coeffs)
dicklyon@534 116 n_ch = coeffs.n_ch;
dicklyon@534 117 state = struct( ...
dicklyon@534 118 'ihc_accum', zeros(n_ch, 1), ...
dicklyon@534 119 'cap_voltage', coeffs.rest_cap * ones(n_ch, 1), ...
dicklyon@534 120 'cap1_voltage', coeffs.rest_cap1 * ones(n_ch, 1), ...
dicklyon@534 121 'cap2_voltage', coeffs.rest_cap2* ones(n_ch, 1), ...
dicklyon@534 122 'lpf1_state', coeffs.rest_output * ones(n_ch, 1), ...
dicklyon@534 123 'lpf2_state', coeffs.rest_output * ones(n_ch, 1) ...
dicklyon@534 124 );
dicklyon@534 125
dicklyon@534 126