annotate matlab/bmm/carfac/CARFAC_Run.m @ 504:a0869cb1c99b

Major update to how the DOHC works; like in recent book OHC chapter; Design Doc update (a bit)
author dicklyon@google.com
date Thu, 24 May 2012 22:26:56 +0000
parents 37c007925536
children 1d720e7fffdf
rev   line source
tom@455 1 % Copyright 2012, Google, Inc.
dicklyon@462 2 % Author Richard F. Lyon
tom@455 3 %
tom@455 4 % This Matlab file is part of an implementation of Lyon's cochlear model:
tom@455 5 % "Cascade of Asymmetric Resonators with Fast-Acting Compression"
tom@455 6 % to supplement Lyon's upcoming book "Human and Machine Hearing"
tom@455 7 %
tom@455 8 % Licensed under the Apache License, Version 2.0 (the "License");
tom@455 9 % you may not use this file except in compliance with the License.
tom@455 10 % You may obtain a copy of the License at
tom@455 11 %
tom@455 12 % http://www.apache.org/licenses/LICENSE-2.0
tom@455 13 %
tom@455 14 % Unless required by applicable law or agreed to in writing, software
tom@455 15 % distributed under the License is distributed on an "AS IS" BASIS,
tom@455 16 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tom@455 17 % See the License for the specific language governing permissions and
tom@455 18 % limitations under the License.
tom@455 19
dicklyon@502 20 function [CF, decim_naps, naps, BM, ohc, agc] = CARFAC_Run ...
tom@455 21 (CF, input_waves, AGC_plot_fig_num)
dicklyon@502 22 % function [CF, decim_naps, naps, BM, ohc, agc] = CARFAC_Run ...
dicklyon@462 23 % (CF, input_waves, AGC_plot_fig_num)
tom@455 24 % This function runs the CARFAC; that is, filters a 1 or more channel
tom@455 25 % sound input to make one or more neural activity patterns (naps).
tom@455 26 %
tom@455 27 % The CF struct holds the filterbank design and state; if you want to
tom@455 28 % break the input up into segments, you need to use the updated CF
tom@455 29 % to keep the state between segments.
tom@455 30 %
tom@455 31 % input_waves is a column vector if there's just one audio channel;
tom@455 32 % more generally, it has a row per time sample, a column per audio channel.
tom@455 33 %
tom@455 34 % naps has a row per time sample, a column per filterbank channel, and
tom@455 35 % a layer per audio channel if more than 1.
tom@455 36 % decim_naps is like naps but time-decimated by the int CF.decimation.
tom@455 37 %
tom@455 38 % the input_waves are assumed to be sampled at the same rate as the
tom@455 39 % CARFAC is designed for; a resampling may be needed before calling this.
tom@455 40 %
dicklyon@502 41 % ohc and agc are optional extra outputs for diagnosing internals.
tom@455 42
dicklyon@473 43 [n_samp, n_ears] = size(input_waves);
tom@455 44 n_ch = CF.n_ch;
tom@455 45
tom@455 46 if nargin < 3
tom@455 47 AGC_plot_fig_num = 0;
tom@455 48 end
tom@455 49
dicklyon@475 50 if nargout > 3
dicklyon@475 51 BM = zeros(n_samp, n_ch, n_ears);
dicklyon@475 52 else
dicklyon@475 53 BM = [];
dicklyon@475 54 end
dicklyon@475 55
dicklyon@502 56 if nargout > 4
dicklyon@502 57 ohc = zeros(n_samp, n_ch, n_ears);
dicklyon@502 58 else
dicklyon@502 59 ohc = [];
dicklyon@502 60 end
dicklyon@502 61
dicklyon@502 62 if nargout > 5
dicklyon@502 63 agc = zeros(n_samp, n_ch, n_ears);
dicklyon@502 64 else
dicklyon@502 65 agc = [];
dicklyon@502 66 end
dicklyon@502 67
dicklyon@473 68 if n_ears ~= CF.n_ears
tom@455 69 error('bad number of input_waves channels passed to CARFAC_Run')
tom@455 70 end
tom@455 71
dicklyon@473 72
dicklyon@473 73 naps = zeros(n_samp, n_ch, n_ears);
dicklyon@473 74
dicklyon@504 75 seglen = 441; % anything should work; this is 20 ms at default fs
dicklyon@473 76 n_segs = ceil(n_samp / seglen);
dicklyon@473 77
dicklyon@473 78 if nargout > 1
tom@455 79 % make decimated detect output:
dicklyon@473 80 decim_naps = zeros(n_segs, CF.n_ch, CF.n_ears);
tom@455 81 else
tom@455 82 decim_naps = [];
tom@455 83 end
tom@455 84
dicklyon@473 85 if nargout > 2
dicklyon@473 86 % make decimated detect output:
dicklyon@473 87 naps = zeros(n_samp, CF.n_ch, CF.n_ears);
dicklyon@473 88 else
dicklyon@473 89 naps = [];
dicklyon@473 90 end
tom@455 91
dicklyon@473 92 for seg_num = 1:n_segs
dicklyon@473 93 if seg_num == n_segs
dicklyon@473 94 % The last segement may be short of seglen, but do it anyway:
dicklyon@473 95 k_range = (seglen*(seg_num - 1) + 1):n_samp;
dicklyon@473 96 else
dicklyon@473 97 k_range = seglen*(seg_num - 1) + (1:seglen);
tom@455 98 end
dicklyon@473 99 % Process a segment to get a slice of decim_naps, and plot AGC state:
dicklyon@475 100 if ~isempty(BM)
dicklyon@502 101 % ask for everything in this case, for laziness:
dicklyon@502 102 [seg_naps, CF, seg_BM, seg_ohc, seg_agc] = CARFAC_Run_Segment(CF, input_waves(k_range, :));
dicklyon@475 103 else
dicklyon@475 104 [seg_naps, CF] = CARFAC_Run_Segment(CF, input_waves(k_range, :));
dicklyon@475 105 end
dicklyon@475 106
dicklyon@475 107 if ~isempty(BM)
dicklyon@475 108 for ear = 1:n_ears
dicklyon@475 109 % Accumulate segment BM to make full BM
dicklyon@475 110 BM(k_range, :, ear) = seg_BM(:, :, ear);
dicklyon@475 111 end
dicklyon@475 112 end
dicklyon@473 113
dicklyon@473 114 if ~isempty(naps)
dicklyon@473 115 for ear = 1:n_ears
dicklyon@473 116 % Accumulate segment naps to make full naps
dicklyon@473 117 naps(k_range, :, ear) = seg_naps(:, :, ear);
tom@455 118 end
dicklyon@462 119 end
dicklyon@462 120
dicklyon@502 121 if ~isempty(ohc)
dicklyon@502 122 for ear = 1:n_ears
dicklyon@502 123 % Accumulate segment naps to make full naps
dicklyon@502 124 ohc(k_range, :, ear) = seg_ohc(:, :, ear);
dicklyon@502 125 end
dicklyon@502 126 end
dicklyon@502 127
dicklyon@502 128 if ~isempty(agc)
dicklyon@502 129 for ear = 1:n_ears
dicklyon@502 130 % Accumulate segment naps to make full naps
dicklyon@502 131 agc(k_range, :, ear) = seg_agc(:, :, ear);
dicklyon@502 132 end
dicklyon@502 133 end
dicklyon@502 134
dicklyon@473 135 if ~isempty(decim_naps)
dicklyon@473 136 for ear = 1:n_ears
dicklyon@500 137 decim_naps(seg_num, :, ear) = CF.ears(ear).IHC_state.ihc_accum / seglen;
dicklyon@500 138 CF.ears(ear).IHC_state.ihc_accum = zeros(n_ch,1);
dicklyon@473 139 end
tom@455 140 end
dicklyon@462 141
dicklyon@473 142 if AGC_plot_fig_num
dicklyon@462 143 figure(AGC_plot_fig_num); hold off; clf
dicklyon@473 144 for ear = 1:n_ears
dicklyon@500 145 maxes(ear) = max(CF.ears(ear).AGC_state.AGC_memory(:));
dicklyon@462 146 hold on
dicklyon@476 147 for stage = 1:4;
dicklyon@500 148 plot(2^(stage-1) * CF.ears(ear).AGC_state.AGC_memory(:, stage));
dicklyon@462 149 end
dicklyon@462 150 end
dicklyon@475 151 axis([0, CF.n_ch+1, 0.0, max(maxes) * 1.01 + 0.002]);
dicklyon@462 152 drawnow
dicklyon@462 153 end
dicklyon@473 154
tom@455 155 end
tom@455 156
dicklyon@473 157
dicklyon@473 158