annotate matlab/bmm/carfac/CARFAC_Run_Open_Loop.m @ 655:8a2b0cbd7a64

Move design of SAI out of SAI_Run.m
author ronw@google.com
date Thu, 27 Jun 2013 20:47:02 +0000
parents 52f659be9008
children
rev   line source
dicklyon@475 1 % Copyright 2012, Google, Inc.
dicklyon@475 2 % Author Richard F. Lyon
dicklyon@475 3 %
dicklyon@475 4 % This Matlab file is part of an implementation of Lyon's cochlear model:
dicklyon@475 5 % "Cascade of Asymmetric Resonators with Fast-Acting Compression"
dicklyon@475 6 % to supplement Lyon's upcoming book "Human and Machine Hearing"
dicklyon@475 7 %
dicklyon@475 8 % Licensed under the Apache License, Version 2.0 (the "License");
dicklyon@475 9 % you may not use this file except in compliance with the License.
dicklyon@475 10 % You may obtain a copy of the License at
dicklyon@475 11 %
dicklyon@475 12 % http://www.apache.org/licenses/LICENSE-2.0
dicklyon@475 13 %
dicklyon@475 14 % Unless required by applicable law or agreed to in writing, software
dicklyon@475 15 % distributed under the License is distributed on an "AS IS" BASIS,
dicklyon@475 16 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dicklyon@475 17 % See the License for the specific language governing permissions and
dicklyon@475 18 % limitations under the License.
dicklyon@475 19
dicklyon@475 20 function [CF, decim_naps, naps] = CARFAC_Run_Open_Loop ...
dicklyon@475 21 (CF, input_waves, AGC_plot_fig_num)
dicklyon@475 22 % function [CF, decim_naps, naps] = CARFAC_Run_Open_Loop ...
dicklyon@475 23 % (CF, input_waves, AGC_plot_fig_num)
dicklyon@475 24 %
dicklyon@475 25 % Freeze the damping by disabling AGC feedback, and run so we can
dicklyon@475 26 % see what the filters and AGC do in that frozen state. And zap the
dicklyon@475 27 % stage gain in the AGC so we can see the state filters without combining
dicklyon@475 28 % them.
dicklyon@475 29
dicklyon@475 30 [n_samp, n_ears] = size(input_waves);
dicklyon@475 31 n_ch = CF.n_ch;
dicklyon@475 32
dicklyon@475 33 if nargin < 3
dicklyon@475 34 AGC_plot_fig_num = 0;
dicklyon@475 35 end
dicklyon@475 36
dicklyon@475 37 if n_ears ~= CF.n_ears
dicklyon@475 38 error('bad number of input_waves channels passed to CARFAC_Run')
dicklyon@475 39 end
dicklyon@475 40
dicklyon@475 41
dicklyon@475 42 naps = zeros(n_samp, n_ch, n_ears);
dicklyon@475 43
dicklyon@475 44 seglen = 16;
dicklyon@475 45 n_segs = ceil(n_samp / seglen);
dicklyon@475 46
dicklyon@475 47 if nargout > 1
dicklyon@475 48 % make decimated detect output:
dicklyon@475 49 decim_naps = zeros(n_segs, CF.n_ch, CF.n_ears);
dicklyon@475 50 else
dicklyon@475 51 decim_naps = [];
dicklyon@475 52 end
dicklyon@475 53
dicklyon@475 54 if nargout > 2
dicklyon@475 55 % make decimated detect output:
dicklyon@475 56 naps = zeros(n_samp, CF.n_ch, CF.n_ears);
dicklyon@475 57 else
dicklyon@475 58 naps = [];
dicklyon@475 59 end
dicklyon@475 60
dicklyon@475 61 % zero the deltas:
dicklyon@475 62 for ear = 1:CF.n_ears
dicklyon@475 63 CF.CAR_state(ear).dzB_memory = 0;
dicklyon@475 64 CF.CAR_state(ear).dg_memory = 0;
dicklyon@475 65 end
dicklyon@475 66 open_loop = 1;
dicklyon@475 67 CF.AGC_coeffs.AGC_stage_gain = 0; % HACK to see the stages separately
dicklyon@475 68
dicklyon@475 69 smoothed_state = 0;
dicklyon@475 70
dicklyon@475 71 for seg_num = 1:n_segs
dicklyon@475 72 if seg_num == n_segs
dicklyon@475 73 % The last segement may be short of seglen, but do it anyway:
dicklyon@475 74 k_range = (seglen*(seg_num - 1) + 1):n_samp;
dicklyon@475 75 else
dicklyon@475 76 k_range = seglen*(seg_num - 1) + (1:seglen);
dicklyon@475 77 end
dicklyon@475 78 % Process a segment to get a slice of decim_naps, and plot AGC state:
dicklyon@475 79 [seg_naps, CF] = CARFAC_Run_Segment(CF, input_waves(k_range, :), ...
dicklyon@475 80 open_loop);
dicklyon@475 81
dicklyon@475 82 if ~isempty(naps)
dicklyon@475 83 for ear = 1:n_ears
dicklyon@475 84 % Accumulate segment naps to make full naps
dicklyon@475 85 naps(k_range, :, ear) = seg_naps(:, :, ear);
dicklyon@475 86 end
dicklyon@475 87 end
dicklyon@475 88
dicklyon@475 89 if ~isempty(decim_naps)
dicklyon@475 90 for ear = 1:n_ears
dicklyon@475 91 decim_naps(seg_num, :, ear) = CF.IHC_state(ear).ihc_accum / seglen;
dicklyon@475 92 CF.IHC_state(ear).ihc_accum = zeros(n_ch,1);
dicklyon@475 93 end
dicklyon@475 94 end
dicklyon@475 95
dicklyon@475 96 if AGC_plot_fig_num
dicklyon@475 97 figure(AGC_plot_fig_num); hold off; clf
dicklyon@475 98 set(gca, 'Position', [.25, .25, .5, .5])
dicklyon@475 99 smoothed_state = (3*smoothed_state + CF.AGC_state(1).AGC_memory) / 4;
dicklyon@475 100 for ear = 1
dicklyon@475 101 total_state = 0;
dicklyon@475 102 for stage = 1:4;
dicklyon@475 103 weighted_state = smoothed_state(:, stage) * 2^(stage-1);
dicklyon@475 104 plot(weighted_state, 'k-', 'LineWidth', 0.4);
dicklyon@475 105 hold on
dicklyon@475 106 total_state = total_state + weighted_state;
dicklyon@475 107 end
dicklyon@475 108 maxes(ear) = max(total_state);
dicklyon@475 109 plot(total_state, 'k-', 'LineWidth', 1.1)
dicklyon@475 110 end
dicklyon@475 111
dicklyon@475 112 axis([0, CF.n_ch+1, 0.0, max(maxes) * 1.01 + 0.002]);
dicklyon@475 113 drawnow
dicklyon@475 114 end
dicklyon@475 115
dicklyon@475 116 end
dicklyon@475 117
dicklyon@475 118
dicklyon@475 119