annotate matlab/bmm/carfac/CARFAC_Run_Segment.m @ 498:056df17e0898

Simplify AGC_Step, moving multi-aural cross coupling to new function CARFAC_Cross_Couple
author dicklyon@google.com
date Tue, 10 Apr 2012 05:40:18 +0000
parents 52f659be9008
children 896620d9d539
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@475 20 function [naps, CF, BM] = CARFAC_Run_Segment(CF, input_waves, open_loop)
dicklyon@475 21 % function [naps, CF, BM] = CARFAC_Run_Segment(CF, input_waves, open_loop)
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@475 34 % BM is basilar membrane motion (filter outputs before detection).
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@475 48 if nargin < 3
dicklyon@475 49 open_loop = 0;
dicklyon@475 50 end
dicklyon@475 51
dicklyon@475 52 if nargout > 2
dicklyon@475 53 do_BM = 1;
dicklyon@475 54 else
dicklyon@475 55 do_BM = 0;
dicklyon@475 56 end
dicklyon@475 57
dicklyon@473 58 [n_samp, n_ears] = size(input_waves);
dicklyon@473 59
dicklyon@473 60 if n_ears ~= CF.n_ears
dicklyon@473 61 error('bad number of input_waves channels passed to CARFAC_Run')
dicklyon@473 62 end
dicklyon@473 63
dicklyon@473 64 n_ch = CF.n_ch;
dicklyon@473 65 naps = zeros(n_samp, n_ch, n_ears); % allocate space for result
dicklyon@475 66 if do_BM
dicklyon@475 67 BM = zeros(n_samp, n_ch, n_ears);
dicklyon@475 68 end
dicklyon@473 69
dicklyon@473 70 detects = zeros(n_ch, n_ears);
dicklyon@473 71 for k = 1:n_samp
dicklyon@473 72 % at each time step, possibly handle multiple channels
dicklyon@473 73 for ear = 1:n_ears
dicklyon@473 74 [car_out, CF.CAR_state(ear)] = CARFAC_CAR_Step( ...
dicklyon@473 75 input_waves(k, ear), CF.CAR_coeffs, CF.CAR_state(ear));
dicklyon@473 76
dicklyon@473 77 % update IHC state & output on every time step, too
dicklyon@473 78 [ihc_out, CF.IHC_state(ear)] = CARFAC_IHC_Step( ...
dicklyon@473 79 car_out, CF.IHC_coeffs, CF.IHC_state(ear));
dicklyon@473 80
dicklyon@498 81 % run the AGC update step, decimating internally,
dicklyon@498 82 [CF.AGC_state(ear), updated] = CARFAC_AGC_Step( ...
dicklyon@498 83 CF.AGC_coeffs, ihc_out, CF.AGC_state(ear));
dicklyon@498 84
dicklyon@498 85 % save some output data:
dicklyon@498 86 naps(k, :, ear) = ihc_out; % output to neural activity pattern
dicklyon@475 87 if do_BM
dicklyon@475 88 BM(k, :, ear) = car_out;
dicklyon@475 89 end
dicklyon@473 90 end
dicklyon@473 91
dicklyon@498 92 % connect the feedback from AGC_state to CAR_state when it updates;
dicklyon@498 93 % all ears together here due to mixing across them:
dicklyon@498 94 if updated
dicklyon@498 95 if n_ears > 1
dicklyon@498 96 % do multi-aural cross-coupling:
dicklyon@498 97 CF.AGC_state = CARFAC_Cross_Couple(CF.AGC_coeffs, CF.AGC_state);
dicklyon@498 98 end
dicklyon@498 99 if ~open_loop
dicklyon@498 100 CF = CARFAC_Close_AGC_Loop(CF);
dicklyon@498 101 end
dicklyon@473 102 end
dicklyon@473 103 end
dicklyon@473 104