/* Copyright 2018 The TensorFlow Authors. All Rights Reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ==============================================================================*/ // Class for generating spectrogram slices from a waveform. // Initialize() should be called before calls to other functions. Once // Initialize() has been called and returned true, The Compute*() functions can // be called repeatedly with sequential input data (ie. the first element of the // next input vector directly follows the last element of the previous input // vector). Whenever enough audio samples are buffered to produce a // new frame, it will be placed in output. Output is cleared on each // call to Compute*(). This class is thread-unsafe, and should only be // called from one thread at a time. // With the default parameters, the output of this class should be very // close to the results of the following MATLAB code: // overlap_samples = window_length_samples - step_samples; // window = hann(window_length_samples, 'periodic'); // S = abs(spectrogram(audio, window, overlap_samples)).^2; #ifndef TENSORFLOW_CONTRIB_LITE_KERNELS_INTERNAL_SPECTROGRAM_H_ #define TENSORFLOW_CONTRIB_LITE_KERNELS_INTERNAL_SPECTROGRAM_H_ #include #include #include #include "third_party/fft2d/fft.h" namespace tflite { namespace internal { class Spectrogram { public: Spectrogram() : initialized_(false) {} ~Spectrogram() {} // Initializes the class with a given window length and step length // (both in samples). Internally a Hann window is used as the window // function. Returns true on success, after which calls to Process() // are possible. window_length must be greater than 1 and step // length must be greater than 0. bool Initialize(int window_length, int step_length); // Initialize with an explicit window instead of a length. bool Initialize(const std::vector& window, int step_length); // Processes an arbitrary amount of audio data (contained in input) // to yield complex spectrogram frames. After a successful call to // Initialize(), Process() may be called repeatedly with new input data // each time. The audio input is buffered internally, and the output // vector is populated with as many temporally-ordered spectral slices // as it is possible to generate from the input. The output is cleared // on each call before the new frames (if any) are added. // // The template parameters can be float or double. template bool ComputeComplexSpectrogram( const std::vector& input, std::vector>>* output); // This function works as the one above, but returns the power // (the L2 norm, or the squared magnitude) of each complex value. template bool ComputeSquaredMagnitudeSpectrogram( const std::vector& input, std::vector>* output); // Return reference to the window function used internally. const std::vector& GetWindow() const { return window_; } // Return the number of frequency channels in the spectrogram. int output_frequency_channels() const { return output_frequency_channels_; } private: template bool GetNextWindowOfSamples(const std::vector& input, int* input_start); void ProcessCoreFFT(); int fft_length_; int output_frequency_channels_; int window_length_; int step_length_; bool initialized_; int samples_to_next_step_; std::vector window_; std::vector fft_input_output_; std::deque input_queue_; // Working data areas for the FFT routines. std::vector fft_integer_working_area_; std::vector fft_double_working_area_; }; } // namespace internal } // namespace tflite #endif // TENSORFLOW_CONTRIB_LITE_KERNELS_INTERNAL_SPECTROGRAM_H_