tom@513: % Copyright 2012 Google Inc. All Rights Reserved. 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: %% Test/demo hacking for CARFAC Matlab stuff: tom@455: tom@455: clear variables tom@455: tom@455: %% dicklyon@504: dicklyon@606: use_wav_file = 1; dicklyon@606: dB_list = -40; % -60:20:40 dicklyon@504: dicklyon@606: if use_wav_file dicklyon@606: wav_fn = 'plan.wav'; dicklyon@475: dicklyon@606: wav_fn dicklyon@606: file_signal = wavread(wav_fn); dicklyon@606: file_signal = file_signal(:, 1); % Mono test only. dicklyon@475: else dicklyon@606: % A tone complex. dicklyon@504: flist = 1400 + (1:4)*200; dicklyon@504: alist = [1, 1, 1, 1]; dicklyon@502: sine_signal = 0; dicklyon@504: times = (0:9999)' / 22050; dicklyon@502: for fno = 1:length(flist) dicklyon@502: sine_signal = sine_signal + alist(fno)*sin(flist(fno)*2*pi*times); dicklyon@502: end dicklyon@502: growth_power = 0; % use 0 for flat, 4 or more for near exponential dicklyon@502: file_signal = 1.0 * (sine_signal .* (times/max(times)).^growth_power); dicklyon@475: end tom@455: tom@455: % make a long test signal by repeating at different levels: dicklyon@502: % dB = dB_list(1); dicklyon@502: % test_signal = 10^(dB/20)* file_signal(1:4000) % lead-in []; dicklyon@502: test_signal = []; dicklyon@502: for dB = dB_list tom@455: test_signal = [test_signal; file_signal * 10^(dB/20)]; tom@455: end tom@455: tom@455: %% Run mono, then stereo test: tom@455: tom@455: agc_plot_fig_num = 6; tom@455: dicklyon@504: for n_ears = 1:2 dicklyon@500: dicklyon@500: CF_struct = CARFAC_Design(n_ears); % default design dicklyon@502: dicklyon@498: if n_ears == 2 dicklyon@498: % For the 2-channel pass, add a silent second channel: dicklyon@498: test_signal = [test_signal, zeros(size(test_signal))]; dicklyon@498: end dicklyon@498: dicklyon@500: CF_struct = CARFAC_Init(CF_struct); dicklyon@502: dicklyon@606: [CF_struct, nap_decim, nap] = CARFAC_Run(CF_struct, test_signal, ... dicklyon@473: agc_plot_fig_num); dicklyon@606: dicklyon@606: smoothed = filter(1, [1, -0.995], nap(:, :, :)); dicklyon@502: dicklyon@498: % only ear 1: dicklyon@606: smoothed = max(0, smoothed(50:50:end, :, 1)); dicklyon@606: MultiScaleSmooth(smoothed.^0.5, 1); dicklyon@502: dicklyon@606: figure(1) dicklyon@606: starti = 0; % Adjust if you want to plot a later part. dicklyon@606: imagesc(nap(starti+(1:15000), :)'); dicklyon@606: dicklyon@473: % Display results for 1 or 2 ears: dicklyon@473: for ear = 1:n_ears dicklyon@473: smooth_nap = nap_decim(:, :, ear); dicklyon@473: if n_ears == 1 tom@455: mono_max = max(smooth_nap(:)); tom@455: end dicklyon@606: figure(3 + ear + n_ears) % Makes figures 5, 6, 7. tom@455: image(63 * ((max(0, smooth_nap)/mono_max)' .^ 0.5)) tom@455: title('smooth nap from nap decim') tom@455: colormap(1 - gray); tom@455: end dicklyon@502: tom@455: % Show resulting data, even though M-Lint complains: tom@455: CF_struct dicklyon@502: CF_struct.ears(1).CAR_state dicklyon@502: CF_struct.ears(1).AGC_state tom@455: min_max_decim = [min(nap_decim(:)), max(nap_decim(:))] dicklyon@502: tom@455: end tom@455: tom@455: % Expected result: Figure 3 looks like figure 2, a tiny bit darker. tom@455: % and figure 4 is empty (all zero)