tom@516
|
1 % Copyright 2012, Google, Inc.
|
dicklyon@523
|
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@563
|
20 function [CF, decim_naps, naps, BM, ohc, agc] = CARFAC_Run ...
|
tom@516
|
21 (CF, input_waves, AGC_plot_fig_num)
|
dicklyon@563
|
22 % function [CF, decim_naps, naps, BM, ohc, agc] = CARFAC_Run ...
|
dicklyon@523
|
23 % (CF, input_waves, AGC_plot_fig_num)
|
tom@516
|
24 % This function runs the CARFAC; that is, filters a 1 or more channel
|
tom@516
|
25 % sound input to make one or more neural activity patterns (naps).
|
tom@516
|
26 %
|
tom@516
|
27 % The CF struct holds the filterbank design and state; if you want to
|
tom@516
|
28 % break the input up into segments, you need to use the updated CF
|
tom@516
|
29 % to keep the state between segments.
|
tom@516
|
30 %
|
tom@516
|
31 % input_waves is a column vector if there's just one audio channel;
|
tom@516
|
32 % more generally, it has a row per time sample, a column per audio channel.
|
tom@516
|
33 %
|
tom@516
|
34 % naps has a row per time sample, a column per filterbank channel, and
|
tom@516
|
35 % a layer per audio channel if more than 1.
|
tom@516
|
36 % decim_naps is like naps but time-decimated by the int CF.decimation.
|
tom@516
|
37 %
|
tom@516
|
38 % the input_waves are assumed to be sampled at the same rate as the
|
tom@516
|
39 % CARFAC is designed for; a resampling may be needed before calling this.
|
tom@516
|
40 %
|
dicklyon@563
|
41 % ohc and agc are optional extra outputs for diagnosing internals.
|
tom@516
|
42
|
dicklyon@534
|
43 [n_samp, n_ears] = size(input_waves);
|
tom@516
|
44 n_ch = CF.n_ch;
|
tom@516
|
45
|
tom@516
|
46 if nargin < 3
|
tom@516
|
47 AGC_plot_fig_num = 0;
|
tom@516
|
48 end
|
tom@516
|
49
|
dicklyon@536
|
50 if nargout > 3
|
dicklyon@536
|
51 BM = zeros(n_samp, n_ch, n_ears);
|
dicklyon@536
|
52 else
|
dicklyon@536
|
53 BM = [];
|
dicklyon@536
|
54 end
|
dicklyon@536
|
55
|
dicklyon@563
|
56 if nargout > 4
|
dicklyon@563
|
57 ohc = zeros(n_samp, n_ch, n_ears);
|
dicklyon@563
|
58 else
|
dicklyon@563
|
59 ohc = [];
|
dicklyon@563
|
60 end
|
dicklyon@563
|
61
|
dicklyon@563
|
62 if nargout > 5
|
dicklyon@563
|
63 agc = zeros(n_samp, n_ch, n_ears);
|
dicklyon@563
|
64 else
|
dicklyon@563
|
65 agc = [];
|
dicklyon@563
|
66 end
|
dicklyon@563
|
67
|
dicklyon@534
|
68 if n_ears ~= CF.n_ears
|
tom@516
|
69 error('bad number of input_waves channels passed to CARFAC_Run')
|
tom@516
|
70 end
|
tom@516
|
71
|
dicklyon@534
|
72
|
dicklyon@534
|
73 naps = zeros(n_samp, n_ch, n_ears);
|
dicklyon@534
|
74
|
dicklyon@565
|
75 seglen = 441; % anything should work; this is 20 ms at default fs
|
dicklyon@534
|
76 n_segs = ceil(n_samp / seglen);
|
dicklyon@534
|
77
|
dicklyon@534
|
78 if nargout > 1
|
tom@516
|
79 % make decimated detect output:
|
dicklyon@534
|
80 decim_naps = zeros(n_segs, CF.n_ch, CF.n_ears);
|
tom@516
|
81 else
|
tom@516
|
82 decim_naps = [];
|
tom@516
|
83 end
|
tom@516
|
84
|
dicklyon@534
|
85 if nargout > 2
|
dicklyon@534
|
86 % make decimated detect output:
|
dicklyon@534
|
87 naps = zeros(n_samp, CF.n_ch, CF.n_ears);
|
dicklyon@534
|
88 else
|
dicklyon@534
|
89 naps = [];
|
dicklyon@534
|
90 end
|
tom@516
|
91
|
dicklyon@534
|
92 for seg_num = 1:n_segs
|
dicklyon@534
|
93 if seg_num == n_segs
|
dicklyon@534
|
94 % The last segement may be short of seglen, but do it anyway:
|
dicklyon@534
|
95 k_range = (seglen*(seg_num - 1) + 1):n_samp;
|
dicklyon@534
|
96 else
|
dicklyon@534
|
97 k_range = seglen*(seg_num - 1) + (1:seglen);
|
tom@516
|
98 end
|
dicklyon@534
|
99 % Process a segment to get a slice of decim_naps, and plot AGC state:
|
dicklyon@536
|
100 if ~isempty(BM)
|
dicklyon@563
|
101 % ask for everything in this case, for laziness:
|
dicklyon@563
|
102 [seg_naps, CF, seg_BM, seg_ohc, seg_agc] = CARFAC_Run_Segment(CF, input_waves(k_range, :));
|
dicklyon@536
|
103 else
|
dicklyon@536
|
104 [seg_naps, CF] = CARFAC_Run_Segment(CF, input_waves(k_range, :));
|
dicklyon@536
|
105 end
|
dicklyon@536
|
106
|
dicklyon@536
|
107 if ~isempty(BM)
|
dicklyon@536
|
108 for ear = 1:n_ears
|
dicklyon@536
|
109 % Accumulate segment BM to make full BM
|
dicklyon@536
|
110 BM(k_range, :, ear) = seg_BM(:, :, ear);
|
dicklyon@536
|
111 end
|
dicklyon@536
|
112 end
|
dicklyon@534
|
113
|
dicklyon@534
|
114 if ~isempty(naps)
|
dicklyon@534
|
115 for ear = 1:n_ears
|
dicklyon@534
|
116 % Accumulate segment naps to make full naps
|
dicklyon@534
|
117 naps(k_range, :, ear) = seg_naps(:, :, ear);
|
tom@516
|
118 end
|
dicklyon@523
|
119 end
|
dicklyon@523
|
120
|
dicklyon@563
|
121 if ~isempty(ohc)
|
dicklyon@563
|
122 for ear = 1:n_ears
|
dicklyon@563
|
123 % Accumulate segment naps to make full naps
|
dicklyon@563
|
124 ohc(k_range, :, ear) = seg_ohc(:, :, ear);
|
dicklyon@563
|
125 end
|
dicklyon@563
|
126 end
|
dicklyon@563
|
127
|
dicklyon@563
|
128 if ~isempty(agc)
|
dicklyon@563
|
129 for ear = 1:n_ears
|
dicklyon@563
|
130 % Accumulate segment naps to make full naps
|
dicklyon@563
|
131 agc(k_range, :, ear) = seg_agc(:, :, ear);
|
dicklyon@563
|
132 end
|
dicklyon@563
|
133 end
|
dicklyon@563
|
134
|
dicklyon@534
|
135 if ~isempty(decim_naps)
|
dicklyon@534
|
136 for ear = 1:n_ears
|
dicklyon@561
|
137 decim_naps(seg_num, :, ear) = CF.ears(ear).IHC_state.ihc_accum / seglen;
|
dicklyon@561
|
138 CF.ears(ear).IHC_state.ihc_accum = zeros(n_ch,1);
|
dicklyon@534
|
139 end
|
tom@516
|
140 end
|
dicklyon@523
|
141
|
dicklyon@534
|
142 if AGC_plot_fig_num
|
dicklyon@523
|
143 figure(AGC_plot_fig_num); hold off; clf
|
dicklyon@534
|
144 for ear = 1:n_ears
|
dicklyon@561
|
145 maxes(ear) = max(CF.ears(ear).AGC_state.AGC_memory(:));
|
dicklyon@523
|
146 hold on
|
dicklyon@537
|
147 for stage = 1:4;
|
dicklyon@561
|
148 plot(2^(stage-1) * CF.ears(ear).AGC_state.AGC_memory(:, stage));
|
dicklyon@523
|
149 end
|
dicklyon@523
|
150 end
|
dicklyon@536
|
151 axis([0, CF.n_ch+1, 0.0, max(maxes) * 1.01 + 0.002]);
|
dicklyon@523
|
152 drawnow
|
dicklyon@523
|
153 end
|
dicklyon@534
|
154
|
tom@516
|
155 end
|
tom@516
|
156
|
dicklyon@534
|
157
|
dicklyon@534
|
158
|