annotate trunk/matlab/bmm/carfac/CARFAC_GenerateTestData.m @ 705:be2b68ced23d

Make the C++ SAI match the Matlab implementation. - Fix syntax error which caused a linear window to be used instead of the correct sine window. - Correct off-by-one indexing errors to compensate for 1-based indexing in Matlab. - Enable test to verify that the C++ output matches Matlab with high precision.
author ronw@google.com
date Tue, 16 Jul 2013 19:56:16 +0000
parents e9855b95cd04
children
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@700 37 sai_struct = struct('width', 500, ...
ronw@700 38 'future_lags', 250, ...
ronw@700 39 'window_width', 2000, ...
ronw@700 40 'n_window_pos', 2, ...
ronw@700 41 'channel_smoothing_scale', 0);
ronw@700 42
ronw@700 43
ronw@698 44 test_name = 'binaural_test';
ronw@698 45 samples_to_read = [9000, 9903]; % Trim for a faster test.
ronw@698 46 signal = wavread([test_data_dir test_name '.wav'], samples_to_read);
ronw@698 47 assert(size(signal, 2) == 1, 'Expected mono signal.');
ronw@698 48 % Construct a binaural signal by delaying the signal between the ears.
ronw@698 49 itd_offset = 22; % about 1 ms
ronw@698 50 signal = [signal((itd_offset+1):end), signal(1:(end-itd_offset))] / 10;
ronw@698 51 n_ears = size(signal, 2);
ronw@698 52 CF_struct = CARFAC_Design(n_ears);
ronw@700 53 WriteTestData(test_data_dir, 'binaural_test', signal, CF_struct, sai_struct);
ronw@698 54
ronw@698 55 test_name = 'long_test';
ronw@698 56 samples_to_read = [80001, 82000]; % Trim for a faster test.
ronw@698 57 [signal, fs] = wavread([test_data_dir test_name '.wav'], samples_to_read);
ronw@698 58 assert(size(signal, 2) == 2, 'Expected stereo signal.');
ronw@698 59 n_ears = size(signal, 2);
ronw@698 60 CF_struct = CARFAC_Design(n_ears, fs);
ronw@700 61 WriteTestData(test_data_dir, 'long_test', signal, CF_struct, sai_struct);
ronw@698 62
ronw@698 63
ronw@700 64 function WriteTestData(test_data_dir, test_name, signal, CF_struct, sai_struct)
alexbrandmeyer@678 65 % The following section generates data for the binaural test of the C++
alexbrandmeyer@678 66 % version of CARFAC.
ronw@698 67 filename_prefix = [test_data_dir test_name];
ronw@698 68
ronw@698 69 WriteMatrixToFile([filename_prefix '-audio.txt'], signal);
ronw@698 70
ronw@698 71 CF_struct = CARFAC_Init(CF_struct);
ronw@698 72 [CF_struct, nap_decim, nap, bm, ohc, agc] = CARFAC_Run(CF_struct, signal);
alexbrandmeyer@678 73
ronw@698 74 % Store the data for each ear of each output signal in a separate file.
ronw@698 75 for ear = 1:CF_struct.n_ears
ronw@698 76 WriteMatrixToFile([filename_prefix '-matlab-nap' num2str(ear) '.txt'], ...
ronw@698 77 nap(:,:,ear));
ronw@698 78 WriteMatrixToFile([filename_prefix '-matlab-bm' num2str(ear) '.txt'], ...
ronw@698 79 bm(:,:,ear));
ronw@698 80 end
alexbrandmeyer@668 81
ronw@700 82 ear = 1;
ronw@700 83 sai_struct = SAI_Run_Segment(sai_struct, nap(:,:,ear));
ronw@700 84 WriteMatrixToFile([filename_prefix '-matlab-sai' num2str(ear) '.txt'], ...
ronw@700 85 sai_struct.frame);
ronw@700 86
alexbrandmeyer@668 87
ronw@698 88 function WriteMatrixToFile(filename, matrix)
ronw@698 89 precision_level = 9;
ronw@698 90 dlmwrite(filename, matrix, 'precision', precision_level, 'delimiter', ' ');