diff carfac/carfac_test.cc @ 637:efc5b1b54f63

Sixth revision of Alex Brandmeyer's C++ implementation. Only small changes in response to Lyon's comments from r285. Note: I tried to use a consistent indentation with two spaces, but also preserving parenthetical structure to make reading the longer equations easier. Please advise if this is OK. Additional documentation and tests with non-standard parameter sets will be added in a subsequent revision.
author alexbrandmeyer
date Tue, 28 May 2013 15:54:54 +0000
parents 27f2d9b76075
children d08c02c8e26f
line wrap: on
line diff
--- a/carfac/carfac_test.cc	Mon May 27 16:36:54 2013 +0000
+++ b/carfac/carfac_test.cc	Tue May 28 15:54:54 2013 +0000
@@ -25,27 +25,31 @@
 // GoogleTest is now included for running unit tests
 #include <gtest/gtest.h>
 #include "carfac.h"
+
 using std::vector;
 using std::string;
 using std::ifstream;
 using std::ofstream;
 
-#define TEST_SRC_DIR "./test_data/"
+// This is the 'test_data' subdirectory of aimc/carfac that specifies where to
+// locate the text files produced by 'CARFAC_GenerateTestData.m' for comparing
+// the ouput of the Matlab version of CARFAC with this C++ version.
+static const char* kTestSourceDir= "./test_data/";
 // Here we specify the level to which the output should match (7 decimals).
-#define PRECISION_LEVEL 1.0e-07
+static const float kPrecisionLevel = 1.0e-7;
 
 // Three helper functions are defined here for loading the test data generated
 // by the Matlab version of CARFAC.
 // This loads one-dimensional FloatArrays from single-column text files.
 void WriteNAPOutput(CARFACOutput output, const string filename, int ear) {
-  string fullfile = TEST_SRC_DIR + filename;
+  string fullfile = kTestSourceDir + filename;
   ofstream ofile(fullfile.c_str());
-  int32_t n_timepoints = output.nap_.size();
-  int channels = output.nap_[0][0].size();
+  int32_t n_timepoints = output.nap().size();
+  int channels = output.nap()[0][0].size();
   if (ofile.is_open()) {
     for (int32_t i = 0; i < n_timepoints; ++i) {
       for (int j = 0; j < channels; ++j) {
-        ofile << output.nap(ear, i, j);
+        ofile << output.nap()[i][ear](j);
         if ( j < channels - 1) {
           ofile << " ";
         }
@@ -56,8 +60,8 @@
   ofile.close();
 }
 
-FloatArray LoadTestData(const std::string filename, const int number_points) {
-  string fullfile = TEST_SRC_DIR + filename;
+FloatArray LoadTestData(const string filename, const int number_points) {
+  string fullfile = kTestSourceDir + filename;
   ifstream file(fullfile.c_str());
   FPType myarray[number_points];
   FloatArray output(number_points);
@@ -74,7 +78,7 @@
 // This loads a vector of FloatArrays from multi-column text files.
 vector<FloatArray> Load2dTestData(const string filename, const int rows,
                             const int columns) {
-  string fullfile = TEST_SRC_DIR + filename;
+  string fullfile = kTestSourceDir + filename;
   ifstream file(fullfile.c_str());
   FPType myarray[rows][columns];
   vector<FloatArray> output;
@@ -98,7 +102,7 @@
 // Matlab using the wavread() function.
 vector<vector<float>> Load2dAudioVector(string filename, int timepoints,
                                         int channels) {
-  string fullfile = TEST_SRC_DIR + filename;
+  string fullfile = kTestSourceDir + filename;
   ifstream file(fullfile.c_str());
   vector<vector<float>> output;
   output.resize(channels);
@@ -120,22 +124,17 @@
   int n_timepoints = 882;
   int n_channels = 71;
   int n_ears = 2;
-  std::string filename = "binaural_test_nap1.txt";
-  std::vector<FloatArray> nap1 = Load2dTestData(filename, n_timepoints,
-                                                n_channels);
+  string filename = "binaural_test_nap1.txt";
+  vector<FloatArray> nap1 = Load2dTestData(filename, n_timepoints, n_channels);
   filename = "binaural_test_bm1.txt";
-  std::vector<FloatArray> bm1 = Load2dTestData(filename, n_timepoints,
-                                               n_channels);
+  vector<FloatArray> bm1 = Load2dTestData(filename, n_timepoints, n_channels);
   filename = "binaural_test_nap2.txt";
-  std::vector<FloatArray> nap2 = Load2dTestData(filename, n_timepoints,
-                                                n_channels);
+  vector<FloatArray> nap2 = Load2dTestData(filename, n_timepoints, n_channels);
   filename = "binaural_test_bm2.txt";
-  std::vector<FloatArray> bm2 = Load2dTestData(filename, n_timepoints,
-                                               n_channels);
+  vector<FloatArray> bm2 = Load2dTestData(filename, n_timepoints, n_channels);
   filename = "file_signal_binaural_test.txt";
-  std::vector<std::vector<float>> sound_data = Load2dAudioVector(filename,
-                                                                 n_timepoints,
-                                                                 n_ears);
+  vector<vector<float>> sound_data = Load2dAudioVector(filename, n_timepoints,
+                                                       n_ears);
   CARParams car_params;
   IHCParams ihc_params;
   AGCParams agc_params;
@@ -153,23 +152,23 @@
   int n_ch = 71;
   for (int timepoint = 0; timepoint < n_timepoints; ++timepoint) {
     for (int channel = 0; channel < n_ch; ++channel) {
-      FPType cplusplus = my_output.nap(ear, timepoint, channel);
+      FPType cplusplus = my_output.nap()[timepoint][ear](channel);
       FPType matlab = nap1[timepoint](channel);
-      ASSERT_NEAR(cplusplus, matlab, PRECISION_LEVEL);
-      cplusplus = my_output.bm(ear, timepoint, channel);
+      ASSERT_NEAR(cplusplus, matlab, kPrecisionLevel);
+      cplusplus = my_output.bm()[timepoint][ear](channel);
       matlab = bm1[timepoint](channel);
-      ASSERT_NEAR(cplusplus, matlab, PRECISION_LEVEL);
+      ASSERT_NEAR(cplusplus, matlab, kPrecisionLevel);
     }
   }
   ear = 1;
   for (int timepoint = 0; timepoint < n_timepoints; ++timepoint) {
     for (int channel = 0; channel < n_ch; ++channel) {
-      FPType cplusplus = my_output.nap(ear, timepoint, channel);
+      FPType cplusplus = my_output.nap()[timepoint][ear](channel);
       FPType matlab = nap2[timepoint](channel);
-      ASSERT_NEAR(cplusplus, matlab, PRECISION_LEVEL);
-      cplusplus = my_output.bm(ear, timepoint, channel);
+      ASSERT_NEAR(cplusplus, matlab, kPrecisionLevel);
+      cplusplus = my_output.bm()[timepoint][ear](channel);
       matlab = bm2[timepoint](channel);
-      ASSERT_NEAR(cplusplus, matlab, PRECISION_LEVEL);
+      ASSERT_NEAR(cplusplus, matlab, kPrecisionLevel);
     }
   }
 }
@@ -179,21 +178,16 @@
   int n_channels = 83;
   int n_ears = 2;
   string filename = "long_test_nap1.txt";
-  vector<FloatArray> nap1 = Load2dTestData(filename, n_timepoints,
-                                                n_channels);
+  vector<FloatArray> nap1 = Load2dTestData(filename, n_timepoints, n_channels);
   filename = "long_test_bm1.txt";
-  vector<FloatArray> bm1 = Load2dTestData(filename, n_timepoints,
-                                               n_channels);
+  vector<FloatArray> bm1 = Load2dTestData(filename, n_timepoints, n_channels);
   filename = "long_test_nap2.txt";
-  vector<FloatArray> nap2 = Load2dTestData(filename, n_timepoints,
-                                                n_channels);
+  vector<FloatArray> nap2 = Load2dTestData(filename, n_timepoints, n_channels);
   filename = "long_test_bm2.txt";
-  vector<FloatArray> bm2 = Load2dTestData(filename, n_timepoints,
-                                               n_channels);
+  vector<FloatArray> bm2 = Load2dTestData(filename, n_timepoints, n_channels);
   filename = "file_signal_long_test.txt";
-  vector<vector<float>> sound_data = Load2dAudioVector(filename,
-                                                                 n_timepoints,
-                                                                 n_ears);
+  vector<vector<float>> sound_data = Load2dAudioVector(filename, n_timepoints,
+                                                       n_ears);
   CARParams car_params;
   IHCParams ihc_params;
   AGCParams agc_params;
@@ -210,30 +204,23 @@
   int ear = 0;
   for (int timepoint = 0; timepoint < n_timepoints; ++timepoint) {
     for (int channel = 0; channel < n_channels; ++channel) {
-      FPType cplusplus = my_output.nap(ear, timepoint, channel);
+      FPType cplusplus = my_output.nap()[timepoint][ear](channel);
       FPType matlab = nap1[timepoint](channel);
-      ASSERT_NEAR(cplusplus, matlab, PRECISION_LEVEL);
-      cplusplus = my_output.bm(ear, timepoint, channel);
+      ASSERT_NEAR(cplusplus, matlab, kPrecisionLevel);
+      cplusplus = my_output.bm()[timepoint][ear](channel);
       matlab = bm1[timepoint](channel);
-      ASSERT_NEAR(cplusplus, matlab, PRECISION_LEVEL);
+      ASSERT_NEAR(cplusplus, matlab, kPrecisionLevel);
     }
   }
   ear = 1;
   for (int timepoint = 0; timepoint < n_timepoints; ++timepoint) {
     for (int channel = 0; channel < n_channels; ++channel) {
-      FPType cplusplus = my_output.nap(ear, timepoint, channel);
+      FPType cplusplus = my_output.nap()[timepoint][ear](channel);
       FPType matlab = nap2[timepoint](channel);
-      ASSERT_NEAR(cplusplus, matlab, PRECISION_LEVEL);
-      cplusplus = my_output.bm(ear, timepoint, channel);
+      ASSERT_NEAR(cplusplus, matlab, kPrecisionLevel);
+      cplusplus = my_output.bm()[timepoint][ear](channel);
       matlab = bm2[timepoint](channel);
-      ASSERT_NEAR(cplusplus, matlab, PRECISION_LEVEL);
+      ASSERT_NEAR(cplusplus, matlab, kPrecisionLevel);
     }
   }
-}
-
-int main(int argc, char **argv) {
-  // This initializes the GoogleTest unit testing framework.
-  ::testing::InitGoogleTest(&argc, argv);
-  // This runs all of the tests that we've defined above.
-  return RUN_ALL_TESTS();
 }
\ No newline at end of file