annotate matlab/bmm/carfac/CARFAC_SAI.m @ 456:6ddf64b38211

Bug fixes to make binaural work
author dicklyon@google.com
date Thu, 16 Feb 2012 18:34:04 +0000
parents f8ba7ad93fa9
children 3a873d04a7fe
rev   line source
tom@455 1 % Copyright 2012, Google, Inc.
tom@455 2 % Author: Richard F. Lyon
tom@455 3 %
tom@455 4 % This Matlab file is part of an implementation of Lyon's cochlear model:
tom@455 5 % "Cascade of Asymmetric Resonators with Fast-Acting Compression"
tom@455 6 % to supplement Lyon's upcoming book "Human and Machine Hearing"
tom@455 7 %
tom@455 8 % Licensed under the Apache License, Version 2.0 (the "License");
tom@455 9 % you may not use this file except in compliance with the License.
tom@455 10 % You may obtain a copy of the License at
tom@455 11 %
tom@455 12 % http://www.apache.org/licenses/LICENSE-2.0
tom@455 13 %
tom@455 14 % Unless required by applicable law or agreed to in writing, software
tom@455 15 % distributed under the License is distributed on an "AS IS" BASIS,
tom@455 16 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
tom@455 17 % See the License for the specific language governing permissions and
tom@455 18 % limitations under the License.
tom@455 19
dicklyon@456 20 function [sai_frame, sai_state, naps] = CARFAC_SAI(naps, k, sai_state, SAI_params)
dicklyon@456 21 % function sai = CARFAC_SAI(naps, k, sai_state, SAI_params)
tom@455 22 %
dicklyon@456 23 % Calculate the Stabilized Auditory Image from naps;
dicklyon@456 24 % I think this is a binaural SAI by Steven Ness
dicklyon@456 25 %
dicklyon@456 26 % k seems to be a time index; it's an incremental update of the images...
dicklyon@456 27 % but this doesn't sound like a proper incremental approach...
tom@455 28 %
tom@455 29
dicklyon@456 30 [n_samp, n_ch, n_mics] = size(naps);
tom@455 31
dicklyon@456 32 if nargin < 4
dicklyon@456 33 SAI_params = struct( ...
dicklyon@456 34 'frame_jump', 200, ...
dicklyon@456 35 'sai_width', 500, ...
dicklyon@456 36 'threshold_alpha', 0.99, ...
dicklyon@456 37 'threshold_jump_factor', 1.2, ...
dicklyon@456 38 'threshold_jump_offset', 0.1};
dicklyon@456 39 end
tom@455 40
dicklyon@456 41 threshold_alpha = SAI_params.threshold_alpha;
dicklyon@456 42 threshold_jump = SAI_params.threshold_jump_factor;
dicklyon@456 43 threshold_offset = SAI_params.threshold_jump_offset;
tom@455 44
dicklyon@456 45 sai2 = reshape(sai_state.sai, SAI_params.sai_width * n_ch, n_mics);
dicklyon@456 46 naps2 = reshape(naps, n_samp * n_ch, n_mics);
tom@455 47
dicklyon@456 48 for mic = 1:n_mics
dicklyon@456 49 data = naps(k, :, mic)';
dicklyon@456 50 above_threshold = (sai_state(mic).lastdata > ...
dicklyon@456 51 sai_state(mic).thresholds) & ...
dicklyon@456 52 (sai_state(mic).lastdata > data);
dicklyon@456 53 sai_state(mic).thresholds(above_threshold) = ...
dicklyon@456 54 data(above_threshold) * threshold_jump + threshold_offset;
dicklyon@456 55 sai_state(mic).thresholds(~above_threshold) = ...
dicklyon@456 56 sai_state(mic).thresholds(~above_threshold) * threshold_alpha;
dicklyon@456 57 sai_state(mic).lastdata = data;
dicklyon@456 58
dicklyon@456 59 % Update SAI image with strobe data.
dicklyon@456 60 othermic = 3 - mic;
dicklyon@456 61
dicklyon@456 62 % Channels that are above the threhsold
dicklyon@456 63 above_ch = find(above_threshold);
dicklyon@456 64
dicklyon@456 65 % If we are above the threshold, set the trigger index and reset the
dicklyon@456 66 % sai_index
dicklyon@456 67 sai_state(mic).trigger_index(above_ch) = k;
dicklyon@456 68 sai_state(mic).sai_index(above_ch) = 1;
dicklyon@456 69
dicklyon@456 70 % Copy the right data from the nap to the sai
dicklyon@456 71 chans = (1:n_ch)';
dicklyon@456 72 fromindices = sai_state(mic).trigger_index() + (chans - 1) * n_samp;
dicklyon@456 73 toindices = min((sai_state(mic).sai_index() + (chans - 1) * sai_params.sai_width), sai_params.sai_width * n_ch);
dicklyon@456 74 sai2(toindices,mic) = naps2(fromindices, othermic);
dicklyon@456 75
dicklyon@456 76 sai_state(mic).trigger_index(:) = sai_state(mic).trigger_index(:) + 1;
dicklyon@456 77 sai_state(mic).sai_index(:) = sai_state(mic).sai_index(:) + 1;
dicklyon@456 78 end
tom@455 79
tom@455 80
dicklyon@456 81 sai_frame = reshape(sai2,sai_params.sai_width,n_ch,n_mics);
dicklyon@456 82 sai_state.sai = sai; % probably this is not exactly what we want to store as state...
tom@455 83
tom@455 84