annotate matlab/bmm/carfac/CARFAC_AGC_Step.m @ 476:b084aed83e87

Simplify plotting mess in CARFAC_Run
author dicklyon@google.com
date Thu, 22 Mar 2012 23:06:46 +0000
parents 52f659be9008
children 056df17e0898
rev   line source
dicklyon@473 1 % Copyright 2012, Google, Inc.
dicklyon@473 2 % Author: Richard F. Lyon
dicklyon@473 3 %
dicklyon@473 4 % This Matlab file is part of an implementation of Lyon's cochlear model:
dicklyon@473 5 % "Cascade of Asymmetric Resonators with Fast-Acting Compression"
dicklyon@473 6 % to supplement Lyon's upcoming book "Human and Machine Hearing"
dicklyon@473 7 %
dicklyon@473 8 % Licensed under the Apache License, Version 2.0 (the "License");
dicklyon@473 9 % you may not use this file except in compliance with the License.
dicklyon@473 10 % You may obtain a copy of the License at
dicklyon@473 11 %
dicklyon@473 12 % http://www.apache.org/licenses/LICENSE-2.0
dicklyon@473 13 %
dicklyon@473 14 % Unless required by applicable law or agreed to in writing, software
dicklyon@473 15 % distributed under the License is distributed on an "AS IS" BASIS,
dicklyon@473 16 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dicklyon@473 17 % See the License for the specific language governing permissions and
dicklyon@473 18 % limitations under the License.
dicklyon@473 19
dicklyon@473 20 function [state, updated] = CARFAC_AGC_Step(AGC_coeffs, detects, state)
dicklyon@473 21 % function [state, updated] = CARFAC_AGC_Step(AGC_coeffs, detects, state)
dicklyon@473 22 %
dicklyon@473 23 % one time step (at decimated low AGC rate) of the AGC state update
dicklyon@473 24
dicklyon@473 25 n_ears = length(state);
dicklyon@473 26 [n_ch, n_AGC_stages] = size(state(1).AGC_memory); % number of channels
dicklyon@473 27
dicklyon@473 28 optimize_for_mono = n_ears == 1; % mono optimization
dicklyon@473 29
dicklyon@473 30 stage = 1;
dicklyon@473 31 ins = AGC_coeffs.detect_scale * detects;
dicklyon@473 32 [state, updated] = CARFAC_AGC_Recurse(AGC_coeffs, ins, n_AGC_stages, ...
dicklyon@473 33 n_ears, n_ch, optimize_for_mono, stage, state);
dicklyon@473 34
dicklyon@473 35
dicklyon@473 36
dicklyon@473 37
dicklyon@473 38
dicklyon@473 39 function [state, updated] = CARFAC_AGC_Recurse(coeffs, ins, n_stages, ...
dicklyon@473 40 n_ears, n_ch, mono, stage, state)
dicklyon@473 41 % function [state, updated = CARFAC_AGC_Recurse(coeffs, ins, n_stages, ...
dicklyon@473 42 % n_ears, n_ch, mono, stage, state)
dicklyon@473 43
dicklyon@473 44 decim = coeffs.decimation(stage); % decim phase for this stage
dicklyon@473 45 decim_phase = mod(state(1).decim_phase(stage) + 1, decim);
dicklyon@473 46 state(1).decim_phase(stage) = decim_phase;
dicklyon@473 47
dicklyon@473 48 % accumulate input for this stage from detect or previous stage:
dicklyon@473 49 for ear = 1:n_ears
dicklyon@473 50 state(ear).input_accum(:, stage) = ...
dicklyon@473 51 state(ear).input_accum(:, stage) + ins(:, ear);
dicklyon@473 52 end
dicklyon@473 53
dicklyon@473 54 % nothing else to do if it's not the right decim_phase
dicklyon@473 55 if decim_phase == 0
dicklyon@473 56 % do lots of work, at decimated rate
dicklyon@473 57
dicklyon@473 58 % decimated inputs for this stage, and to be decimated more for next:
dicklyon@473 59 for ear = 1:n_ears
dicklyon@473 60 ins(:,ear) = state(ear).input_accum(:, stage) / decim;
dicklyon@473 61 state(ear).input_accum(:, stage) = 0; % reset accumulator
dicklyon@473 62 end
dicklyon@473 63
dicklyon@473 64 if stage < n_stages % recurse to evaluate next stage(s)
dicklyon@473 65 state = CARFAC_AGC_Recurse(coeffs, ins, n_stages, ...
dicklyon@473 66 n_ears, n_ch, mono, stage+1, state);
dicklyon@473 67 end
dicklyon@473 68
dicklyon@473 69 epsilon = coeffs.AGC_epsilon(stage); % for this stage's LPF pole
dicklyon@473 70 stage_gain = coeffs.AGC_stage_gain;
dicklyon@473 71
dicklyon@473 72 for ear = 1:n_ears
dicklyon@473 73 AGC_in = ins(:,ear); % the newly decimated input for this ear
dicklyon@475 74
dicklyon@473 75 % add the latest output (state) of next stage...
dicklyon@473 76 if stage < n_stages
dicklyon@473 77 AGC_in = AGC_in + stage_gain * state(ear).AGC_memory(:, stage+1);
dicklyon@473 78 end
dicklyon@473 79
dicklyon@473 80 AGC_stage_state = state(ear).AGC_memory(:, stage);
dicklyon@473 81 % first-order recursive smoothing filter update, in time:
dicklyon@473 82 AGC_stage_state = AGC_stage_state + ...
dicklyon@473 83 epsilon * (AGC_in - AGC_stage_state);
dicklyon@473 84 % spatial smooth:
dicklyon@473 85 AGC_stage_state = ...
dicklyon@473 86 CARFAC_Spatial_Smooth(coeffs, stage, AGC_stage_state);
dicklyon@473 87 % and store the state back (in C++, do it all in place?)
dicklyon@473 88 state(ear).AGC_memory(:, stage) = AGC_stage_state;
dicklyon@473 89
dicklyon@473 90 if ~mono
dicklyon@473 91 if ear == 1
dicklyon@473 92 this_stage_sum = AGC_stage_state;
dicklyon@473 93 else
dicklyon@473 94 this_stage_sum = this_stage_sum + AGC_stage_state;
dicklyon@473 95 end
dicklyon@473 96 end
dicklyon@473 97 end
dicklyon@473 98 if ~mono
dicklyon@473 99 mix_coeff = coeffs.AGC_mix_coeffs(stage);
dicklyon@473 100 if mix_coeff > 0
dicklyon@473 101 this_stage_mean = this_stage_sum / n_ears;
dicklyon@473 102 for ear = 1:n_ears
dicklyon@473 103 state(ear).AGC_memory(:, stage) = ...
dicklyon@473 104 state(ear).AGC_memory(:, stage) + ...
dicklyon@473 105 mix_coeff * ...
dicklyon@473 106 (this_stage_mean - state(ear).AGC_memory(:, stage));
dicklyon@473 107 end
dicklyon@473 108 end
dicklyon@473 109 end
dicklyon@473 110 updated = 1; % bool to say we have new state
dicklyon@473 111 else
dicklyon@473 112 updated = 0;
dicklyon@473 113 end