tom@516: % Copyright 2012, Google, Inc. tom@516: % Author: Richard F. Lyon tom@516: % tom@516: % This Matlab file is part of an implementation of Lyon's cochlear model: tom@516: % "Cascade of Asymmetric Resonators with Fast-Acting Compression" tom@516: % to supplement Lyon's upcoming book "Human and Machine Hearing" tom@516: % tom@516: % Licensed under the Apache License, Version 2.0 (the "License"); tom@516: % you may not use this file except in compliance with the License. tom@516: % You may obtain a copy of the License at tom@516: % tom@516: % http://www.apache.org/licenses/LICENSE-2.0 tom@516: % tom@516: % Unless required by applicable law or agreed to in writing, software tom@516: % distributed under the License is distributed on an "AS IS" BASIS, tom@516: % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. tom@516: % See the License for the specific language governing permissions and tom@516: % limitations under the License. tom@516: tom@516: function [CF, sai] = CARFAC_SAI(CF, k, n_mics, naps, sai) tom@516: % function sai = CARFAC_SAI(CF_struct, n_mics, naps, sai) tom@516: % tom@516: % Calculate the Stabilized Auditory Image from naps tom@516: % tom@516: tom@516: threshold_alpha = CF.sai_params.threshold_alpha; tom@516: threshold_jump = CF.sai_params.threshold_jump_factor; tom@516: threshold_offset = CF.sai_params.threshold_jump_offset; tom@516: tom@516: sai2 = reshape(sai,CF.sai_params.sai_width * CF.n_ch,n_mics); tom@516: naps2 = reshape(naps,CF.n_samp * CF.n_ch,n_mics); tom@516: tom@516: for mic = 1:n_mics tom@516: data = naps(k, :, mic)'; tom@516: above_threshold = (CF.sai_state(mic).lastdata > ... tom@516: CF.sai_state(mic).thresholds) & ... tom@516: (CF.sai_state(mic).lastdata > data); tom@516: CF.sai_state(mic).thresholds(above_threshold) = ... tom@516: data(above_threshold) * threshold_jump + threshold_offset; tom@516: CF.sai_state(mic).thresholds(~above_threshold) = ... tom@516: CF.sai_state(mic).thresholds(~above_threshold) * threshold_alpha; tom@516: CF.sai_state(mic).lastdata = data; tom@516: tom@516: % Update SAI image with strobe data. tom@516: othermic = 3 - mic; tom@516: tom@516: % Channels that are above the threhsold tom@516: above_ch = find(above_threshold); tom@516: tom@516: % If we are above the threshold, set the trigger index and reset the tom@516: % sai_index tom@516: CF.sai_state(mic).trigger_index(above_ch) = k; tom@516: CF.sai_state(mic).sai_index(above_ch) = 1; tom@516: tom@516: % Copy the right data from the nap to the sai tom@516: chans = (1:CF.n_ch)'; tom@516: fromindices = CF.sai_state(mic).trigger_index() + (chans - 1) * CF.n_samp; tom@516: toindices = min((CF.sai_state(mic).sai_index() + (chans - 1) * ... tom@516: CF.sai_params.sai_width), ... tom@516: CF.sai_params.sai_width * CF.n_ch); tom@516: sai2(toindices,mic) = naps2(fromindices,othermic); tom@516: tom@516: CF.sai_state(mic).trigger_index(:) = CF.sai_state(mic).trigger_index(:) + 1; tom@516: CF.sai_state(mic).sai_index(:) = CF.sai_state(mic).sai_index(:) + 1; tom@516: tom@516: end tom@516: tom@516: sai = reshape(sai2,CF.sai_params.sai_width,CF.n_ch,n_mics); tom@516: naps = reshape(naps2,CF.n_samp, CF.n_ch,n_mics); tom@516: