Home | History | Annotate | Download | only in kernels
      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_mel_filterbank.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 
     25 TEST(MfccMelFilterbankTest, AgreesWithPythonGoldenValues) {
     26   // This test verifies the Mel filterbank against "golden values".
     27   // Golden values are from an independent Python Mel implementation.
     28   MfccMelFilterbank filterbank;
     29 
     30   std::vector<double> input;
     31   const int kSampleCount = 513;
     32   input.reserve(kSampleCount);
     33   for (int i = 0; i < kSampleCount; ++i) {
     34     input.push_back(i + 1);
     35   }
     36   const int kChannelCount = 20;
     37   filterbank.Initialize(
     38       input.size(), 22050 /* sample rate */, kChannelCount /* channels */,
     39       20.0 /*  lower frequency limit */, 4000.0 /* upper frequency limit */);
     40 
     41   std::vector<double> output;
     42   filterbank.Compute(input, &output);
     43 
     44   std::vector<double> expected = {
     45       7.38894574,   10.30330648, 13.72703292,  17.24158686,  21.35253118,
     46       25.77781089,  31.30624108, 37.05877236,  43.9436536,   51.80306637,
     47       60.79867148,  71.14363376, 82.90910141,  96.50069158,  112.08428368,
     48       129.96721968, 150.4277597, 173.74997634, 200.86037462, 231.59802942};
     49 
     50   ASSERT_EQ(output.size(), kChannelCount);
     51 
     52   for (int i = 0; i < kChannelCount; ++i) {
     53     EXPECT_NEAR(output[i], expected[i], 1e-04);
     54   }
     55 }
     56 
     57 TEST(MfccMelFilterbankTest, IgnoresExistingContentOfOutputVector) {
     58   // Test for bug where the output vector was not cleared before
     59   // accumulating next frame's weighted spectral values.
     60   MfccMelFilterbank filterbank;
     61 
     62   const int kSampleCount = 513;
     63   std::vector<double> input;
     64   std::vector<double> output;
     65 
     66   filterbank.Initialize(kSampleCount, 22050 /* sample rate */,
     67                         20 /* channels */, 20.0 /*  lower frequency limit */,
     68                         4000.0 /* upper frequency limit */);
     69 
     70   // First call with nonzero input value, and an empty output vector,
     71   // will resize the output and fill it with the correct, nonzero outputs.
     72   input.assign(kSampleCount, 1.0);
     73   filterbank.Compute(input, &output);
     74   for (const double value : output) {
     75     EXPECT_LE(0.0, value);
     76   }
     77 
     78   // Second call with zero input should also generate zero output.  However,
     79   // the output vector now is already the correct size, but full of nonzero
     80   // values.  Make sure these don't affect the output.
     81   input.assign(kSampleCount, 0.0);
     82   filterbank.Compute(input, &output);
     83   for (const double value : output) {
     84     EXPECT_EQ(0.0, value);
     85   }
     86 }
     87 
     88 }  // namespace tensorflow
     89