annotate carfac/test_util.h @ 660:499ffd3a50ba

Factor out common test utility functions and clean up carfac_test.
author ronw@google.com
date Mon, 01 Jul 2013 19:02:32 +0000
parents
children 7a0031c321da
rev   line source
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@660 31 #include "common.h"
ronw@660 32
ronw@660 33 // Location of the text files produced by 'CARFAC_GenerateTestData.m' for
ronw@660 34 // comparing the ouput of the Matlab implementation with the C++ one.
ronw@660 35 static const char* kTestDataDir = "./test_data/";
ronw@660 36
ronw@660 37 // Reads a matrix (size rows vector of size columns Container objects)
ronw@660 38 // from a text file written using the Matlab dlmwrite function.
ronw@660 39 template <typename Container = ArrayX, bool ColMajor = true>
ronw@660 40 std::vector<Container> LoadMatrix(const std::string& filename, int rows,
ronw@660 41 int columns) {
ronw@660 42 std::string fullfile = kTestDataDir + filename;
ronw@660 43 std::ifstream file(fullfile.c_str());
ronw@660 44 std::vector<Container> output;
ronw@660 45 if (ColMajor) {
ronw@660 46 output.assign(rows, Container(columns));
ronw@660 47 } else {
ronw@660 48 output.assign(columns, Container(rows));
ronw@660 49 }
ronw@660 50 if (file.is_open()) {
ronw@660 51 for (int i = 0; i < rows; ++i) {
ronw@660 52 for (int j = 0; j < columns; ++j) {
ronw@660 53 if (ColMajor) {
ronw@660 54 file >> output[i][j];
ronw@660 55 } else {
ronw@660 56 file >> output[j][i];
ronw@660 57 }
ronw@660 58 }
ronw@660 59 }
ronw@660 60 }
ronw@660 61 file.close();
ronw@660 62 return output;
ronw@660 63 }
ronw@660 64
ronw@660 65 bool ArraysNear(const ArrayX& expected, const ArrayX& actual,
ronw@660 66 double precision) {
ronw@660 67 return (expected - actual).cwiseAbs().maxCoeff() <= precision;
ronw@660 68 }
ronw@660 69
ronw@660 70 #endif // CARFAC_TEST_UTIL_H