annotate matlab/bmm/carfac/SAI_RunLayered.m @ 608:fc353426eaad

Better functionization of SAI, and new way to make picture with lag marginal and smoothed history of lag marginal.
author dicklyon@google.com
date Mon, 13 May 2013 21:15:56 +0000
parents ec3a1c74ec54
children b3118c9ed67f
rev   line source
dicklyon@604 1 % Copyright 2013, Google, Inc.
dicklyon@604 2 % Author: Richard F. Lyon
dicklyon@604 3 %
dicklyon@604 4 % This Matlab file is part of an implementation of Lyon's cochlear model:
dicklyon@604 5 % "Cascade of Asymmetric Resonators with Fast-Acting Compression"
dicklyon@604 6 % to supplement Lyon's upcoming book "Human and Machine Hearing"
dicklyon@604 7 %
dicklyon@604 8 % Licensed under the Apache License, Version 2.0 (the "License");
dicklyon@604 9 % you may not use this file except in compliance with the License.
dicklyon@604 10 % You may obtain a copy of the License at
dicklyon@604 11 %
dicklyon@604 12 % http://www.apache.org/licenses/LICENSE-2.0
dicklyon@604 13 %
dicklyon@604 14 % Unless required by applicable law or agreed to in writing, software
dicklyon@604 15 % distributed under the License is distributed on an "AS IS" BASIS,
dicklyon@604 16 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dicklyon@604 17 % See the License for the specific language governing permissions and
dicklyon@604 18 % limitations under the License.
dicklyon@604 19
dicklyon@604 20 function [frame_rate, num_frames] = SAI_RunLayered(CF, input_waves)
dicklyon@604 21 % function [CF, SAI_movie] = CARFAC_Run_Layered_SAI(CF, input_waves)
dicklyon@604 22 % This function runs the CARFAC and generates an SAI movie, dumped as PNG
dicklyon@604 23 % files for now.
dicklyon@604 24
dicklyon@604 25 % Layer 1 is not decimated from the 22050 rate; subsequent layers have
dicklyon@604 26 % smoothing and 2X decimation each. All layers get composited togehter
dicklyon@604 27 % into movie frames.
dicklyon@604 28
dicklyon@604 29 n_ch = CF.n_ch;
dicklyon@604 30 [n_samp, n_ears] = size(input_waves);
dicklyon@604 31 if n_ears ~= CF.n_ears
dicklyon@604 32 error('bad number of input_waves channels passed to CARFAC_Run')
dicklyon@604 33 end
dicklyon@604 34 fs = CF.fs;
dicklyon@604 35
dicklyon@604 36 % Design the composite log-lag SAI using these parameters and defaults.
dicklyon@604 37 n_layers = 10;
dicklyon@604 38 width_per_layer = 40;
dicklyon@604 39 [layer_array, total_width] = SAI_DesignLayers(n_layers, width_per_layer);
dicklyon@604 40
dicklyon@604 41 % Make the composite SAI image array.
dicklyon@604 42 composite_frame = zeros(n_ch, total_width);
dicklyon@604 43
dicklyon@608 44 seglen = round(fs / 30); % Pick about 60 fps
dicklyon@604 45 frame_rate = fs / seglen;
dicklyon@604 46 n_segs = ceil(n_samp / seglen);
dicklyon@604 47
dicklyon@604 48 % Make the history buffers in the layers_array:
dicklyon@604 49 for layer = 1:n_layers
dicklyon@604 50 layer_array(layer).nap_buffer = zeros(layer_array(layer).buffer_width, n_ch);
dicklyon@604 51 layer_array(layer).nap_fraction = 0; % leftover fraction to shift in.
dicklyon@608 52 % The SAI frame is transposed to be image-like.
dicklyon@608 53 layer_array(layer).frame = zeros(n_ch, layer_array(layer).frame_width);
dicklyon@604 54 end
dicklyon@604 55
dicklyon@608 56 n_marginal_rows = 34;
dicklyon@608 57 marginals = [];
dicklyon@608 58
dicklyon@608 59 average_frame = 0;
dicklyon@604 60 for seg_num = 1:n_segs
dicklyon@604 61 % k_range is the range of input sample indices for this segment
dicklyon@604 62 if seg_num == n_segs
dicklyon@604 63 % The last segment may be short of seglen, but do it anyway:
dicklyon@604 64 k_range = (seglen*(seg_num - 1) + 1):n_samp;
dicklyon@604 65 else
dicklyon@604 66 k_range = seglen*(seg_num - 1) + (1:seglen);
dicklyon@604 67 end
dicklyon@604 68 % Process a segment to get a slice of decim_naps, and plot AGC state:
dicklyon@604 69 [seg_naps, CF] = CARFAC_Run_Segment(CF, input_waves(k_range, :));
dicklyon@604 70
dicklyon@604 71 seg_naps = max(0, seg_naps); % Rectify
dicklyon@604 72
dicklyon@604 73 if seg_num == n_segs % pad out the last result
dicklyon@604 74 seg_naps = [seg_naps; zeros(seglen - size(seg_naps,1), size(seg_naps, 2))];
dicklyon@604 75 end
dicklyon@604 76
dicklyon@604 77 % Shift new data into some or all of the layer buffers:
dicklyon@604 78 layer_array = SAI_UpdateBuffers(layer_array, seg_naps, seg_num);
dicklyon@604 79
dicklyon@604 80
dicklyon@604 81 for layer = n_layers:-1:1 % blend from coarse to fine
dicklyon@604 82 update_interval = layer_array(layer).update_interval;
dicklyon@604 83 if 0 == mod(seg_num, update_interval)
dicklyon@608 84 layer_array(layer) = SAI_StabilizeLayer(layer_array(layer));
dicklyon@608 85 new_frame = layer_array(layer).frame;
dicklyon@608 86 composite_frame = SAI_BlendFrameIntoComposite( ...
dicklyon@604 87 layer_array(layer), composite_frame);
dicklyon@604 88 end
dicklyon@604 89 end
dicklyon@608 90
dicklyon@608 91 if isempty(marginals)
dicklyon@608 92 composite_width = size(composite_frame, 2);
dicklyon@608 93 marginals = zeros(n_marginal_rows, composite_width);
dicklyon@608 94 end
dicklyon@608 95 for row = n_marginal_rows:-1:11
dicklyon@608 96 % smooth from row above (lower number)
dicklyon@608 97 marginals(row, :) = marginals(row, :) + ...
dicklyon@608 98 2^((10 - row)/2) * (1.06*marginals(row - 1, :) - marginals(row, :));
dicklyon@608 99 end
dicklyon@604 100 lag_marginal = mean(composite_frame, 1); % means max out near 1 or 2
dicklyon@608 101 for row = 10:-1:1
dicklyon@608 102 marginals(row, :) = (lag_marginal - smooth1d(lag_marginal, 30)') - ...
dicklyon@608 103 (10 - row) / 40;
dicklyon@604 104 end
dicklyon@604 105
dicklyon@604 106 if 0 == mod(seg_num, update_interval) || seg_num == 1
dicklyon@604 107 coc_gram = layer_array(end).nap_buffer';
dicklyon@604 108 [n_ch, n_width] = size(composite_frame);
dicklyon@604 109 coc_gram = [coc_gram, zeros(n_ch, n_width - size(coc_gram, 2))];
dicklyon@604 110 end
dicklyon@604 111
dicklyon@608 112 display_frame = [coc_gram; ...
dicklyon@608 113 composite_frame(floor(1:0.5:end), :); 20*max(0,marginals)];
dicklyon@604 114
dicklyon@604 115 cmap = jet;
dicklyon@604 116 cmap = 1 - gray; % jet
dicklyon@604 117 figure(10)
dicklyon@604 118 image(32*display_frame);
dicklyon@604 119 colormap(cmap);
dicklyon@604 120
dicklyon@604 121 drawnow
dicklyon@604 122 imwrite(32*display_frame, cmap, sprintf('frames/frame%05d.png', seg_num));
dicklyon@604 123 end
dicklyon@604 124
dicklyon@604 125 num_frames = seg_num;
dicklyon@604 126
dicklyon@604 127 return
dicklyon@604 128
dicklyon@604 129
dicklyon@604 130
dicklyon@604 131