1 /* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/core/kernels/mfcc_dct.h"
17 
18 #include <vector>
19 
20 #include "tensorflow/core/platform/test.h"
21 #include "tensorflow/core/platform/types.h"
22 
23 namespace tensorflow {
24 
TEST(MfccDctTest,AgreesWithMatlab)25 TEST(MfccDctTest, AgreesWithMatlab) {
26   // This test verifies the DCT against MATLAB's dct function.
27   MfccDct dct;
28   std::vector<double> input = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
29   const int kCoefficientCount = 6;
30   ASSERT_TRUE(dct.Initialize(input.size(), kCoefficientCount));
31   std::vector<double> output;
32   dct.Compute(input, &output);
33   // Note, the matlab dct function divides the first coefficient by
34   // sqrt(2), whereas we don't, so we multiply the first element of
35   // the matlab result by sqrt(2) to get the expected values below.
36   std::vector<double> expected = {12.1243556530, -4.1625617959, 0.0,
37                                   -0.4082482905, 0.0,           -0.0800788912};
38   ASSERT_EQ(output.size(), kCoefficientCount);
39   for (int i = 0; i < kCoefficientCount; ++i) {
40     EXPECT_NEAR(output[i], expected[i], 1e-10);
41   }
42 }
43 
TEST(MfccDctTest,InitializeFailsOnInvalidInput)44 TEST(MfccDctTest, InitializeFailsOnInvalidInput) {
45   MfccDct dct1;
46   EXPECT_FALSE(dct1.Initialize(-50, 1));
47   EXPECT_FALSE(dct1.Initialize(10, -4));
48   EXPECT_FALSE(dct1.Initialize(-1, -1));
49   EXPECT_FALSE(dct1.Initialize(20, 21));
50 }
51 
52 }  // namespace tensorflow
53