annotate matlab/bmm/carfac/CARFAC_Init.m @ 475:52f659be9008

New design params, including narrower AGC, Greenwood map for more channels, default 71, some renaming, open loop feature, ...
author dicklyon@google.com
date Thu, 22 Mar 2012 22:37:56 +0000
parents b4da807f4318
children 69955736f586
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@475 31 n_ears = 1; % monaural default
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
dicklyon@473 58 function state = CAR_Init_State(coeffs)
dicklyon@473 59 n_ch = coeffs.n_ch;
dicklyon@473 60 state = struct( ...
dicklyon@473 61 'z1_memory', zeros(n_ch, 1), ...
dicklyon@473 62 'z2_memory', zeros(n_ch, 1), ...
dicklyon@473 63 'zA_memory', zeros(n_ch, 1), ...
dicklyon@473 64 'zB_memory', zeros(n_ch, 1), ...
dicklyon@473 65 'dzB_memory', zeros(n_ch, 1), ...
dicklyon@473 66 'zY_memory', zeros(n_ch, 1), ...
dicklyon@473 67 'g_memory', coeffs.g0_coeffs, ...
dicklyon@473 68 'dg_memory', zeros(n_ch, 1) ...
dicklyon@473 69 );
dicklyon@473 70
dicklyon@473 71
dicklyon@473 72 function state = AGC_Init_State(coeffs)
dicklyon@473 73 n_ch = coeffs.n_ch;
dicklyon@473 74 n_AGC_stages = coeffs.n_AGC_stages;
dicklyon@473 75 state = struct( ...
dicklyon@473 76 'AGC_memory', zeros(n_ch, n_AGC_stages), ...
dicklyon@473 77 'input_accum', zeros(n_ch, n_AGC_stages), ...
dicklyon@473 78 'decim_phase', zeros(n_AGC_stages, 1) ... % integer decimator phase
dicklyon@473 79 );
dicklyon@473 80
dicklyon@473 81
dicklyon@473 82 function state = IHC_Init_State(coeffs)
dicklyon@473 83 n_ch = coeffs.n_ch;
dicklyon@473 84 state = struct( ...
dicklyon@473 85 'ihc_accum', zeros(n_ch, 1), ...
dicklyon@473 86 'cap_voltage', coeffs.rest_cap * ones(n_ch, 1), ...
dicklyon@473 87 'cap1_voltage', coeffs.rest_cap1 * ones(n_ch, 1), ...
dicklyon@473 88 'cap2_voltage', coeffs.rest_cap2* ones(n_ch, 1), ...
dicklyon@473 89 'lpf1_state', coeffs.rest_output * ones(n_ch, 1), ...
dicklyon@473 90 'lpf2_state', coeffs.rest_output * ones(n_ch, 1) ...
dicklyon@473 91 );
dicklyon@473 92
dicklyon@473 93