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