annotate trunk/matlab/bmm/carfac/CARFAC_GenerateTestData.m @ 698:cdb7fb83a03b

Unify naming scheme for test data and clean up code that generates it.
author ronw@google.com
date Thu, 27 Jun 2013 20:48:27 +0000
parents 7f424c1a8b78
children 597913e855fd
rev   line source
alexbrandmeyer@668 1 % Author: Alex Brandmeyer
alexbrandmeyer@668 2 %
alexbrandmeyer@668 3 % This Matlab file is part of an implementation of Lyon's cochlear model:
alexbrandmeyer@668 4 % "Cascade of Asymmetric Resonators with Fast-Acting Compression"
alexbrandmeyer@668 5 % to supplement Lyon's upcoming book "Human and Machine Hearing"
alexbrandmeyer@668 6 %
alexbrandmeyer@668 7 % Licensed under the Apache License, Version 2.0 (the "License");
alexbrandmeyer@668 8 % you may not use this file except in compliance with the License.
alexbrandmeyer@668 9 % You may obtain a copy of the License at
alexbrandmeyer@668 10 %
alexbrandmeyer@668 11 % http://www.apache.org/licenses/LICENSE-2.0
alexbrandmeyer@668 12 %
alexbrandmeyer@668 13 % Unless required by applicable law or agreed to in writing, software
alexbrandmeyer@668 14 % distributed under the License is distributed on an "AS IS" BASIS,
alexbrandmeyer@668 15 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
alexbrandmeyer@668 16 % See the License for the specific language governing permissions and
alexbrandmeyer@668 17 % limitations under the License.
alexbrandmeyer@668 18
alexbrandmeyer@668 19 function CARFAC_GenerateTestData()
alexbrandmeyer@668 20 % function GenerateTestData()
alexbrandmeyer@668 21 % This function generates a set of text files in the AIMC repository that
alexbrandmeyer@668 22 % can be used to compare the output of the C++ version of CARFAC with that
alexbrandmeyer@668 23 % of the Matlab version.
ronw@698 24 %
ronw@698 25 % Naming convention for files containing audio samples for file test_name.wav:
ronw@698 26 % test_name-audio.txt
ronw@698 27 % Each line contains a space-separated list of samples from each channel.
ronw@698 28 %
ronw@698 29 % Naming convention for files containing CARFAC/SAI outputs:
ronw@698 30 % test_name-{matlab,cpp}-signal_name(optional_channel_number).txt
ronw@698 31 % Each line contains a space-separated list of elements from a single row.
alexbrandmeyer@668 32
alexbrandmeyer@668 33 % This designates a subdirectory of the C++ CARFAC folder to store the
ronw@698 34 % test data.
ronw@698 35 test_data_dir = '../../../carfac/test_data/';
alexbrandmeyer@668 36
ronw@698 37 test_name = 'binaural_test';
ronw@698 38 samples_to_read = [9000, 9903]; % Trim for a faster test.
ronw@698 39 signal = wavread([test_data_dir test_name '.wav'], samples_to_read);
ronw@698 40 assert(size(signal, 2) == 1, 'Expected mono signal.');
ronw@698 41 % Construct a binaural signal by delaying the signal between the ears.
ronw@698 42 itd_offset = 22; % about 1 ms
ronw@698 43 signal = [signal((itd_offset+1):end), signal(1:(end-itd_offset))] / 10;
ronw@698 44 n_ears = size(signal, 2);
ronw@698 45 CF_struct = CARFAC_Design(n_ears);
ronw@698 46 WriteTestData(test_data_dir, 'binaural_test', signal, CF_struct);
ronw@698 47
ronw@698 48
ronw@698 49 test_name = 'long_test';
ronw@698 50 samples_to_read = [80001, 82000]; % Trim for a faster test.
ronw@698 51 [signal, fs] = wavread([test_data_dir test_name '.wav'], samples_to_read);
ronw@698 52 assert(size(signal, 2) == 2, 'Expected stereo signal.');
ronw@698 53 n_ears = size(signal, 2);
ronw@698 54 CF_struct = CARFAC_Design(n_ears, fs);
ronw@698 55 WriteTestData(test_data_dir, 'long_test', signal, CF_struct);
ronw@698 56
ronw@698 57
ronw@698 58 function WriteTestData(test_data_dir, test_name, signal, CF_struct)
alexbrandmeyer@678 59 % The following section generates data for the binaural test of the C++
alexbrandmeyer@678 60 % version of CARFAC.
ronw@698 61 filename_prefix = [test_data_dir test_name];
ronw@698 62
ronw@698 63 WriteMatrixToFile([filename_prefix '-audio.txt'], signal);
ronw@698 64
ronw@698 65 CF_struct = CARFAC_Init(CF_struct);
ronw@698 66 [CF_struct, nap_decim, nap, bm, ohc, agc] = CARFAC_Run(CF_struct, signal);
alexbrandmeyer@678 67
ronw@698 68 % Store the data for each ear of each output signal in a separate file.
ronw@698 69 for ear = 1:CF_struct.n_ears
ronw@698 70 WriteMatrixToFile([filename_prefix '-matlab-nap' num2str(ear) '.txt'], ...
ronw@698 71 nap(:,:,ear));
ronw@698 72 WriteMatrixToFile([filename_prefix '-matlab-bm' num2str(ear) '.txt'], ...
ronw@698 73 bm(:,:,ear));
ronw@698 74 end
alexbrandmeyer@668 75
alexbrandmeyer@668 76
ronw@698 77 function WriteMatrixToFile(filename, matrix)
ronw@698 78 precision_level = 9;
ronw@698 79 dlmwrite(filename, matrix, 'precision', precision_level, 'delimiter', ' ');