dicklyon@534: % Copyright 2012, Google, Inc. dicklyon@534: % Author Richard F. Lyon dicklyon@534: % dicklyon@534: % This Matlab file is part of an implementation of Lyon's cochlear model: dicklyon@534: % "Cascade of Asymmetric Resonators with Fast-Acting Compression" dicklyon@534: % to supplement Lyon's upcoming book "Human and Machine Hearing" dicklyon@534: % dicklyon@534: % Licensed under the Apache License, Version 2.0 (the "License"); dicklyon@534: % you may not use this file except in compliance with the License. dicklyon@534: % You may obtain a copy of the License at dicklyon@534: % dicklyon@534: % http://www.apache.org/licenses/LICENSE-2.0 dicklyon@534: % dicklyon@534: % Unless required by applicable law or agreed to in writing, software dicklyon@534: % distributed under the License is distributed on an "AS IS" BASIS, dicklyon@534: % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. dicklyon@534: % See the License for the specific language governing permissions and dicklyon@534: % limitations under the License. dicklyon@534: dicklyon@563: function [naps, CF, BM, seg_ohc, seg_agc] = CARFAC_Run_Segment(... dicklyon@563: CF, input_waves, open_loop) dicklyon@563: % function [naps, CF, BM, seg_ohc, seg_agc] = CARFAC_Run_Segment(... dicklyon@563: % CF, input_waves, open_loop) dicklyon@534: % dicklyon@534: % This function runs the CARFAC; that is, filters a 1 or more channel dicklyon@534: % sound input segment to make one or more neural activity patterns (naps); dicklyon@534: % it can be called multiple times for successive segments of any length, dicklyon@534: % as long as the returned CF with modified state is passed back in each dicklyon@534: % time. dicklyon@534: % dicklyon@534: % input_waves is a column vector if there's just one audio channel; dicklyon@534: % more generally, it has a row per time sample, a column per audio channel. dicklyon@534: % dicklyon@534: % naps has a row per time sample, a column per filterbank channel, and dicklyon@534: % a layer per audio channel if more than 1. dicklyon@536: % BM is basilar membrane motion (filter outputs before detection). dicklyon@534: % dicklyon@534: % the input_waves are assumed to be sampled at the same rate as the dicklyon@534: % CARFAC is designed for; a resampling may be needed before calling this. dicklyon@534: % dicklyon@534: % The function works as an outer iteration on time, updating all the dicklyon@534: % filters and AGC states concurrently, so that the different channels can dicklyon@534: % interact easily. The inner loops are over filterbank channels, and dicklyon@534: % this level should be kept efficient. dicklyon@534: % dicklyon@563: % seg_ohc seg_agc are optional extra outputs useful for seeing what the dicklyon@563: % ohc nonlinearity and agc are doing; both in terms of extra damping. dicklyon@534: dicklyon@536: if nargin < 3 dicklyon@536: open_loop = 0; dicklyon@536: end dicklyon@536: dicklyon@536: if nargout > 2 dicklyon@536: do_BM = 1; dicklyon@536: else dicklyon@536: do_BM = 0; dicklyon@536: end dicklyon@536: dicklyon@534: [n_samp, n_ears] = size(input_waves); dicklyon@534: dicklyon@534: if n_ears ~= CF.n_ears dicklyon@534: error('bad number of input_waves channels passed to CARFAC_Run') dicklyon@534: end dicklyon@534: dicklyon@534: n_ch = CF.n_ch; dicklyon@534: naps = zeros(n_samp, n_ch, n_ears); % allocate space for result dicklyon@536: if do_BM dicklyon@536: BM = zeros(n_samp, n_ch, n_ears); dicklyon@563: seg_ohc = zeros(n_samp, n_ch, n_ears); dicklyon@563: seg_agc = zeros(n_samp, n_ch, n_ears); dicklyon@536: end dicklyon@534: dicklyon@534: detects = zeros(n_ch, n_ears); dicklyon@534: for k = 1:n_samp dicklyon@534: % at each time step, possibly handle multiple channels dicklyon@534: for ear = 1:n_ears dicklyon@563: dicklyon@563: % This would be cleaner if we could just get and use a reference to dicklyon@563: % CF.ears(ear), but Matlab doesn't work that way... dicklyon@563: dicklyon@561: [car_out, CF.ears(ear).CAR_state] = CARFAC_CAR_Step( ... dicklyon@561: input_waves(k, ear), CF.ears(ear).CAR_coeffs, CF.ears(ear).CAR_state); dicklyon@534: dicklyon@534: % update IHC state & output on every time step, too dicklyon@561: [ihc_out, CF.ears(ear).IHC_state] = CARFAC_IHC_Step( ... dicklyon@561: car_out, CF.ears(ear).IHC_coeffs, CF.ears(ear).IHC_state); dicklyon@534: dicklyon@559: % run the AGC update step, decimating internally, dicklyon@561: [CF.ears(ear).AGC_state, updated] = CARFAC_AGC_Step( ... dicklyon@565: ihc_out, CF.ears(ear).AGC_coeffs, CF.ears(ear).AGC_state); dicklyon@559: dicklyon@559: % save some output data: dicklyon@559: naps(k, :, ear) = ihc_out; % output to neural activity pattern dicklyon@536: if do_BM dicklyon@536: BM(k, :, ear) = car_out; dicklyon@563: state = CF.ears(ear).CAR_state; dicklyon@563: seg_ohc(k, :, ear) = state.zA_memory; dicklyon@563: seg_agc(k, :, ear) = state.zB_memory;; dicklyon@536: end dicklyon@534: end dicklyon@534: dicklyon@559: % connect the feedback from AGC_state to CAR_state when it updates; dicklyon@559: % all ears together here due to mixing across them: dicklyon@559: if updated dicklyon@559: if n_ears > 1 dicklyon@559: % do multi-aural cross-coupling: dicklyon@561: CF.ears = CARFAC_Cross_Couple(CF.ears); dicklyon@559: end dicklyon@559: if ~open_loop dicklyon@559: CF = CARFAC_Close_AGC_Loop(CF); dicklyon@559: end dicklyon@534: end dicklyon@534: end dicklyon@534: