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 // Basic class for computing MFCCs from spectrogram slices.
17 
18 #ifndef TENSORFLOW_LITE_KERNELS_INTERNAL_MFCC_H_
19 #define TENSORFLOW_LITE_KERNELS_INTERNAL_MFCC_H_
20 
21 #include <vector>
22 
23 #include "tensorflow/lite/kernels/internal/mfcc_dct.h"
24 #include "tensorflow/lite/kernels/internal/mfcc_mel_filterbank.h"
25 
26 namespace tflite {
27 namespace internal {
28 
29 class Mfcc {
30  public:
31   Mfcc();
32   bool Initialize(int input_length, double input_sample_rate);
33 
34   // Input is a single squared-magnitude spectrogram frame. The input spectrum
35   // is converted to linear magnitude and weighted into bands using a
36   // triangular mel filterbank, and a discrete cosine transform (DCT) of the
37   // values is taken. Output is populated with the lowest dct_coefficient_count
38   // of these values.
39   void Compute(const std::vector<double>& spectrogram_frame,
40                std::vector<double>* output) const;
41 
set_upper_frequency_limit(double upper_frequency_limit)42   void set_upper_frequency_limit(double upper_frequency_limit) {
43     // CHECK(!initialized_) << "Set frequency limits before calling
44     // Initialize.";
45     upper_frequency_limit_ = upper_frequency_limit;
46   }
47 
set_lower_frequency_limit(double lower_frequency_limit)48   void set_lower_frequency_limit(double lower_frequency_limit) {
49     // CHECK(!initialized_) << "Set frequency limits before calling
50     // Initialize.";
51     lower_frequency_limit_ = lower_frequency_limit;
52   }
53 
set_filterbank_channel_count(int filterbank_channel_count)54   void set_filterbank_channel_count(int filterbank_channel_count) {
55     /// CHECK(!initialized_) << "Set channel count before calling Initialize.";
56     filterbank_channel_count_ = filterbank_channel_count;
57   }
58 
set_dct_coefficient_count(int dct_coefficient_count)59   void set_dct_coefficient_count(int dct_coefficient_count) {
60     // CHECK(!initialized_) << "Set coefficient count before calling
61     // Initialize.";
62     dct_coefficient_count_ = dct_coefficient_count;
63   }
64 
65  private:
66   MfccMelFilterbank mel_filterbank_;
67   MfccDct dct_;
68   bool initialized_;
69   double lower_frequency_limit_;
70   double upper_frequency_limit_;
71   int filterbank_channel_count_;
72   int dct_coefficient_count_;
73 };
74 
75 }  // namespace internal
76 }  // namespace tflite
77 
78 #endif  // TENSORFLOW_LITE_KERNELS_INTERNAL_MFCC_H_
79