alexbrandmeyer@668: % Author: Alex Brandmeyer alexbrandmeyer@668: % alexbrandmeyer@668: % This Matlab file is part of an implementation of Lyon's cochlear model: alexbrandmeyer@668: % "Cascade of Asymmetric Resonators with Fast-Acting Compression" alexbrandmeyer@668: % to supplement Lyon's upcoming book "Human and Machine Hearing" alexbrandmeyer@668: % alexbrandmeyer@668: % Licensed under the Apache License, Version 2.0 (the "License"); alexbrandmeyer@668: % you may not use this file except in compliance with the License. alexbrandmeyer@668: % You may obtain a copy of the License at alexbrandmeyer@668: % alexbrandmeyer@668: % http://www.apache.org/licenses/LICENSE-2.0 alexbrandmeyer@668: % alexbrandmeyer@668: % Unless required by applicable law or agreed to in writing, software alexbrandmeyer@668: % distributed under the License is distributed on an "AS IS" BASIS, alexbrandmeyer@668: % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. alexbrandmeyer@668: % See the License for the specific language governing permissions and alexbrandmeyer@668: % limitations under the License. alexbrandmeyer@668: alexbrandmeyer@668: function CARFAC_GenerateTestData() alexbrandmeyer@668: % function GenerateTestData() alexbrandmeyer@668: % This function generates a set of text files in the AIMC repository that alexbrandmeyer@668: % can be used to compare the output of the C++ version of CARFAC with that alexbrandmeyer@668: % of the Matlab version. ronw@698: % ronw@698: % Naming convention for files containing audio samples for file test_name.wav: ronw@698: % test_name-audio.txt ronw@698: % Each line contains a space-separated list of samples from each channel. ronw@698: % ronw@698: % Naming convention for files containing CARFAC/SAI outputs: ronw@698: % test_name-{matlab,cpp}-signal_name(optional_channel_number).txt ronw@698: % Each line contains a space-separated list of elements from a single row. alexbrandmeyer@668: alexbrandmeyer@668: % This designates a subdirectory of the C++ CARFAC folder to store the ronw@698: % test data. ronw@698: test_data_dir = '../../../carfac/test_data/'; alexbrandmeyer@668: ronw@698: test_name = 'binaural_test'; ronw@698: samples_to_read = [9000, 9903]; % Trim for a faster test. ronw@698: signal = wavread([test_data_dir test_name '.wav'], samples_to_read); ronw@698: assert(size(signal, 2) == 1, 'Expected mono signal.'); ronw@698: % Construct a binaural signal by delaying the signal between the ears. ronw@698: itd_offset = 22; % about 1 ms ronw@698: signal = [signal((itd_offset+1):end), signal(1:(end-itd_offset))] / 10; ronw@698: n_ears = size(signal, 2); ronw@698: CF_struct = CARFAC_Design(n_ears); ronw@698: WriteTestData(test_data_dir, 'binaural_test', signal, CF_struct); ronw@698: ronw@698: ronw@698: test_name = 'long_test'; ronw@698: samples_to_read = [80001, 82000]; % Trim for a faster test. ronw@698: [signal, fs] = wavread([test_data_dir test_name '.wav'], samples_to_read); ronw@698: assert(size(signal, 2) == 2, 'Expected stereo signal.'); ronw@698: n_ears = size(signal, 2); ronw@698: CF_struct = CARFAC_Design(n_ears, fs); ronw@698: WriteTestData(test_data_dir, 'long_test', signal, CF_struct); ronw@698: ronw@698: ronw@698: function WriteTestData(test_data_dir, test_name, signal, CF_struct) alexbrandmeyer@678: % The following section generates data for the binaural test of the C++ alexbrandmeyer@678: % version of CARFAC. ronw@698: filename_prefix = [test_data_dir test_name]; ronw@698: ronw@698: WriteMatrixToFile([filename_prefix '-audio.txt'], signal); ronw@698: ronw@698: CF_struct = CARFAC_Init(CF_struct); ronw@698: [CF_struct, nap_decim, nap, bm, ohc, agc] = CARFAC_Run(CF_struct, signal); alexbrandmeyer@678: ronw@698: % Store the data for each ear of each output signal in a separate file. ronw@698: for ear = 1:CF_struct.n_ears ronw@698: WriteMatrixToFile([filename_prefix '-matlab-nap' num2str(ear) '.txt'], ... ronw@698: nap(:,:,ear)); ronw@698: WriteMatrixToFile([filename_prefix '-matlab-bm' num2str(ear) '.txt'], ... ronw@698: bm(:,:,ear)); ronw@698: end alexbrandmeyer@668: alexbrandmeyer@668: ronw@698: function WriteMatrixToFile(filename, matrix) ronw@698: precision_level = 9; ronw@698: dlmwrite(filename, matrix, 'precision', precision_level, 'delimiter', ' ');