comparison trunk/matlab/bmm/carfac/CARFAC_Init.m @ 516:68c15d43fcc8

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 aa282a2b61bb
comparison
equal deleted inserted replaced
454:49b7b984e957 516:68c15d43fcc8
1 % Copyright 2012, Google, Inc.
2 % Author: Richard F. Lyon
3 %
4 % This Matlab file is part of an implementation of Lyon's cochlear model:
5 % "Cascade of Asymmetric Resonators with Fast-Acting Compression"
6 % to supplement Lyon's upcoming book "Human and Machine Hearing"
7 %
8 % Licensed under the Apache License, Version 2.0 (the "License");
9 % you may not use this file except in compliance with the License.
10 % You may obtain a copy of the License at
11 %
12 % http://www.apache.org/licenses/LICENSE-2.0
13 %
14 % Unless required by applicable law or agreed to in writing, software
15 % distributed under the License is distributed on an "AS IS" BASIS,
16 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 % See the License for the specific language governing permissions and
18 % limitations under the License.
19
20 function CF_struct = CARFAC_Init(CF_struct, n_mics)
21 % function CF_struct = CARFAC_Init(CF_struct, n_mics)
22 %
23 % Initialize state for n_mics channels (default 1).
24 % This allocates and zeros all the state vector storage in the CF_struct.
25
26 % TODO (dicklyon): Review whether storing state in the same struct as
27 % the design is a good thing, or whether we want another
28 % level of object. I like fewer structs and class types.
29
30 if nargin < 2
31 n_mics = 1; % monaural
32 end
33
34 % % this is probably what I'd do in the C++ version:
35 % if CF_struct.n_mics ~= n_mics;
36 % % free the state and make new number of channels
37 % % make a struct arrray, one element per mic channel, numbered:
38 % for k = 1:n_mics
39 % CF_struct.state(k) = struct('mic_number', k);
40 % end
41 % end
42 % But this code doesn't work because I don't understand struct arrays.
43
44 % For now I don't ever free anything if n_mics is reduced;
45 % so be sure to respect n_mics, not the size of the state struct array.
46
47 AGC_time_constants = CF_struct.AGC_params.time_constants;
48 n_AGC_stages = length(AGC_time_constants);
49
50 CF_struct.n_mics = n_mics;
51 CF_struct.k_mod_decim = 0; % time index phase, cumulative over segments
52
53 % This zeroing grows the struct array as needed:
54 for mic = 1:n_mics
55 CF_struct.filter_state(mic).z1_memory = zeros(CF_struct.n_ch, 1);
56 CF_struct.filter_state(mic).z2_memory = zeros(CF_struct.n_ch, 1);
57 % cubic loop
58 CF_struct.filter_state(mic).zA_memory = zeros(CF_struct.n_ch, 1);
59 % AGC interp
60 CF_struct.filter_state(mic).zB_memory = zeros(CF_struct.n_ch, 1);
61 % AGC incr
62 CF_struct.filter_state(mic).dzB_memory = zeros(CF_struct.n_ch, 1);
63 CF_struct.filter_state(mic).zY_memory = zeros(CF_struct.n_ch, 1);
64 CF_struct.filter_state(mic).detect_accum = zeros(CF_struct.n_ch, 1);
65 % AGC loop filters' state:
66 % HACK init
67 CF_struct.AGC_state(mic).AGC_memory = zeros(CF_struct.n_ch, n_AGC_stages);
68 CF_struct.AGC_state(mic).AGC_sum = zeros(CF_struct.n_ch, 1);
69 % IHC state:
70 if CF_struct.IHC_coeffs.just_hwr
71 CF_struct.IHC_state(mic).ihc_accum = zeros(CF_struct.n_ch, 1);
72 else
73 CF_struct.IHC_state(mic).cap_voltage = ...
74 CF_struct.IHC_coeffs(mic).rest_cap * ones(CF_struct.n_ch, 1);
75 CF_struct.IHC_state(mic).cap1_voltage = ...
76 CF_struct.IHC_coeffs(mic).rest_cap1 * ones(CF_struct.n_ch, 1);
77 CF_struct.IHC_state(mic).cap2_voltage = ...
78 CF_struct.IHC_coeffs(mic).rest_cap2 * ones(CF_struct.n_ch, 1);
79 CF_struct.IHC_state(mic).lpf1_state = ...
80 CF_struct.IHC_coeffs(mic).rest_output * zeros(CF_struct.n_ch, 1);
81 CF_struct.IHC_state(mic).lpf2_state = ...
82 CF_struct.IHC_coeffs(mic).rest_output * zeros(CF_struct.n_ch, 1);
83 CF_struct.IHC_state(mic).ihc_accum = zeros(CF_struct.n_ch, 1);
84 end
85 end
86
87