annotate trunk/matlab/bmm/carfac/CARFAC_Run.m @ 534:95a11cca4619

Add CARFAC_Design_Doc.txt, CARFAC_Run_Segment.m, and some renames; rename various variables to be more parallel; clean up init code and such.
author dicklyon@google.com
date Fri, 16 Mar 2012 04:19:24 +0000
parents fb60ea429bb8
children 2964a3b4a00a
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
dicklyon@534 20 function [CF, decim_naps, naps] = CARFAC_Run ...
tom@516 21 (CF, input_waves, AGC_plot_fig_num)
dicklyon@534 22 % function [CF, decim_naps, 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
dicklyon@534 46 [n_samp, n_ears] = size(input_waves);
tom@516 47 n_ch = CF.n_ch;
tom@516 48
tom@516 49 if nargin < 3
tom@516 50 AGC_plot_fig_num = 0;
tom@516 51 end
tom@516 52
dicklyon@534 53 if n_ears ~= CF.n_ears
tom@516 54 error('bad number of input_waves channels passed to CARFAC_Run')
tom@516 55 end
tom@516 56
dicklyon@534 57
dicklyon@534 58 naps = zeros(n_samp, n_ch, n_ears);
dicklyon@534 59
dicklyon@534 60 seglen = 16;
dicklyon@534 61 n_segs = ceil(n_samp / seglen);
dicklyon@534 62
dicklyon@534 63 if nargout > 1
tom@516 64 % make decimated detect output:
dicklyon@534 65 decim_naps = zeros(n_segs, CF.n_ch, CF.n_ears);
tom@516 66 else
tom@516 67 decim_naps = [];
tom@516 68 end
tom@516 69
dicklyon@534 70 if nargout > 2
dicklyon@534 71 % make decimated detect output:
dicklyon@534 72 naps = zeros(n_samp, CF.n_ch, CF.n_ears);
dicklyon@534 73 else
dicklyon@534 74 naps = [];
dicklyon@534 75 end
tom@516 76
dicklyon@534 77 for seg_num = 1:n_segs
dicklyon@534 78 if seg_num == n_segs
dicklyon@534 79 % The last segement may be short of seglen, but do it anyway:
dicklyon@534 80 k_range = (seglen*(seg_num - 1) + 1):n_samp;
dicklyon@534 81 else
dicklyon@534 82 k_range = seglen*(seg_num - 1) + (1:seglen);
tom@516 83 end
dicklyon@534 84 % Process a segment to get a slice of decim_naps, and plot AGC state:
dicklyon@534 85 [seg_naps, CF] = CARFAC_Run_Segment(CF, input_waves(k_range, :));
dicklyon@534 86
dicklyon@534 87 if ~isempty(naps)
dicklyon@534 88 for ear = 1:n_ears
dicklyon@534 89 % Accumulate segment naps to make full naps
dicklyon@534 90 naps(k_range, :, ear) = seg_naps(:, :, ear);
tom@516 91 end
dicklyon@523 92 end
dicklyon@523 93
dicklyon@534 94 if ~isempty(decim_naps)
dicklyon@534 95 for ear = 1:n_ears
dicklyon@534 96 decim_naps(seg_num, :, ear) = CF.IHC_state(ear).ihc_accum / seglen;
dicklyon@534 97 CF.IHC_state(ear).ihc_accum = zeros(n_ch,1);
dicklyon@534 98 end
tom@516 99 end
dicklyon@523 100
dicklyon@534 101 if AGC_plot_fig_num
dicklyon@523 102 figure(AGC_plot_fig_num); hold off; clf
dicklyon@523 103 set(gca, 'Position', [.25, .25, .5, .5])
dicklyon@523 104
dicklyon@534 105 for ear = 1:n_ears
dicklyon@534 106 plot(CF.AGC_state(ear).AGC_memory(:, 1), 'k-', 'LineWidth', 1)
dicklyon@534 107 maxes(ear) = max(CF.AGC_state(ear).AGC_memory(:));
dicklyon@523 108 hold on
dicklyon@523 109 for stage = 1:3;
dicklyon@534 110 plot(2^(stage-1) * (CF.AGC_state(ear).AGC_memory(:, stage) - ...
dicklyon@534 111 2 * CF.AGC_state(ear).AGC_memory(:, stage+1)));
dicklyon@523 112 end
dicklyon@523 113 stage = 4;
dicklyon@534 114 plot(2^(stage-1) * CF.AGC_state(ear).AGC_memory(:, stage));
dicklyon@523 115 end
dicklyon@534 116 axis([0, CF.n_ch+1, 0.0, max(maxes) + 0.01]);
dicklyon@523 117 drawnow
dicklyon@523 118 end
dicklyon@534 119
tom@516 120 end
tom@516 121
dicklyon@534 122
dicklyon@534 123