annotate matlab/bmm/carfac/CARFAC_Run.m @ 476:b084aed83e87

Simplify plotting mess in CARFAC_Run
author dicklyon@google.com
date Thu, 22 Mar 2012 23:06:46 +0000
parents 52f659be9008
children 896620d9d539
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@475 20 function [CF, decim_naps, naps, BM] = CARFAC_Run ...
tom@455 21 (CF, input_waves, AGC_plot_fig_num)
dicklyon@473 22 % function [CF, decim_naps, naps] = 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 %
tom@455 41 % The function works as an outer iteration on time, updating all the
tom@455 42 % filters and AGC states concurrently, so that the different channels can
tom@455 43 % interact easily. The inner loops are over filterbank channels, and
tom@455 44 % this level should be kept efficient.
tom@455 45
dicklyon@473 46 [n_samp, n_ears] = size(input_waves);
tom@455 47 n_ch = CF.n_ch;
tom@455 48
tom@455 49 if nargin < 3
tom@455 50 AGC_plot_fig_num = 0;
tom@455 51 end
tom@455 52
dicklyon@475 53 if nargout > 3
dicklyon@475 54 BM = zeros(n_samp, n_ch, n_ears);
dicklyon@475 55 else
dicklyon@475 56 BM = [];
dicklyon@475 57 end
dicklyon@475 58
dicklyon@473 59 if n_ears ~= CF.n_ears
tom@455 60 error('bad number of input_waves channels passed to CARFAC_Run')
tom@455 61 end
tom@455 62
dicklyon@473 63
dicklyon@473 64 naps = zeros(n_samp, n_ch, n_ears);
dicklyon@473 65
dicklyon@475 66 seglen = 256;
dicklyon@473 67 n_segs = ceil(n_samp / seglen);
dicklyon@473 68
dicklyon@473 69 if nargout > 1
tom@455 70 % make decimated detect output:
dicklyon@473 71 decim_naps = zeros(n_segs, CF.n_ch, CF.n_ears);
tom@455 72 else
tom@455 73 decim_naps = [];
tom@455 74 end
tom@455 75
dicklyon@473 76 if nargout > 2
dicklyon@473 77 % make decimated detect output:
dicklyon@473 78 naps = zeros(n_samp, CF.n_ch, CF.n_ears);
dicklyon@473 79 else
dicklyon@473 80 naps = [];
dicklyon@473 81 end
tom@455 82
dicklyon@473 83 for seg_num = 1:n_segs
dicklyon@473 84 if seg_num == n_segs
dicklyon@473 85 % The last segement may be short of seglen, but do it anyway:
dicklyon@473 86 k_range = (seglen*(seg_num - 1) + 1):n_samp;
dicklyon@473 87 else
dicklyon@473 88 k_range = seglen*(seg_num - 1) + (1:seglen);
tom@455 89 end
dicklyon@473 90 % Process a segment to get a slice of decim_naps, and plot AGC state:
dicklyon@475 91 if ~isempty(BM)
dicklyon@475 92 [seg_naps, CF, seg_BM] = CARFAC_Run_Segment(CF, input_waves(k_range, :));
dicklyon@475 93 else
dicklyon@475 94 [seg_naps, CF] = CARFAC_Run_Segment(CF, input_waves(k_range, :));
dicklyon@475 95 end
dicklyon@475 96
dicklyon@475 97 if ~isempty(BM)
dicklyon@475 98 for ear = 1:n_ears
dicklyon@475 99 % Accumulate segment BM to make full BM
dicklyon@475 100 BM(k_range, :, ear) = seg_BM(:, :, ear);
dicklyon@475 101 end
dicklyon@475 102 end
dicklyon@473 103
dicklyon@473 104 if ~isempty(naps)
dicklyon@473 105 for ear = 1:n_ears
dicklyon@473 106 % Accumulate segment naps to make full naps
dicklyon@473 107 naps(k_range, :, ear) = seg_naps(:, :, ear);
tom@455 108 end
dicklyon@462 109 end
dicklyon@462 110
dicklyon@473 111 if ~isempty(decim_naps)
dicklyon@473 112 for ear = 1:n_ears
dicklyon@473 113 decim_naps(seg_num, :, ear) = CF.IHC_state(ear).ihc_accum / seglen;
dicklyon@473 114 CF.IHC_state(ear).ihc_accum = zeros(n_ch,1);
dicklyon@473 115 end
tom@455 116 end
dicklyon@462 117
dicklyon@473 118 if AGC_plot_fig_num
dicklyon@462 119 figure(AGC_plot_fig_num); hold off; clf
dicklyon@473 120 for ear = 1:n_ears
dicklyon@473 121 maxes(ear) = max(CF.AGC_state(ear).AGC_memory(:));
dicklyon@462 122 hold on
dicklyon@476 123 for stage = 1:4;
dicklyon@476 124 plot(2^(stage-1) * CF.AGC_state(ear).AGC_memory(:, stage));
dicklyon@462 125 end
dicklyon@462 126 end
dicklyon@475 127 axis([0, CF.n_ch+1, 0.0, max(maxes) * 1.01 + 0.002]);
dicklyon@462 128 drawnow
dicklyon@462 129 end
dicklyon@473 130
tom@455 131 end
tom@455 132
dicklyon@473 133
dicklyon@473 134