dicklyon@473: % Copyright 2012, Google, Inc. dicklyon@473: % Author Richard F. Lyon dicklyon@473: % dicklyon@473: % This Matlab file is part of an implementation of Lyon's cochlear model: dicklyon@473: % "Cascade of Asymmetric Resonators with Fast-Acting Compression" dicklyon@473: % to supplement Lyon's upcoming book "Human and Machine Hearing" dicklyon@473: % dicklyon@473: % Licensed under the Apache License, Version 2.0 (the "License"); dicklyon@473: % you may not use this file except in compliance with the License. dicklyon@473: % You may obtain a copy of the License at dicklyon@473: % dicklyon@473: % http://www.apache.org/licenses/LICENSE-2.0 dicklyon@473: % dicklyon@473: % Unless required by applicable law or agreed to in writing, software dicklyon@473: % distributed under the License is distributed on an "AS IS" BASIS, dicklyon@473: % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. dicklyon@473: % See the License for the specific language governing permissions and dicklyon@473: % limitations under the License. dicklyon@473: dicklyon@473: function [naps, CF] = CARFAC_Run_Segment(CF, input_waves) dicklyon@473: % function [naps, CF, decim_naps] = CARFAC_Run_Segment(CF, input_waves) dicklyon@473: % dicklyon@473: % This function runs the CARFAC; that is, filters a 1 or more channel dicklyon@473: % sound input segment to make one or more neural activity patterns (naps); dicklyon@473: % it can be called multiple times for successive segments of any length, dicklyon@473: % as long as the returned CF with modified state is passed back in each dicklyon@473: % time. dicklyon@473: % dicklyon@473: % input_waves is a column vector if there's just one audio channel; dicklyon@473: % more generally, it has a row per time sample, a column per audio channel. dicklyon@473: % dicklyon@473: % naps has a row per time sample, a column per filterbank channel, and dicklyon@473: % a layer per audio channel if more than 1. dicklyon@473: % decim_naps is like naps but time-decimated by the int CF.decimation. dicklyon@473: % dicklyon@473: % the input_waves are assumed to be sampled at the same rate as the dicklyon@473: % CARFAC is designed for; a resampling may be needed before calling this. dicklyon@473: % dicklyon@473: % The function works as an outer iteration on time, updating all the dicklyon@473: % filters and AGC states concurrently, so that the different channels can dicklyon@473: % interact easily. The inner loops are over filterbank channels, and dicklyon@473: % this level should be kept efficient. dicklyon@473: % dicklyon@473: % See other functions for designing and characterizing the CARFAC: dicklyon@473: % CF = CARFAC_Design(fs, CF_CAR_params, CF_AGC_params, n_ears) dicklyon@473: % transfns = CARFAC_Transfer_Functions(CF, to_chans, from_chans) dicklyon@473: dicklyon@473: [n_samp, n_ears] = size(input_waves); dicklyon@473: dicklyon@473: if n_ears ~= CF.n_ears dicklyon@473: error('bad number of input_waves channels passed to CARFAC_Run') dicklyon@473: end dicklyon@473: dicklyon@473: n_ch = CF.n_ch; dicklyon@473: naps = zeros(n_samp, n_ch, n_ears); % allocate space for result dicklyon@473: dicklyon@473: detects = zeros(n_ch, n_ears); dicklyon@473: for k = 1:n_samp dicklyon@473: % at each time step, possibly handle multiple channels dicklyon@473: for ear = 1:n_ears dicklyon@473: [car_out, CF.CAR_state(ear)] = CARFAC_CAR_Step( ... dicklyon@473: input_waves(k, ear), CF.CAR_coeffs, CF.CAR_state(ear)); dicklyon@473: dicklyon@473: % update IHC state & output on every time step, too dicklyon@473: [ihc_out, CF.IHC_state(ear)] = CARFAC_IHC_Step( ... dicklyon@473: car_out, CF.IHC_coeffs, CF.IHC_state(ear)); dicklyon@473: dicklyon@473: detects(:, ear) = ihc_out; % for input to AGC, and out to SAI dicklyon@473: naps(k, :, ear) = ihc_out; % output to neural activity pattern dicklyon@473: end dicklyon@473: % run the AGC update step, taking input from IHC_state, dicklyon@473: % decimating internally, all ears at once due to mixing across them: dicklyon@473: [CF.AGC_state, updated] = CARFAC_AGC_Step( ... dicklyon@473: CF.AGC_coeffs, detects, CF.AGC_state); dicklyon@473: dicklyon@473: % connect the feedback from AGC_state to CAR_state when it updates dicklyon@473: if updated dicklyon@473: CF = CARFAC_Close_AGC_Loop(CF); dicklyon@473: end dicklyon@473: end dicklyon@473: