• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2018 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 <math.h>
17 
18 #include "tensorflow/lite/kernels/internal/mfcc.h"
19 
20 namespace tflite {
21 namespace internal {
22 
23 const double kDefaultUpperFrequencyLimit = 4000;
24 const double kDefaultLowerFrequencyLimit = 20;
25 const double kFilterbankFloor = 1e-12;
26 const int kDefaultFilterbankChannelCount = 40;
27 const int kDefaultDCTCoefficientCount = 13;
28 
Mfcc()29 Mfcc::Mfcc()
30     : initialized_(false),
31       lower_frequency_limit_(kDefaultLowerFrequencyLimit),
32       upper_frequency_limit_(kDefaultUpperFrequencyLimit),
33       filterbank_channel_count_(kDefaultFilterbankChannelCount),
34       dct_coefficient_count_(kDefaultDCTCoefficientCount) {}
35 
Initialize(int input_length,double input_sample_rate)36 bool Mfcc::Initialize(int input_length, double input_sample_rate) {
37   bool initialized = mel_filterbank_.Initialize(
38       input_length, input_sample_rate, filterbank_channel_count_,
39       lower_frequency_limit_, upper_frequency_limit_);
40   initialized &=
41       dct_.Initialize(filterbank_channel_count_, dct_coefficient_count_);
42   initialized_ = initialized;
43   return initialized;
44 }
45 
Compute(const std::vector<double> & spectrogram_frame,std::vector<double> * output) const46 void Mfcc::Compute(const std::vector<double>& spectrogram_frame,
47                    std::vector<double>* output) const {
48   if (!initialized_) {
49     // LOG(ERROR) << "Mfcc not initialized.";
50     return;
51   }
52   std::vector<double> working;
53   mel_filterbank_.Compute(spectrogram_frame, &working);
54   for (int i = 0; i < working.size(); ++i) {
55     double val = working[i];
56     if (val < kFilterbankFloor) {
57       val = kFilterbankFloor;
58     }
59     working[i] = log(val);
60   }
61   dct_.Compute(working, output);
62 }
63 
64 }  // namespace internal
65 }  // namespace tflite
66