blob: 88f073963b884619fe298a9da07d22ed9b69f50d [file] [log] [blame]
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/transient/wpd_tree.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000012
kwiberg85d8bb02016-02-16 20:39:36 -080013#include <memory>
pbos@webrtc.org788acd12014-12-15 09:41:24 +000014#include <sstream>
15#include <string>
16
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "modules/audio_processing/transient/daubechies_8_wavelet_coeffs.h"
18#include "modules/audio_processing/transient/file_utils.h"
Karl Wiberg6a4d4112018-03-23 10:39:34 +010019#include "rtc_base/system/file_wrapper.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "test/gtest.h"
21#include "test/testsupport/fileutils.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000022
23namespace webrtc {
24
25TEST(WPDTreeTest, Construction) {
26 const size_t kTestBufferSize = 100;
27 const int kLevels = 5;
28 const int kExpectedNumberOfNodes = (1 << (kLevels + 1)) - 1;
29
30 float test_buffer[kTestBufferSize];
31 memset(test_buffer, 0.f, kTestBufferSize * sizeof(*test_buffer));
32 float test_coefficients[] = {1.f, 2.f, 3.f, 4.f, 5.f};
Yves Gerey665174f2018-06-19 15:03:05 +020033 const size_t kTestCoefficientsLength =
34 sizeof(test_coefficients) / sizeof(test_coefficients[0]);
35 WPDTree tree(kTestBufferSize, test_coefficients, test_coefficients,
36 kTestCoefficientsLength, kLevels);
pbos@webrtc.org788acd12014-12-15 09:41:24 +000037 ASSERT_EQ(kExpectedNumberOfNodes, tree.num_nodes());
38 // Checks for NodeAt(level, index).
39 int nodes_at_level = 0;
40 for (int level = 0; level <= kLevels; ++level) {
41 nodes_at_level = 1 << level;
42 for (int i = 0; i < nodes_at_level; ++i) {
43 ASSERT_TRUE(NULL != tree.NodeAt(level, i));
44 }
45 // Out of bounds.
46 EXPECT_EQ(NULL, tree.NodeAt(level, -1));
47 EXPECT_EQ(NULL, tree.NodeAt(level, -12));
48 EXPECT_EQ(NULL, tree.NodeAt(level, nodes_at_level));
49 EXPECT_EQ(NULL, tree.NodeAt(level, nodes_at_level + 5));
50 }
51 // Out of bounds.
52 EXPECT_EQ(NULL, tree.NodeAt(-1, 0));
53 EXPECT_EQ(NULL, tree.NodeAt(-12, 0));
54 EXPECT_EQ(NULL, tree.NodeAt(kLevels + 1, 0));
55 EXPECT_EQ(NULL, tree.NodeAt(kLevels + 5, 0));
56 // Checks for Update().
57 EXPECT_EQ(0, tree.Update(test_buffer, kTestBufferSize));
58 EXPECT_EQ(-1, tree.Update(NULL, kTestBufferSize));
59 EXPECT_EQ(-1, tree.Update(test_buffer, kTestBufferSize - 1));
60}
61
62// This test is for the correctness of the tree.
63// Checks the results from the Matlab equivalent, it is done comparing the
64// results that are stored in the output files from Matlab.
65// It also writes the results in its own set of files in the out directory.
66// Matlab and output files contain all the results in double precision (Little
67// endian) appended.
Peter Boströme2976c82016-01-04 22:44:05 +010068#if defined(WEBRTC_IOS)
69TEST(WPDTreeTest, DISABLED_CorrectnessBasedOnMatlabFiles) {
70#else
71TEST(WPDTreeTest, CorrectnessBasedOnMatlabFiles) {
72#endif
pbos@webrtc.org788acd12014-12-15 09:41:24 +000073 // 10 ms at 16000 Hz.
74 const size_t kTestBufferSize = 160;
75 const int kLevels = 3;
76 const int kLeaves = 1 << kLevels;
77 const size_t kLeavesSamples = kTestBufferSize >> kLevels;
78 // Create tree with Discrete Meyer Wavelet Coefficients.
Yves Gerey665174f2018-06-19 15:03:05 +020079 WPDTree tree(kTestBufferSize, kDaubechies8HighPassCoefficients,
80 kDaubechies8LowPassCoefficients, kDaubechies8CoefficientsLength,
pbos@webrtc.org788acd12014-12-15 09:41:24 +000081 kLevels);
82 // Allocate and open all matlab and out files.
kwiberg85d8bb02016-02-16 20:39:36 -080083 std::unique_ptr<FileWrapper> matlab_files_data[kLeaves];
84 std::unique_ptr<FileWrapper> out_files_data[kLeaves];
pbos@webrtc.org788acd12014-12-15 09:41:24 +000085
86 for (int i = 0; i < kLeaves; ++i) {
87 // Matlab files.
88 matlab_files_data[i].reset(FileWrapper::Create());
89
90 std::ostringstream matlab_stream;
91 matlab_stream << "audio_processing/transient/wpd" << i;
92 std::string matlab_string = test::ResourcePath(matlab_stream.str(), "dat");
tommia6219cc2016-06-15 10:30:14 -070093 matlab_files_data[i]->OpenFile(matlab_string.c_str(), true); // Read only.
pbos@webrtc.org788acd12014-12-15 09:41:24 +000094
tommia6219cc2016-06-15 10:30:14 -070095 bool file_opened = matlab_files_data[i]->is_open();
pbos@webrtc.org788acd12014-12-15 09:41:24 +000096 ASSERT_TRUE(file_opened) << "File could not be opened.\n" << matlab_string;
97
98 // Out files.
99 out_files_data[i].reset(FileWrapper::Create());
100
101 std::ostringstream out_stream;
102 out_stream << test::OutputPath() << "wpd_" << i << ".out";
103 std::string out_string = out_stream.str();
104
tommia6219cc2016-06-15 10:30:14 -0700105 out_files_data[i]->OpenFile(out_string.c_str(), false); // Write mode.
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000106
tommia6219cc2016-06-15 10:30:14 -0700107 file_opened = out_files_data[i]->is_open();
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000108 ASSERT_TRUE(file_opened) << "File could not be opened.\n" << out_string;
109 }
110
111 // Prepare the test file.
112 std::string test_file_name = test::ResourcePath(
113 "audio_processing/transient/ajm-macbook-1-spke16m", "pcm");
114
kwiberg85d8bb02016-02-16 20:39:36 -0800115 std::unique_ptr<FileWrapper> test_file(FileWrapper::Create());
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000116
tommia6219cc2016-06-15 10:30:14 -0700117 test_file->OpenFile(test_file_name.c_str(), true); // Read only.
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000118
tommia6219cc2016-06-15 10:30:14 -0700119 bool file_opened = test_file->is_open();
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000120 ASSERT_TRUE(file_opened) << "File could not be opened.\n" << test_file_name;
121
122 float test_buffer[kTestBufferSize];
123
124 // Only the first frames of the audio file are tested. The matlab files also
125 // only contains information about the first frames.
126 const size_t kMaxFramesToTest = 100;
127 const float kTolerance = 0.03f;
128
129 size_t frames_read = 0;
130
131 // Read first buffer from the PCM test file.
Yves Gerey665174f2018-06-19 15:03:05 +0200132 size_t file_samples_read = ReadInt16FromFileToFloatBuffer(
133 test_file.get(), kTestBufferSize, test_buffer);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000134 while (file_samples_read > 0 && frames_read < kMaxFramesToTest) {
135 ++frames_read;
136
137 if (file_samples_read < kTestBufferSize) {
138 // Pad the rest of the buffer with zeros.
139 for (size_t i = file_samples_read; i < kTestBufferSize; ++i) {
140 test_buffer[i] = 0.0;
141 }
142 }
143 tree.Update(test_buffer, kTestBufferSize);
144 double matlab_buffer[kTestBufferSize];
145
146 // Compare results with data from the matlab test files.
147 for (int i = 0; i < kLeaves; ++i) {
148 // Compare data values
Yves Gerey665174f2018-06-19 15:03:05 +0200149 size_t matlab_samples_read = ReadDoubleBufferFromFile(
150 matlab_files_data[i].get(), kLeavesSamples, matlab_buffer);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000151
152 ASSERT_EQ(kLeavesSamples, matlab_samples_read)
153 << "Matlab test files are malformed.\n"
154 << "File: 3_" << i;
155 // Get output data from the corresponding node
156 const float* node_data = tree.NodeAt(kLevels, i)->data();
157 // Compare with matlab files.
158 for (size_t j = 0; j < kLeavesSamples; ++j) {
159 EXPECT_NEAR(matlab_buffer[j], node_data[j], kTolerance)
160 << "\nLeaf: " << i << "\nSample: " << j
161 << "\nFrame: " << frames_read - 1;
162 }
163
164 // Write results to out files.
Yves Gerey665174f2018-06-19 15:03:05 +0200165 WriteFloatBufferToFile(out_files_data[i].get(), kLeavesSamples,
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000166 node_data);
167 }
168
169 // Read next buffer from the PCM test file.
Yves Gerey665174f2018-06-19 15:03:05 +0200170 file_samples_read = ReadInt16FromFileToFloatBuffer(
171 test_file.get(), kTestBufferSize, test_buffer);
pbos@webrtc.org788acd12014-12-15 09:41:24 +0000172 }
173
174 // Close all matlab and out files.
175 for (int i = 0; i < kLeaves; ++i) {
176 matlab_files_data[i]->CloseFile();
177 out_files_data[i]->CloseFile();
178 }
179
180 test_file->CloseFile();
181}
182
183} // namespace webrtc