annotate trunk/matlab/bmm/carfac/CARFAC_Run.m @ 523:2b96cb7ea4f7

Major AGC improvements mostly
author dicklyon@google.com
date Thu, 01 Mar 2012 19:49:24 +0000
parents aa282a2b61bb
children 741187dc780f
rev   line source
tom@516 1 % Copyright 2012, Google, Inc.
dicklyon@523 2 % Author Richard F. Lyon
tom@516 3 %
tom@516 4 % This Matlab file is part of an implementation of Lyon's cochlear model:
tom@516 5 % "Cascade of Asymmetric Resonators with Fast-Acting Compression"
tom@516 6 % to supplement Lyon's upcoming book "Human and Machine Hearing"
tom@516 7 %
tom@516 8 % Licensed under the Apache License, Version 2.0 (the "License");
tom@516 9 % you may not use this file except in compliance with the License.
tom@516 10 % You may obtain a copy of the License at
tom@516 11 %
tom@516 12 % http://www.apache.org/licenses/LICENSE-2.0
tom@516 13 %
tom@516 14 % Unless required by applicable law or agreed to in writing, software
tom@516 15 % distributed under the License is distributed on an "AS IS" BASIS,
tom@516 16 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tom@516 17 % See the License for the specific language governing permissions and
tom@516 18 % limitations under the License.
tom@516 19
tom@516 20 function [naps, CF, decim_naps] = CARFAC_Run ...
tom@516 21 (CF, input_waves, AGC_plot_fig_num)
dicklyon@523 22 % function [naps, CF, decim_naps] = CARFAC_Run ...
dicklyon@523 23 % (CF, input_waves, AGC_plot_fig_num)
tom@516 24 % This function runs the CARFAC; that is, filters a 1 or more channel
tom@516 25 % sound input to make one or more neural activity patterns (naps).
tom@516 26 %
tom@516 27 % The CF struct holds the filterbank design and state; if you want to
tom@516 28 % break the input up into segments, you need to use the updated CF
tom@516 29 % to keep the state between segments.
tom@516 30 %
tom@516 31 % input_waves is a column vector if there's just one audio channel;
tom@516 32 % more generally, it has a row per time sample, a column per audio channel.
tom@516 33 %
tom@516 34 % naps has a row per time sample, a column per filterbank channel, and
tom@516 35 % a layer per audio channel if more than 1.
tom@516 36 % decim_naps is like naps but time-decimated by the int CF.decimation.
tom@516 37 %
tom@516 38 % the input_waves are assumed to be sampled at the same rate as the
tom@516 39 % CARFAC is designed for; a resampling may be needed before calling this.
tom@516 40 %
tom@516 41 % The function works as an outer iteration on time, updating all the
tom@516 42 % filters and AGC states concurrently, so that the different channels can
tom@516 43 % interact easily. The inner loops are over filterbank channels, and
tom@516 44 % this level should be kept efficient.
tom@516 45 %
tom@516 46 % See other functions for designing and characterizing the CARFAC:
tom@516 47 % CF = CARFAC_Design(fs, CF_filter_params, CF_AGC_params, n_mics)
tom@516 48 % transfns = CARFAC_Transfer_Functions(CF, to_chans, from_chans)
tom@516 49
tom@516 50 [n_samp, n_mics] = size(input_waves);
tom@516 51 n_ch = CF.n_ch;
tom@516 52
tom@516 53 if nargin < 3
tom@516 54 AGC_plot_fig_num = 0;
tom@516 55 end
tom@516 56
tom@516 57 if n_mics ~= CF.n_mics
tom@516 58 error('bad number of input_waves channels passed to CARFAC_Run')
tom@516 59 end
tom@516 60
dicklyon@523 61 % fastest decimated rate determines some interp needed:
dicklyon@523 62 decim1 = CF.AGC_params.decimation(1);
tom@516 63
tom@516 64 naps = zeros(n_samp, n_ch, n_mics);
dicklyon@523 65 decim_k = 0;
dicklyon@523 66 k_NAP_decim = 0;
dicklyon@523 67 NAP_decim = 8;
tom@516 68 if nargout > 2
tom@516 69 % make decimated detect output:
dicklyon@523 70 decim_naps = zeros(ceil(n_samp/NAP_decim), CF.n_ch, CF.n_mics);
tom@516 71 else
tom@516 72 decim_naps = [];
tom@516 73 end
tom@516 74
tom@516 75
dicklyon@523 76 k_AGC = 0;
dicklyon@523 77 AGC_plot_decim = 16; % how often to plot AGC state; TODO: use segments
tom@516 78
dicklyon@523 79
dicklyon@523 80 detects = zeros(n_ch, n_mics);
tom@516 81 for k = 1:n_samp
dicklyon@523 82 CF.k_mod_decim = mod(CF.k_mod_decim + 1, decim1); % global time phase
dicklyon@523 83 k_NAP_decim = mod(k_NAP_decim + 1, NAP_decim); % phase of decimated nap
tom@516 84 % at each time step, possibly handle multiple channels
tom@516 85 for mic = 1:n_mics
tom@516 86 [filters_out, CF.filter_state(mic)] = CARFAC_FilterStep( ...
tom@516 87 input_waves(k, mic), CF.filter_coeffs, CF.filter_state(mic));
dicklyon@523 88
tom@516 89 % update IHC state & output on every time step, too
tom@516 90 [ihc_out, CF.IHC_state(mic)] = CARFAC_IHCStep( ...
tom@516 91 filters_out, CF.IHC_coeffs, CF.IHC_state(mic));
dicklyon@523 92
dicklyon@523 93 detects(:, mic) = ihc_out; % for input to AGC, and out to SAI
dicklyon@523 94
tom@516 95 naps(k, :, mic) = ihc_out; % output to neural activity pattern
dicklyon@523 96
tom@516 97 end
dicklyon@523 98 if ~isempty(decim_naps) && (k_NAP_decim == 0)
dicklyon@523 99 decim_k = decim_k + 1; % index of decimated NAP
dicklyon@523 100 for mic = 1:n_mics
dicklyon@523 101 decim_naps(decim_k, :, mic) = CF.IHC_state(mic).ihc_accum / ...
dicklyon@523 102 NAP_decim; % for cochleagram
dicklyon@523 103 CF.IHC_state(mic).ihc_accum = zeros(n_ch,1);
tom@516 104 end
dicklyon@523 105 end
dicklyon@523 106 % run the AGC update step, taking input from IHC_state, decimating
dicklyon@523 107 % internally, all mics at once due to mixing across them:
dicklyon@523 108 [CF.AGC_state, updated] = ...
dicklyon@523 109 CARFAC_AGCStep(CF.AGC_coeffs, detects, CF.AGC_state);
dicklyon@523 110
dicklyon@523 111 % connect the feedback from AGC_state to filter_state when it updates
dicklyon@523 112 if updated
tom@516 113 for mic = 1:n_mics
dicklyon@523 114 new_damping = CF.AGC_state(mic).AGC_memory(:, 1); % stage 1 result
tom@516 115 % set the delta needed to get to new_damping:
dicklyon@523 116 % TODO: update this to use da and dc instead of dr maybe?
tom@516 117 CF.filter_state(mic).dzB_memory = ...
tom@516 118 (new_damping - CF.filter_state(mic).zB_memory) ...
dicklyon@523 119 / decim1;
tom@516 120 end
tom@516 121 end
dicklyon@523 122
dicklyon@523 123 k_AGC = mod(k_AGC + 1, AGC_plot_decim);
dicklyon@523 124 if AGC_plot_fig_num && k_AGC == 0
dicklyon@523 125 figure(AGC_plot_fig_num); hold off; clf
dicklyon@523 126 set(gca, 'Position', [.25, .25, .5, .5])
dicklyon@523 127
dicklyon@523 128 maxsum = 0;
dicklyon@523 129 for mic = 1:n_mics
dicklyon@523 130 plot(CF.AGC_state(mic).AGC_memory(:, 1), 'k-', 'LineWidth', 1)
dicklyon@523 131 maxes(mic) = max(CF.AGC_state(mic).AGC_memory(:));
dicklyon@523 132 hold on
dicklyon@523 133 stage1 = 4; % as opposed to stage
dicklyon@523 134 for stage = 1:3;
dicklyon@523 135 plot(2^(stage1-1) * (CF.AGC_state(mic).AGC_memory(:, stage) - ...
dicklyon@523 136 2 * CF.AGC_state(mic).AGC_memory(:, stage+1)));
dicklyon@523 137 end
dicklyon@523 138 stage = 4;
dicklyon@523 139 plot(2^(stage1-1) * CF.AGC_state(mic).AGC_memory(:, stage));
dicklyon@523 140 end
dicklyon@523 141 axis([0, CF.n_ch+1, -0.01, max(maxes) + 0.01]);
dicklyon@523 142 drawnow
dicklyon@523 143 end
dicklyon@523 144
tom@516 145 end
tom@516 146