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