blob: 939404c7cddd934e9b98691273f7999894fd93a0 [file] [log] [blame]
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +00001/*
2 * Copyright (c) 2014 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
11#include <complex>
12
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +000013#include "webrtc/modules/audio_processing/beamformer/matrix.h"
14#include "webrtc/modules/audio_processing/beamformer/matrix_test_helpers.h"
kwibergac9f8762016-09-30 22:29:43 -070015#include "webrtc/test/gtest.h"
aluebs@webrtc.org0c39e912014-12-18 22:22:04 +000016
17namespace webrtc {
18
19using std::complex;
20
21TEST(MatrixTest, TestMultiplySameSize) {
22 const int kNumRows = 2;
23 const int kNumCols = 2;
24 const float kValuesLeft[kNumRows][kNumCols] = {{1.1f, 2.2f}, {3.3f, 4.4f}};
25 const float kValuesRight[kNumRows][kNumCols] = {{5.4f, 127.f},
26 {4600.f, -555.f}};
27 const float kValuesExpected[kNumRows][kNumCols] = {{10125.94f, -1081.3f},
28 {20257.82f, -2022.9f}};
29
30 Matrix<float> lh_mat(*kValuesLeft, kNumRows, kNumCols);
31 Matrix<float> rh_mat(*kValuesRight, kNumRows, kNumCols);
32 Matrix<float> expected_result(*kValuesExpected, kNumRows, kNumCols);
33 Matrix<float> actual_result(kNumRows, kNumCols);
34
35 actual_result.Multiply(lh_mat, rh_mat);
36 MatrixTestHelpers::ValidateMatrixEquality(expected_result, actual_result);
37
38 lh_mat.Multiply(rh_mat);
39 MatrixTestHelpers::ValidateMatrixEquality(lh_mat, actual_result);
40}
41
42TEST(MatrixTest, TestMultiplyDifferentSize) {
43 const int kNumRowsLeft = 2;
44 const int kNumColsLeft = 3;
45 const int kNumRowsRight = 3;
46 const int kNumColsRight = 2;
47 const int kValuesLeft[kNumRowsLeft][kNumColsLeft] = {{35, 466, -15},
48 {-3, 3422, 9}};
49 const int kValuesRight[kNumRowsRight][kNumColsRight] = {
50 {765, -42}, {0, 194}, {625, 66321}};
51 const int kValuesExpected[kNumRowsLeft][kNumColsRight] = {{17400, -905881},
52 {3330, 1260883}};
53
54 Matrix<int> lh_mat(*kValuesLeft, kNumRowsLeft, kNumColsLeft);
55 Matrix<int> rh_mat(*kValuesRight, kNumRowsRight, kNumColsRight);
56 Matrix<int> expected_result(*kValuesExpected, kNumRowsLeft, kNumColsRight);
57 Matrix<int> actual_result(kNumRowsLeft, kNumColsRight);
58
59 actual_result.Multiply(lh_mat, rh_mat);
60 MatrixTestHelpers::ValidateMatrixEquality(expected_result, actual_result);
61
62 lh_mat.Multiply(rh_mat);
63 MatrixTestHelpers::ValidateMatrixEquality(lh_mat, actual_result);
64}
65
66TEST(MatrixTest, TestTranspose) {
67 const int kNumInitialRows = 2;
68 const int kNumInitialCols = 4;
69 const int kNumResultRows = 4;
70 const int kNumResultCols = 2;
71 const float kValuesInitial[kNumInitialRows][kNumInitialCols] = {
72 {1.1f, 2.2f, 3.3f, 4.4f}, {5.5f, 6.6f, 7.7f, 8.8f}};
73 const float kValuesExpected[kNumResultRows][kNumResultCols] = {
74 {1.1f, 5.5f}, {2.2f, 6.6f}, {3.3f, 7.7f}, {4.4f, 8.8f}};
75
76 Matrix<float> initial_mat(*kValuesInitial, kNumInitialRows, kNumInitialCols);
77 Matrix<float> expected_result(
78 *kValuesExpected, kNumResultRows, kNumResultCols);
79 Matrix<float> actual_result(kNumResultRows, kNumResultCols);
80
81 actual_result.Transpose(initial_mat);
82 MatrixTestHelpers::ValidateMatrixEqualityFloat(expected_result,
83 actual_result);
84 initial_mat.Transpose();
85 MatrixTestHelpers::ValidateMatrixEqualityFloat(initial_mat, actual_result);
86}
87
88TEST(MatrixTest, TestScale) {
89 const int kNumRows = 3;
90 const int kNumCols = 3;
91 const int kScaleFactor = -9;
92 const int kValuesInitial[kNumRows][kNumCols] = {
93 {1, 20, 5000}, {-3, -29, 66}, {7654, 0, -23455}};
94 const int kValuesExpected[kNumRows][kNumCols] = {
95 {-9, -180, -45000}, {27, 261, -594}, {-68886, 0, 211095}};
96
97 Matrix<int> initial_mat(*kValuesInitial, kNumRows, kNumCols);
98 Matrix<int> expected_result(*kValuesExpected, kNumRows, kNumCols);
99 Matrix<int> actual_result;
100
101 actual_result.Scale(initial_mat, kScaleFactor);
102 MatrixTestHelpers::ValidateMatrixEquality(expected_result, actual_result);
103
104 initial_mat.Scale(kScaleFactor);
105 MatrixTestHelpers::ValidateMatrixEquality(initial_mat, actual_result);
106}
107
108TEST(MatrixTest, TestPointwiseAdd) {
109 const int kNumRows = 2;
110 const int kNumCols = 3;
111 const float kValuesLeft[kNumRows][kNumCols] = {{1.1f, 210.45f, -549.2f},
112 {11.876f, 586.7f, -64.35f}};
113 const float kValuesRight[kNumRows][kNumCols] = {{-50.4f, 1.f, 0.5f},
114 {460.f, -554.2f, 4566.f}};
115 const float kValuesExpected[kNumRows][kNumCols] = {
116 {-49.3f, 211.45f, -548.7f}, {471.876f, 32.5f, 4501.65f}};
117
118 Matrix<float> lh_mat(*kValuesLeft, kNumRows, kNumCols);
119 Matrix<float> rh_mat(*kValuesRight, kNumRows, kNumCols);
120 Matrix<float> expected_result(*kValuesExpected, kNumRows, kNumCols);
121 Matrix<float> actual_result;
122
123 actual_result.Add(lh_mat, rh_mat);
124 MatrixTestHelpers::ValidateMatrixEqualityFloat(expected_result,
125 actual_result);
126 lh_mat.Add(rh_mat);
127 MatrixTestHelpers::ValidateMatrixEqualityFloat(lh_mat, actual_result);
128}
129
130TEST(MatrixTest, TestPointwiseSubtract) {
131 const int kNumRows = 3;
132 const int kNumCols = 2;
133 const float kValuesLeft[kNumRows][kNumCols] = {
134 {1.1f, 210.45f}, {-549.2f, 11.876f}, {586.7f, -64.35f}};
135 const float kValuesRight[kNumRows][kNumCols] = {
136 {-50.4f, 1.f}, {0.5f, 460.f}, {-554.2f, 4566.f}};
137 const float kValuesExpected[kNumRows][kNumCols] = {
138 {51.5f, 209.45f}, {-549.7f, -448.124f}, {1140.9f, -4630.35f}};
139
140 Matrix<float> lh_mat(*kValuesLeft, kNumRows, kNumCols);
141 Matrix<float> rh_mat(*kValuesRight, kNumRows, kNumCols);
142 Matrix<float> expected_result(*kValuesExpected, kNumRows, kNumCols);
143 Matrix<float> actual_result;
144
145 actual_result.Subtract(lh_mat, rh_mat);
146 MatrixTestHelpers::ValidateMatrixEqualityFloat(expected_result,
147 actual_result);
148
149 lh_mat.Subtract(rh_mat);
150 MatrixTestHelpers::ValidateMatrixEqualityFloat(lh_mat, actual_result);
151}
152
153TEST(MatrixTest, TestPointwiseMultiply) {
154 const int kNumRows = 1;
155 const int kNumCols = 5;
156 const float kValuesLeft[kNumRows][kNumCols] = {
157 {1.1f, 6.4f, 0.f, -1.f, -88.3f}};
158 const float kValuesRight[kNumRows][kNumCols] = {
159 {53.2f, -210.45f, -549.2f, 99.99f, -45.2f}};
160 const float kValuesExpected[kNumRows][kNumCols] = {
161 {58.52f, -1346.88f, 0.f, -99.99f, 3991.16f}};
162
163 Matrix<float> lh_mat(*kValuesLeft, kNumRows, kNumCols);
164 Matrix<float> rh_mat(*kValuesRight, kNumRows, kNumCols);
165 Matrix<float> expected_result(*kValuesExpected, kNumRows, kNumCols);
166 Matrix<float> actual_result;
167
168 actual_result.PointwiseMultiply(lh_mat, rh_mat);
169 MatrixTestHelpers::ValidateMatrixEqualityFloat(expected_result,
170 actual_result);
171
172 lh_mat.PointwiseMultiply(rh_mat);
173 MatrixTestHelpers::ValidateMatrixEqualityFloat(lh_mat, actual_result);
174}
175
176TEST(MatrixTest, TestPointwiseDivide) {
177 const int kNumRows = 5;
178 const int kNumCols = 1;
179 const float kValuesLeft[kNumRows][kNumCols] = {
180 {1.1f}, {6.4f}, {0.f}, {-1.f}, {-88.3f}};
181 const float kValuesRight[kNumRows][kNumCols] = {
182 {53.2f}, {-210.45f}, {-549.2f}, {99.99f}, {-45.2f}};
183 const float kValuesExpected[kNumRows][kNumCols] = {
184 {0.020676691f}, {-0.03041102399f}, {0.f}, {-0.010001f}, {1.9535398f}};
185
186 Matrix<float> lh_mat(*kValuesLeft, kNumRows, kNumCols);
187 Matrix<float> rh_mat(*kValuesRight, kNumRows, kNumCols);
188 Matrix<float> expected_result(*kValuesExpected, kNumRows, kNumCols);
189 Matrix<float> actual_result;
190
191 actual_result.PointwiseDivide(lh_mat, rh_mat);
192 MatrixTestHelpers::ValidateMatrixEqualityFloat(expected_result,
193 actual_result);
194
195 lh_mat.PointwiseDivide(rh_mat);
196 MatrixTestHelpers::ValidateMatrixEqualityFloat(lh_mat, actual_result);
197}
198
199TEST(MatrixTest, TestPointwiseSquareRoot) {
200 const int kNumRows = 2;
201 const int kNumCols = 2;
202 const int kValues[kNumRows][kNumCols] = {{4, 9}, {16, 0}};
203 const int kValuesExpected[kNumRows][kNumCols] = {{2, 3}, {4, 0}};
204
205 Matrix<int> operand_mat(*kValues, kNumRows, kNumCols);
206 Matrix<int> expected_result(*kValuesExpected, kNumRows, kNumCols);
207 Matrix<int> actual_result;
208
209 actual_result.PointwiseSquareRoot(operand_mat);
210 MatrixTestHelpers::ValidateMatrixEquality(expected_result, actual_result);
211
212 operand_mat.PointwiseSquareRoot();
213 MatrixTestHelpers::ValidateMatrixEquality(operand_mat, actual_result);
214}
215
216TEST(MatrixTest, TestPointwiseSquareRootComplex) {
217 const int kNumRows = 1;
218 const int kNumCols = 3;
219 const complex<float> kValues[kNumRows][kNumCols] = {
220 {complex<float>(-4.f, 0), complex<float>(0, 9), complex<float>(3, -4)}};
221 const complex<float> kValuesExpected[kNumRows][kNumCols] = {
222 {complex<float>(0.f, 2.f), complex<float>(2.1213202f, 2.1213202f),
223 complex<float>(2.f, -1.f)}};
224
225 Matrix<complex<float> > operand_mat(*kValues, kNumRows, kNumCols);
226 Matrix<complex<float> > expected_result(*kValuesExpected, kNumRows, kNumCols);
227 Matrix<complex<float> > actual_result;
228
229 actual_result.PointwiseSquareRoot(operand_mat);
230 MatrixTestHelpers::ValidateMatrixEqualityComplexFloat(expected_result,
231 actual_result);
232
233 operand_mat.PointwiseSquareRoot();
234 MatrixTestHelpers::ValidateMatrixEqualityComplexFloat(operand_mat,
235 actual_result);
236}
237
238TEST(MatrixTest, TestPointwiseAbsoluteValue) {
239 const int kNumRows = 1;
240 const int kNumCols = 3;
241 const complex<float> kValues[kNumRows][kNumCols] = {
242 {complex<float>(-4.f, 0), complex<float>(0, 9), complex<float>(3, -4)}};
243 const complex<float> kValuesExpected[kNumRows][kNumCols] = {
244 {complex<float>(4.f, 0), complex<float>(9.f, 0), complex<float>(5.f, 0)}};
245
246 Matrix<complex<float> > operand_mat(*kValues, kNumRows, kNumCols);
247 Matrix<complex<float> > expected_result(*kValuesExpected, kNumRows, kNumCols);
248 Matrix<complex<float> > actual_result;
249
250 actual_result.PointwiseAbsoluteValue(operand_mat);
251 MatrixTestHelpers::ValidateMatrixEqualityComplexFloat(expected_result,
252 actual_result);
253
254 operand_mat.PointwiseAbsoluteValue();
255 MatrixTestHelpers::ValidateMatrixEqualityComplexFloat(operand_mat,
256 actual_result);
257}
258
259TEST(MatrixTest, TestPointwiseSquare) {
260 const int kNumRows = 1;
261 const int kNumCols = 3;
262 const float kValues[kNumRows][kNumCols] = {{2.4f, -4.f, 3.3f}};
263 const float kValuesExpected[kNumRows][kNumCols] = {{5.76f, 16.f, 10.89f}};
264
265 Matrix<float> operand_mat(*kValues, kNumRows, kNumCols);
266 Matrix<float> expected_result(*kValuesExpected, kNumRows, kNumCols);
267 Matrix<float> actual_result;
268
269 actual_result.PointwiseSquare(operand_mat);
270 MatrixTestHelpers::ValidateMatrixEqualityFloat(expected_result,
271 actual_result);
272
273 operand_mat.PointwiseSquare();
274 MatrixTestHelpers::ValidateMatrixEqualityFloat(operand_mat, actual_result);
275}
276
277TEST(MatrixTest, TestComplexOperations) {
278 const int kNumRows = 2;
279 const int kNumCols = 2;
280
281 const complex<float> kValuesLeft[kNumRows][kNumCols] = {
282 {complex<float>(1.f, 1.f), complex<float>(2.f, 2.f)},
283 {complex<float>(3.f, 3.f), complex<float>(4.f, 4.f)}};
284
285 const complex<float> kValuesRight[kNumRows][kNumCols] = {
286 {complex<float>(5.f, 5.f), complex<float>(6.f, 6.f)},
287 {complex<float>(7.f, 7.f), complex<float>(8.f, 8.f)}};
288
289 const complex<float> kValuesExpectedAdd[kNumRows][kNumCols] = {
290 {complex<float>(6.f, 6.f), complex<float>(8.f, 8.f)},
291 {complex<float>(10.f, 10.f), complex<float>(12.f, 12.f)}};
292
293 const complex<float> kValuesExpectedMultiply[kNumRows][kNumCols] = {
294 {complex<float>(0.f, 38.f), complex<float>(0.f, 44.f)},
295 {complex<float>(0.f, 86.f), complex<float>(0.f, 100.f)}};
296
297 const complex<float> kValuesExpectedPointwiseDivide[kNumRows][kNumCols] = {
298 {complex<float>(0.2f, 0.f), complex<float>(0.33333333f, 0.f)},
299 {complex<float>(0.42857143f, 0.f), complex<float>(0.5f, 0.f)}};
300
301 Matrix<complex<float> > lh_mat(*kValuesLeft, kNumRows, kNumCols);
302 Matrix<complex<float> > rh_mat(*kValuesRight, kNumRows, kNumCols);
303 Matrix<complex<float> > expected_result_add(
304 *kValuesExpectedAdd, kNumRows, kNumCols);
305 Matrix<complex<float> > expected_result_multiply(
306 *kValuesExpectedMultiply, kNumRows, kNumCols);
307 Matrix<complex<float> > expected_result_pointwise_divide(
308 *kValuesExpectedPointwiseDivide, kNumRows, kNumCols);
309 Matrix<complex<float> > actual_result_add;
310 Matrix<complex<float> > actual_result_multiply(kNumRows, kNumCols);
311 Matrix<complex<float> > actual_result_pointwise_divide;
312
313 actual_result_add.Add(lh_mat, rh_mat);
314 MatrixTestHelpers::ValidateMatrixEqualityComplexFloat(expected_result_add,
315 actual_result_add);
316
317 actual_result_multiply.Multiply(lh_mat, rh_mat);
318 MatrixTestHelpers::ValidateMatrixEqualityComplexFloat(
319 expected_result_multiply, actual_result_multiply);
320
321 actual_result_pointwise_divide.PointwiseDivide(lh_mat, rh_mat);
322 MatrixTestHelpers::ValidateMatrixEqualityComplexFloat(
323 expected_result_pointwise_divide, actual_result_pointwise_divide);
324}
325
326} // namespace webrtc