kaldi-native-fbank C++ API
Kaldi-compatible feature extraction C++ API
Loading...
Searching...
No Matches
test-golden.h
Go to the documentation of this file.
1
2//
3// Copyright (c) 2026 Xiaomi Corporation
4//
5// This file is part of kaldi-native-fbank.
6//
7// See ../../LICENSE for more information
8
18
19#ifndef KALDI_NATIVE_FBANK_CSRC_TEST_GOLDEN_H_
20#define KALDI_NATIVE_FBANK_CSRC_TEST_GOLDEN_H_
21
22#include <stdint.h>
23
24#include <cmath>
25#include <fstream>
26#include <string>
27#include <vector>
28
30
31namespace knf {
32
38inline bool LoadNpy(const std::string &path, MatrixD *out) {
39 std::ifstream is(path, std::ios::binary);
40 if (!is) {
41 return false;
42 }
43 char magic[6];
44 is.read(magic, 6);
45 if (std::string(magic, 6) != "\x93NUMPY") {
46 return false;
47 }
48 char ver[2];
49 is.read(ver, 2);
50 uint32_t hlen = 0;
51 if (ver[0] == 1) {
52 uint16_t h16 = 0;
53 is.read(reinterpret_cast<char *>(&h16), 2);
54 hlen = h16;
55 } else {
56 is.read(reinterpret_cast<char *>(&hlen), 4);
57 }
58 std::string header(hlen, '\0');
59 is.read(&header[0], hlen);
60
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);
66 int32_t rows = 0;
67 int32_t cols = 0;
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(',')));
71 // 1-D arrays have shape "(N,)", i.e. an empty column part.
72 cols = col_part.find_first_not_of(" \t") == std::string::npos
73 ? 1
74 : std::stoi(col_part);
75 } else {
76 rows = std::stoi(shape);
77 cols = 1;
78 }
79 *out = MatrixD(rows, cols);
80 if (descr == "<f8") {
81 for (int32_t r = 0; r < rows; ++r) {
82 is.read(reinterpret_cast<char *>((*out).row(r).data()),
83 cols * sizeof(double));
84 }
85 } else if (descr == "<i2") {
86 // int16 (e.g. raw wav samples stored as .npy).
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];
92 }
93 }
94 } else {
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];
100 }
101 }
102 }
103 return true;
104}
105
111inline std::string GoldenDir() {
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";
116}
117
120inline double MaxAbsDiff(const MatrixF &a, const MatrixD &b) {
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) {
126 max_diff = diff;
127 }
128 }
129 }
130 return max_diff;
131}
132
143constexpr double kGoldenDotProductTol = 1e-4;
144
145} // namespace knf
146
147#endif // KALDI_NATIVE_FBANK_CSRC_TEST_GOLDEN_H_
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