ronw@660
|
1 // Copyright 2013, Google, Inc.
|
ronw@660
|
2 // Author: Ron Weiss <ronw@google.com>
|
ronw@660
|
3 //
|
ronw@660
|
4 // This C++ file is part of an implementation of Lyon's cochlear model:
|
ronw@660
|
5 // "Cascade of Asymmetric Resonators with Fast-Acting Compression"
|
ronw@660
|
6 // to supplement Lyon's upcoming book "Human and Machine Hearing"
|
ronw@660
|
7 //
|
ronw@660
|
8 // Licensed under the Apache License, Version 2.0 (the "License");
|
ronw@660
|
9 // you may not use this file except in compliance with the License.
|
ronw@660
|
10 // You may obtain a copy of the License at
|
ronw@660
|
11 //
|
ronw@660
|
12 // http://www.apache.org/licenses/LICENSE-2.0
|
ronw@660
|
13 //
|
ronw@660
|
14 // Unless required by applicable law or agreed to in writing, software
|
ronw@660
|
15 // distributed under the License is distributed on an "AS IS" BASIS,
|
ronw@660
|
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
ronw@660
|
17 // See the License for the specific language governing permissions and
|
ronw@660
|
18 // limitations under the License.
|
ronw@660
|
19
|
ronw@660
|
20 // Shared test utilities.
|
ronw@660
|
21
|
ronw@660
|
22 #ifndef CARFAC_TEST_UTIL_H
|
ronw@660
|
23 #define CARFAC_TEST_UTIL_H
|
ronw@660
|
24
|
ronw@660
|
25 #include <fstream>
|
ronw@660
|
26 #include <string>
|
ronw@660
|
27 #include <vector>
|
ronw@660
|
28
|
ronw@660
|
29 #include <Eigen/Core>
|
ronw@660
|
30
|
ronw@661
|
31 #include "gtest/gtest.h"
|
ronw@661
|
32
|
ronw@660
|
33 #include "common.h"
|
ronw@660
|
34
|
ronw@660
|
35 // Location of the text files produced by 'CARFAC_GenerateTestData.m' for
|
ronw@660
|
36 // comparing the ouput of the Matlab implementation with the C++ one.
|
ronw@660
|
37 static const char* kTestDataDir = "./test_data/";
|
ronw@660
|
38
|
ronw@660
|
39 // Reads a matrix (size rows vector of size columns Container objects)
|
ronw@660
|
40 // from a text file written using the Matlab dlmwrite function.
|
ronw@660
|
41 template <typename Container = ArrayX, bool ColMajor = true>
|
ronw@660
|
42 std::vector<Container> LoadMatrix(const std::string& filename, int rows,
|
ronw@660
|
43 int columns) {
|
ronw@660
|
44 std::string fullfile = kTestDataDir + filename;
|
ronw@660
|
45 std::ifstream file(fullfile.c_str());
|
ronw@660
|
46 std::vector<Container> output;
|
ronw@660
|
47 if (ColMajor) {
|
ronw@660
|
48 output.assign(rows, Container(columns));
|
ronw@660
|
49 } else {
|
ronw@660
|
50 output.assign(columns, Container(rows));
|
ronw@660
|
51 }
|
ronw@660
|
52 if (file.is_open()) {
|
ronw@660
|
53 for (int i = 0; i < rows; ++i) {
|
ronw@660
|
54 for (int j = 0; j < columns; ++j) {
|
ronw@660
|
55 if (ColMajor) {
|
ronw@660
|
56 file >> output[i][j];
|
ronw@660
|
57 } else {
|
ronw@660
|
58 file >> output[j][i];
|
ronw@660
|
59 }
|
ronw@660
|
60 }
|
ronw@660
|
61 }
|
ronw@660
|
62 }
|
ronw@660
|
63 file.close();
|
ronw@660
|
64 return output;
|
ronw@660
|
65 }
|
ronw@660
|
66
|
ronw@661
|
67 void WriteMatrix(const std::string& filename, const ArrayXX& matrix) {
|
ronw@661
|
68 std::string fullfile = kTestDataDir + filename;
|
ronw@661
|
69 std::ofstream ofile(fullfile.c_str());
|
ronw@661
|
70 const int kPrecision = 9;
|
ronw@661
|
71 ofile.precision(kPrecision);
|
ronw@661
|
72 if (ofile.is_open()) {
|
ronw@661
|
73 Eigen::IOFormat ioformat(kPrecision, Eigen::DontAlignCols);
|
ronw@661
|
74 ofile << matrix.format(ioformat) << std::endl;
|
ronw@661
|
75 }
|
ronw@661
|
76 ofile.close();
|
ronw@661
|
77 }
|
ronw@661
|
78
|
ronw@661
|
79 void AssertArrayNear(const ArrayX& expected, const ArrayX& actual,
|
ronw@661
|
80 double precision) {
|
ronw@661
|
81 ArrayX abs_difference = (expected - actual).cwiseAbs();
|
ronw@661
|
82 ASSERT_TRUE(abs_difference.maxCoeff() <= precision)
|
ronw@661
|
83 << "expected differs from actual by more than " << precision
|
ronw@661
|
84 << "\n max(abs(expected - actual)) = " << abs_difference.maxCoeff()
|
ronw@661
|
85 << "\n max(abs(expected)) = " << expected.cwiseAbs().maxCoeff()
|
ronw@661
|
86 << "\n max(abs(actual)) = " << actual.cwiseAbs().maxCoeff()
|
ronw@661
|
87 << "\n" << abs_difference;
|
ronw@660
|
88 }
|
ronw@660
|
89
|
ronw@660
|
90 #endif // CARFAC_TEST_UTIL_H
|