annotate matlab/bmm/carfac/CARFAC_Init.m @ 473:b4da807f4318

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 bc0618485ad4
children 52f659be9008
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@473 20 function CF_struct = CARFAC_Init(CF_struct, n_ears)
dicklyon@473 21 % function CF_struct = CARFAC_Init(CF_struct, n_ears)
tom@455 22 %
dicklyon@473 23 % Initialize state for n_ears channels (default 1).
tom@455 24 % This allocates and zeros all the state vector storage in the CF_struct.
tom@455 25
tom@455 26 % TODO (dicklyon): Review whether storing state in the same struct as
tom@455 27 % the design is a good thing, or whether we want another
tom@455 28 % level of object. I like fewer structs and class types.
tom@455 29
tom@455 30 if nargin < 2
dicklyon@473 31 n_ears = 1; % monaural
tom@455 32 end
tom@455 33
tom@455 34 % % this is probably what I'd do in the C++ version:
dicklyon@473 35 % if CF_struct.n_ears ~= n_ears;
tom@455 36 % % free the state and make new number of channels
dicklyon@473 37 % % make a struct arrray, one element per ear channel, numbered:
dicklyon@473 38 % for k = 1:n_ears
dicklyon@473 39 % CF_struct.state(k) = struct('ear_number', k);
tom@455 40 % end
tom@455 41 % end
tom@455 42 % But this code doesn't work because I don't understand struct arrays.
tom@455 43
dicklyon@473 44 % For now I don't ever free anything if n_ears is reduced;
dicklyon@473 45 % so be sure to respect n_ears, not the size of the state struct array.
tom@455 46
dicklyon@473 47 CF_struct.n_ears = n_ears;
tom@455 48
dicklyon@473 49 % These inits grow the struct arrays as needed:
dicklyon@473 50 for ear = 1:n_ears
dicklyon@473 51 % for now there's only one coeffs, not one per ear
dicklyon@473 52 CF_struct.CAR_state(ear) = CAR_Init_State(CF_struct.CAR_coeffs);
dicklyon@473 53 CF_struct.IHC_state(ear) = IHC_Init_State(CF_struct.IHC_coeffs);
dicklyon@473 54 CF_struct.AGC_state(ear) = AGC_Init_State(CF_struct.AGC_coeffs);
dicklyon@473 55 end
dicklyon@462 56
dicklyon@473 57 % for ear = 1:n_ears
dicklyon@473 58 % CF_struct.CAR_state(ear).z1_memory = zeros(n_ch, 1);
dicklyon@473 59 % CF_struct.CAR_state(ear).z2_memory = zeros(n_ch, 1);
dicklyon@473 60 % CF_struct.CAR_state(ear).zA_memory = zeros(n_ch, 1); % cubic loop
dicklyon@473 61 % CF_struct.CAR_state(ear).zB_memory = zeros(n_ch, 1); % AGC interp
dicklyon@473 62 % CF_struct.CAR_state(ear).dzB_memory = zeros(n_ch, 1); % AGC incr
dicklyon@473 63 % CF_struct.CAR_state(ear).zY_memory = zeros(n_ch, 1);
dicklyon@473 64 % CF_struct.CAR_state(ear).detect_accum = zeros(n_ch, 1);
dicklyon@473 65 % CF_struct.CAR_state(ear).g_memory = ...
dicklyon@473 66 % CF_struct.CAR_coeffs(ear).g0_coeffs; % initial g for min_zeta
dicklyon@473 67 % CF_struct.CAR_state(ear).dg_memory = zeros(n_ch, 1); % g interp
dicklyon@473 68 % % AGC loop filters' state:
dicklyon@473 69 % CF_struct.AGC_state(ear).AGC_memory = zeros(n_ch, n_AGC_stages); % HACK init
dicklyon@473 70 % CF_struct.AGC_state(ear).input_accum = zeros(n_ch, n_AGC_stages); % HACK init
dicklyon@473 71 % % IHC state:
dicklyon@473 72 % if CF_struct.IHC_coeffs.just_hwr
dicklyon@473 73 % CF_struct.IHC_state(ear).ihc_accum = zeros(n_ch, 1);
dicklyon@473 74 % else
dicklyon@473 75 % CF_struct.IHC_state(ear).cap_voltage = ...
dicklyon@473 76 % CF_struct.IHC_coeffs.rest_cap * ones(n_ch, 1);
dicklyon@473 77 % CF_struct.IHC_state(ear).cap1_voltage = ...
dicklyon@473 78 % CF_struct.IHC_coeffs.rest_cap1 * ones(n_ch, 1);
dicklyon@473 79 % CF_struct.IHC_state(ear).cap2_voltage = ...
dicklyon@473 80 % CF_struct.IHC_coeffs.rest_cap2 * ones(n_ch, 1);
dicklyon@473 81 % CF_struct.IHC_state(ear).lpf1_state = ...
dicklyon@473 82 % CF_struct.IHC_coeffs.rest_output * zeros(n_ch, 1);
dicklyon@473 83 % CF_struct.IHC_state(ear).lpf2_state = ...
dicklyon@473 84 % CF_struct.IHC_coeffs.rest_output * zeros(n_ch, 1);
dicklyon@473 85 % CF_struct.IHC_state(ear).ihc_accum = zeros(n_ch, 1);
dicklyon@473 86 % end
dicklyon@473 87 % end
tom@455 88
dicklyon@473 89
dicklyon@473 90 function state = CAR_Init_State(coeffs)
dicklyon@473 91 n_ch = coeffs.n_ch;
dicklyon@473 92 state = struct( ...
dicklyon@473 93 'z1_memory', zeros(n_ch, 1), ...
dicklyon@473 94 'z2_memory', zeros(n_ch, 1), ...
dicklyon@473 95 'zA_memory', zeros(n_ch, 1), ...
dicklyon@473 96 'zB_memory', zeros(n_ch, 1), ...
dicklyon@473 97 'dzB_memory', zeros(n_ch, 1), ...
dicklyon@473 98 'zY_memory', zeros(n_ch, 1), ...
dicklyon@473 99 'detect_accum', zeros(n_ch, 1), ...
dicklyon@473 100 'g_memory', coeffs.g0_coeffs, ...
dicklyon@473 101 'dg_memory', zeros(n_ch, 1) ...
dicklyon@473 102 );
dicklyon@473 103
dicklyon@473 104
dicklyon@473 105 function state = AGC_Init_State(coeffs)
dicklyon@473 106 n_ch = coeffs.n_ch;
dicklyon@473 107 n_AGC_stages = coeffs.n_AGC_stages;
dicklyon@473 108 state = struct( ...
dicklyon@473 109 'AGC_memory', zeros(n_ch, n_AGC_stages), ...
dicklyon@473 110 'input_accum', zeros(n_ch, n_AGC_stages), ...
dicklyon@473 111 'decim_phase', zeros(n_AGC_stages, 1) ... % integer decimator phase
dicklyon@473 112 );
dicklyon@473 113
dicklyon@473 114
dicklyon@473 115 function state = IHC_Init_State(coeffs)
dicklyon@473 116 n_ch = coeffs.n_ch;
dicklyon@473 117 state = struct( ...
dicklyon@473 118 'ihc_accum', zeros(n_ch, 1), ...
dicklyon@473 119 'cap_voltage', coeffs.rest_cap * ones(n_ch, 1), ...
dicklyon@473 120 'cap1_voltage', coeffs.rest_cap1 * ones(n_ch, 1), ...
dicklyon@473 121 'cap2_voltage', coeffs.rest_cap2* ones(n_ch, 1), ...
dicklyon@473 122 'lpf1_state', coeffs.rest_output * ones(n_ch, 1), ...
dicklyon@473 123 'lpf2_state', coeffs.rest_output * ones(n_ch, 1) ...
dicklyon@473 124 );
dicklyon@473 125
dicklyon@473 126