Skip to content

Instantly share code, notes, and snippets.

@petewarden
Created October 29, 2018 05:28
Show Gist options
  • Save petewarden/82d30afa7333a66380c72c1a046cec5b to your computer and use it in GitHub Desktop.
Save petewarden/82d30afa7333a66380c72c1a046cec5b to your computer and use it in GitHub Desktop.
Reference implementation of speech preprocessing
/* 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.
==============================================================================*/
#include "third_party/tensorflow/contrib/lite/experimental/micro/examples/micro_speech/preprocessor.h"
#include <cmath>
namespace {
constexpr int kInputSize = 512;
constexpr int kAverageWindowSize = 6;
constexpr int kOutputSize =
((kInputSize / 2) + (kAverageWindowSize - 1)) / kAverageWindowSize;
void CalculateDiscreteFourierTransform(float* time_series, int time_series_size,
float* fourier_output) {
for (int i = 0; i < time_series_size / 2; ++i) {
float real = 0;
for (int j = 0; j < time_series_size; ++j) {
real += time_series[j] * cos(j * i * M_PI * 2 / time_series_size);
}
float imaginary = 0;
for (int j = 0; j < time_series_size; ++j) {
imaginary -= time_series[j] * sin(j * i * M_PI * 2 / time_series_size);
}
fourier_output[(i * 2) + 0] = real;
fourier_output[(i * 2) + 1] = imaginary;
}
}
void CalculatePeriodicHann(int window_length, float* window_function) {
for (int i = 0; i < window_length; ++i) {
window_function[i] = 0.5 - 0.5 * cos((2 * M_PI * i) / window_length);
}
}
} // namespace
TfLiteStatus Preprocess(tflite::ErrorReporter* error_reporter,
const int16_t* input, int input_size, int output_size,
uint8_t* output) {
if (input_size > kInputSize) {
error_reporter->Report("Input size %d larger than %d", input_size,
kInputSize);
return kTfLiteError;
}
if (output_size != kOutputSize) {
error_reporter->Report("Requested output size %d doesn't match %d",
output_size, kOutputSize);
return kTfLiteError;
}
float window_function[kInputSize];
CalculatePeriodicHann(input_size, window_function);
float float_input[kInputSize];
for (int i = 0; i < kInputSize; ++i) {
if (i <= input_size) {
float_input[i] =
(input[i] * window_function[i]) / static_cast<float>(1 << 15);
} else {
float_input[i] = 0.0f;
}
}
float fourier_values[kInputSize];
CalculateDiscreteFourierTransform(float_input, kInputSize, fourier_values);
float power_spectrum[kInputSize / 2];
for (int i = 0; i < (kInputSize / 2); ++i) {
const float real = fourier_values[(i * 2) + 0];
const float imaginary = fourier_values[(i * 2) + 1];
power_spectrum[i] = ((real * real) + (imaginary * imaginary)) / 1;
}
for (int i = 0; i < kOutputSize; ++i) {
float total = 0.0f;
for (int j = 0; j < kAverageWindowSize; ++j) {
const int index = (i * kAverageWindowSize) + j;
if (index < (kInputSize / 2)) {
total += power_spectrum[index];
}
}
const float average = total / kAverageWindowSize;
int quantized_average = roundf(average * (255.0f / 127.5f));
if (quantized_average < 0) {
quantized_average = 0;
}
if (quantized_average > 255) {
quantized_average = 255;
}
output[i] = quantized_average;
}
return kTfLiteOk;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment