annotate matlab/bmm/carfac/CARFAC_AGCStep.m @ 460:2e7ebca0d28e

Final automation steps for starting the server.
author tom@acousticscale.org
date Wed, 22 Feb 2012 14:09:47 +0000
parents f8ba7ad93fa9
children 87699cb4cf71
rev   line source
tom@455 1 % Copyright 2012, Google, Inc.
tom@455 2 % Author: Richard F. Lyon
tom@455 3 %
tom@455 4 % This Matlab file is part of an implementation of Lyon's cochlear model:
tom@455 5 % "Cascade of Asymmetric Resonators with Fast-Acting Compression"
tom@455 6 % to supplement Lyon's upcoming book "Human and Machine Hearing"
tom@455 7 %
tom@455 8 % Licensed under the Apache License, Version 2.0 (the "License");
tom@455 9 % you may not use this file except in compliance with the License.
tom@455 10 % You may obtain a copy of the License at
tom@455 11 %
tom@455 12 % http://www.apache.org/licenses/LICENSE-2.0
tom@455 13 %
tom@455 14 % Unless required by applicable law or agreed to in writing, software
tom@455 15 % distributed under the License is distributed on an "AS IS" BASIS,
tom@455 16 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tom@455 17 % See the License for the specific language governing permissions and
tom@455 18 % limitations under the License.
tom@455 19
tom@455 20 function state = CARFAC_AGCStep(AGC_coeffs, avg_detects, state)
tom@455 21 % function state = CARFAC_AGCStep(AGC_coeffs, avg_detects, state)
tom@455 22 %
tom@455 23 % one time step (at decimated low AGC rate) of the AGC state update
tom@455 24
tom@455 25 n_AGC_stages = length(AGC_coeffs.AGC_epsilon);
tom@455 26 n_mics = length(state);
tom@455 27 n_ch = size(state(1).AGC_sum, 1); % number of channels
tom@455 28
tom@455 29 optimize_for_mono = n_mics == 1; % mono optimization
tom@455 30 if ~optimize_for_mono
tom@455 31 stage_sum = zeros(n_ch, 1);
tom@455 32 end
tom@455 33
tom@455 34 for stage = 1:n_AGC_stages
tom@455 35 if ~optimize_for_mono % skip if mono
tom@455 36 if stage > 1
tom@455 37 prev_stage_mean = stage_sum / n_mics;
tom@455 38 end
tom@455 39 stage_sum(:) = 0; % sum accumulating over mics at this stage
tom@455 40 end
tom@455 41 epsilon = AGC_coeffs.AGC_epsilon(stage); % for this stage's LPF pole
tom@455 42 polez1 = AGC_coeffs.AGC1_polez(stage);
tom@455 43 polez2 = AGC_coeffs.AGC2_polez(stage);
tom@455 44 for mic = 1:n_mics
tom@455 45 if stage == 1
tom@455 46 AGC_in = AGC_coeffs.detect_scale * avg_detects(:,mic);
tom@455 47 AGC_in = max(0, AGC_in); % don't let neg inputs in
tom@455 48 else
tom@455 49 % prev. stage mixed with prev_stage_sum
tom@455 50 if optimize_for_mono
tom@455 51 % Mono optimization ignores AGC_mix_coeff,
tom@455 52 % assuming all(prev_stage_mean == AGC_memory(:, stage - 1));
tom@455 53 % but we also don't even allocate or compute the sum or mean.
tom@455 54 AGC_in = AGC_coeffs.AGC_stage_gain * ...
tom@455 55 state(mic).AGC_memory(:, stage - 1);
tom@455 56 else
tom@455 57 AGC_in = AGC_coeffs.AGC_stage_gain * ...
tom@455 58 (AGC_coeffs.AGC_mix_coeff * prev_stage_mean + ...
tom@455 59 (1 - AGC_coeffs.AGC_mix_coeff) * ...
tom@455 60 state(mic).AGC_memory(:, stage - 1));
tom@455 61 end
tom@455 62 end
tom@455 63 AGC_stage = state(mic).AGC_memory(:, stage);
tom@455 64 % first-order recursive smooting filter update:
tom@455 65 AGC_stage = AGC_stage + epsilon * (AGC_in - AGC_stage);
tom@455 66
tom@455 67 % spatially spread it; using diffusion coeffs like in smooth1d
tom@455 68 AGC_stage = SmoothDoubleExponential(AGC_stage, polez1, polez2);
tom@455 69
tom@455 70 state(mic).AGC_memory(:, stage) = AGC_stage;
tom@455 71 if stage == 1
tom@455 72 state(mic).sum_AGC = AGC_stage;
tom@455 73 else
tom@455 74 state(mic).sum_AGC = state(mic).sum_AGC + AGC_stage;
tom@455 75 end
tom@455 76 if ~optimize_for_mono
tom@455 77 stage_sum = stage_sum + AGC_stage;
tom@455 78 end
tom@455 79 end
tom@455 80 end
tom@455 81
tom@455 82