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.h"
16
17 #include <string.h>
18
19 #define FIXED_POINT 16
20 #include "kiss_fft.h"
21 #include "tools/kiss_fftr.h"
22
FftCompute(struct FftState * state,const int16_t * input,int input_scale_shift)23 void FftCompute(struct FftState* state, const int16_t* input,
24 int input_scale_shift) {
25 const size_t input_size = state->input_size;
26 const size_t fft_size = state->fft_size;
27
28 int16_t* fft_input = state->input;
29 // First, scale the input by the given shift.
30 size_t i;
31 for (i = 0; i < input_size; ++i) {
32 fft_input[i] = static_cast<int16_t>(static_cast<uint16_t>(input[i])
33 << input_scale_shift);
34 }
35 // Zero out whatever else remains in the top part of the input.
36 for (; i < fft_size; ++i) {
37 fft_input[i] = 0;
38 }
39
40 // Apply the FFT.
41 kiss_fftr(reinterpret_cast<kiss_fftr_cfg>(state->scratch),
42 state->input,
43 reinterpret_cast<kiss_fft_cpx*>(state->output));
44 }
45
FftInit(struct FftState * state)46 void FftInit(struct FftState* state) {
47 // All the initialization is done in FftPopulateState()
48 }
49
FftReset(struct FftState * state)50 void FftReset(struct FftState* state) {
51 memset(state->input, 0, state->fft_size * sizeof(*state->input));
52 memset(state->output, 0, (state->fft_size / 2 + 1) * sizeof(*state->output));
53 }
54