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@536
|
31 n_ears = 1; % monaural default
|
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
|
dicklyon@534
|
58 function state = CAR_Init_State(coeffs)
|
dicklyon@534
|
59 n_ch = coeffs.n_ch;
|
dicklyon@534
|
60 state = struct( ...
|
dicklyon@534
|
61 'z1_memory', zeros(n_ch, 1), ...
|
dicklyon@534
|
62 'z2_memory', zeros(n_ch, 1), ...
|
dicklyon@534
|
63 'zA_memory', zeros(n_ch, 1), ...
|
dicklyon@534
|
64 'zB_memory', zeros(n_ch, 1), ...
|
dicklyon@534
|
65 'dzB_memory', zeros(n_ch, 1), ...
|
dicklyon@534
|
66 'zY_memory', zeros(n_ch, 1), ...
|
dicklyon@534
|
67 'g_memory', coeffs.g0_coeffs, ...
|
dicklyon@534
|
68 'dg_memory', zeros(n_ch, 1) ...
|
dicklyon@534
|
69 );
|
dicklyon@534
|
70
|
dicklyon@534
|
71
|
dicklyon@534
|
72 function state = AGC_Init_State(coeffs)
|
dicklyon@534
|
73 n_ch = coeffs.n_ch;
|
dicklyon@534
|
74 n_AGC_stages = coeffs.n_AGC_stages;
|
dicklyon@534
|
75 state = struct( ...
|
dicklyon@534
|
76 'AGC_memory', zeros(n_ch, n_AGC_stages), ...
|
dicklyon@534
|
77 'input_accum', zeros(n_ch, n_AGC_stages), ...
|
dicklyon@534
|
78 'decim_phase', zeros(n_AGC_stages, 1) ... % integer decimator phase
|
dicklyon@534
|
79 );
|
dicklyon@534
|
80
|
dicklyon@534
|
81
|
dicklyon@534
|
82 function state = IHC_Init_State(coeffs)
|
dicklyon@534
|
83 n_ch = coeffs.n_ch;
|
dicklyon@556
|
84 if coeffs.just_hwr
|
dicklyon@556
|
85 state = struct('ihc_accum', zeros(n_ch, 1));
|
dicklyon@556
|
86 else
|
dicklyon@556
|
87 if coeffs.one_cap
|
dicklyon@556
|
88 state = struct( ...
|
dicklyon@556
|
89 'ihc_accum', zeros(n_ch, 1), ...
|
dicklyon@556
|
90 'cap_voltage', coeffs.rest_cap * ones(n_ch, 1), ...
|
dicklyon@556
|
91 'lpf1_state', coeffs.rest_output * ones(n_ch, 1), ...
|
dicklyon@556
|
92 'lpf2_state', coeffs.rest_output * ones(n_ch, 1) ...
|
dicklyon@556
|
93 );
|
dicklyon@556
|
94 else
|
dicklyon@556
|
95 state = struct( ...
|
dicklyon@556
|
96 'ihc_accum', zeros(n_ch, 1), ...
|
dicklyon@556
|
97 'cap1_voltage', coeffs.rest_cap1 * ones(n_ch, 1), ...
|
dicklyon@556
|
98 'cap2_voltage', coeffs.rest_cap2* ones(n_ch, 1), ...
|
dicklyon@556
|
99 'lpf1_state', coeffs.rest_output * ones(n_ch, 1), ...
|
dicklyon@556
|
100 'lpf2_state', coeffs.rest_output * ones(n_ch, 1) ...
|
dicklyon@556
|
101 );
|
dicklyon@556
|
102 end
|
dicklyon@556
|
103 end
|
dicklyon@534
|
104
|
dicklyon@556
|
105
|
dicklyon@556
|
106
|