Home | History | Annotate | Download | only in lib
      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/window_util.h"
     16 
     17 #include <math.h>
     18 #include <stdio.h>
     19 #include <stdlib.h>
     20 #include <string.h>
     21 
     22 void WindowFillConfigWithDefaults(struct WindowConfig* config) {
     23   config->size_ms = 25;
     24   config->step_size_ms = 10;
     25 }
     26 
     27 int WindowPopulateState(const struct WindowConfig* config,
     28                         struct WindowState* state, int sample_rate) {
     29   state->size = config->size_ms * sample_rate / 1000;
     30   state->step = config->step_size_ms * sample_rate / 1000;
     31 
     32   state->coefficients = malloc(
     33       state->size * sizeof(*state->coefficients));
     34   if (state->coefficients == NULL) {
     35     fprintf(stderr, "Failed to allocate window coefficients\n");
     36     return 0;
     37   }
     38 
     39   // Populate the window values.
     40   const float arg = M_PI * 2.0 / ((float) state->size);
     41   int i;
     42   for (i = 0; i < state->size; ++i) {
     43     float float_value = 0.5 - (0.5 * cos(arg * (i + 0.5)));
     44     // Scale it to fixed point and round it.
     45     state->coefficients[i] =
     46         floor(float_value * (1 << kFrontendWindowBits) + 0.5);
     47   }
     48 
     49   state->input_used = 0;
     50   state->input = malloc(
     51       state->size * sizeof(*state->input));
     52   if (state->input == NULL) {
     53     fprintf(stderr, "Failed to allocate window input\n");
     54     return 0;
     55   }
     56 
     57   state->output = malloc(
     58       state->size * sizeof(*state->output));
     59   if (state->output == NULL) {
     60     fprintf(stderr, "Failed to allocate window output\n");
     61     return 0;
     62   }
     63 
     64   return 1;
     65 }
     66 
     67 void WindowFreeStateContents(struct WindowState* state) {
     68   free(state->coefficients);
     69   free(state->input);
     70   free(state->output);
     71 }
     72