comparison trunk/matlab/bmm/carfac/SAI_Run.m @ 676:edfe3a98504b

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