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 applying a mel-scale mapping to a power spectrum. |
17 | |
18 | #ifndef TENSORFLOW_CORE_KERNELS_MFCC_MEL_FILTERBANK_H_ |
19 | #define TENSORFLOW_CORE_KERNELS_MFCC_MEL_FILTERBANK_H_ |
20 | |
21 | #include <vector> |
22 | #include "tensorflow/core/framework/op_kernel.h" |
23 | |
24 | namespace tensorflow { |
25 | |
26 | class MfccMelFilterbank { |
27 | public: |
28 | MfccMelFilterbank(); |
29 | bool Initialize(int input_length, // Number of unique FFT bins fftsize/2+1. |
30 | double input_sample_rate, int output_channel_count, |
31 | double lower_frequency_limit, double upper_frequency_limit); |
32 | |
33 | // Takes a squared-magnitude spectrogram slice as input, computes a |
34 | // triangular-mel-weighted linear-magnitude filterbank, and places the result |
35 | // in output. |
36 | void Compute(const std::vector<double>& input, |
37 | std::vector<double>* output) const; |
38 | |
39 | private: |
40 | double FreqToMel(double freq) const; |
41 | bool initialized_; |
42 | int num_channels_; |
43 | double sample_rate_; |
44 | int input_length_; |
45 | std::vector<double> center_frequencies_; // In mel, for each mel channel. |
46 | |
47 | // Each FFT bin b contributes to two triangular mel channels, with |
48 | // proportion weights_[b] going into mel channel band_mapper_[b], and |
49 | // proportion (1 - weights_[b]) going into channel band_mapper_[b] + 1. |
50 | // Thus, weights_ contains the weighting applied to each FFT bin for the |
51 | // upper-half of the triangular band. |
52 | std::vector<double> weights_; // Right-side weight for this fft bin. |
53 | |
54 | // FFT bin i contributes to the upper side of mel channel band_mapper_[i] |
55 | std::vector<int> band_mapper_; |
56 | int start_index_; // Lowest FFT bin used to calculate mel spectrum. |
57 | int end_index_; // Highest FFT bin used to calculate mel spectrum. |
58 | |
59 | TF_DISALLOW_COPY_AND_ASSIGN(MfccMelFilterbank); |
60 | }; |
61 | |
62 | } // namespace tensorflow |
63 | |
64 | #endif // TENSORFLOW_CORE_KERNELS_MFCC_MEL_FILTERBANK_H_ |
65 | |