alexbrandmeyer@626: // alexbrandmeyer@626: // carfac_test.cc alexbrandmeyer@626: // CARFAC Open Source C++ Library alexbrandmeyer@626: // alexbrandmeyer@626: // Created by Alex Brandmeyer on 5/22/13. alexbrandmeyer@626: // alexbrandmeyer@626: // This C++ file is part of an implementation of Lyon's cochlear model: alexbrandmeyer@626: // "Cascade of Asymmetric Resonators with Fast-Acting Compression" alexbrandmeyer@626: // to supplement Lyon's upcoming book "Human and Machine Hearing" alexbrandmeyer@626: // alexbrandmeyer@626: // Licensed under the Apache License, Version 2.0 (the "License"); alexbrandmeyer@626: // you may not use this file except in compliance with the License. alexbrandmeyer@626: // You may obtain a copy of the License at alexbrandmeyer@626: // alexbrandmeyer@626: // http://www.apache.org/licenses/LICENSE-2.0 alexbrandmeyer@626: // alexbrandmeyer@626: // Unless required by applicable law or agreed to in writing, software alexbrandmeyer@626: // distributed under the License is distributed on an "AS IS" BASIS, alexbrandmeyer@626: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. alexbrandmeyer@626: // See the License for the specific language governing permissions and alexbrandmeyer@626: // limitations under the License. alexbrandmeyer@626: ronw@647: #include "carfac.h" ronw@647: alexbrandmeyer@636: #include alexbrandmeyer@640: #include ronw@642: ronw@660: #include ronw@660: ronw@642: #include "gtest/gtest.h" alexbrandmeyer@643: ronw@646: #include "agc.h" alexbrandmeyer@643: #include "car.h" ronw@647: #include "carfac_output.h" alexbrandmeyer@643: #include "common.h" ronw@646: #include "ihc.h" ronw@660: #include "test_util.h" alexbrandmeyer@637: ronw@652: using std::deque; ronw@646: using std::string; ronw@646: using std::vector; alexbrandmeyer@636: ronw@660: // Reads a two dimensional vector of audio data from a text file ronw@660: // containing the output of the Matlab wavread() function. ronw@660: vector> LoadAudio(const string& filename, int timepoints, ronw@660: int num_channels) { ronw@660: return LoadMatrix, false>(filename, timepoints, num_channels); ronw@660: } ronw@630: ronw@652: // Writes the CARFAC NAP output to a text file. ronw@652: void WriteNAPOutput(const CARFACOutput& output, const string& filename, ronw@646: int ear) { ronw@661: const int num_samples = output.nap().size(); ronw@661: const int num_channels = output.nap()[0][0].size(); ronw@661: ArrayXX nap_matrix(num_samples, num_channels); ronw@661: for (int i = 0; i < num_samples; ++i) { ronw@661: nap_matrix.row(i) = output.nap()[i][ear]; alexbrandmeyer@636: } ronw@661: ronw@661: WriteMatrix(filename, nap_matrix); alexbrandmeyer@636: } alexbrandmeyer@636: ronw@652: class CARFACTest : public testing::Test { ronw@652: protected: ronw@654: deque> LoadTestData(const string& basename, ronw@654: int num_samples, ronw@654: int num_ears, ronw@654: int num_channels) const { ronw@654: deque> test_data(num_samples, vector(num_ears)); ronw@654: for (int ear = 0; ear < num_ears; ++ear) { ronw@652: string filename = basename + std::to_string(ear + 1) + ".txt"; ronw@660: vector data = LoadMatrix(filename, num_samples, num_channels); ronw@654: for (int i = 0; i < num_samples; ++i) { ronw@652: test_data[i][ear] = data[i]; ronw@652: } ronw@652: } ronw@652: return test_data; alexbrandmeyer@626: } ronw@652: ronw@652: void AssertCARFACOutputNear(const deque>& expected, ronw@652: const deque>& actual, ronw@654: int num_samples, ronw@660: int num_ears) const { ronw@654: for (int timepoint = 0; timepoint < num_samples; ++timepoint) { ronw@654: for (int ear = 0; ear < num_ears; ++ear) { ronw@660: const float kPrecisionLevel = 1.0e-7; ronw@661: AssertArrayNear(expected[timepoint][ear], actual[timepoint][ear], ronw@661: kPrecisionLevel); ronw@652: } alexbrandmeyer@626: } alexbrandmeyer@626: } ronw@652: ronw@660: void RunCARFACAndCompareWithMatlab(const string& test_name, ronw@660: int num_samples, ronw@660: int num_ears, ronw@660: int num_channels, ronw@660: FPType sample_rate) const { ronw@660: vector> sound_data = ronw@660: LoadAudio(test_name + "-audio.txt", num_samples, num_ears); ronw@660: ronw@660: CARParams car_params; ronw@660: IHCParams ihc_params; ronw@660: AGCParams agc_params; ronw@660: CARFAC carfac(num_ears, sample_rate, car_params, ihc_params, agc_params); ronw@660: CARFACOutput output(true, true, false, false); ronw@660: const bool kOpenLoop = false; ronw@660: const int length = sound_data[0].size(); ronw@660: carfac.RunSegment(sound_data, 0, length, kOpenLoop, &output); ronw@660: ronw@660: // TODO(ronw): Don't unconditionally overwrite files that are ronw@660: // checked in to the repository on every test run. ronw@660: WriteNAPOutput(output, test_name + "-cpp-nap1.txt", 0); ronw@660: WriteNAPOutput(output, test_name + "-cpp-nap2.txt", 1); ronw@660: ronw@660: deque> expected_nap = LoadTestData( ronw@660: test_name + "-matlab-nap", num_samples, num_ears, num_channels); ronw@660: AssertCARFACOutputNear(expected_nap, output.nap(), num_samples, num_ears); ronw@660: deque> expected_bm = LoadTestData( ronw@660: test_name + "-matlab-bm", num_samples, num_ears, num_channels); ronw@660: AssertCARFACOutputNear(expected_bm, output.bm(), num_samples, num_ears); ronw@660: } ronw@652: }; ronw@652: ronw@660: TEST_F(CARFACTest, MatchesMatlabOnBinauralData) { ronw@654: const int kNumSamples = 882; ronw@654: const int kNumEars = 2; ronw@654: const int kNumChannels = 71; ronw@660: const FPType kSampleRate = 22050.0; ronw@660: RunCARFACAndCompareWithMatlab( ronw@660: "binaural_test", kNumSamples, kNumEars, kNumChannels, kSampleRate); alexbrandmeyer@626: } alexbrandmeyer@626: ronw@660: TEST_F(CARFACTest, MatchesMatlabOnLongBinauralData) { ronw@654: const int kNumSamples = 2000; ronw@654: const int kNumEars = 2; ronw@654: const int kNumChannels = 83; ronw@660: const FPType kSampleRate = 44100.0; ronw@660: RunCARFACAndCompareWithMatlab( ronw@660: "long_test", kNumSamples, kNumEars, kNumChannels, kSampleRate); alexbrandmeyer@626: }