annotate matlab/bmm/carfac/SAI_Run.m @ 635:0bdd58ee6e92

ffmpeg no longer accepts a qscale value of 0. Changed qscale to 1, which also gives a very high quality output
author sness@sness.net
date Fri, 24 May 2013 22:38:09 +0000
parents 68bc9e908578
children 6206696d42ac
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
ronw@634 20 function [frame_rate, num_frames] = SAI_Run(CF, input_waves)
ronw@634 21 % function [CF, SAI_movie] = SAI_Run(CF, input_waves)
ronw@634 22 % This function runs the CARFAC and display an SAI movie.
dicklyon@604 23
dicklyon@604 24 n_ch = CF.n_ch;
dicklyon@604 25 [n_samp, n_ears] = size(input_waves);
dicklyon@604 26 if n_ears ~= CF.n_ears
dicklyon@604 27 error('bad number of input_waves channels passed to CARFAC_Run')
dicklyon@604 28 end
dicklyon@604 29 fs = CF.fs;
dicklyon@604 30
dicklyon@623 31 seglen = round(fs / 30); % Pick about 30 fps
dicklyon@623 32 frame_rate = fs / seglen;
dicklyon@604 33 n_segs = ceil(n_samp / seglen);
dicklyon@604 34
ronw@634 35 % Design the SAI parameters.
ronw@634 36 sai_struct.width = 72;
ronw@634 37 sai_struct.future_lags = 0;
ronw@634 38 sai_struct.window_width = seglen;
ronw@634 39 n_triggers = 2;
ronw@634 40 sai_struct.n_window_pos = n_triggers;
ronw@634 41 sai_struct.channel_smoothing_scale = 0;
dicklyon@604 42
dicklyon@608 43
ronw@634 44 % State stored in sai_struct.
ronw@634 45 % Make the history buffer.
ronw@634 46 buffer_width = sai_struct.width + ...
ronw@634 47 floor((1 + (n_triggers - 1)/2) * sai_struct.window_width);
ronw@634 48 sai_struct.nap_buffer = zeros(buffer_width, n_ch);
ronw@634 49 % The SAI frame is transposed to be image-like.
ronw@634 50 sai_struct.frame = zeros(n_ch, sai_struct.width);
dicklyon@623 51
dicklyon@604 52 for seg_num = 1:n_segs
ronw@634 53 % seg_range is the range of input sample indices for this segment
dicklyon@604 54 if seg_num == n_segs
dicklyon@604 55 % The last segment may be short of seglen, but do it anyway:
ronw@634 56 seg_range = (seglen*(seg_num - 1) + 1):n_samp;
dicklyon@604 57 else
ronw@634 58 seg_range = seglen*(seg_num - 1) + (1:seglen);
dicklyon@604 59 end
dicklyon@604 60 % Process a segment to get a slice of decim_naps, and plot AGC state:
ronw@634 61 % NOTE: seg_naps might have multiple channels.
ronw@634 62 [seg_naps, CF] = CARFAC_Run_Segment(CF, input_waves(seg_range, :));
ronw@634 63
ronw@634 64 % Rectify.
ronw@634 65 % NOTE: This might not be necessary.
ronw@634 66 seg_naps = max(0, seg_naps);
ronw@634 67
dicklyon@604 68 if seg_num == n_segs % pad out the last result
dicklyon@604 69 seg_naps = [seg_naps; zeros(seglen - size(seg_naps,1), size(seg_naps, 2))];
dicklyon@604 70 end
dicklyon@604 71
ronw@634 72 % Shift new data into the buffer.
ronw@634 73 n_shift = size(seg_naps, 1);
ronw@634 74 sai_struct.nap_buffer = [sai_struct.nap_buffer((1 + n_shift):end,:); seg_naps];
dicklyon@604 75
ronw@634 76 sai_struct = SAI_StabilizeLayer(sai_struct);
dicklyon@623 77
dicklyon@604 78 cmap = 1 - gray; % jet
dicklyon@604 79 figure(10)
ronw@634 80 image(32 * sai_struct.frame);
dicklyon@604 81 colormap(cmap);
ronw@634 82 colorbar
dicklyon@604 83
dicklyon@604 84 drawnow
ronw@634 85 % imwrite(32*display_frame, cmap, sprintf('frames/frame%05d.png', seg_num));
dicklyon@604 86 end
dicklyon@604 87
dicklyon@604 88 num_frames = seg_num;
dicklyon@604 89
dicklyon@604 90 return