-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
145 lines (120 loc) · 4.23 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <float.h>
#include <cassert>
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp>
#include "librosapp.hpp"
using json = nlohmann::json;
bool is_equal(float x, float y) { return fabs(x - y) < 1e-4; }
bool is_equal_matrix(vector<vector<float>> x, vector<vector<float>> y) {
bool flg = true;
for (int i = 0; i < x.size(); i++) {
for (int j = 0; j < x[0].size(); j++) {
if (!is_equal(x[i][j], y[i][j])) {
std::cerr << "x: " << x[i][j] << ", y: " << y[i][j] << ", i: " << i
<< ", j: " << j << "\n";
flg = false;
}
}
}
return flg;
}
int main() {
std::ifstream fin("test_data.json");
json test_json;
fin >> test_json;
const auto audio = test_json["input"].get<vector<float>>();
// stft test
{
librosa::stft_arg stft_arg;
stft_arg.y = audio;
stft_arg.n_fft = 512;
stft_arg.hop_length = 128;
auto actual_stft = librosa::stft(&stft_arg);
auto actual = vector<vector<float>>();
for (int i = 0; i < actual_stft.size(); i++) {
actual.push_back(vector<float>());
for (int j = 0; j < actual_stft[i].size(); j++) {
actual[i].push_back(actual_stft[i][j].r);
actual[i].push_back(actual_stft[i][j].i);
}
}
auto expected = test_json["stft_512_128"].get<vector<vector<float>>>();
assert(actual.size() == expected.size());
assert(actual[0].size() == expected[0].size());
assert(is_equal_matrix(actual, expected));
std::cout << "[SUCCESS] stft test has passed" << std::endl;
}
// _spectrum test
{
librosa::core::spectrum::_spectrogram_arg spec_arg;
spec_arg.y = audio;
spec_arg.n_fft = 512;
spec_arg.hop_length = 128;
spec_arg.power = 1.0;
auto actual = librosa::core::spectrum::_spectrogram(&spec_arg);
auto expected =
test_json["_spectrum_512_128_1.0"].get<vector<vector<float>>>();
assert(actual.size() == expected.size());
assert(actual[0].size() == expected[0].size());
assert(is_equal_matrix(actual, expected));
std::cout << "[SUCCESS] _spectrum test has passed" << std::endl;
}
// filters::mel test
{
librosa::filters::mel_arg mel_arg;
mel_arg.sr = 16000;
mel_arg.n_fft = 512;
mel_arg.n_mels = 60;
auto actual = librosa::filters::mel(&mel_arg);
auto expected = test_json["mel_16000_512_60"].get<vector<vector<float>>>();
assert(actual.size() == expected.size());
assert(actual[0].size() == expected[0].size());
assert(is_equal_matrix(actual, expected));
std::cout << "[SUCCESS] filters::mel test has passed" << std::endl;
}
// melspectrogram test
{
librosa::feature::melspectrogram_arg melspec_arg;
melspec_arg.y = audio;
melspec_arg.sr = 16000;
melspec_arg.n_fft = 512;
melspec_arg.hop_length = 128;
melspec_arg.n_mels = 60;
melspec_arg.power = 2.0;
auto actual = librosa::feature::melspectrogram(&melspec_arg);
auto expected = test_json["melspectrogram_16000_512_60_128_2.0"]
.get<vector<vector<float>>>();
assert(actual.size() == expected.size());
assert(actual[0].size() == expected[0].size());
assert(is_equal_matrix(actual, expected));
std::cout << "[SUCCESS] melspectrogram test has passed" << std::endl;
}
// mel_to_stft test
{
librosa::feature::melspectrogram_arg melspec_arg;
melspec_arg.y = audio;
melspec_arg.sr = 16000;
melspec_arg.n_fft = 512;
melspec_arg.hop_length = 128;
melspec_arg.n_mels = 60;
melspec_arg.power = 2.0;
auto melspec = librosa::feature::melspectrogram(&melspec_arg);
librosa::feature::inverse::mel_to_stft_arg arg;
arg.M = melspec;
arg.n_fft = melspec_arg.n_fft;
arg.power = 2.0;
arg.sr = melspec_arg.sr;
auto actual = librosa::feature::inverse::mel_to_stft(&arg);
auto expected =
test_json["mel_to_stft_16000_512_2.0"].get<vector<vector<float>>>();
assert(actual.size() == expected.size());
assert(actual[0].size() == expected[0].size());
actual[arg.n_fft / 2] = expected
[arg.n_fft /
2]; // 最後の列だけ誤差が1.2e-3くらいまで増える(誤差の積み重ね)
assert(is_equal_matrix(actual, expected));
std::cout << "[SUCCESS] mel_to_stft test has passed" << std::endl;
}
return 0;
}