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
|
tom@516
|
20 function state = CARFAC_AGCStep(AGC_coeffs, avg_detects, state)
|
tom@516
|
21 % function state = CARFAC_AGCStep(AGC_coeffs, avg_detects, state)
|
tom@516
|
22 %
|
tom@516
|
23 % one time step (at decimated low AGC rate) of the AGC state update
|
tom@516
|
24
|
tom@516
|
25 n_AGC_stages = length(AGC_coeffs.AGC_epsilon);
|
tom@516
|
26 n_mics = length(state);
|
tom@516
|
27 n_ch = size(state(1).AGC_sum, 1); % number of channels
|
tom@516
|
28
|
tom@516
|
29 optimize_for_mono = n_mics == 1; % mono optimization
|
tom@516
|
30 if ~optimize_for_mono
|
tom@516
|
31 stage_sum = zeros(n_ch, 1);
|
tom@516
|
32 end
|
tom@516
|
33
|
tom@516
|
34 for stage = 1:n_AGC_stages
|
tom@516
|
35 if ~optimize_for_mono % skip if mono
|
tom@516
|
36 if stage > 1
|
tom@516
|
37 prev_stage_mean = stage_sum / n_mics;
|
tom@516
|
38 end
|
tom@516
|
39 stage_sum(:) = 0; % sum accumulating over mics at this stage
|
tom@516
|
40 end
|
tom@516
|
41 epsilon = AGC_coeffs.AGC_epsilon(stage); % for this stage's LPF pole
|
tom@516
|
42 polez1 = AGC_coeffs.AGC1_polez(stage);
|
tom@516
|
43 polez2 = AGC_coeffs.AGC2_polez(stage);
|
tom@516
|
44 for mic = 1:n_mics
|
tom@516
|
45 if stage == 1
|
tom@516
|
46 AGC_in = AGC_coeffs.detect_scale * avg_detects(:,mic);
|
tom@516
|
47 AGC_in = max(0, AGC_in); % don't let neg inputs in
|
tom@516
|
48 else
|
tom@516
|
49 % prev. stage mixed with prev_stage_sum
|
tom@516
|
50 if optimize_for_mono
|
tom@516
|
51 % Mono optimization ignores AGC_mix_coeff,
|
tom@516
|
52 % assuming all(prev_stage_mean == AGC_memory(:, stage - 1));
|
tom@516
|
53 % but we also don't even allocate or compute the sum or mean.
|
tom@516
|
54 AGC_in = AGC_coeffs.AGC_stage_gain * ...
|
tom@516
|
55 state(mic).AGC_memory(:, stage - 1);
|
tom@516
|
56 else
|
tom@516
|
57 AGC_in = AGC_coeffs.AGC_stage_gain * ...
|
tom@516
|
58 (AGC_coeffs.AGC_mix_coeff * prev_stage_mean + ...
|
tom@516
|
59 (1 - AGC_coeffs.AGC_mix_coeff) * ...
|
tom@516
|
60 state(mic).AGC_memory(:, stage - 1));
|
tom@516
|
61 end
|
tom@516
|
62 end
|
tom@516
|
63 AGC_stage = state(mic).AGC_memory(:, stage);
|
tom@516
|
64 % first-order recursive smooting filter update:
|
tom@516
|
65 AGC_stage = AGC_stage + epsilon * (AGC_in - AGC_stage);
|
tom@516
|
66
|
tom@516
|
67 % spatially spread it; using diffusion coeffs like in smooth1d
|
tom@516
|
68 AGC_stage = SmoothDoubleExponential(AGC_stage, polez1, polez2);
|
tom@516
|
69
|
tom@516
|
70 state(mic).AGC_memory(:, stage) = AGC_stage;
|
tom@516
|
71 if stage == 1
|
tom@516
|
72 state(mic).sum_AGC = AGC_stage;
|
tom@516
|
73 else
|
tom@516
|
74 state(mic).sum_AGC = state(mic).sum_AGC + AGC_stage;
|
tom@516
|
75 end
|
tom@516
|
76 if ~optimize_for_mono
|
tom@516
|
77 stage_sum = stage_sum + AGC_stage;
|
tom@516
|
78 end
|
tom@516
|
79 end
|
tom@516
|
80 end
|
tom@516
|
81
|
tom@516
|
82
|