dicklyon@615: % Copyright 2013, Google, Inc. dicklyon@615: % Author: Richard F. Lyon dicklyon@615: % dicklyon@615: % This Matlab file is part of an implementation of Lyon's cochlear model: dicklyon@615: % "Cascade of Asymmetric Resonators with Fast-Acting Compression" dicklyon@615: % to supplement Lyon's upcoming book "Human and Machine Hearing" dicklyon@615: % dicklyon@615: % Licensed under the Apache License, Version 2.0 (the "License"); dicklyon@615: % you may not use this file except in compliance with the License. dicklyon@615: % You may obtain a copy of the License at dicklyon@615: % dicklyon@615: % http://www.apache.org/licenses/LICENSE-2.0 dicklyon@615: % dicklyon@615: % Unless required by applicable law or agreed to in writing, software dicklyon@615: % distributed under the License is distributed on an "AS IS" BASIS, dicklyon@615: % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. dicklyon@615: % See the License for the specific language governing permissions and dicklyon@615: % limitations under the License. dicklyon@615: ronw@697: function [frame_rate, num_frames] = SAI_Run(CF, sai_struct, input_waves) ronw@697: % function [frame_rate, num_frames] = SAI_Run(CF, sai_struct, input_waves) ronw@676: % This function runs the CARFAC and display an SAI movie. dicklyon@615: dicklyon@615: n_ch = CF.n_ch; dicklyon@615: [n_samp, n_ears] = size(input_waves); dicklyon@615: if n_ears ~= CF.n_ears dicklyon@615: error('bad number of input_waves channels passed to CARFAC_Run') dicklyon@615: end dicklyon@615: fs = CF.fs; dicklyon@615: dicklyon@665: seglen = round(fs / 30); % Pick about 30 fps dicklyon@665: frame_rate = fs / seglen; dicklyon@615: n_segs = ceil(n_samp / seglen); dicklyon@615: ronw@676: % State stored in sai_struct. ronw@676: % Make the history buffer. ronw@676: buffer_width = sai_struct.width + ... ronw@697: floor((1 + (sai_struct.n_window_pos - 1)/2) * sai_struct.window_width); ronw@676: sai_struct.nap_buffer = zeros(buffer_width, n_ch); ronw@676: % The SAI frame is transposed to be image-like. ronw@676: sai_struct.frame = zeros(n_ch, sai_struct.width); dicklyon@665: dicklyon@615: for seg_num = 1:n_segs ronw@676: % seg_range is the range of input sample indices for this segment dicklyon@615: if seg_num == n_segs dicklyon@615: % The last segment may be short of seglen, but do it anyway: ronw@676: seg_range = (seglen*(seg_num - 1) + 1):n_samp; dicklyon@615: else ronw@676: seg_range = seglen*(seg_num - 1) + (1:seglen); dicklyon@615: end ronw@676: % NOTE: seg_naps might have multiple channels. ronw@676: [seg_naps, CF] = CARFAC_Run_Segment(CF, input_waves(seg_range, :)); ronw@676: ronw@676: % Rectify. ronw@676: % NOTE: This might not be necessary. ronw@676: seg_naps = max(0, seg_naps); ronw@676: ronw@700: sai_struct = SAI_Run_Segment(sai_struct, seg_naps); dicklyon@665: dicklyon@615: cmap = 1 - gray; % jet dicklyon@615: figure(10) ronw@697: imagesc(32 * sai_struct.frame); dicklyon@615: colormap(cmap); ronw@676: colorbar dicklyon@615: dicklyon@615: drawnow ronw@676: % imwrite(32*display_frame, cmap, sprintf('frames/frame%05d.png', seg_num)); dicklyon@615: end dicklyon@615: dicklyon@615: num_frames = seg_num; dicklyon@615: dicklyon@615: return