• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "tensorflow/lite/experimental/microfrontend/lib/fft_util.h"
16 
17 #include <stdio.h>
18 
19 #define FIXED_POINT 16
20 #include "kiss_fft.h"
21 #include "tools/kiss_fftr.h"
22 
FftPopulateState(struct FftState * state,size_t input_size)23 int FftPopulateState(struct FftState* state, size_t input_size) {
24   state->input_size = input_size;
25   state->fft_size = 1;
26   while (state->fft_size < state->input_size) {
27     state->fft_size <<= 1;
28   }
29 
30   state->input = reinterpret_cast<int16_t*>(
31       malloc(state->fft_size * sizeof(*state->input)));
32   if (state->input == nullptr) {
33     fprintf(stderr, "Failed to alloc fft input buffer\n");
34     return 0;
35   }
36 
37   state->output = reinterpret_cast<complex_int16_t*>(
38       malloc((state->fft_size / 2 + 1) * sizeof(*state->output) * 2));
39   if (state->output == nullptr) {
40     fprintf(stderr, "Failed to alloc fft output buffer\n");
41     return 0;
42   }
43 
44   // Ask kissfft how much memory it wants.
45   size_t scratch_size = 0;
46   kiss_fftr_cfg kfft_cfg = kiss_fftr_alloc(
47       state->fft_size, 0, nullptr, &scratch_size);
48   if (kfft_cfg != nullptr) {
49     fprintf(stderr, "Kiss memory sizing failed.\n");
50     return 0;
51   }
52   state->scratch = malloc(scratch_size);
53   if (state->scratch == nullptr) {
54     fprintf(stderr, "Failed to alloc fft scratch buffer\n");
55     return 0;
56   }
57   state->scratch_size = scratch_size;
58   // Let kissfft configure the scratch space we just allocated
59   kfft_cfg = kiss_fftr_alloc(state->fft_size, 0,
60                                               state->scratch, &scratch_size);
61   if (kfft_cfg != state->scratch) {
62     fprintf(stderr, "Kiss memory preallocation strategy failed.\n");
63     return 0;
64   }
65   return 1;
66 }
67 
FftFreeStateContents(struct FftState * state)68 void FftFreeStateContents(struct FftState* state) {
69   free(state->input);
70   free(state->output);
71   free(state->scratch);
72 }
73