19#ifndef KALDI_NATIVE_FBANK_CSRC_TEST_GOLDEN_H_
20#define KALDI_NATIVE_FBANK_CSRC_TEST_GOLDEN_H_
39 std::ifstream is(path, std::ios::binary);
45 if (std::string(magic, 6) !=
"\x93NUMPY") {
53 is.read(
reinterpret_cast<char *
>(&h16), 2);
56 is.read(
reinterpret_cast<char *
>(&hlen), 4);
58 std::string header(hlen,
'\0');
59 is.read(&header[0], hlen);
61 auto dp = header.find(
"'descr': '") + 10;
62 std::string descr = header.substr(dp, 3);
63 auto sp = header.find(
"'shape': (") + 10;
64 auto ep = header.find(
')', sp);
65 std::string shape = header.substr(sp, ep - sp);
68 if (shape.find(
',') != std::string::npos) {
69 std::string col_part = shape.substr(shape.find(
',') + 1);
70 rows = std::stoi(shape.substr(0, shape.find(
',')));
72 cols = col_part.find_first_not_of(
" \t") == std::string::npos
74 : std::stoi(col_part);
76 rows = std::stoi(shape);
81 for (int32_t r = 0; r < rows; ++r) {
82 is.read(
reinterpret_cast<char *
>((*out).row(r).data()),
83 cols *
sizeof(
double));
85 }
else if (descr ==
"<i2") {
87 std::vector<int16_t> tmp(cols);
88 for (int32_t r = 0; r < rows; ++r) {
89 is.read(
reinterpret_cast<char *
>(tmp.data()), cols *
sizeof(int16_t));
90 for (int32_t c = 0; c < cols; ++c) {
91 (*out)(r, c) = tmp[c];
95 std::vector<float> tmp(cols);
96 for (int32_t r = 0; r < rows; ++r) {
97 is.read(
reinterpret_cast<char *
>(tmp.data()), cols *
sizeof(
float));
98 for (int32_t c = 0; c < cols; ++c) {
99 (*out)(r, c) = tmp[c];
112 std::string dir = __FILE__;
113 std::string::size_type pos = dir.find_last_of(
"/\\");
114 dir = (pos == std::string::npos) ? std::string(
".") : dir.substr(0, pos);
115 return dir +
"/../../tests/data/feat_golden";
121 double max_diff = 0.0;
122 for (int32_t r = 0; r < a.rows(); ++r) {
123 for (int32_t c = 0; c < a.cols(); ++c) {
124 double diff = std::fabs(
static_cast<double>(a(r, c)) - b(r, c));
125 if (diff > max_diff) {
kaldi-native-fbank/kaldi-native-fbank/csrc/feature-matrix.h
This file is part of kaldi-native-fbank.
Definition feature-cmvn.h:42
bool LoadNpy(const std::string &path, MatrixD *out)
Minimal .npy (v1/v2, C-order, float32/float64) reader for golden files.
Definition test-golden.h:38
double MaxAbsDiff(const MatrixF &a, const MatrixD &b)
Return the maximum absolute difference between a float matrix and a double matrix of the same shape (...
Definition test-golden.h:120
constexpr double kGoldenDotProductTol
Comparison tolerance for golden matrices whose kaldi reference was produced by matrix multiplications...
Definition test-golden.h:143
Eigen::Matrix< float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MatrixF
Row-major feature matrix: rows are frames, columns are features.
Definition feature-matrix.h:26
std::string GoldenDir()
Locate tests/data/feat_golden relative to this header.
Definition test-golden.h:111
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MatrixD
Double-precision accumulator/statistics matrix (e.g.
Definition feature-matrix.h:31