annotate trunk/matlab/bmm/carfac/SAI_Run.m @ 696:d8a404fbc4df

Rename variables to be consistent with the rest of the library.
author ronw@google.com
date Thu, 27 Jun 2013 15:30:46 +0000
parents be55786eeb04
children c64fdcc4f250
rev   line source
dicklyon@615 1 % Copyright 2013, Google, Inc.
dicklyon@615 2 % Author: Richard F. Lyon
dicklyon@615 3 %
dicklyon@615 4 % This Matlab file is part of an implementation of Lyon's cochlear model:
dicklyon@615 5 % "Cascade of Asymmetric Resonators with Fast-Acting Compression"
dicklyon@615 6 % to supplement Lyon's upcoming book "Human and Machine Hearing"
dicklyon@615 7 %
dicklyon@615 8 % Licensed under the Apache License, Version 2.0 (the "License");
dicklyon@615 9 % you may not use this file except in compliance with the License.
dicklyon@615 10 % You may obtain a copy of the License at
dicklyon@615 11 %
dicklyon@615 12 % http://www.apache.org/licenses/LICENSE-2.0
dicklyon@615 13 %
dicklyon@615 14 % Unless required by applicable law or agreed to in writing, software
dicklyon@615 15 % distributed under the License is distributed on an "AS IS" BASIS,
dicklyon@615 16 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dicklyon@615 17 % See the License for the specific language governing permissions and
dicklyon@615 18 % limitations under the License.
dicklyon@615 19
ronw@676 20 function [frame_rate, num_frames] = SAI_Run(CF, input_waves)
ronw@676 21 % function [CF, SAI_movie] = SAI_Run(CF, input_waves)
ronw@676 22 % This function runs the CARFAC and display an SAI movie.
dicklyon@615 23
dicklyon@615 24 n_ch = CF.n_ch;
dicklyon@615 25 [n_samp, n_ears] = size(input_waves);
dicklyon@615 26 if n_ears ~= CF.n_ears
dicklyon@615 27 error('bad number of input_waves channels passed to CARFAC_Run')
dicklyon@615 28 end
dicklyon@615 29 fs = CF.fs;
dicklyon@615 30
dicklyon@665 31 seglen = round(fs / 30); % Pick about 30 fps
dicklyon@665 32 frame_rate = fs / seglen;
dicklyon@615 33 n_segs = ceil(n_samp / seglen);
dicklyon@615 34
ronw@676 35 % Design the SAI parameters.
ronw@680 36 sai_struct.width = 256;
ronw@680 37 sai_struct.future_lags = sai_struct.width / 2;
ronw@676 38 sai_struct.window_width = seglen;
ronw@676 39 n_triggers = 2;
ronw@676 40 sai_struct.n_window_pos = n_triggers;
ronw@676 41 sai_struct.channel_smoothing_scale = 0;
dicklyon@615 42
dicklyon@619 43
ronw@676 44 % State stored in sai_struct.
ronw@676 45 % Make the history buffer.
ronw@676 46 buffer_width = sai_struct.width + ...
ronw@676 47 floor((1 + (n_triggers - 1)/2) * sai_struct.window_width);
ronw@676 48 sai_struct.nap_buffer = zeros(buffer_width, n_ch);
ronw@676 49 % The SAI frame is transposed to be image-like.
ronw@676 50 sai_struct.frame = zeros(n_ch, sai_struct.width);
dicklyon@665 51
dicklyon@615 52 for seg_num = 1:n_segs
ronw@676 53 % seg_range is the range of input sample indices for this segment
dicklyon@615 54 if seg_num == n_segs
dicklyon@615 55 % The last segment may be short of seglen, but do it anyway:
ronw@676 56 seg_range = (seglen*(seg_num - 1) + 1):n_samp;
dicklyon@615 57 else
ronw@676 58 seg_range = seglen*(seg_num - 1) + (1:seglen);
dicklyon@615 59 end
ronw@676 60 % NOTE: seg_naps might have multiple channels.
ronw@676 61 [seg_naps, CF] = CARFAC_Run_Segment(CF, input_waves(seg_range, :));
ronw@676 62
ronw@676 63 % Rectify.
ronw@676 64 % NOTE: This might not be necessary.
ronw@676 65 seg_naps = max(0, seg_naps);
ronw@676 66
dicklyon@615 67 if seg_num == n_segs % pad out the last result
dicklyon@615 68 seg_naps = [seg_naps; zeros(seglen - size(seg_naps,1), size(seg_naps, 2))];
dicklyon@615 69 end
dicklyon@615 70
ronw@676 71 % Shift new data into the buffer.
ronw@676 72 n_shift = size(seg_naps, 1);
ronw@676 73 sai_struct.nap_buffer = [sai_struct.nap_buffer((1 + n_shift):end,:); seg_naps];
dicklyon@615 74
ronw@676 75 sai_struct = SAI_StabilizeLayer(sai_struct);
dicklyon@665 76
dicklyon@615 77 cmap = 1 - gray; % jet
dicklyon@615 78 figure(10)
ronw@676 79 image(32 * sai_struct.frame);
dicklyon@615 80 colormap(cmap);
ronw@676 81 colorbar
dicklyon@615 82
dicklyon@615 83 drawnow
ronw@676 84 % imwrite(32*display_frame, cmap, sprintf('frames/frame%05d.png', seg_num));
dicklyon@615 85 end
dicklyon@615 86
dicklyon@615 87 num_frames = seg_num;
dicklyon@615 88
dicklyon@615 89 return