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@536
|
20 function [naps, CF, BM] = CARFAC_Run_Segment(CF, input_waves, open_loop)
|
dicklyon@536
|
21 % function [naps, CF, BM] = CARFAC_Run_Segment(CF, input_waves, open_loop)
|
dicklyon@534
|
22 %
|
dicklyon@534
|
23 % This function runs the CARFAC; that is, filters a 1 or more channel
|
dicklyon@534
|
24 % sound input segment to make one or more neural activity patterns (naps);
|
dicklyon@534
|
25 % it can be called multiple times for successive segments of any length,
|
dicklyon@534
|
26 % as long as the returned CF with modified state is passed back in each
|
dicklyon@534
|
27 % time.
|
dicklyon@534
|
28 %
|
dicklyon@534
|
29 % input_waves is a column vector if there's just one audio channel;
|
dicklyon@534
|
30 % more generally, it has a row per time sample, a column per audio channel.
|
dicklyon@534
|
31 %
|
dicklyon@534
|
32 % naps has a row per time sample, a column per filterbank channel, and
|
dicklyon@534
|
33 % a layer per audio channel if more than 1.
|
dicklyon@536
|
34 % BM is basilar membrane motion (filter outputs before detection).
|
dicklyon@534
|
35 %
|
dicklyon@534
|
36 % the input_waves are assumed to be sampled at the same rate as the
|
dicklyon@534
|
37 % CARFAC is designed for; a resampling may be needed before calling this.
|
dicklyon@534
|
38 %
|
dicklyon@534
|
39 % The function works as an outer iteration on time, updating all the
|
dicklyon@534
|
40 % filters and AGC states concurrently, so that the different channels can
|
dicklyon@534
|
41 % interact easily. The inner loops are over filterbank channels, and
|
dicklyon@534
|
42 % this level should be kept efficient.
|
dicklyon@534
|
43 %
|
dicklyon@534
|
44 % See other functions for designing and characterizing the CARFAC:
|
dicklyon@534
|
45 % CF = CARFAC_Design(fs, CF_CAR_params, CF_AGC_params, n_ears)
|
dicklyon@534
|
46 % transfns = CARFAC_Transfer_Functions(CF, to_chans, from_chans)
|
dicklyon@534
|
47
|
dicklyon@536
|
48 if nargin < 3
|
dicklyon@536
|
49 open_loop = 0;
|
dicklyon@536
|
50 end
|
dicklyon@536
|
51
|
dicklyon@536
|
52 if nargout > 2
|
dicklyon@536
|
53 do_BM = 1;
|
dicklyon@536
|
54 else
|
dicklyon@536
|
55 do_BM = 0;
|
dicklyon@536
|
56 end
|
dicklyon@536
|
57
|
dicklyon@534
|
58 [n_samp, n_ears] = size(input_waves);
|
dicklyon@534
|
59
|
dicklyon@534
|
60 if n_ears ~= CF.n_ears
|
dicklyon@534
|
61 error('bad number of input_waves channels passed to CARFAC_Run')
|
dicklyon@534
|
62 end
|
dicklyon@534
|
63
|
dicklyon@534
|
64 n_ch = CF.n_ch;
|
dicklyon@534
|
65 naps = zeros(n_samp, n_ch, n_ears); % allocate space for result
|
dicklyon@536
|
66 if do_BM
|
dicklyon@536
|
67 BM = zeros(n_samp, n_ch, n_ears);
|
dicklyon@536
|
68 end
|
dicklyon@534
|
69
|
dicklyon@534
|
70 detects = zeros(n_ch, n_ears);
|
dicklyon@534
|
71 for k = 1:n_samp
|
dicklyon@534
|
72 % at each time step, possibly handle multiple channels
|
dicklyon@534
|
73 for ear = 1:n_ears
|
dicklyon@561
|
74 [car_out, CF.ears(ear).CAR_state] = CARFAC_CAR_Step( ...
|
dicklyon@561
|
75 input_waves(k, ear), CF.ears(ear).CAR_coeffs, CF.ears(ear).CAR_state);
|
dicklyon@534
|
76
|
dicklyon@534
|
77 % update IHC state & output on every time step, too
|
dicklyon@561
|
78 [ihc_out, CF.ears(ear).IHC_state] = CARFAC_IHC_Step( ...
|
dicklyon@561
|
79 car_out, CF.ears(ear).IHC_coeffs, CF.ears(ear).IHC_state);
|
dicklyon@534
|
80
|
dicklyon@559
|
81 % run the AGC update step, decimating internally,
|
dicklyon@561
|
82 [CF.ears(ear).AGC_state, updated] = CARFAC_AGC_Step( ...
|
dicklyon@561
|
83 CF.ears(ear).AGC_coeffs, ihc_out, CF.ears(ear).AGC_state);
|
dicklyon@559
|
84
|
dicklyon@559
|
85 % save some output data:
|
dicklyon@559
|
86 naps(k, :, ear) = ihc_out; % output to neural activity pattern
|
dicklyon@536
|
87 if do_BM
|
dicklyon@536
|
88 BM(k, :, ear) = car_out;
|
dicklyon@536
|
89 end
|
dicklyon@534
|
90 end
|
dicklyon@534
|
91
|
dicklyon@559
|
92 % connect the feedback from AGC_state to CAR_state when it updates;
|
dicklyon@559
|
93 % all ears together here due to mixing across them:
|
dicklyon@559
|
94 if updated
|
dicklyon@559
|
95 if n_ears > 1
|
dicklyon@559
|
96 % do multi-aural cross-coupling:
|
dicklyon@561
|
97 CF.ears = CARFAC_Cross_Couple(CF.ears);
|
dicklyon@559
|
98 end
|
dicklyon@559
|
99 if ~open_loop
|
dicklyon@559
|
100 CF = CARFAC_Close_AGC_Loop(CF);
|
dicklyon@559
|
101 end
|
dicklyon@534
|
102 end
|
dicklyon@534
|
103 end
|
dicklyon@534
|
104
|