tom@513: % Copyright 2012 Google Inc. All Rights Reserved. dicklyon@462: % Author Richard F. Lyon tom@455: % tom@455: % This Matlab file is part of an implementation of Lyon's cochlear model: tom@455: % "Cascade of Asymmetric Resonators with Fast-Acting Compression" tom@455: % to supplement Lyon's upcoming book "Human and Machine Hearing" tom@455: % tom@455: % Licensed under the Apache License, Version 2.0 (the "License"); tom@455: % you may not use this file except in compliance with the License. tom@455: % You may obtain a copy of the License at tom@455: % tom@455: % http://www.apache.org/licenses/LICENSE-2.0 tom@455: % tom@455: % Unless required by applicable law or agreed to in writing, software tom@455: % distributed under the License is distributed on an "AS IS" BASIS, tom@455: % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. tom@455: % See the License for the specific language governing permissions and tom@455: % limitations under the License. tom@455: dicklyon@502: function [CF, decim_naps, naps, BM, ohc, agc] = CARFAC_Run ... tom@455: (CF, input_waves, AGC_plot_fig_num) dicklyon@502: % function [CF, decim_naps, naps, BM, ohc, agc] = CARFAC_Run ... dicklyon@462: % (CF, input_waves, AGC_plot_fig_num) tom@455: % This function runs the CARFAC; that is, filters a 1 or more channel tom@455: % sound input to make one or more neural activity patterns (naps). tom@455: % tom@455: % The CF struct holds the filterbank design and state; if you want to tom@455: % break the input up into segments, you need to use the updated CF tom@455: % to keep the state between segments. tom@455: % tom@455: % input_waves is a column vector if there's just one audio channel; tom@455: % more generally, it has a row per time sample, a column per audio channel. tom@455: % tom@455: % naps has a row per time sample, a column per filterbank channel, and tom@455: % a layer per audio channel if more than 1. tom@455: % decim_naps is like naps but time-decimated by the int CF.decimation. tom@455: % tom@455: % the input_waves are assumed to be sampled at the same rate as the tom@455: % CARFAC is designed for; a resampling may be needed before calling this. tom@455: % dicklyon@502: % ohc and agc are optional extra outputs for diagnosing internals. tom@455: dicklyon@473: [n_samp, n_ears] = size(input_waves); tom@455: n_ch = CF.n_ch; tom@455: tom@455: if nargin < 3 tom@455: AGC_plot_fig_num = 0; tom@455: end tom@455: dicklyon@475: if nargout > 3 dicklyon@475: BM = zeros(n_samp, n_ch, n_ears); dicklyon@475: else dicklyon@475: BM = []; dicklyon@475: end dicklyon@475: dicklyon@502: if nargout > 4 dicklyon@502: ohc = zeros(n_samp, n_ch, n_ears); dicklyon@502: else dicklyon@502: ohc = []; dicklyon@502: end dicklyon@502: dicklyon@502: if nargout > 5 dicklyon@502: agc = zeros(n_samp, n_ch, n_ears); dicklyon@502: else dicklyon@502: agc = []; dicklyon@502: end dicklyon@502: dicklyon@473: if n_ears ~= CF.n_ears tom@455: error('bad number of input_waves channels passed to CARFAC_Run') tom@455: end tom@455: dicklyon@473: dicklyon@473: naps = zeros(n_samp, n_ch, n_ears); dicklyon@473: dicklyon@504: seglen = 441; % anything should work; this is 20 ms at default fs dicklyon@473: n_segs = ceil(n_samp / seglen); dicklyon@473: dicklyon@473: if nargout > 1 tom@455: % make decimated detect output: dicklyon@473: decim_naps = zeros(n_segs, CF.n_ch, CF.n_ears); tom@455: else tom@455: decim_naps = []; tom@455: end tom@455: dicklyon@473: if nargout > 2 dicklyon@473: % make decimated detect output: dicklyon@473: naps = zeros(n_samp, CF.n_ch, CF.n_ears); dicklyon@473: else dicklyon@473: naps = []; dicklyon@473: end tom@455: dicklyon@473: for seg_num = 1:n_segs dicklyon@473: if seg_num == n_segs dicklyon@473: % The last segement may be short of seglen, but do it anyway: dicklyon@473: k_range = (seglen*(seg_num - 1) + 1):n_samp; dicklyon@473: else dicklyon@473: k_range = seglen*(seg_num - 1) + (1:seglen); tom@455: end dicklyon@473: % Process a segment to get a slice of decim_naps, and plot AGC state: dicklyon@475: if ~isempty(BM) dicklyon@502: % ask for everything in this case, for laziness: dicklyon@502: [seg_naps, CF, seg_BM, seg_ohc, seg_agc] = CARFAC_Run_Segment(CF, input_waves(k_range, :)); dicklyon@475: else dicklyon@475: [seg_naps, CF] = CARFAC_Run_Segment(CF, input_waves(k_range, :)); dicklyon@475: end dicklyon@475: dicklyon@475: if ~isempty(BM) dicklyon@475: for ear = 1:n_ears dicklyon@475: % Accumulate segment BM to make full BM dicklyon@475: BM(k_range, :, ear) = seg_BM(:, :, ear); dicklyon@475: end dicklyon@475: end dicklyon@473: dicklyon@473: if ~isempty(naps) dicklyon@473: for ear = 1:n_ears dicklyon@473: % Accumulate segment naps to make full naps dicklyon@473: naps(k_range, :, ear) = seg_naps(:, :, ear); tom@455: end dicklyon@462: end dicklyon@462: dicklyon@502: if ~isempty(ohc) dicklyon@502: for ear = 1:n_ears dicklyon@502: % Accumulate segment naps to make full naps dicklyon@502: ohc(k_range, :, ear) = seg_ohc(:, :, ear); dicklyon@502: end dicklyon@502: end dicklyon@502: dicklyon@502: if ~isempty(agc) dicklyon@502: for ear = 1:n_ears dicklyon@502: % Accumulate segment naps to make full naps dicklyon@502: agc(k_range, :, ear) = seg_agc(:, :, ear); dicklyon@502: end dicklyon@502: end dicklyon@502: dicklyon@473: if ~isempty(decim_naps) dicklyon@473: for ear = 1:n_ears dicklyon@500: decim_naps(seg_num, :, ear) = CF.ears(ear).IHC_state.ihc_accum / seglen; dicklyon@500: CF.ears(ear).IHC_state.ihc_accum = zeros(n_ch,1); dicklyon@473: end tom@455: end dicklyon@462: dicklyon@473: if AGC_plot_fig_num dicklyon@462: figure(AGC_plot_fig_num); hold off; clf dicklyon@473: for ear = 1:n_ears dicklyon@500: maxes(ear) = max(CF.ears(ear).AGC_state.AGC_memory(:)); dicklyon@462: hold on dicklyon@476: for stage = 1:4; dicklyon@500: plot(2^(stage-1) * CF.ears(ear).AGC_state.AGC_memory(:, stage)); dicklyon@462: end dicklyon@462: end dicklyon@475: axis([0, CF.n_ch+1, 0.0, max(maxes) * 1.01 + 0.002]); dicklyon@462: drawnow dicklyon@462: end dicklyon@473: tom@455: end tom@455: dicklyon@473: dicklyon@473: