dicklyon@534
|
1 % Copyright 2012, Google, Inc.
|
dicklyon@534
|
2 % Author Richard F. Lyon
|
dicklyon@534
|
3 %
|
dicklyon@534
|
4 % This Matlab file is part of an implementation of Lyon's cochlear model:
|
dicklyon@534
|
5 % "Cascade of Asymmetric Resonators with Fast-Acting Compression"
|
dicklyon@534
|
6 % to supplement Lyon's upcoming book "Human and Machine Hearing"
|
dicklyon@534
|
7 %
|
dicklyon@534
|
8 % Licensed under the Apache License, Version 2.0 (the "License");
|
dicklyon@534
|
9 % you may not use this file except in compliance with the License.
|
dicklyon@534
|
10 % You may obtain a copy of the License at
|
dicklyon@534
|
11 %
|
dicklyon@534
|
12 % http://www.apache.org/licenses/LICENSE-2.0
|
dicklyon@534
|
13 %
|
dicklyon@534
|
14 % Unless required by applicable law or agreed to in writing, software
|
dicklyon@534
|
15 % distributed under the License is distributed on an "AS IS" BASIS,
|
dicklyon@534
|
16 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
dicklyon@534
|
17 % See the License for the specific language governing permissions and
|
dicklyon@534
|
18 % limitations under the License.
|
dicklyon@534
|
19
|
dicklyon@563
|
20 function [naps, CF, BM, seg_ohc, seg_agc] = CARFAC_Run_Segment(...
|
dicklyon@563
|
21 CF, input_waves, open_loop)
|
dicklyon@563
|
22 % function [naps, CF, BM, seg_ohc, seg_agc] = CARFAC_Run_Segment(...
|
dicklyon@563
|
23 % CF, input_waves, open_loop)
|
dicklyon@534
|
24 %
|
dicklyon@534
|
25 % This function runs the CARFAC; that is, filters a 1 or more channel
|
dicklyon@534
|
26 % sound input segment to make one or more neural activity patterns (naps);
|
dicklyon@534
|
27 % it can be called multiple times for successive segments of any length,
|
dicklyon@534
|
28 % as long as the returned CF with modified state is passed back in each
|
dicklyon@534
|
29 % time.
|
dicklyon@534
|
30 %
|
dicklyon@534
|
31 % input_waves is a column vector if there's just one audio channel;
|
dicklyon@534
|
32 % more generally, it has a row per time sample, a column per audio channel.
|
dicklyon@534
|
33 %
|
dicklyon@534
|
34 % naps has a row per time sample, a column per filterbank channel, and
|
dicklyon@534
|
35 % a layer per audio channel if more than 1.
|
dicklyon@536
|
36 % BM is basilar membrane motion (filter outputs before detection).
|
dicklyon@534
|
37 %
|
dicklyon@534
|
38 % the input_waves are assumed to be sampled at the same rate as the
|
dicklyon@534
|
39 % CARFAC is designed for; a resampling may be needed before calling this.
|
dicklyon@534
|
40 %
|
dicklyon@534
|
41 % The function works as an outer iteration on time, updating all the
|
dicklyon@534
|
42 % filters and AGC states concurrently, so that the different channels can
|
dicklyon@534
|
43 % interact easily. The inner loops are over filterbank channels, and
|
dicklyon@534
|
44 % this level should be kept efficient.
|
dicklyon@534
|
45 %
|
dicklyon@563
|
46 % seg_ohc seg_agc are optional extra outputs useful for seeing what the
|
dicklyon@563
|
47 % ohc nonlinearity and agc are doing; both in terms of extra damping.
|
dicklyon@534
|
48
|
dicklyon@536
|
49 if nargin < 3
|
dicklyon@536
|
50 open_loop = 0;
|
dicklyon@536
|
51 end
|
dicklyon@536
|
52
|
dicklyon@536
|
53 if nargout > 2
|
dicklyon@536
|
54 do_BM = 1;
|
dicklyon@536
|
55 else
|
dicklyon@536
|
56 do_BM = 0;
|
dicklyon@536
|
57 end
|
dicklyon@536
|
58
|
dicklyon@534
|
59 [n_samp, n_ears] = size(input_waves);
|
dicklyon@534
|
60
|
dicklyon@534
|
61 if n_ears ~= CF.n_ears
|
dicklyon@534
|
62 error('bad number of input_waves channels passed to CARFAC_Run')
|
dicklyon@534
|
63 end
|
dicklyon@534
|
64
|
dicklyon@534
|
65 n_ch = CF.n_ch;
|
dicklyon@534
|
66 naps = zeros(n_samp, n_ch, n_ears); % allocate space for result
|
dicklyon@536
|
67 if do_BM
|
dicklyon@536
|
68 BM = zeros(n_samp, n_ch, n_ears);
|
dicklyon@563
|
69 seg_ohc = zeros(n_samp, n_ch, n_ears);
|
dicklyon@563
|
70 seg_agc = zeros(n_samp, n_ch, n_ears);
|
dicklyon@536
|
71 end
|
dicklyon@534
|
72
|
dicklyon@534
|
73 detects = zeros(n_ch, n_ears);
|
dicklyon@534
|
74 for k = 1:n_samp
|
dicklyon@534
|
75 % at each time step, possibly handle multiple channels
|
dicklyon@534
|
76 for ear = 1:n_ears
|
dicklyon@563
|
77
|
dicklyon@563
|
78 % This would be cleaner if we could just get and use a reference to
|
dicklyon@563
|
79 % CF.ears(ear), but Matlab doesn't work that way...
|
dicklyon@563
|
80
|
dicklyon@561
|
81 [car_out, CF.ears(ear).CAR_state] = CARFAC_CAR_Step( ...
|
dicklyon@561
|
82 input_waves(k, ear), CF.ears(ear).CAR_coeffs, CF.ears(ear).CAR_state);
|
dicklyon@534
|
83
|
dicklyon@534
|
84 % update IHC state & output on every time step, too
|
dicklyon@561
|
85 [ihc_out, CF.ears(ear).IHC_state] = CARFAC_IHC_Step( ...
|
dicklyon@561
|
86 car_out, CF.ears(ear).IHC_coeffs, CF.ears(ear).IHC_state);
|
dicklyon@534
|
87
|
dicklyon@559
|
88 % run the AGC update step, decimating internally,
|
dicklyon@561
|
89 [CF.ears(ear).AGC_state, updated] = CARFAC_AGC_Step( ...
|
dicklyon@565
|
90 ihc_out, CF.ears(ear).AGC_coeffs, CF.ears(ear).AGC_state);
|
dicklyon@559
|
91
|
dicklyon@559
|
92 % save some output data:
|
dicklyon@559
|
93 naps(k, :, ear) = ihc_out; % output to neural activity pattern
|
dicklyon@536
|
94 if do_BM
|
dicklyon@536
|
95 BM(k, :, ear) = car_out;
|
dicklyon@563
|
96 state = CF.ears(ear).CAR_state;
|
dicklyon@563
|
97 seg_ohc(k, :, ear) = state.zA_memory;
|
dicklyon@563
|
98 seg_agc(k, :, ear) = state.zB_memory;;
|
dicklyon@536
|
99 end
|
dicklyon@534
|
100 end
|
dicklyon@534
|
101
|
dicklyon@559
|
102 % connect the feedback from AGC_state to CAR_state when it updates;
|
dicklyon@559
|
103 % all ears together here due to mixing across them:
|
dicklyon@559
|
104 if updated
|
dicklyon@559
|
105 if n_ears > 1
|
dicklyon@559
|
106 % do multi-aural cross-coupling:
|
dicklyon@561
|
107 CF.ears = CARFAC_Cross_Couple(CF.ears);
|
dicklyon@559
|
108 end
|
dicklyon@559
|
109 if ~open_loop
|
dicklyon@559
|
110 CF = CARFAC_Close_AGC_Loop(CF);
|
dicklyon@559
|
111 end
|
dicklyon@534
|
112 end
|
dicklyon@534
|
113 end
|
dicklyon@534
|
114
|