fft.cc 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #include "tensorflow/lite/experimental/microfrontend/lib/fft.h"
  13. #include <string.h>
  14. #define FIXED_POINT 16
  15. #include "kiss_fft.h"
  16. #include "tools/kiss_fftr.h"
  17. void FftCompute(struct FftState* state, const int16_t* input,
  18. int input_scale_shift) {
  19. const size_t input_size = state->input_size;
  20. const size_t fft_size = state->fft_size;
  21. int16_t* fft_input = state->input;
  22. // First, scale the input by the given shift.
  23. size_t i;
  24. for (i = 0; i < input_size; ++i) {
  25. fft_input[i] = static_cast<int16_t>(static_cast<uint16_t>(input[i])
  26. << input_scale_shift);
  27. }
  28. // Zero out whatever else remains in the top part of the input.
  29. for (; i < fft_size; ++i) {
  30. fft_input[i] = 0;
  31. }
  32. // Apply the FFT.
  33. kiss_fftr(
  34. reinterpret_cast<const kiss_fftr_cfg>(state->scratch),
  35. state->input,
  36. reinterpret_cast<kiss_fft_cpx*>(state->output));
  37. }
  38. void FftInit(struct FftState* state) {
  39. // All the initialization is done in FftPopulateState()
  40. }
  41. void FftReset(struct FftState* state) {
  42. memset(state->input, 0, state->fft_size * sizeof(*state->input));
  43. memset(state->output, 0, (state->fft_size / 2 + 1) * sizeof(*state->output));
  44. }