annotate matlab/bmm/carfac/CARFAC_AGC_Step.m @ 623:b3118c9ed67f

Turn the AGC coeffs inside out: array of structs instead of a struct of little arrays. In C++ use a vector<AGC_coeffs> for this; each of 4 stages has an entry; many fewer places need to do indexing by stage, and this removes the temptation to use little eigen arrays for the 4 stages. Also latest version of experimental log-lag SAI hacks.
author dicklyon@google.com
date Tue, 21 May 2013 04:24:05 +0000
parents a0869cb1c99b
children
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@504 20 function [state, updated] = CARFAC_AGC_Step(detects, coeffs, state)
dicklyon@504 21 % function [state, updated] = CARFAC_AGC_Step(detects, coeffs, state)
dicklyon@473 22 %
dicklyon@498 23 % one time step of the AGC state update; decimates internally
dicklyon@473 24
dicklyon@473 25 stage = 1;
dicklyon@623 26 AGC_in = coeffs(1).detect_scale * detects;
dicklyon@498 27 [state, updated] = CARFAC_AGC_Recurse(coeffs, AGC_in, stage, state);
dicklyon@473 28
dicklyon@473 29
dicklyon@498 30 function [state, updated] = CARFAC_AGC_Recurse(coeffs, AGC_in, ...
dicklyon@498 31 stage, state)
dicklyon@498 32 % function [state, updated] = CARFAC_AGC_Recurse(coeffs, AGC_in, ...
dicklyon@498 33 % stage, state)
dicklyon@473 34
dicklyon@498 35 % decim factor for this stage, relative to input or prev. stage:
dicklyon@623 36 decim = coeffs(stage).decimation;
dicklyon@498 37 % decim phase of this stage (do work on phase 0 only):
dicklyon@623 38 decim_phase = mod(state(stage).decim_phase + 1, decim);
dicklyon@623 39 state(stage).decim_phase = decim_phase;
dicklyon@473 40
dicklyon@473 41 % accumulate input for this stage from detect or previous stage:
dicklyon@623 42 state(stage).input_accum = state(stage).input_accum + AGC_in;
dicklyon@473 43
dicklyon@473 44 % nothing else to do if it's not the right decim_phase
dicklyon@473 45 if decim_phase == 0
dicklyon@498 46 % do lots of work, at decimated rate.
dicklyon@498 47 % decimated inputs for this stage, and to be decimated more for next:
dicklyon@623 48 AGC_in = state(stage).input_accum / decim;
dicklyon@623 49 state(stage).input_accum(:) = 0; % reset accumulator
dicklyon@473 50
dicklyon@623 51 if stage < coeffs(1).n_AGC_stages
dicklyon@498 52 state = CARFAC_AGC_Recurse(coeffs, AGC_in, stage+1, state);
dicklyon@498 53 % and add its output to this stage input, whether it updated or not:
dicklyon@623 54 AGC_in = AGC_in + ...
dicklyon@623 55 coeffs(stage).AGC_stage_gain * state(stage + 1).AGC_memory;
dicklyon@473 56 end
dicklyon@473 57
dicklyon@623 58 AGC_stage_state = state(stage).AGC_memory;
dicklyon@498 59 % first-order recursive smoothing filter update, in time:
dicklyon@498 60 AGC_stage_state = AGC_stage_state + ...
dicklyon@623 61 coeffs(stage).AGC_epsilon * (AGC_in - AGC_stage_state);
dicklyon@498 62 % spatial smooth:
dicklyon@498 63 AGC_stage_state = ...
dicklyon@623 64 CARFAC_Spatial_Smooth(coeffs(stage), AGC_stage_state);
dicklyon@498 65 % and store the state back (in C++, do it all in place?)
dicklyon@623 66 state(stage).AGC_memory = AGC_stage_state;
dicklyon@473 67
dicklyon@473 68 updated = 1; % bool to say we have new state
dicklyon@473 69 else
dicklyon@473 70 updated = 0;
dicklyon@473 71 end