annotate matlab/bmm/carfac/CARFAC_Init.m @ 455:f8ba7ad93fa9

Added MATLAB code for Lyon's CAR-FAC filter cascade.
author tom@acousticscale.org
date Wed, 15 Feb 2012 21:26:40 +0000
parents
children 6ddf64b38211
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
tom@455 20 function CF_struct = CARFAC_Init(CF_struct, n_mics)
tom@455 21 % function CF_struct = CARFAC_Init(CF_struct, n_mics)
tom@455 22 %
tom@455 23 % Initialize state for n_mics 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
tom@455 31 n_mics = 1; % monaural
tom@455 32 end
tom@455 33
tom@455 34 % % this is probably what I'd do in the C++ version:
tom@455 35 % if CF_struct.n_mics ~= n_mics;
tom@455 36 % % free the state and make new number of channels
tom@455 37 % % make a struct arrray, one element per mic channel, numbered:
tom@455 38 % for k = 1:n_mics
tom@455 39 % CF_struct.state(k) = struct('mic_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
tom@455 44 % For now I don't ever free anything if n_mics is reduced;
tom@455 45 % so be sure to respect n_mics, not the size of the state struct array.
tom@455 46
tom@455 47 AGC_time_constants = CF_struct.AGC_params.time_constants;
tom@455 48 n_AGC_stages = length(AGC_time_constants);
tom@455 49
tom@455 50 CF_struct.n_mics = n_mics;
tom@455 51 CF_struct.k_mod_decim = 0; % time index phase, cumulative over segments
tom@455 52
tom@455 53 % This zeroing grows the struct array as needed:
tom@455 54 for mic = 1:n_mics
tom@455 55 CF_struct.filter_state(mic).z1_memory = zeros(CF_struct.n_ch, 1);
tom@455 56 CF_struct.filter_state(mic).z2_memory = zeros(CF_struct.n_ch, 1);
tom@455 57 % cubic loop
tom@455 58 CF_struct.filter_state(mic).zA_memory = zeros(CF_struct.n_ch, 1);
tom@455 59 % AGC interp
tom@455 60 CF_struct.filter_state(mic).zB_memory = zeros(CF_struct.n_ch, 1);
tom@455 61 % AGC incr
tom@455 62 CF_struct.filter_state(mic).dzB_memory = zeros(CF_struct.n_ch, 1);
tom@455 63 CF_struct.filter_state(mic).zY_memory = zeros(CF_struct.n_ch, 1);
tom@455 64 CF_struct.filter_state(mic).detect_accum = zeros(CF_struct.n_ch, 1);
tom@455 65 % AGC loop filters' state:
tom@455 66 % HACK init
tom@455 67 CF_struct.AGC_state(mic).AGC_memory = zeros(CF_struct.n_ch, n_AGC_stages);
tom@455 68 CF_struct.AGC_state(mic).AGC_sum = zeros(CF_struct.n_ch, 1);
tom@455 69 % IHC state:
tom@455 70 if CF_struct.IHC_coeffs.just_hwr
tom@455 71 CF_struct.IHC_state(mic).ihc_accum = zeros(CF_struct.n_ch, 1);
tom@455 72 else
tom@455 73 CF_struct.IHC_state(mic).cap_voltage = ...
tom@455 74 CF_struct.IHC_coeffs(mic).rest_cap * ones(CF_struct.n_ch, 1);
tom@455 75 CF_struct.IHC_state(mic).cap1_voltage = ...
tom@455 76 CF_struct.IHC_coeffs(mic).rest_cap1 * ones(CF_struct.n_ch, 1);
tom@455 77 CF_struct.IHC_state(mic).cap2_voltage = ...
tom@455 78 CF_struct.IHC_coeffs(mic).rest_cap2 * ones(CF_struct.n_ch, 1);
tom@455 79 CF_struct.IHC_state(mic).lpf1_state = ...
tom@455 80 CF_struct.IHC_coeffs(mic).rest_output * zeros(CF_struct.n_ch, 1);
tom@455 81 CF_struct.IHC_state(mic).lpf2_state = ...
tom@455 82 CF_struct.IHC_coeffs(mic).rest_output * zeros(CF_struct.n_ch, 1);
tom@455 83 CF_struct.IHC_state(mic).ihc_accum = zeros(CF_struct.n_ch, 1);
tom@455 84 end
tom@455 85 end
tom@455 86
tom@455 87