annotate matlab/bmm/carfac/CARFAC_Run_Segment.m @ 473:b4da807f4318

Add CARFAC_Design_Doc.txt, CARFAC_Run_Segment.m, and some renames; rename various variables to be more parallel; clean up init code and such.
author dicklyon@google.com
date Fri, 16 Mar 2012 04:19:24 +0000
parents
children 52f659be9008
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 [naps, CF] = CARFAC_Run_Segment(CF, input_waves)
dicklyon@473 21 % function [naps, CF, decim_naps] = CARFAC_Run_Segment(CF, input_waves)
dicklyon@473 22 %
dicklyon@473 23 % This function runs the CARFAC; that is, filters a 1 or more channel
dicklyon@473 24 % sound input segment to make one or more neural activity patterns (naps);
dicklyon@473 25 % it can be called multiple times for successive segments of any length,
dicklyon@473 26 % as long as the returned CF with modified state is passed back in each
dicklyon@473 27 % time.
dicklyon@473 28 %
dicklyon@473 29 % input_waves is a column vector if there's just one audio channel;
dicklyon@473 30 % more generally, it has a row per time sample, a column per audio channel.
dicklyon@473 31 %
dicklyon@473 32 % naps has a row per time sample, a column per filterbank channel, and
dicklyon@473 33 % a layer per audio channel if more than 1.
dicklyon@473 34 % decim_naps is like naps but time-decimated by the int CF.decimation.
dicklyon@473 35 %
dicklyon@473 36 % the input_waves are assumed to be sampled at the same rate as the
dicklyon@473 37 % CARFAC is designed for; a resampling may be needed before calling this.
dicklyon@473 38 %
dicklyon@473 39 % The function works as an outer iteration on time, updating all the
dicklyon@473 40 % filters and AGC states concurrently, so that the different channels can
dicklyon@473 41 % interact easily. The inner loops are over filterbank channels, and
dicklyon@473 42 % this level should be kept efficient.
dicklyon@473 43 %
dicklyon@473 44 % See other functions for designing and characterizing the CARFAC:
dicklyon@473 45 % CF = CARFAC_Design(fs, CF_CAR_params, CF_AGC_params, n_ears)
dicklyon@473 46 % transfns = CARFAC_Transfer_Functions(CF, to_chans, from_chans)
dicklyon@473 47
dicklyon@473 48 [n_samp, n_ears] = size(input_waves);
dicklyon@473 49
dicklyon@473 50 if n_ears ~= CF.n_ears
dicklyon@473 51 error('bad number of input_waves channels passed to CARFAC_Run')
dicklyon@473 52 end
dicklyon@473 53
dicklyon@473 54 n_ch = CF.n_ch;
dicklyon@473 55 naps = zeros(n_samp, n_ch, n_ears); % allocate space for result
dicklyon@473 56
dicklyon@473 57 detects = zeros(n_ch, n_ears);
dicklyon@473 58 for k = 1:n_samp
dicklyon@473 59 % at each time step, possibly handle multiple channels
dicklyon@473 60 for ear = 1:n_ears
dicklyon@473 61 [car_out, CF.CAR_state(ear)] = CARFAC_CAR_Step( ...
dicklyon@473 62 input_waves(k, ear), CF.CAR_coeffs, CF.CAR_state(ear));
dicklyon@473 63
dicklyon@473 64 % update IHC state & output on every time step, too
dicklyon@473 65 [ihc_out, CF.IHC_state(ear)] = CARFAC_IHC_Step( ...
dicklyon@473 66 car_out, CF.IHC_coeffs, CF.IHC_state(ear));
dicklyon@473 67
dicklyon@473 68 detects(:, ear) = ihc_out; % for input to AGC, and out to SAI
dicklyon@473 69 naps(k, :, ear) = ihc_out; % output to neural activity pattern
dicklyon@473 70 end
dicklyon@473 71 % run the AGC update step, taking input from IHC_state,
dicklyon@473 72 % decimating internally, all ears at once due to mixing across them:
dicklyon@473 73 [CF.AGC_state, updated] = CARFAC_AGC_Step( ...
dicklyon@473 74 CF.AGC_coeffs, detects, CF.AGC_state);
dicklyon@473 75
dicklyon@473 76 % connect the feedback from AGC_state to CAR_state when it updates
dicklyon@473 77 if updated
dicklyon@473 78 CF = CARFAC_Close_AGC_Loop(CF);
dicklyon@473 79 end
dicklyon@473 80 end
dicklyon@473 81