Filter_bank ops (#2123) `port c++ filter_bank ops to open source in tflm_signal` -port filter_bank ops and corresponding to new open source location for C++ BUG=[b/289422411](https://b.corp.google.com/issues/289422411)
diff --git a/signal/micro/kernels/BUILD b/signal/micro/kernels/BUILD index 4b01baa..68f5bc7 100644 --- a/signal/micro/kernels/BUILD +++ b/signal/micro/kernels/BUILD
@@ -10,6 +10,10 @@ srcs = [ "delay.cc", "energy.cc", + "filter_bank.cc", + "filter_bank_log.cc", + "filter_bank_spectral_subtraction.cc", + "filter_bank_square_root.cc", "framer.cc", "overlap_add.cc", "rfft.cc", @@ -26,6 +30,10 @@ deps = [ "//signal/src:circular_buffer", "//signal/src:energy", + "//signal/src:filter_bank", + "//signal/src:filter_bank_log", + "//signal/src:filter_bank_spectral_subtraction", + "//signal/src:filter_bank_square_root", "//signal/src:overlap_add", "//signal/src:rfft", "//signal/src:window", @@ -222,3 +230,92 @@ "//tensorflow/lite/micro/testing:micro_test", ], ) + +cc_library( + name = "filter_bank_flexbuffers_generated_data", + srcs = [ + "filter_bank_flexbuffers_generated_data.cc", + ], + hdrs = [ + "filter_bank_flexbuffers_generated_data.h", + ], +) + +cc_library( + name = "filter_bank_log_flexbuffers_generated_data", + srcs = [ + "filter_bank_log_flexbuffers_generated_data.cc", + ], + hdrs = [ + "filter_bank_log_flexbuffers_generated_data.h", + ], +) + +cc_library( + name = "filter_bank_spectral_subtraction_flexbuffers_generated_data", + srcs = [ + "filter_bank_spectral_subtraction_flexbuffers_generated_data.cc", + ], + hdrs = [ + "filter_bank_spectral_subtraction_flexbuffers_generated_data.h", + ], +) + +cc_test( + name = "filter_bank_test", + srcs = [ + "filter_bank_test.cc", + ], + deps = [ + ":filter_bank_flexbuffers_generated_data", + "//tensorflow/lite/c:common", + "//tensorflow/lite/micro:op_resolvers", + "//tensorflow/lite/micro:test_helpers", + "//tensorflow/lite/micro/kernels:kernel_runner", + "//tensorflow/lite/micro/testing:micro_test", + ], +) + +cc_test( + name = "filter_bank_log_test", + srcs = [ + "filter_bank_log_test.cc", + ], + deps = [ + ":filter_bank_log_flexbuffers_generated_data", + "//tensorflow/lite/c:common", + "//tensorflow/lite/micro:op_resolvers", + "//tensorflow/lite/micro:test_helpers", + "//tensorflow/lite/micro/kernels:kernel_runner", + "//tensorflow/lite/micro/testing:micro_test", + ], +) + +cc_test( + name = "filter_bank_spectral_subtraction_test", + srcs = [ + "filter_bank_spectral_subtraction_test.cc", + ], + deps = [ + ":filter_bank_spectral_subtraction_flexbuffers_generated_data", + "//tensorflow/lite/c:common", + "//tensorflow/lite/micro:op_resolvers", + "//tensorflow/lite/micro:test_helpers", + "//tensorflow/lite/micro/kernels:kernel_runner", + "//tensorflow/lite/micro/testing:micro_test", + ], +) + +cc_test( + name = "filter_bank_square_root_test", + srcs = [ + "filter_bank_square_root_test.cc", + ], + deps = [ + "//tensorflow/lite/c:common", + "//tensorflow/lite/micro:op_resolvers", + "//tensorflow/lite/micro:test_helpers", + "//tensorflow/lite/micro/kernels:kernel_runner", + "//tensorflow/lite/micro/testing:micro_test", + ], +)
diff --git a/signal/micro/kernels/filter_bank.cc b/signal/micro/kernels/filter_bank.cc new file mode 100644 index 0000000..7866ac3 --- /dev/null +++ b/signal/micro/kernels/filter_bank.cc
@@ -0,0 +1,175 @@ +/* Copyright 2019 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 "signal/src/filter_bank.h" + +#include <stdint.h> + +#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" +#include "tensorflow/lite/kernels/kernel_util.h" +#include "tensorflow/lite/micro/flatbuffer_utils.h" +#include "tensorflow/lite/micro/kernels/kernel_util.h" +#include "tensorflow/lite/micro/memory_helpers.h" +#include "tensorflow/lite/micro/micro_context.h" +#include "tensorflow/lite/micro/micro_utils.h" + +namespace tflite { +namespace { + +constexpr int kInputTensor = 0; +constexpr int kWeightTensor = 1; +constexpr int kUnweightTensor = 2; +constexpr int kChFreqStartsTensor = 3; +constexpr int kChWeightStartsTensor = 4; +constexpr int kChannelWidthsTensor = 5; +constexpr int kOutputTensor = 0; + +// Indices into the init flexbuffer's vector. +// The parameter's name is in the comment that follows. +// Elements in the vectors are ordered alphabetically by parameter name. +constexpr int kNumChannelsIndex = 0; // 'num_channels' + +struct TFLMSignalFilterBankParams { + tflm_signal::FilterbankConfig config; + uint64_t* work_area; +}; + +void* Init(TfLiteContext* context, const char* buffer, size_t length) { + TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); + + auto* params = static_cast<TFLMSignalFilterBankParams*>( + context->AllocatePersistentBuffer(context, + sizeof(TFLMSignalFilterBankParams))); + if (params == nullptr) { + return nullptr; + } + + tflite::FlexbufferWrapper fbw(reinterpret_cast<const uint8_t*>(buffer), + length); + params->config.num_channels = fbw.ElementAsInt32(kNumChannelsIndex); + + params->work_area = static_cast<uint64_t*>(context->AllocatePersistentBuffer( + context, (params->config.num_channels + 1) * sizeof(uint64_t))); + + if (params->work_area == nullptr) { + return nullptr; + } + + return params; +} + +TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { + TF_LITE_ENSURE_EQ(context, NumInputs(node), 6); + TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); + + MicroContext* micro_context = GetMicroContext(context); + TfLiteTensor* input = + micro_context->AllocateTempInputTensor(node, kInputTensor); + TF_LITE_ENSURE(context, input != nullptr); + TF_LITE_ENSURE_EQ(context, NumDimensions(input), 1); + TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteUInt32); + micro_context->DeallocateTempTfLiteTensor(input); + + input = micro_context->AllocateTempInputTensor(node, kWeightTensor); + TF_LITE_ENSURE(context, input != nullptr); + TF_LITE_ENSURE_EQ(context, NumDimensions(input), 1); + TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteInt16); + micro_context->DeallocateTempTfLiteTensor(input); + + input = micro_context->AllocateTempInputTensor(node, kUnweightTensor); + TF_LITE_ENSURE(context, input != nullptr); + TF_LITE_ENSURE_EQ(context, NumDimensions(input), 1); + TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteInt16); + micro_context->DeallocateTempTfLiteTensor(input); + + input = micro_context->AllocateTempInputTensor(node, kChFreqStartsTensor); + TF_LITE_ENSURE(context, input != nullptr); + TF_LITE_ENSURE_EQ(context, NumDimensions(input), 1); + TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteInt16); + micro_context->DeallocateTempTfLiteTensor(input); + + input = micro_context->AllocateTempInputTensor(node, kChWeightStartsTensor); + TF_LITE_ENSURE(context, input != nullptr); + TF_LITE_ENSURE_EQ(context, NumDimensions(input), 1); + TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteInt16); + micro_context->DeallocateTempTfLiteTensor(input); + + input = micro_context->AllocateTempInputTensor(node, kChannelWidthsTensor); + TF_LITE_ENSURE(context, input != nullptr); + TF_LITE_ENSURE_EQ(context, NumDimensions(input), 1); + TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteInt16); + micro_context->DeallocateTempTfLiteTensor(input); + + TfLiteTensor* output = + micro_context->AllocateTempOutputTensor(node, kOutputTensor); + TF_LITE_ENSURE(context, output != nullptr); + TF_LITE_ENSURE_EQ(context, NumDimensions(output), 1); + TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteUInt64); + micro_context->DeallocateTempTfLiteTensor(output); + + return kTfLiteOk; +} + +TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { + auto* params = reinterpret_cast<TFLMSignalFilterBankParams*>(node->user_data); + + const TfLiteEvalTensor* input0 = + tflite::micro::GetEvalInput(context, node, kInputTensor); + const TfLiteEvalTensor* input1 = + tflite::micro::GetEvalInput(context, node, kWeightTensor); + const TfLiteEvalTensor* input2 = + tflite::micro::GetEvalInput(context, node, kUnweightTensor); + const TfLiteEvalTensor* input3 = + tflite::micro::GetEvalInput(context, node, kChFreqStartsTensor); + const TfLiteEvalTensor* input4 = + tflite::micro::GetEvalInput(context, node, kChWeightStartsTensor); + const TfLiteEvalTensor* input5 = + tflite::micro::GetEvalInput(context, node, kChannelWidthsTensor); + TfLiteEvalTensor* output = + tflite::micro::GetEvalOutput(context, node, kOutputTensor); + + params->config.weights = tflite::micro::GetTensorData<int16_t>(input1); + params->config.unweights = tflite::micro::GetTensorData<int16_t>(input2); + params->config.channel_frequency_starts = + tflite::micro::GetTensorData<int16_t>(input3); + params->config.channel_weight_starts = + tflite::micro::GetTensorData<int16_t>(input4); + params->config.channel_widths = tflite::micro::GetTensorData<int16_t>(input5); + + const uint32_t* input_data = tflite::micro::GetTensorData<uint32_t>(input0); + uint64_t* output_data = tflite::micro::GetTensorData<uint64_t>(output); + tflm_signal::FilterbankAccumulateChannels(¶ms->config, input_data, + params->work_area); + + size_t output_size; + TfLiteTypeSizeOf(output->type, &output_size); + output_size *= ElementCount(*output->dims); + // Discard channel 0, which is just scratch + memcpy(output_data, params->work_area + 1, output_size); + return kTfLiteOk; +} + +} // namespace + +namespace tflm_signal { + +TFLMRegistration* Register_FILTER_BANK() { + static TFLMRegistration r = tflite::micro::RegisterOp(Init, Prepare, Eval); + return &r; +} + +} // namespace tflm_signal + +} // namespace tflite
diff --git a/signal/micro/kernels/filter_bank_flexbuffers_generated_data.cc b/signal/micro/kernels/filter_bank_flexbuffers_generated_data.cc new file mode 100644 index 0000000..be88488 --- /dev/null +++ b/signal/micro/kernels/filter_bank_flexbuffers_generated_data.cc
@@ -0,0 +1,29 @@ +/* Copyright 2021 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. +==============================================================================*/ + +// This file is generated. See: +// tensorflow/lite/micro/kernels/test_data_generation/README.md + +#include "signal/micro/kernels/filter_bank_flexbuffers_generated_data.h" + +const int g_gen_data_size_filter_bank_32_channel = 23; +const unsigned char g_gen_data_filter_bank_32_channel[] = { + 0x6e, 0x75, 0x6d, 0x5f, 0x63, 0x68, 0x61, 0x6e, 0x6e, 0x65, 0x6c, 0x73, + 0x00, 0x01, 0x0e, 0x01, 0x01, 0x01, 0x20, 0x04, 0x02, 0x24, 0x01, +}; +const int g_gen_data_size_filter_bank_16_channel = 23; +const unsigned char g_gen_data_filter_bank_16_channel[] = { + 0x6e, 0x75, 0x6d, 0x5f, 0x63, 0x68, 0x61, 0x6e, 0x6e, 0x65, 0x6c, 0x73, + 0x00, 0x01, 0x0e, 0x01, 0x01, 0x01, 0x10, 0x04, 0x02, 0x24, 0x01, +};
diff --git a/signal/micro/kernels/filter_bank_flexbuffers_generated_data.h b/signal/micro/kernels/filter_bank_flexbuffers_generated_data.h new file mode 100644 index 0000000..59e74e7 --- /dev/null +++ b/signal/micro/kernels/filter_bank_flexbuffers_generated_data.h
@@ -0,0 +1,25 @@ +/* Copyright 2020 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. +==============================================================================*/ + +#ifndef SIGNAL_MICRO_KERNELS_TEST_DATA_GENERATION_GENERATE_FILTER_BANK_FLEXBUFFERS_DATA_H_ +#define SIGNAL_MICRO_KERNELS_TEST_DATA_GENERATION_GENERATE_FILTER_BANK_FLEXBUFFERS_DATA_H_ + +extern const int g_gen_data_size_filter_bank_32_channel; +extern const unsigned char g_gen_data_filter_bank_32_channel[]; + +extern const int g_gen_data_size_filter_bank_16_channel; +extern const unsigned char g_gen_data_filter_bank_16_channel[]; + +#endif // SIGNAL_MICRO_KERNELS_TEST_DATA_GENERATION_GENERATE_FILTER_BANK_FLEXBUFFERS_DATA_H_
diff --git a/signal/micro/kernels/filter_bank_log.cc b/signal/micro/kernels/filter_bank_log.cc new file mode 100644 index 0000000..ea0cdb7 --- /dev/null +++ b/signal/micro/kernels/filter_bank_log.cc
@@ -0,0 +1,112 @@ +/* Copyright 2019 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 "signal/src/filter_bank_log.h" + +#include <stdint.h> + +#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" +#include "tensorflow/lite/kernels/kernel_util.h" +#include "tensorflow/lite/micro/flatbuffer_utils.h" +#include "tensorflow/lite/micro/kernels/kernel_util.h" +#include "tensorflow/lite/micro/memory_helpers.h" +#include "tensorflow/lite/micro/micro_context.h" +#include "tensorflow/lite/micro/micro_utils.h" + +namespace tflite { +namespace { + +constexpr int kInputTensor = 0; +constexpr int kOutputTensor = 0; + +// Indices into the init flexbuffer's vector. +// The parameter's name is in the comment that follows. +// Elements in the vectors are ordered alphabetically by parameter name. +constexpr int kInputCorrectionBitsIndex = 0; // 'input_correction_bits' +constexpr int kOutputScaleIndex = 1; // 'output_scale' + +struct TFLMSignalLogParams { + int input_correction_bits; + int output_scale; +}; + +void* Init(TfLiteContext* context, const char* buffer, size_t length) { + TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); + + auto* params = static_cast<TFLMSignalLogParams*>( + context->AllocatePersistentBuffer(context, sizeof(TFLMSignalLogParams))); + + if (params == nullptr) { + return nullptr; + } + tflite::FlexbufferWrapper fbw(reinterpret_cast<const uint8_t*>(buffer), + length); + + params->input_correction_bits = fbw.ElementAsInt32(kInputCorrectionBitsIndex); + params->output_scale = fbw.ElementAsInt32(kOutputScaleIndex); + return params; +} + +TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { + TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); + TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); + + MicroContext* micro_context = GetMicroContext(context); + TfLiteTensor* input = + micro_context->AllocateTempInputTensor(node, kInputTensor); + TfLiteTensor* output = + micro_context->AllocateTempOutputTensor(node, kOutputTensor); + TF_LITE_ENSURE(context, input != nullptr); + TF_LITE_ENSURE(context, output != nullptr); + + TF_LITE_ENSURE_EQ(context, NumDimensions(input), 1); + TF_LITE_ENSURE_EQ(context, NumDimensions(output), 1); + + TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteUInt32); + TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteInt16); + + micro_context->DeallocateTempTfLiteTensor(input); + micro_context->DeallocateTempTfLiteTensor(output); + return kTfLiteOk; +} + +TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { + auto* params = reinterpret_cast<TFLMSignalLogParams*>(node->user_data); + + const TfLiteEvalTensor* input = + tflite::micro::GetEvalInput(context, node, kInputTensor); + TfLiteEvalTensor* output = + tflite::micro::GetEvalOutput(context, node, kOutputTensor); + + const uint32_t* input_data = tflite::micro::GetTensorData<uint32_t>(input); + int16_t* output_data = tflite::micro::GetTensorData<int16_t>(output); + int num_channels = input->dims->data[0]; + tflm_signal::FilterbankLog(input_data, num_channels, params->output_scale, + params->input_correction_bits, output_data); + return kTfLiteOk; +} + +} // namespace + +namespace tflm_signal { + +TFLMRegistration* Register_FILTER_BANK_LOG() { + static TFLMRegistration r = tflite::micro::RegisterOp(Init, Prepare, Eval); + return &r; +} + +} // namespace tflm_signal + +} // namespace tflite
diff --git a/signal/micro/kernels/filter_bank_log_flexbuffers_generated_data.cc b/signal/micro/kernels/filter_bank_log_flexbuffers_generated_data.cc new file mode 100644 index 0000000..09d7879 --- /dev/null +++ b/signal/micro/kernels/filter_bank_log_flexbuffers_generated_data.cc
@@ -0,0 +1,38 @@ +/* Copyright 2021 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. +==============================================================================*/ + +// This file is generated. See: +// tensorflow/lite/micro/kernels/test_data_generation/README.md + +#include "signal/micro/kernels/filter_bank_log_flexbuffers_generated_data.h" + +const int g_gen_data_size_filter_bank_log_scale_1600_correction_bits_3 = 53; +const unsigned char g_gen_data_filter_bank_log_scale_1600_correction_bits_3[] = + { + 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x5f, 0x73, 0x63, 0x61, 0x6c, + 0x65, 0x00, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x5f, 0x63, 0x6f, 0x72, + 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x62, 0x69, 0x74, + 0x73, 0x00, 0x02, 0x17, 0x25, 0x02, 0x00, 0x01, 0x00, 0x02, 0x00, + 0x03, 0x00, 0x40, 0x06, 0x05, 0x05, 0x06, 0x25, 0x01, +}; +const int g_gen_data_size_filter_bank_log_scale_32768_correction_bits_5 = 65; +const unsigned char g_gen_data_filter_bank_log_scale_32768_correction_bits_5[] = + { + 0x6f, 0x75, 0x74, 0x70, 0x75, 0x74, 0x5f, 0x73, 0x63, 0x61, 0x6c, + 0x65, 0x00, 0x69, 0x6e, 0x70, 0x75, 0x74, 0x5f, 0x63, 0x6f, 0x72, + 0x72, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x5f, 0x62, 0x69, 0x74, + 0x73, 0x00, 0x02, 0x17, 0x25, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, + 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, + 0x00, 0x00, 0x80, 0x00, 0x00, 0x06, 0x06, 0x0a, 0x26, 0x01, +};
diff --git a/signal/micro/kernels/filter_bank_log_flexbuffers_generated_data.h b/signal/micro/kernels/filter_bank_log_flexbuffers_generated_data.h new file mode 100644 index 0000000..dbc3bd9 --- /dev/null +++ b/signal/micro/kernels/filter_bank_log_flexbuffers_generated_data.h
@@ -0,0 +1,27 @@ +/* Copyright 2020 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. +==============================================================================*/ + +#ifndef SIGNAL_MICRO_KERNELS_TEST_DATA_GENERATION_GENERATE_FILTER_BANK_LOG_FLEXBUFFERS_DATA_H_ +#define SIGNAL_MICRO_KERNELS_TEST_DATA_GENERATION_GENERATE_FILTER_BANK_LOG_FLEXBUFFERS_DATA_H_ + +extern const int g_gen_data_size_filter_bank_log_scale_1600_correction_bits_3; +extern const unsigned char + g_gen_data_filter_bank_log_scale_1600_correction_bits_3[]; + +extern const int g_gen_data_size_filter_bank_log_scale_32768_correction_bits_5; +extern const unsigned char + g_gen_data_filter_bank_log_scale_32768_correction_bits_5[]; + +#endif // SIGNAL_MICRO_KERNELS_TEST_DATA_GENERATION_GENERATE_FILTER_BANK_LOG_FLEXBUFFERS_DATA_H_
diff --git a/signal/micro/kernels/filter_bank_log_test.cc b/signal/micro/kernels/filter_bank_log_test.cc new file mode 100644 index 0000000..546c5d6 --- /dev/null +++ b/signal/micro/kernels/filter_bank_log_test.cc
@@ -0,0 +1,116 @@ +/* Copyright 2019 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 <cstdint> + +#include "signal/micro/kernels/filter_bank_log_flexbuffers_generated_data.h" +#include "tensorflow/lite/micro/kernels/kernel_runner.h" +#include "tensorflow/lite/micro/test_helpers.h" +#include "tensorflow/lite/micro/testing/micro_test.h" + +namespace tflite { +namespace testing { +namespace { + +TfLiteStatus TestFilterBankLog(int* input_dims_data, const uint32_t* input_data, + int* output_dims_data, const int16_t* golden, + const uint8_t* flexbuffers_data, + const int flexbuffers_data_len, + int16_t* output_data) { + TfLiteIntArray* input_dims = IntArrayFromInts(input_dims_data); + TfLiteIntArray* output_dims = IntArrayFromInts(output_dims_data); + + constexpr int kInputsSize = 1; + constexpr int kOutputsSize = 2; + constexpr int kTensorsSize = kInputsSize + kOutputsSize; + TfLiteTensor tensors[kTensorsSize] = { + CreateTensor(input_data, input_dims), + CreateTensor(output_data, output_dims), + }; + + int inputs_array_data[] = {1, 0}; + TfLiteIntArray* inputs_array = IntArrayFromInts(inputs_array_data); + int outputs_array_data[] = {1, 1}; + TfLiteIntArray* outputs_array = IntArrayFromInts(outputs_array_data); + + const int output_len = ElementCount(*output_dims); + + TFLMRegistration* registration = + tflite::tflm_signal::Register_FILTER_BANK_LOG(); + micro::KernelRunner runner(*registration, tensors, kTensorsSize, inputs_array, + outputs_array, + /*builtin_data=*/nullptr); + + // TfLite uses a char* for the raw bytes whereas flexbuffers use an unsigned + // char*. This small discrepancy results in compiler warnings unless we + // reinterpret_cast right before passing in the flexbuffer bytes to the + // KernelRunner. + TF_LITE_ENSURE_STATUS(runner.InitAndPrepare( + reinterpret_cast<const char*>(flexbuffers_data), flexbuffers_data_len)); + + TF_LITE_ENSURE_STATUS(runner.Invoke()); + + for (int i = 0; i < output_len; ++i) { + TF_LITE_MICRO_EXPECT_EQ(golden[i], output_data[i]); + } + + return kTfLiteOk; +} + +} // namespace +} // namespace testing +} // namespace tflite + +TF_LITE_MICRO_TESTS_BEGIN + +TF_LITE_MICRO_TEST(FilterBankLogTest32Channel) { + int input_shape[] = {1, 32}; + int output_shape[] = {1, 32}; + const uint32_t input[] = {29, 21, 29, 40, 19, 11, 13, 23, 13, 11, 25, + 17, 5, 4, 46, 14, 17, 14, 20, 14, 10, 10, + 15, 11, 17, 12, 15, 16, 19, 18, 6, 2}; + const int16_t golden[] = {8715, 8198, 8715, 9229, 8038, 7164, 7431, 8344, + 7431, 7164, 8477, 7860, 5902, 5545, 9453, 7550, + 7860, 7550, 8120, 7550, 7011, 7011, 7660, 7164, + 7860, 7303, 7660, 7763, 8038, 7952, 6194, 4436}; + int16_t output[32]; + TF_LITE_MICRO_EXPECT_EQ( + kTfLiteOk, + tflite::testing::TestFilterBankLog( + input_shape, input, output_shape, golden, + g_gen_data_filter_bank_log_scale_1600_correction_bits_3, + g_gen_data_size_filter_bank_log_scale_1600_correction_bits_3, + output)); +} + +TF_LITE_MICRO_TEST(FilterBankLogTest16Channel) { + int input_shape[] = {1, 16}; + int output_shape[] = {1, 16}; + const uint32_t input[] = {48, 20, 19, 24, 35, 47, 23, 30, + 31, 10, 48, 21, 46, 14, 18, 27}; + const int16_t golden[] = {32767, 15121, 13440, 21095, 32767, 32767, + 19701, 28407, 29482, 32767, 32767, 16720, + 32767, 3434, 11669, 24955}; + int16_t output[16]; + TF_LITE_MICRO_EXPECT_EQ( + kTfLiteOk, + tflite::testing::TestFilterBankLog( + input_shape, input, output_shape, golden, + g_gen_data_filter_bank_log_scale_32768_correction_bits_5, + g_gen_data_size_filter_bank_log_scale_32768_correction_bits_5, + output)); +} + +TF_LITE_MICRO_TESTS_END
diff --git a/signal/micro/kernels/filter_bank_spectral_subtraction.cc b/signal/micro/kernels/filter_bank_spectral_subtraction.cc new file mode 100644 index 0000000..f5ea4d7 --- /dev/null +++ b/signal/micro/kernels/filter_bank_spectral_subtraction.cc
@@ -0,0 +1,177 @@ +/* Copyright 2019 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 "signal/src/filter_bank_spectral_subtraction.h" + +#include <stdint.h> + +#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" +#include "tensorflow/lite/kernels/kernel_util.h" +#include "tensorflow/lite/micro/flatbuffer_utils.h" +#include "tensorflow/lite/micro/kernels/kernel_util.h" +#include "tensorflow/lite/micro/memory_helpers.h" +#include "tensorflow/lite/micro/micro_utils.h" + +namespace tflite { +namespace { + +constexpr int kInputTensor = 0; +constexpr int kOutputTensor = 0; +constexpr int kNoiseEstimateTensor = 1; + +// Indices into the init flexbuffer's vector. +// The parameter's name is in the comment that follows. +// Elements in the vectors are ordered alphabetically by parameter name. +// 'alternate_one_minus_smoothing' +constexpr int kAlternateOneMinusSmoothingIndex = 0; +constexpr int kAlternateSmoothingIndex = 1; // 'alternate_smoothing' +constexpr int kClampingIndex = 2; // 'clamping' +constexpr int kMinSignalRemainingIndex = 3; // 'min_signal_remaining' +constexpr int kNumChannelsIndex = 4; // 'num_channels' +constexpr int kOneMinusSmoothingIndex = 5; // 'one_minus_smoothing' +constexpr int kSmoothingIndex = 6; // 'smoothing' +constexpr int kSmoothingBitsIndex = 7; // 'smoothing_bits' +constexpr int kSpectralSubtractionBitsIndex = 8; // 'spectral_subtraction_bits' + +struct TFLMSignalSpectralSubtractionParams { + tflm_signal::SpectralSubtractionConfig config; + uint32_t* noise_estimate; + size_t noise_estimate_size; +}; + +void ResetState(TFLMSignalSpectralSubtractionParams* params) { + memset(params->noise_estimate, 0, + sizeof(uint32_t) * params->config.num_channels); +} + +void* Init(TfLiteContext* context, const char* buffer, size_t length) { + TFLITE_DCHECK(context->AllocatePersistentBuffer != nullptr); + + auto* params = static_cast<TFLMSignalSpectralSubtractionParams*>( + context->AllocatePersistentBuffer( + context, sizeof(TFLMSignalSpectralSubtractionParams))); + + if (params == nullptr) { + return nullptr; + } + + tflite::FlexbufferWrapper fbw(reinterpret_cast<const uint8_t*>(buffer), + length); + params->config.alternate_one_minus_smoothing = + fbw.ElementAsInt32(kAlternateOneMinusSmoothingIndex); + params->config.alternate_smoothing = + fbw.ElementAsInt32(kAlternateSmoothingIndex); + params->config.clamping = fbw.ElementAsBool(kClampingIndex); + params->config.min_signal_remaining = + fbw.ElementAsInt32(kMinSignalRemainingIndex); + params->config.num_channels = fbw.ElementAsInt32(kNumChannelsIndex); + params->config.one_minus_smoothing = + fbw.ElementAsInt32(kOneMinusSmoothingIndex); + params->config.one_minus_smoothing = + fbw.ElementAsInt32(kOneMinusSmoothingIndex); + params->config.smoothing = fbw.ElementAsInt32(kSmoothingIndex); + params->config.smoothing_bits = fbw.ElementAsInt32(kSmoothingBitsIndex); + params->config.spectral_subtraction_bits = + fbw.ElementAsInt32(kSpectralSubtractionBitsIndex); + params->noise_estimate = + static_cast<uint32_t*>(context->AllocatePersistentBuffer( + context, params->config.num_channels * sizeof(uint32_t))); + + if (params->noise_estimate == nullptr) { + return nullptr; + } + + return params; +} + +TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { + TF_LITE_ENSURE_EQ(context, NumInputs(node), 1); + TF_LITE_ENSURE_EQ(context, NumOutputs(node), 2); + + MicroContext* micro_context = GetMicroContext(context); + + TfLiteTensor* input = + micro_context->AllocateTempInputTensor(node, kInputTensor); + TfLiteTensor* output = + micro_context->AllocateTempOutputTensor(node, kOutputTensor); + TfLiteTensor* noise_estimate = + micro_context->AllocateTempOutputTensor(node, kNoiseEstimateTensor); + TF_LITE_ENSURE(context, input != nullptr); + TF_LITE_ENSURE(context, output != nullptr); + TF_LITE_ENSURE(context, noise_estimate != nullptr); + + TF_LITE_ENSURE_EQ(context, NumDimensions(input), 1); + TF_LITE_ENSURE_EQ(context, NumDimensions(output), 1); + TF_LITE_ENSURE_EQ(context, NumDimensions(noise_estimate), 1); + + TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteUInt32); + TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteUInt32); + TF_LITE_ENSURE_TYPES_EQ(context, noise_estimate->type, kTfLiteUInt32); + + auto* params = + reinterpret_cast<TFLMSignalSpectralSubtractionParams*>(node->user_data); + TfLiteTypeSizeOf(output->type, ¶ms->noise_estimate_size); + params->noise_estimate_size *= ElementCount(*noise_estimate->dims); + + ResetState(params); + + micro_context->DeallocateTempTfLiteTensor(input); + micro_context->DeallocateTempTfLiteTensor(output); + micro_context->DeallocateTempTfLiteTensor(noise_estimate); + return kTfLiteOk; +} + +TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { + auto* params = + reinterpret_cast<TFLMSignalSpectralSubtractionParams*>(node->user_data); + + const TfLiteEvalTensor* input = + tflite::micro::GetEvalInput(context, node, kInputTensor); + TfLiteEvalTensor* output = + tflite::micro::GetEvalOutput(context, node, kOutputTensor); + TfLiteEvalTensor* noise_estimate = + tflite::micro::GetEvalOutput(context, node, kNoiseEstimateTensor); + + const uint32_t* input_data = tflite::micro::GetTensorData<uint32_t>(input); + uint32_t* output_data = tflite::micro::GetTensorData<uint32_t>(output); + uint32_t* noise_estimate_data = + tflite::micro::GetTensorData<uint32_t>(noise_estimate); + + FilterbankSpectralSubtraction(¶ms->config, input_data, output_data, + params->noise_estimate); + + memcpy(noise_estimate_data, params->noise_estimate, + params->noise_estimate_size); + + return kTfLiteOk; +} + +void Reset(TfLiteContext* context, void* buffer) { + ResetState(static_cast<TFLMSignalSpectralSubtractionParams*>(buffer)); +} + +} // namespace + +namespace tflm_signal { + +TFLMRegistration* Register_FILTER_BANK_SPECTRAL_SUBTRACTION() { + static TFLMRegistration r = + tflite::micro::RegisterOp(Init, Prepare, Eval, /*Free*/ nullptr, Reset); + return &r; +} + +} // namespace tflm_signal + +} // namespace tflite
diff --git a/signal/micro/kernels/filter_bank_spectral_subtraction_flexbuffers_generated_data.cc b/signal/micro/kernels/filter_bank_spectral_subtraction_flexbuffers_generated_data.cc new file mode 100644 index 0000000..d085dd2 --- /dev/null +++ b/signal/micro/kernels/filter_bank_spectral_subtraction_flexbuffers_generated_data.cc
@@ -0,0 +1,61 @@ +/* Copyright 2021 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. +==============================================================================*/ + +// This file is generated. See: +// tensorflow/lite/micro/kernels/test_data_generation/README.md + +#include "signal/micro/kernels/filter_bank_spectral_subtraction_flexbuffers_generated_data.h" + +const int g_gen_data_size_filter_bank_spectral_subtraction_32_channel = 210; +const unsigned char g_gen_data_filter_bank_spectral_subtraction_32_channel[] = { + 0x6e, 0x75, 0x6d, 0x5f, 0x63, 0x68, 0x61, 0x6e, 0x6e, 0x65, 0x6c, 0x73, + 0x00, 0x73, 0x6d, 0x6f, 0x6f, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x00, 0x6f, + 0x6e, 0x65, 0x5f, 0x6d, 0x69, 0x6e, 0x75, 0x73, 0x5f, 0x73, 0x6d, 0x6f, + 0x6f, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x00, 0x61, 0x6c, 0x74, 0x65, 0x72, + 0x6e, 0x61, 0x74, 0x65, 0x5f, 0x73, 0x6d, 0x6f, 0x6f, 0x74, 0x68, 0x69, + 0x6e, 0x67, 0x00, 0x61, 0x6c, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x74, 0x65, + 0x5f, 0x6f, 0x6e, 0x65, 0x5f, 0x6d, 0x69, 0x6e, 0x75, 0x73, 0x5f, 0x73, + 0x6d, 0x6f, 0x6f, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x00, 0x73, 0x6d, 0x6f, + 0x6f, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x5f, 0x62, 0x69, 0x74, 0x73, 0x00, + 0x6d, 0x69, 0x6e, 0x5f, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x6c, 0x5f, 0x72, + 0x65, 0x6d, 0x61, 0x69, 0x6e, 0x69, 0x6e, 0x67, 0x00, 0x63, 0x6c, 0x61, + 0x6d, 0x70, 0x69, 0x6e, 0x67, 0x00, 0x73, 0x70, 0x65, 0x63, 0x74, 0x72, + 0x61, 0x6c, 0x5f, 0x73, 0x75, 0x62, 0x74, 0x72, 0x61, 0x63, 0x74, 0x69, + 0x6f, 0x6e, 0x5f, 0x62, 0x69, 0x74, 0x73, 0x00, 0x09, 0x66, 0x7b, 0x26, + 0x3c, 0xa9, 0x93, 0x9e, 0x4f, 0x23, 0x09, 0x00, 0x01, 0x00, 0x09, 0x00, + 0x71, 0x3d, 0x8f, 0x02, 0x00, 0x00, 0x33, 0x03, 0x20, 0x00, 0x71, 0x3d, + 0x8f, 0x02, 0x00, 0x00, 0x0e, 0x00, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, + 0x05, 0x05, 0x05, 0x1b, 0x25, 0x01, +}; +const int g_gen_data_size_filter_bank_spectral_subtraction_16_channel = 210; +const unsigned char g_gen_data_filter_bank_spectral_subtraction_16_channel[] = { + 0x6e, 0x75, 0x6d, 0x5f, 0x63, 0x68, 0x61, 0x6e, 0x6e, 0x65, 0x6c, 0x73, + 0x00, 0x73, 0x6d, 0x6f, 0x6f, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x00, 0x6f, + 0x6e, 0x65, 0x5f, 0x6d, 0x69, 0x6e, 0x75, 0x73, 0x5f, 0x73, 0x6d, 0x6f, + 0x6f, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x00, 0x61, 0x6c, 0x74, 0x65, 0x72, + 0x6e, 0x61, 0x74, 0x65, 0x5f, 0x73, 0x6d, 0x6f, 0x6f, 0x74, 0x68, 0x69, + 0x6e, 0x67, 0x00, 0x61, 0x6c, 0x74, 0x65, 0x72, 0x6e, 0x61, 0x74, 0x65, + 0x5f, 0x6f, 0x6e, 0x65, 0x5f, 0x6d, 0x69, 0x6e, 0x75, 0x73, 0x5f, 0x73, + 0x6d, 0x6f, 0x6f, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x00, 0x73, 0x6d, 0x6f, + 0x6f, 0x74, 0x68, 0x69, 0x6e, 0x67, 0x5f, 0x62, 0x69, 0x74, 0x73, 0x00, + 0x6d, 0x69, 0x6e, 0x5f, 0x73, 0x69, 0x67, 0x6e, 0x61, 0x6c, 0x5f, 0x72, + 0x65, 0x6d, 0x61, 0x69, 0x6e, 0x69, 0x6e, 0x67, 0x00, 0x63, 0x6c, 0x61, + 0x6d, 0x70, 0x69, 0x6e, 0x67, 0x00, 0x73, 0x70, 0x65, 0x63, 0x74, 0x72, + 0x61, 0x6c, 0x5f, 0x73, 0x75, 0x62, 0x74, 0x72, 0x61, 0x63, 0x74, 0x69, + 0x6f, 0x6e, 0x5f, 0x62, 0x69, 0x74, 0x73, 0x00, 0x09, 0x66, 0x7b, 0x26, + 0x3c, 0xa9, 0x93, 0x9e, 0x4f, 0x23, 0x09, 0x00, 0x01, 0x00, 0x09, 0x00, + 0x71, 0x3d, 0x8f, 0x02, 0x00, 0x00, 0x33, 0x03, 0x10, 0x00, 0x71, 0x3d, + 0x8f, 0x02, 0x00, 0x00, 0x0e, 0x00, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, + 0x05, 0x05, 0x05, 0x1b, 0x25, 0x01, +};
diff --git a/signal/micro/kernels/filter_bank_spectral_subtraction_flexbuffers_generated_data.h b/signal/micro/kernels/filter_bank_spectral_subtraction_flexbuffers_generated_data.h new file mode 100644 index 0000000..175a14e --- /dev/null +++ b/signal/micro/kernels/filter_bank_spectral_subtraction_flexbuffers_generated_data.h
@@ -0,0 +1,26 @@ +/* Copyright 2020 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. +==============================================================================*/ + +#ifndef SIGNAL_MICRO_KERNELS_TEST_DATA_GENERATION_GENERATE_FILTER_BANK_SPECTRAL_SUBTRACTION_FLEXBUFFERS_DATA_H_ +#define SIGNAL_MICRO_KERNELS_TEST_DATA_GENERATION_GENERATE_FILTER_BANK_SPECTRAL_SUBTRACTION_FLEXBUFFERS_DATA_H_ + +extern const int g_gen_data_size_filter_bank_spectral_subtraction_32_channel; +extern const unsigned char + g_gen_data_filter_bank_spectral_subtraction_32_channel[]; +extern const int g_gen_data_size_filter_bank_spectral_subtraction_16_channel; +extern const unsigned char + g_gen_data_filter_bank_spectral_subtraction_16_channel[]; + +#endif // SIGNAL_MICRO_KERNELS_TEST_DATA_GENERATION_GENERATE_FILTER_BANK_SPECTRAL_SUBTRACTION_FLEXBUFFERS_DATA_H_
diff --git a/signal/micro/kernels/filter_bank_spectral_subtraction_test.cc b/signal/micro/kernels/filter_bank_spectral_subtraction_test.cc new file mode 100644 index 0000000..885995c --- /dev/null +++ b/signal/micro/kernels/filter_bank_spectral_subtraction_test.cc
@@ -0,0 +1,239 @@ +/* Copyright 2019 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 <cstdint> + +#include "signal/micro/kernels/filter_bank_spectral_subtraction_flexbuffers_generated_data.h" +#include "tensorflow/lite/c/common.h" +#include "tensorflow/lite/micro/kernels/kernel_runner.h" +#include "tensorflow/lite/micro/test_helpers.h" +#include "tensorflow/lite/micro/testing/micro_test.h" + +namespace tflite { +namespace testing { +namespace { + +constexpr int kInputsSize = 1; +constexpr int kOutputsSize = 2; +constexpr int kTensorsSize = kInputsSize + kOutputsSize; + +// Speicalized Kernel Runner for running test for the Filter Bank Spectral +// Subtract +// OP . +class FilterBankSpectralSubtractKernelRunner { + public: + explicit FilterBankSpectralSubtractKernelRunner(int* input_dims_data, + const uint32_t* input_data, + int* output_dims_data, + uint32_t* output_data1, + uint32_t* output_data2) + : inputs_array_(IntArrayFromInts(inputs_array_data_)), + outputs_array_(IntArrayFromInts(outputs_array_data_)), + kernel_runner_(*registration_, tensors_, kTensorsSize, inputs_array_, + outputs_array_, nullptr) { + tensors_[0] = tflite::testing::CreateTensor( + input_data, tflite::testing::IntArrayFromInts(input_dims_data)); + + tensors_[1] = tflite::testing::CreateTensor( + output_data1, tflite::testing::IntArrayFromInts(output_dims_data)); + + tensors_[2] = tflite::testing::CreateTensor( + output_data2, tflite::testing::IntArrayFromInts(output_dims_data)); + } + + tflite::micro::KernelRunner& kernel_runner() { return kernel_runner_; } + + private: + int inputs_array_data_[kInputsSize + 1] = {1, 0}; + int outputs_array_data_[kOutputsSize + 1] = {2, 1, 2}; + TfLiteTensor tensors_[kTensorsSize] = {}; + TfLiteIntArray* inputs_array_ = nullptr; + TfLiteIntArray* outputs_array_ = nullptr; + TFLMRegistration* registration_ = + tflite::tflm_signal::Register_FILTER_BANK_SPECTRAL_SUBTRACTION(); + tflite::micro::KernelRunner kernel_runner_; +}; + +TfLiteStatus TestFilterBankSpectralSubtractionInvoke( + int* output_dims_data, const uint32_t* golden1, const uint32_t* golden2, + uint32_t* output1_data, uint32_t* output2_data, + tflite::micro::KernelRunner& kernel_runner) { + TfLiteIntArray* output_dims = IntArrayFromInts(output_dims_data); + const int output_len = ElementCount(*output_dims); + + TF_LITE_ENSURE_STATUS(kernel_runner.Invoke()); + + for (int i = 0; i < output_len; ++i) { + TF_LITE_MICRO_EXPECT_EQ(golden1[i], output1_data[i]); + TF_LITE_MICRO_EXPECT_EQ(golden2[i], output2_data[i]); + } + + return kTfLiteOk; +} + +TfLiteStatus TestFilterBankSpectralSubtraction( + int* input_dims_data, const uint32_t* input_data, int* output_dims_data, + const uint32_t* golden1, const uint32_t* golden2, + const uint8_t* flexbuffers_data, const int flexbuffers_data_len, + uint32_t* output1_data, uint32_t* output2_data) { + FilterBankSpectralSubtractKernelRunner filter_bank_spectral_subtract_runner( + input_dims_data, input_data, output_dims_data, output1_data, + output2_data); + + // TfLite uses a char* for the raw bytes whereas flexbuffers use an unsigned + // char*. This small discrepancy results in compiler warnings unless we + // reinterpret_cast right before passing in the flexbuffer bytes to the + // KernelRunner. + TF_LITE_ENSURE_STATUS( + filter_bank_spectral_subtract_runner.kernel_runner().InitAndPrepare( + reinterpret_cast<const char*>(flexbuffers_data), + flexbuffers_data_len)); + + TF_LITE_ENSURE_STATUS(TestFilterBankSpectralSubtractionInvoke( + output_dims_data, golden1, golden2, output1_data, output2_data, + filter_bank_spectral_subtract_runner.kernel_runner())); + + return kTfLiteOk; +} + +TfLiteStatus TestFilterBankSpectralSubtractionReset( + int* input_dims_data, const uint32_t* input_data, int* output_dims_data, + const uint32_t* golden1, const uint32_t* golden2, + const uint8_t* flexbuffers_data, const int flexbuffers_data_len, + uint32_t* output1_data, uint32_t* output2_data) { + FilterBankSpectralSubtractKernelRunner filter_bank_spectral_subtract_runner( + input_dims_data, input_data, output_dims_data, output1_data, + output2_data); + + // TfLite uses a char* for the raw bytes whereas flexbuffers use an unsigned + // char*. This small discrepancy results in compiler warnings unless we + // reinterpret_cast right before passing in the flexbuffer bytes to the + // KernelRunner. + TF_LITE_ENSURE_STATUS( + filter_bank_spectral_subtract_runner.kernel_runner().InitAndPrepare( + reinterpret_cast<const char*>(flexbuffers_data), + flexbuffers_data_len)); + + TF_LITE_ENSURE_STATUS(TestFilterBankSpectralSubtractionInvoke( + output_dims_data, golden1, golden2, output1_data, output2_data, + filter_bank_spectral_subtract_runner.kernel_runner())); + filter_bank_spectral_subtract_runner.kernel_runner().Reset(); + TF_LITE_ENSURE_STATUS(TestFilterBankSpectralSubtractionInvoke( + output_dims_data, golden1, golden2, output1_data, output2_data, + filter_bank_spectral_subtract_runner.kernel_runner())); + + return kTfLiteOk; +} + +} // namespace +} // namespace testing +} // namespace tflite + +TF_LITE_MICRO_TESTS_BEGIN + +TF_LITE_MICRO_TEST(FilterBankSpectralSubtractionTest32Channel) { + int input_shape[] = {1, 32}; + int output_shape[] = {1, 32}; + + const uint32_t input[] = {322, 308, 210, 212, 181, 251, 403, 259, 65, 48, 76, + 48, 50, 46, 53, 52, 112, 191, 136, 59, 70, 51, + 39, 64, 33, 44, 41, 49, 74, 107, 262, 479}; + const uint32_t golden1[] = {310, 296, 202, 204, 174, 241, 387, 249, + 63, 47, 73, 47, 49, 45, 51, 50, + 108, 184, 131, 57, 68, 49, 38, 62, + 32, 43, 40, 48, 72, 103, 252, 460}; + const uint32_t golden2[] = {12, 12, 8, 8, 7, 10, 16, 10, 2, 1, 3, + 1, 1, 1, 2, 2, 4, 7, 5, 2, 2, 2, + 1, 2, 1, 1, 1, 1, 2, 4, 10, 19}; + uint32_t output1[32]; + uint32_t output2[32]; + TF_LITE_MICRO_EXPECT_EQ( + kTfLiteOk, + tflite::testing::TestFilterBankSpectralSubtraction( + input_shape, input, output_shape, golden1, golden2, + g_gen_data_filter_bank_spectral_subtraction_32_channel, + g_gen_data_size_filter_bank_spectral_subtraction_32_channel, output1, + output2)); +} + +TF_LITE_MICRO_TEST(FilterBankSpectralSubtractionTest16Channel) { + int input_shape[] = {1, 16}; + int output_shape[] = {1, 16}; + + const uint32_t input[] = {393, 213, 408, 1, 361, 385, 386, 326, + 170, 368, 368, 305, 152, 322, 213, 319}; + const uint32_t golden1[] = {378, 205, 392, 1, 347, 370, 371, 313, + 164, 354, 354, 293, 146, 310, 205, 307}; + const uint32_t golden2[] = {15, 8, 16, 0, 14, 15, 15, 13, + 6, 14, 14, 12, 6, 12, 8, 12}; + uint32_t output1[32]; + uint32_t output2[32]; + TF_LITE_MICRO_EXPECT_EQ( + kTfLiteOk, + tflite::testing::TestFilterBankSpectralSubtraction( + input_shape, input, output_shape, golden1, golden2, + g_gen_data_filter_bank_spectral_subtraction_16_channel, + g_gen_data_size_filter_bank_spectral_subtraction_16_channel, output1, + output2)); +} + +TF_LITE_MICRO_TEST(FilterBankSpectralSubtractionTest32ChannelReset) { + int input_shape[] = {1, 32}; + int output_shape[] = {1, 32}; + + const uint32_t input[] = {322, 308, 210, 212, 181, 251, 403, 259, 65, 48, 76, + 48, 50, 46, 53, 52, 112, 191, 136, 59, 70, 51, + 39, 64, 33, 44, 41, 49, 74, 107, 262, 479}; + const uint32_t golden1[] = {310, 296, 202, 204, 174, 241, 387, 249, + 63, 47, 73, 47, 49, 45, 51, 50, + 108, 184, 131, 57, 68, 49, 38, 62, + 32, 43, 40, 48, 72, 103, 252, 460}; + const uint32_t golden2[] = {12, 12, 8, 8, 7, 10, 16, 10, 2, 1, 3, + 1, 1, 1, 2, 2, 4, 7, 5, 2, 2, 2, + 1, 2, 1, 1, 1, 1, 2, 4, 10, 19}; + + uint32_t output1[32]; + uint32_t output2[32]; + TF_LITE_MICRO_EXPECT_EQ( + kTfLiteOk, + tflite::testing::TestFilterBankSpectralSubtractionReset( + input_shape, input, output_shape, golden1, golden2, + g_gen_data_filter_bank_spectral_subtraction_32_channel, + g_gen_data_size_filter_bank_spectral_subtraction_32_channel, output1, + output2)); +} + +TF_LITE_MICRO_TEST(FilterBankSpectralSubtractionTest16ChannelReset) { + int input_shape[] = {1, 16}; + int output_shape[] = {1, 16}; + + const uint32_t input[] = {393, 213, 408, 1, 361, 385, 386, 326, + 170, 368, 368, 305, 152, 322, 213, 319}; + const uint32_t golden1[] = {378, 205, 392, 1, 347, 370, 371, 313, + 164, 354, 354, 293, 146, 310, 205, 307}; + const uint32_t golden2[] = {15, 8, 16, 0, 14, 15, 15, 13, + 6, 14, 14, 12, 6, 12, 8, 12}; + uint32_t output1[32]; + uint32_t output2[32]; + TF_LITE_MICRO_EXPECT_EQ( + kTfLiteOk, + tflite::testing::TestFilterBankSpectralSubtractionReset( + input_shape, input, output_shape, golden1, golden2, + g_gen_data_filter_bank_spectral_subtraction_16_channel, + g_gen_data_size_filter_bank_spectral_subtraction_16_channel, output1, + output2)); +} + +TF_LITE_MICRO_TESTS_END
diff --git a/signal/micro/kernels/filter_bank_square_root.cc b/signal/micro/kernels/filter_bank_square_root.cc new file mode 100644 index 0000000..8df2617 --- /dev/null +++ b/signal/micro/kernels/filter_bank_square_root.cc
@@ -0,0 +1,92 @@ +/* Copyright 2019 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 "signal/src/filter_bank_square_root.h" + +#include <stdint.h> + +#include "tensorflow/lite/kernels/internal/tensor_ctypes.h" +#include "tensorflow/lite/kernels/kernel_util.h" +#include "tensorflow/lite/micro/kernels/kernel_util.h" +#include "tensorflow/lite/micro/memory_helpers.h" +#include "tensorflow/lite/micro/micro_utils.h" + +namespace tflite { +namespace { + +constexpr int kInputTensor = 0; +constexpr int kScaleBitsTensor = 1; +constexpr int kOutputTensor = 0; + +TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) { + TF_LITE_ENSURE_EQ(context, NumInputs(node), 2); + TF_LITE_ENSURE_EQ(context, NumOutputs(node), 1); + + MicroContext* micro_context = GetMicroContext(context); + + TfLiteTensor* input = + micro_context->AllocateTempInputTensor(node, kInputTensor); + TfLiteTensor* scale_bits = + micro_context->AllocateTempInputTensor(node, kScaleBitsTensor); + TfLiteTensor* output = + micro_context->AllocateTempOutputTensor(node, kOutputTensor); + TF_LITE_ENSURE(context, input != nullptr); + TF_LITE_ENSURE(context, scale_bits != nullptr); + TF_LITE_ENSURE(context, output != nullptr); + + TF_LITE_ENSURE_EQ(context, NumDimensions(input), 1); + TF_LITE_ENSURE_EQ(context, NumDimensions(scale_bits), 0); + TF_LITE_ENSURE_EQ(context, NumDimensions(output), 1); + + TF_LITE_ENSURE_TYPES_EQ(context, input->type, kTfLiteUInt64); + TF_LITE_ENSURE_TYPES_EQ(context, scale_bits->type, kTfLiteInt32); + TF_LITE_ENSURE_TYPES_EQ(context, output->type, kTfLiteUInt32); + + micro_context->DeallocateTempTfLiteTensor(input); + micro_context->DeallocateTempTfLiteTensor(output); + micro_context->DeallocateTempTfLiteTensor(scale_bits); + return kTfLiteOk; +} + +TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { + const TfLiteEvalTensor* input = + tflite::micro::GetEvalInput(context, node, kInputTensor); + const TfLiteEvalTensor* scale_bits = + tflite::micro::GetEvalInput(context, node, kScaleBitsTensor); + TfLiteEvalTensor* output = + tflite::micro::GetEvalOutput(context, node, kOutputTensor); + + const uint64_t* input_data = tflite::micro::GetTensorData<uint64_t>(input); + const int32_t* scale_bits_data = + tflite::micro::GetTensorData<int32_t>(scale_bits); + uint32_t* output_data = tflite::micro::GetTensorData<uint32_t>(output); + int32_t num_channels = input->dims->data[0]; + tflm_signal::FilterbankSqrt(input_data, num_channels, *scale_bits_data, + output_data); + return kTfLiteOk; +} + +} // namespace + +namespace tflm_signal { + +TFLMRegistration* Register_FILTER_BANK_SQUARE_ROOT() { + static TFLMRegistration r = tflite::micro::RegisterOp(nullptr, Prepare, Eval); + return &r; +} + +} // namespace tflm_signal + +} // namespace tflite
diff --git a/signal/micro/kernels/filter_bank_square_root_test.cc b/signal/micro/kernels/filter_bank_square_root_test.cc new file mode 100644 index 0000000..5ae50b5 --- /dev/null +++ b/signal/micro/kernels/filter_bank_square_root_test.cc
@@ -0,0 +1,117 @@ +/* Copyright 2019 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 <cstdint> + +#include "tensorflow/lite/micro/kernels/kernel_runner.h" +#include "tensorflow/lite/micro/test_helpers.h" +#include "tensorflow/lite/micro/testing/micro_test.h" + +namespace tflite { +namespace testing { +namespace { + +TfLiteStatus TestFilterBankSquareRoot( + int* input1_dims_data, const uint64_t* input1_data, int* input2_dims_data, + const int32_t* input2_data, int* output_dims_data, const uint32_t* golden, + uint32_t* output_data) { + TfLiteIntArray* input1_dims = IntArrayFromInts(input1_dims_data); + TfLiteIntArray* input2_dims = IntArrayFromInts(input2_dims_data); + TfLiteIntArray* output_dims = IntArrayFromInts(output_dims_data); + + constexpr int kInputsSize = 1; + constexpr int kOutputsSize = 2; + constexpr int kTensorsSize = kInputsSize + kOutputsSize; + TfLiteTensor tensors[kTensorsSize] = { + CreateTensor(input1_data, input1_dims), + CreateTensor(input2_data, input2_dims), + CreateTensor(output_data, output_dims), + }; + + int inputs_array_data[] = {2, 0, 1}; + TfLiteIntArray* inputs_array = IntArrayFromInts(inputs_array_data); + int outputs_array_data[] = {1, 2}; + TfLiteIntArray* outputs_array = IntArrayFromInts(outputs_array_data); + + const int output_len = ElementCount(*output_dims); + + TFLMRegistration* registration = + tflite::tflm_signal::Register_FILTER_BANK_SQUARE_ROOT(); + micro::KernelRunner runner(*registration, tensors, kTensorsSize, inputs_array, + outputs_array, + /*builtin_data=*/nullptr); + + // TfLite uses a char* for the raw bytes whereas flexbuffers use an unsigned + // char*. This small discrepancy results in compiler warnings unless we + // reinterpret_cast right before passing in the flexbuffer bytes to the + // KernelRunner. + TF_LITE_ENSURE_STATUS(runner.InitAndPrepare(nullptr, 0)); + + TF_LITE_ENSURE_STATUS(runner.Invoke()); + + for (int i = 0; i < output_len; ++i) { + TF_LITE_MICRO_EXPECT_EQ(golden[i], output_data[i]); + } + + return kTfLiteOk; +} + +} // namespace +} // namespace testing +} // namespace tflite + +TF_LITE_MICRO_TESTS_BEGIN + +TF_LITE_MICRO_TEST(FilterBankSquareRoot32Channel) { + int input1_shape[] = {1, 32}; + int input2_shape[] = {0}; + int output_shape[] = {1, 32}; + const uint64_t input1[] = { + 10528000193, 28362909357, 47577133750, 8466055850, 5842710800, 2350911449, + 2989811430, 2646718839, 515262774, 276394561, 469831522, 55815334, + 28232446, 11591835, 40329249, 67658028, 183446654, 323189165, + 117473797, 41339272, 25846050, 12428673, 18670978, 22521722, + 78477733, 54207503, 25150296, 43098592, 28211625, 15736687, + 20990296, 17907031}; + const int32_t input2[] = {7}; + const uint32_t golden[] = {801, 1315, 1704, 718, 597, 378, 427, 401, + 177, 129, 169, 58, 41, 26, 49, 64, + 105, 140, 84, 50, 39, 27, 33, 37, + 69, 57, 39, 51, 41, 30, 35, 33}; + uint32_t output[32]; + TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, tflite::testing::TestFilterBankSquareRoot( + input1_shape, input1, input2_shape, + input2, output_shape, golden, output)); +} + +TF_LITE_MICRO_TEST(FilterBankSquareRoot16Channel) { + int input1_shape[] = {1, 16}; + int input2_shape[] = {0}; + int output_shape[] = {1, 16}; + const uint64_t input1[] = { + 13051415151, 14932650877, 18954728418, 8730126017, + 6529665275, 12952546517, 10314975609, 8919697835, + 8053663348, 17231208421, 7366899760, 1372112200, + 19953434807, 17012385332, 4710443222, 17765594053}; + const int32_t input2[] = {5}; + const uint32_t golden[] = {3570, 3818, 4302, 2919, 2525, 3556, 3173, 2951, + 2804, 4102, 2682, 1157, 4414, 4076, 2144, 4165}; + uint32_t output[16]; + TF_LITE_MICRO_EXPECT_EQ(kTfLiteOk, tflite::testing::TestFilterBankSquareRoot( + input1_shape, input1, input2_shape, + input2, output_shape, golden, output)); +} + +TF_LITE_MICRO_TESTS_END
diff --git a/signal/micro/kernels/filter_bank_test.cc b/signal/micro/kernels/filter_bank_test.cc new file mode 100644 index 0000000..924fdca --- /dev/null +++ b/signal/micro/kernels/filter_bank_test.cc
@@ -0,0 +1,261 @@ +/* Copyright 2019 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 <cstdint> + +#include "signal/micro/kernels/filter_bank_flexbuffers_generated_data.h" +#include "tensorflow/lite/micro/kernels/kernel_runner.h" +#include "tensorflow/lite/micro/test_helpers.h" +#include "tensorflow/lite/micro/testing/micro_test.h" + +namespace tflite { +namespace testing { +namespace { + +TfLiteStatus TestFilterBank(int* input1_dims_data, const uint32_t* input1_data, + int* input2_dims_data, const int16_t* input2_data, + int* input3_dims_data, const int16_t* input3_data, + int* input4_dims_data, const int16_t* input4_data, + int* input5_dims_data, const int16_t* input5_data, + int* input6_dims_data, const int16_t* input6_data, + int* output_dims_data, const uint64_t* golden, + const uint8_t* flexbuffers_data, + const int flexbuffers_data_len, + uint64_t* output_data) { + TfLiteIntArray* input1_dims = IntArrayFromInts(input1_dims_data); + TfLiteIntArray* input2_dims = IntArrayFromInts(input2_dims_data); + TfLiteIntArray* input3_dims = IntArrayFromInts(input3_dims_data); + TfLiteIntArray* input4_dims = IntArrayFromInts(input4_dims_data); + TfLiteIntArray* input5_dims = IntArrayFromInts(input5_dims_data); + TfLiteIntArray* input6_dims = IntArrayFromInts(input6_dims_data); + TfLiteIntArray* output_dims = IntArrayFromInts(output_dims_data); + + constexpr int kInputsSize = 6; + constexpr int kOutputsSize = 1; + constexpr int kTensorsSize = kInputsSize + kOutputsSize; + TfLiteTensor tensors[kTensorsSize] = { + CreateTensor(input1_data, input1_dims), + CreateTensor(input2_data, input2_dims), + CreateTensor(input3_data, input3_dims), + CreateTensor(input4_data, input4_dims), + CreateTensor(input5_data, input5_dims), + CreateTensor(input6_data, input6_dims), + CreateTensor(output_data, output_dims), + }; + + int inputs_array_data[] = {6, 0, 1, 2, 3, 4, 5}; + TfLiteIntArray* inputs_array = IntArrayFromInts(inputs_array_data); + int outputs_array_data[] = {1, 6}; + TfLiteIntArray* outputs_array = IntArrayFromInts(outputs_array_data); + + const int output_len = ElementCount(*output_dims); + + TFLMRegistration* registration = tflite::tflm_signal::Register_FILTER_BANK(); + micro::KernelRunner runner(*registration, tensors, kTensorsSize, inputs_array, + outputs_array, + /*builtin_data=*/nullptr); + + // TfLite uses a char* for the raw bytes whereas flexbuffers use an unsigned + // char*. This small discrepancy results in compiler warnings unless we + // reinterpret_cast right before passing in the flexbuffer bytes to the + // KernelRunner. + TF_LITE_ENSURE_STATUS(runner.InitAndPrepare( + reinterpret_cast<const char*>(flexbuffers_data), flexbuffers_data_len)); + + TF_LITE_ENSURE_STATUS(runner.Invoke()); + + for (int i = 0; i < output_len; ++i) { + TF_LITE_MICRO_EXPECT_EQ(golden[i], output_data[i]); + } + return kTfLiteOk; +} + +} // namespace +} // namespace testing +} // namespace tflite + +TF_LITE_MICRO_TESTS_BEGIN + +TF_LITE_MICRO_TEST(FilterBankTest32Channel) { + int input1_shape[] = {1, 257}; + int input2_shape[] = {1, 117}; + int input3_shape[] = {1, 117}; + int input4_shape[] = {1, 33}; + int input5_shape[] = {1, 33}; + int input6_shape[] = {1, 33}; + int output_shape[] = {1, 32}; + + uint64_t output[32]; + + const uint32_t input1[] = { + 65451, 11468838, 4280615122, 4283105055, 30080683, 969970, + 1168164, 192770, 344209, 1811809, 1740724, 586130, + 305045, 17981, 169273, 103321, 85277, 529901, + 524660, 116609, 29653, 64345, 13121, 273956, + 593748, 463432, 348169, 77545, 2117, 19277, + 13837, 85, 16322, 1325, 69584, 233930, + 253273, 94180, 8642, 104245, 151937, 231970, + 90405, 95849, 106285, 81938, 76226, 103337, + 303250, 337705, 75140, 43874, 33730, 44761, + 117608, 57322, 9945, 19816, 48674, 19465, + 15696, 52229, 103738, 102541, 126421, 133157, + 33680, 7738, 45029, 57122, 61605, 60138, + 26170, 41444, 210994, 238338, 74324, 21460, + 33125, 3940, 15481, 7709, 24929, 17714, + 170993, 91978, 45965, 214133, 96832, 1800, + 16717, 42341, 87421, 114341, 65161, 26260, + 135077, 245000, 122117, 81188, 107753, 74125, + 86432, 91460, 29648, 2069, 3161, 5002, + 784, 1152, 1424, 277, 452, 2696, + 3610, 2120, 2617, 562, 1153, 4610, + 2906, 65, 786450, 4293722107, 0, 393208, + 2, 196608, 65539, 65537, 4294967295, 65537, + 4294901762, 65535, 4294770689, 65533, 131073, 4294901761, + 131071, 131071, 65535, 4294901764, 4294967295, 0, + 4294901758, 4294901761, 196607, 4294836224, 131070, 4294901762, + 4294901759, 196608, 4294901761, 131071, 131070, 65538, + 0, 4294901761, 65536, 4294836225, 65536, 4294836225, + 4294901757, 65535, 4294901760, 196607, 4294967295, 0, + 131071, 4294901762, 4294836221, 196608, 65536, 1, + 131074, 4294770690, 4294967291, 196611, 4294770687, 262143, + 4294901759, 131071, 1, 4294901759, 196607, 4294705153, + 196607, 4294967294, 65536, 1, 4294901759, 65536, + 0, 65536, 65537, 4294901759, 65536, 3, + 4294836222, 65534, 65536, 65538, 4294836225, 4294901760, + 4294901761, 4294967293, 0, 65534, 131070, 65537, + 4294901762, 65536, 2, 4294836224, 1, 4294901760, + 0, 4294967294, 131073, 4294901760, 65535, 131073, + 4294836224, 65536, 4294901760, 4294901760, 4294967295, 4294901761, + 131071, 4294901760, 131071, 4294836224, 2, 4294901758, + 4294967292, 131073, 0, 65535, 0, 4294901760, + 4294967295, 131073, 4294901764, 4294836223, 4294967295, 65535, + 65537, 65533, 3, 131072, 4294836224, 65537, + 1, 4294967293, 196611, 4294901759, 1}; + + const int16_t input2[] = { + 1133, 2373, 3712, 1047, 2564, 66, 1740, 3486, 1202, 3079, 919, 2913, + 865, 2964, 1015, 3210, 1352, 3633, 1859, 123, 2520, 856, 3323, 1726, + 161, 2722, 1215, 3833, 2382, 956, 3652, 2276, 923, 3689, 2380, 1093, + 3922, 2676, 1448, 239, 3144, 1970, 814, 3770, 2646, 1538, 445, 3463, + 2399, 1349, 313, 3386, 2376, 1379, 394, 3517, 2556, 1607, 668, 3837, + 2920, 2013, 1117, 231, 3450, 2583, 1725, 877, 37, 3302, 2480, 1666, + 861, 63, 3369, 2588, 1813, 1046, 287, 3630, 2885, 2147, 1415, 690, + 4067, 3355, 2650, 1950, 1257, 569, 3984, 3308, 2638, 1973, 1314, 661, + 12, 3465, 2827, 2194, 1566, 943, 325, 3808, 3199, 2595, 1996, 1401, + 810, 224, 3738, 3160, 2586, 2017, 1451, 890, 332}; + + const int16_t input3[] = { + 2962, 1722, 383, 3048, 1531, 4029, 2355, 609, 2893, 1016, 3176, 1182, + 3230, 1131, 3080, 885, 2743, 462, 2236, 3972, 1575, 3239, 772, 2369, + 3934, 1373, 2880, 262, 1713, 3139, 443, 1819, 3172, 406, 1715, 3002, + 173, 1419, 2647, 3856, 951, 2125, 3281, 325, 1449, 2557, 3650, 632, + 1696, 2746, 3782, 709, 1719, 2716, 3701, 578, 1539, 2488, 3427, 258, + 1175, 2082, 2978, 3864, 645, 1512, 2370, 3218, 4058, 793, 1615, 2429, + 3234, 4032, 726, 1507, 2282, 3049, 3808, 465, 1210, 1948, 2680, 3405, + 28, 740, 1445, 2145, 2838, 3526, 111, 787, 1457, 2122, 2781, 3434, + 4083, 630, 1268, 1901, 2529, 3152, 3770, 287, 896, 1500, 2099, 2694, + 3285, 3871, 357, 935, 1509, 2078, 2644, 3205, 3763}; + + const int16_t input4[] = {5, 6, 7, 9, 11, 12, 14, 16, 18, 20, 22, + 25, 27, 30, 32, 35, 38, 41, 45, 48, 52, 56, + 60, 64, 69, 74, 79, 84, 89, 95, 102, 108, 115}; + + const int16_t input5[] = {0, 1, 2, 4, 6, 7, 9, 11, 13, 15, 17, + 20, 22, 25, 27, 30, 33, 36, 40, 43, 47, 51, + 55, 59, 64, 69, 74, 79, 84, 90, 97, 103, 110}; + + const int16_t input6[] = {1, 1, 2, 2, 1, 2, 2, 2, 2, 2, 3, 2, 3, 2, 3, 3, 3, + 4, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 7, 6, 7, 7}; + + const uint64_t golden[] = { + 5645104312, 3087527471, 5883346002, 10807122775, 2465336182, 853935004, + 1206905130, 3485828019, 1134726750, 832725041, 4442875878, 2122064365, + 178483220, 151483681, 1742660113, 1309124116, 1954305288, 1323857378, + 2750861165, 1340947482, 792522630, 669257768, 1659699572, 940652856, + 1957080469, 1034203505, 1541805928, 1710818326, 2432875876, 2254716277, + 275382345, 57293224}; + + TF_LITE_MICRO_EXPECT_EQ( + kTfLiteOk, + tflite::testing::TestFilterBank( + input1_shape, input1, input2_shape, input2, input3_shape, input3, + input4_shape, input4, input5_shape, input5, input6_shape, input6, + output_shape, golden, g_gen_data_filter_bank_32_channel, + g_gen_data_size_filter_bank_32_channel, output)); +} + +TF_LITE_MICRO_TEST(FilterBankTest16Channel) { + int input1_shape[] = {1, 129}; + int input2_shape[] = {1, 59}; + int input3_shape[] = {1, 59}; + int input4_shape[] = {1, 17}; + int input5_shape[] = {1, 17}; + int input6_shape[] = {1, 17}; + int output_shape[] = {1, 16}; + + uint64_t output[16]; + + const uint32_t input1[] = { + 645050, 4644, 3653, 24262, 56660, 43260, 50584, 57902, 31702, 5401, + 45555, 34852, 8518, 43556, 13358, 19350, 40221, 18017, 27284, 64491, + 60099, 17863, 11001, 29076, 32666, 65268, 50947, 28694, 32377, 30014, + 25607, 22547, 45086, 10654, 46797, 8622, 47348, 43085, 5747, 51544, + 50364, 6208, 20696, 59782, 14429, 60125, 37079, 32673, 63457, 60142, + 34042, 11280, 1874, 33734, 62118, 13766, 54398, 47818, 50976, 46930, + 25906, 59441, 25958, 59136, 1756, 18652, 29213, 13379, 51845, 1207, + 55626, 27108, 43771, 35236, 3374, 40959, 47707, 41540, 34282, 27094, + 36329, 13593, 65257, 47006, 46857, 1114, 37106, 18738, 25969, 15461, + 2842, 36470, 32489, 61622, 23613, 29624, 32820, 30438, 9543, 6767, + 23037, 52896, 12059, 32264, 11575, 42400, 43344, 27511, 16712, 6877, + 4910, 50047, 61569, 57237, 48558, 2310, 22192, 7874, 46141, 64056, + 61997, 7298, 31372, 25316, 683, 58940, 18755, 17898, 19196}; + + const int16_t input2[] = { + -2210, 1711, 3237, 1247, 2507, 61, 1019, 899, 206, 146, 2849, 2756, + 1260, 1280, 1951, 213, 617, 2047, 211, 347, 2821, 3747, 150, 1924, + 3962, 942, 1430, 2678, 993, 308, 3364, 2491, 954, 1308, 879, 3950, + 1, 3556, 3628, 2104, 78, 1298, 1080, 342, 1337, 1639, 2352, 829, + 1358, 2498, 1647, 2507, 3816, 3767, 3735, 1155, 2221, 2196, 1160}; + + const int16_t input3[] = { + 408, 3574, 1880, 2561, 2011, 3394, 1019, 445, 3901, 343, 1874, 3846, + 3566, 1830, 327, 111, 623, 1037, 2803, 1947, 1518, 661, 3239, 2351, + 1257, 269, 1574, 3431, 3972, 2487, 2181, 1458, 552, 717, 679, 1031, + 1738, 1782, 128, 2242, 353, 1460, 3305, 1424, 3813, 2895, 164, 272, + 3886, 3135, 141, 747, 3233, 1478, 2612, 3837, 3271, 73, 1746}; + + const int16_t input4[] = {5, 6, 7, 9, 11, 12, 14, 16, 18, + 20, 22, 25, 27, 30, 32, 35, 33}; + + const int16_t input5[] = {0, 1, 2, 4, 6, 7, 9, 11, 13, + 15, 17, 20, 22, 25, 27, 30, 33}; + + const int16_t input6[] = {1, 1, 2, 2, 1, 2, 2, 2, 2, 2, 3, 2, 3, 2, 3, 3, 3}; + + const uint64_t golden[] = {104199304, 407748384, 206363744, 200989269, + 52144406, 230780884, 174394190, 379684049, + 94840835, 57788823, 531528204, 318265707, + 263149795, 188110467, 501443259, 200747781}; + + TF_LITE_MICRO_EXPECT_EQ( + kTfLiteOk, + tflite::testing::TestFilterBank( + input1_shape, input1, input2_shape, input2, input3_shape, input3, + input4_shape, input4, input5_shape, input5, input6_shape, input6, + output_shape, golden, g_gen_data_filter_bank_16_channel, + g_gen_data_size_filter_bank_16_channel, output)); +} + +TF_LITE_MICRO_TESTS_END
diff --git a/signal/src/BUILD b/signal/src/BUILD index 638adcf..04e0605 100644 --- a/signal/src/BUILD +++ b/signal/src/BUILD
@@ -9,6 +9,42 @@ ) cc_library( + name = "square_root_32", + srcs = ["square_root_32.cc"], + hdrs = ["square_root.h"], + deps = [":msb_32"], +) + +cc_library( + name = "square_root_64", + srcs = ["square_root_64.cc"], + hdrs = ["square_root.h"], + deps = [ + ":msb_64", + ":square_root_32", + ], +) + +cc_library( + name = "log", + srcs = ["log.cc"], + hdrs = ["log.h"], + deps = [":msb_32"], +) + +cc_library( + name = "msb_32", + srcs = ["msb_32.cc"], + hdrs = ["msb.h"], +) + +cc_library( + name = "msb_64", + srcs = ["msb_64.cc"], + hdrs = ["msb.h"], +) + +cc_library( name = "rfft", srcs = [ "rfft_float.cc", @@ -46,3 +82,33 @@ hdrs = ["energy.h"], deps = [":complex"], ) + +cc_library( + name = "filter_bank", + srcs = ["filter_bank.cc"], + hdrs = ["filter_bank.h"], +) + +cc_library( + name = "filter_bank_log", + srcs = ["filter_bank_log.cc"], + hdrs = ["filter_bank_log.h"], + deps = [ + ":log", + ], +) + +cc_library( + name = "filter_bank_spectral_subtraction", + srcs = ["filter_bank_spectral_subtraction.cc"], + hdrs = ["filter_bank_spectral_subtraction.h"], +) + +cc_library( + name = "filter_bank_square_root", + srcs = ["filter_bank_square_root.cc"], + hdrs = ["filter_bank_square_root.h"], + deps = [ + ":square_root_64", + ], +)
diff --git a/signal/src/filter_bank.cc b/signal/src/filter_bank.cc new file mode 100644 index 0000000..8517e45 --- /dev/null +++ b/signal/src/filter_bank.cc
@@ -0,0 +1,53 @@ +/* Copyright 2019 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 "signal/src/filter_bank.h" + +namespace tflite { +namespace tflm_signal { + +void FilterbankAccumulateChannels(const FilterbankConfig* config, + const uint32_t* input, uint64_t* output) { + // With a log mel filterbank, the energy at each frequency gets added to + // two adjacent filterbank filters/channels. + // For the first filter bank channel, its energy is first multiplied by + // some weight 'w', then gets accumulated. + // For the subsequent filter bank, its power is first multiplied by 1-'w' + // (called unweight here), then gets accumulated. + // For this reason, we need to calculate (config->num_channels + 1) output + // where element 0 is only used as scratch storage for the unweights of + // element 1 (channel 0). The caller should discard element 0. + // Writing the code like this doesn't save multiplications, but it lends + // itself better to optimization, because input[freq_start + j] only needs + // to be loaded once. + uint64_t weight_accumulator = 0; + uint64_t unweight_accumulator = 0; + for (int i = 0; i < config->num_channels + 1; i++) { + const int16_t freq_start = config->channel_frequency_starts[i]; + const int16_t weight_start = config->channel_weight_starts[i]; + for (int j = 0; j < config->channel_widths[i]; ++j) { + weight_accumulator += config->weights[weight_start + j] * + static_cast<uint64_t>(input[freq_start + j]); + unweight_accumulator += config->unweights[weight_start + j] * + static_cast<uint64_t>(input[freq_start + j]); + } + output[i] = weight_accumulator; + weight_accumulator = unweight_accumulator; + unweight_accumulator = 0; + } +} + +} // namespace tflm_signal +} // namespace tflite \ No newline at end of file
diff --git a/signal/src/filter_bank.h b/signal/src/filter_bank.h new file mode 100644 index 0000000..95b2168 --- /dev/null +++ b/signal/src/filter_bank.h
@@ -0,0 +1,69 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef SIGNAL_SRC_FILTER_BANK_H_ +#define SIGNAL_SRC_FILTER_BANK_H_ + +#include <stdint.h> + +namespace tflite { +namespace tflm_signal { +// TODO(b/286250473): remove namespace once de-duped libraries above + +struct FilterbankConfig { + // Number of filterbank channels + int32_t num_channels; + + // Each of the following three arrays is of size num_channels + 1 + // An extra channel is needed for scratch. See implementation of + // FilterbankAccumulateChannels() for more details + + // For each channel, the index in the input (spectrum) where its band starts + const int16_t* channel_frequency_starts; + // For each channel, the index in the weights/unweights arrays where + // it filter weights start + const int16_t* channel_weight_starts; + // For each channel, the number of bins in the input (spectrum) that span + // its band + const int16_t* channel_widths; + + // The weights array holds the triangular filter weights of all the filters + // in the bank. The output of each filter in the bank is caluclated by + // multiplying the elements in the input spectrum that are in its band + // (see above: channel_frequency_starts, channel_widths) by the filter weights + // then accumulating. Each element in the unweights array holds the 1 minus + // corresponding elements in the weights array and is used to make this + // operation more efficient. For more details, see documnetation in + // FilterbankAccumulateChannels() + const int16_t* weights; + const int16_t* unweights; + int32_t output_scale; + + int32_t input_correction_bits; +}; + +// Accumulate the energy spectrum bins in `input` into filter bank channels +// contained in `output`. +// * `input` - Spectral energy array +// * `output` - of size `config.num_channels` + 1. +// Elements [1:num_channels] contain the filter bank channels. +// Element 0 is used as scratch and should be ignored +void FilterbankAccumulateChannels(const FilterbankConfig* config, + const uint32_t* input, uint64_t* output); + +} // namespace tflm_signal +} // namespace tflite + +#endif // SIGNAL_SRC_FILTER_BANK_H_
diff --git a/signal/src/filter_bank_log.cc b/signal/src/filter_bank_log.cc new file mode 100644 index 0000000..c670a7c --- /dev/null +++ b/signal/src/filter_bank_log.cc
@@ -0,0 +1,40 @@ +/* Copyright 2019 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 "signal/src/filter_bank_log.h" + +#include "signal/src/log.h" + +namespace tflite { +namespace tflm_signal { + +void FilterbankLog(const uint32_t* input, int num_channels, + int32_t output_scale, uint32_t correction_bits, + int16_t* output) { + for (int i = 0; i < num_channels; ++i) { + const uint32_t scaled = input[i] << correction_bits; + if (scaled > 1) { + const uint32_t log_value = Log32(scaled, output_scale); + output[i] = ((log_value < static_cast<uint32_t>(INT16_MAX)) + ? log_value + : static_cast<uint32_t>(INT16_MAX)); + } else { + output[i] = 0; + } + } +} + +} // namespace tflm_signal +} // namespace tflite
diff --git a/signal/src/filter_bank_log.h b/signal/src/filter_bank_log.h new file mode 100644 index 0000000..e8514c7 --- /dev/null +++ b/signal/src/filter_bank_log.h
@@ -0,0 +1,38 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef SIGNAL_SRC_FILTER_BANK_LOG_H_ +#define SIGNAL_SRC_FILTER_BANK_LOG_H_ + +#include <stdint.h> + +namespace tflite { +namespace tflm_signal { +// TODO(b/286250473): remove namespace once de-duped libraries above + +// Apply natural log to each element in array `input` of size `num_channels` +// with pre-shift and post scaling. +// The operation is roughly equivalent to: +// `output` = min(Log(`input` << `correction_bits`) * `output_scale`, INT16_MAX) +// Where: +// If (input << `correction_bits`) is 1 or 0, the function returns 0 +void FilterbankLog(const uint32_t* input, int num_channels, + int32_t output_scale, uint32_t correction_bits, + int16_t* output); + +} // namespace tflm_signal +} // namespace tflite + +#endif // SIGNAL_SRC_FILTER_BANK_LOG_H_
diff --git a/signal/src/filter_bank_spectral_subtraction.cc b/signal/src/filter_bank_spectral_subtraction.cc new file mode 100644 index 0000000..bbad86b --- /dev/null +++ b/signal/src/filter_bank_spectral_subtraction.cc
@@ -0,0 +1,64 @@ +/* Copyright 2019 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 "signal/src/filter_bank_spectral_subtraction.h" + +namespace tflite { +namespace tflm_signal { + +void FilterbankSpectralSubtraction(const SpectralSubtractionConfig* config, + const uint32_t* input, uint32_t* output, + uint32_t* noise_estimate) { + const bool data_clamping = config->clamping; + const int smoothing_bits = config->smoothing_bits; + const int num_channels = config->num_channels; + + for (int i = 0; i < num_channels; ++i) { + uint32_t smoothing; + uint32_t one_minus_smoothing; + if ((i & 1) == 0) { + smoothing = config->smoothing; + one_minus_smoothing = config->one_minus_smoothing; + } else { // Use alternate smoothing coefficient on odd-index channels. + smoothing = config->alternate_smoothing; + one_minus_smoothing = config->alternate_one_minus_smoothing; + } + + // Scale up signal[i] for smoothing filter computation. + const uint32_t signal_scaled_up = input[i] << smoothing_bits; + noise_estimate[i] = + ((static_cast<uint64_t>(signal_scaled_up) * smoothing) + + (static_cast<uint64_t>(noise_estimate[i]) * one_minus_smoothing)) >> + config->spectral_subtraction_bits; + + uint32_t estimate_scaled_up = noise_estimate[i]; + // Make sure that we can't get a negative value for the signal - estimate. + if (estimate_scaled_up > signal_scaled_up) { + estimate_scaled_up = signal_scaled_up; + if (data_clamping) { + noise_estimate[i] = estimate_scaled_up; + } + } + const uint32_t floor = + (static_cast<uint64_t>(input[i]) * config->min_signal_remaining) >> + config->spectral_subtraction_bits; + const uint32_t subtracted = + (signal_scaled_up - estimate_scaled_up) >> smoothing_bits; + output[i] = subtracted > floor ? subtracted : floor; + } +} + +} // namespace tflm_signal +} // namespace tflite
diff --git a/signal/src/filter_bank_spectral_subtraction.h b/signal/src/filter_bank_spectral_subtraction.h new file mode 100644 index 0000000..e862d77 --- /dev/null +++ b/signal/src/filter_bank_spectral_subtraction.h
@@ -0,0 +1,73 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef SIGNAL_SRC_FILTER_BANK_SPECTRAL_SUBTRACTION_H_ +#define SIGNAL_SRC_FILTER_BANK_SPECTRAL_SUBTRACTION_H_ + +#include <stdint.h> + +namespace tflite { +namespace tflm_signal { +// TODO(b/286250473): remove namespace once de-duped libraries above + +struct SpectralSubtractionConfig { + // Number of filterbank channels in input and output + int32_t num_channels; + // The constant used for the lowpass filter for finding the noise. + // Higher values correspond to more aggressively adapting estimates + // of the noise. + // Scale is 1 << spectral_subtraction_bits + uint32_t smoothing; + // One minus smoothing constant for low pass filter. + // Scale is 1 << spectral_subtraction_bits + uint32_t one_minus_smoothing; + // The maximum cap to subtract away from the signal (ie, if this is + // 0.2, then the result of spectral subtraction will not go below + // 0.2 * signal). + // Scale is 1 << spectral_subtraction_bits + uint32_t min_signal_remaining; + // If positive, specifies the filter coefficient for odd-index + // channels, while 'smoothing' is used as the coefficient for even- + // index channels. Otherwise, the same filter coefficient is + // used on all channels. + // Scale is 1 << spectral_subtraction_bits + uint32_t alternate_smoothing; + // Alternate One minus smoothing constant for low pass filter. + // Scale is 1 << spectral_subtraction_bits + uint32_t alternate_one_minus_smoothing; + // Extra fractional bits for the noise_estimate smoothing filter. + uint32_t smoothing_bits; + // Scaling bits for some members of this struct + uint32_t spectral_subtraction_bits; + // If true, when the filterbank level drops below the output, + // the noise estimate will be forced down to the new noise level. + // If false, the noise estimate will remain above the current + // filterbank output (but the subtraction will still keep the + // output non negative). + bool clamping; +}; + +// Apply spectral subtraction to each element in `input`, then write the result +// to `output` and `noise_estimate`. `input`, `output` and `noise estimate` +// must all be of size `config.num_channels`. `config` holds the +// parameters of the spectral subtraction algorithm. +void FilterbankSpectralSubtraction(const SpectralSubtractionConfig* config, + const uint32_t* input, uint32_t* output, + uint32_t* noise_estimate); + +} // namespace tflm_signal +} // namespace tflite + +#endif // SIGNAL_SRC_FILTER_BANK_SPECTRAL_SUBTRACTION_H_
diff --git a/signal/src/filter_bank_square_root.cc b/signal/src/filter_bank_square_root.cc new file mode 100644 index 0000000..25e8b23 --- /dev/null +++ b/signal/src/filter_bank_square_root.cc
@@ -0,0 +1,31 @@ +/* Copyright 2019 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 "signal/src/filter_bank_square_root.h" + +#include "signal/src/square_root.h" + +namespace tflite { +namespace tflm_signal { + +void FilterbankSqrt(const uint64_t* input, int num_channels, + int scale_down_bits, uint32_t* output) { + for (int i = 0; i < num_channels; ++i) { + output[i] = Sqrt64(input[i]) >> scale_down_bits; + } +} + +} // namespace tflm_signal +} // namespace tflite
diff --git a/signal/src/filter_bank_square_root.h b/signal/src/filter_bank_square_root.h new file mode 100644 index 0000000..7d484b9 --- /dev/null +++ b/signal/src/filter_bank_square_root.h
@@ -0,0 +1,34 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef SIGNAL_SRC_FILTER_BANK_SQUARE_ROOT_H_ +#define SIGNAL_SRC_FILTER_BANK_SQUARE_ROOT_H_ + +#include <stdint.h> + +namespace tflite { +namespace tflm_signal { +// TODO(b/286250473): remove namespace once de-duped libraries above + +// Apply square root to each element in `input`, then shift right by +// `scale_down_bits` before writing the result to `output`, +// `input` and `output` must both be of size `num_channels` +void FilterbankSqrt(const uint64_t* input, int num_channels, + int scale_down_bits, uint32_t* output); + +} // namespace tflm_signal +} // namespace tflite + +#endif // SIGNAL_SRC_FILTER_BANK_SQUARE_ROOT_H_
diff --git a/signal/src/log.cc b/signal/src/log.cc new file mode 100644 index 0000000..86f8e72 --- /dev/null +++ b/signal/src/log.cc
@@ -0,0 +1,84 @@ +/* Copyright 2019 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 "signal/src/log.h" + +#include "signal/src/msb.h" + +namespace tflite { +namespace tflm_signal { +namespace { + +const uint16_t kLogLut[] = { + 0, 224, 442, 654, 861, 1063, 1259, 1450, 1636, 1817, 1992, 2163, + 2329, 2490, 2646, 2797, 2944, 3087, 3224, 3358, 3487, 3611, 3732, 3848, + 3960, 4068, 4172, 4272, 4368, 4460, 4549, 4633, 4714, 4791, 4864, 4934, + 5001, 5063, 5123, 5178, 5231, 5280, 5326, 5368, 5408, 5444, 5477, 5507, + 5533, 5557, 5578, 5595, 5610, 5622, 5631, 5637, 5640, 5641, 5638, 5633, + 5626, 5615, 5602, 5586, 5568, 5547, 5524, 5498, 5470, 5439, 5406, 5370, + 5332, 5291, 5249, 5203, 5156, 5106, 5054, 5000, 4944, 4885, 4825, 4762, + 4697, 4630, 4561, 4490, 4416, 4341, 4264, 4184, 4103, 4020, 3935, 3848, + 3759, 3668, 3575, 3481, 3384, 3286, 3186, 3084, 2981, 2875, 2768, 2659, + 2549, 2437, 2323, 2207, 2090, 1971, 1851, 1729, 1605, 1480, 1353, 1224, + 1094, 963, 830, 695, 559, 421, 282, 142, 0, 0}; + +// Number of segments in the log lookup table. The table will be kLogSegments+1 +// in length (with some padding). +// constexpr int kLogSegments = 128; +constexpr int kLogSegmentsLog2 = 7; + +// Scale used by lookup table. +constexpr int kLogScale = 65536; +constexpr int kLogScaleLog2 = 16; +constexpr int kLogCoeff = 45426; + +uint32_t Log2FractionPart32(uint32_t x, uint32_t log2x) { + // Part 1 + int32_t frac = x - (1LL << log2x); + if (log2x < kLogScaleLog2) { + frac <<= kLogScaleLog2 - log2x; + } else { + frac >>= log2x - kLogScaleLog2; + } + // Part 2 + const uint32_t base_seg = frac >> (kLogScaleLog2 - kLogSegmentsLog2); + const uint32_t seg_unit = (UINT32_C(1) << kLogScaleLog2) >> kLogSegmentsLog2; + + // ASSERT(base_seg < kLogSegments); + const int32_t c0 = kLogLut[base_seg]; + const int32_t c1 = kLogLut[base_seg + 1]; + const int32_t seg_base = seg_unit * base_seg; + const int32_t rel_pos = ((c1 - c0) * (frac - seg_base)) >> kLogScaleLog2; + return frac + c0 + rel_pos; +} + +} // namespace + +// Calculate integer logarithm, 32 Bit version +uint32_t Log32(uint32_t x, uint32_t out_scale) { + // ASSERT(x != 0); + const uint32_t integer = MostSignificantBit32(x) - 1; + const uint32_t fraction = Log2FractionPart32(x, integer); + const uint32_t log2 = (integer << kLogScaleLog2) + fraction; + const uint32_t round = kLogScale / 2; + const uint32_t loge = + ((static_cast<uint64_t>(kLogCoeff)) * log2 + round) >> kLogScaleLog2; + // Finally scale to our output scale + const uint32_t loge_scaled = (out_scale * loge + round) >> kLogScaleLog2; + return loge_scaled; +} + +} // namespace tflm_signal +} // namespace tflite
diff --git a/signal/src/log.h b/signal/src/log.h new file mode 100644 index 0000000..13045f4 --- /dev/null +++ b/signal/src/log.h
@@ -0,0 +1,30 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef SIGNAL_SRC_LOG_H_ +#define SIGNAL_SRC_LOG_H_ + +#include <stdint.h> + +namespace tflite { +namespace tflm_signal { +// TODO(b/286250473): remove namespace once de-duped libraries above + +// Natural logarithm of an integer. The result is multiplied by out_scale +uint32_t Log32(uint32_t x, uint32_t out_scale); + +} // namespace tflm_signal +} // namespace tflite +#endif // SIGNAL_SRC_LOG_H_
diff --git a/signal/src/msb.h b/signal/src/msb.h new file mode 100644 index 0000000..2bdcb47 --- /dev/null +++ b/signal/src/msb.h
@@ -0,0 +1,32 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef SIGNAL_SRC_MSB_H_ +#define SIGNAL_SRC_MSB_H_ + +#include <stdint.h> + +namespace tflite { +namespace tflm_signal { +// TODO(b/286250473): remove namespace once de-duped libraries above + +// Index of the most significant bit +uint32_t MostSignificantBit32(uint32_t x); +uint32_t MostSignificantBit64(uint64_t x); + +} // namespace tflm_signal +} // namespace tflite + +#endif // SIGNAL_SRC_MSB_H_
diff --git a/signal/src/msb_32.cc b/signal/src/msb_32.cc new file mode 100644 index 0000000..6f12dc4 --- /dev/null +++ b/signal/src/msb_32.cc
@@ -0,0 +1,40 @@ +/* Copyright 2019 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 "signal/src/msb.h" + +namespace tflite { +namespace tflm_signal { +// TODO(b/286250473): remove namespace once de-duped libraries above + +// TODO(b/291167350): can allow __builtin_clz to be used in more cases here +uint32_t MostSignificantBit32(uint32_t x) { +#if defined(__GNUC__) + if (x) { + return 32 - __builtin_clz(x); + } + return 32; +#else + uint32_t temp = 0; + while (x) { + x = x >> 1; + ++temp; + } + return temp; +#endif +} + +} // namespace tflm_signal +} // namespace tflite
diff --git a/signal/src/msb_64.cc b/signal/src/msb_64.cc new file mode 100644 index 0000000..233985c --- /dev/null +++ b/signal/src/msb_64.cc
@@ -0,0 +1,39 @@ +/* Copyright 2019 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 "signal/src/msb.h" + +namespace tflite { +namespace tflm_signal { +// TODO(b/286250473): remove namespace once de-duped libraries above + +uint32_t MostSignificantBit64(uint64_t x) { +#if defined(__GNUC__) + if (x) { + return 64 - __builtin_clzll(x); + } + return 64; +#else + uint32_t temp = 0; + while (x) { + x = x >> 1; + ++temp; + } + return temp; +#endif +} + +} // namespace tflm_signal +} // namespace tflite
diff --git a/signal/src/square_root.h b/signal/src/square_root.h new file mode 100644 index 0000000..b32855c --- /dev/null +++ b/signal/src/square_root.h
@@ -0,0 +1,32 @@ +/* Copyright 2019 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. +==============================================================================*/ + +#ifndef SIGNAL_SRC_SQUARE_ROOT_H_ +#define SIGNAL_SRC_SQUARE_ROOT_H_ + +#include <stdint.h> + +namespace tflite { +namespace tflm_signal { +// TODO(b/286250473): remove namespace once de-duped libraries above + +// Square root +uint16_t Sqrt32(uint32_t num); +uint32_t Sqrt64(uint64_t num); + +} // namespace tflm_signal +} // namespace tflite + +#endif // SIGNAL_SRC_SQUARE_ROOT_H_
diff --git a/signal/src/square_root_32.cc b/signal/src/square_root_32.cc new file mode 100644 index 0000000..3ca11ef --- /dev/null +++ b/signal/src/square_root_32.cc
@@ -0,0 +1,46 @@ +/* Copyright 2019 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 "signal/src/msb.h" +#include "signal/src/square_root.h" + +namespace tflite { +namespace tflm_signal { + +uint16_t Sqrt32(uint32_t num) { + if (num == 0) { + return 0; + }; + uint32_t res = 0; + int max_bit_number = 32 - MostSignificantBit32(num); + max_bit_number |= 1; + uint32_t bit = 1u << (31 - max_bit_number); + int iterations = (31 - max_bit_number) / 2 + 1; + while (iterations--) { + if (num >= res + bit) { + num -= res + bit; + res = (res >> 1U) + bit; + } else { + res >>= 1U; + } + bit >>= 2U; + } + // Do rounding - if we have the bits. + if (num > res && res != 0xFFFF) ++res; + return res; +} + +} // namespace tflm_signal +} // namespace tflite
diff --git a/signal/src/square_root_64.cc b/signal/src/square_root_64.cc new file mode 100644 index 0000000..54e8439 --- /dev/null +++ b/signal/src/square_root_64.cc
@@ -0,0 +1,49 @@ +/* Copyright 2019 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 "signal/src/msb.h" +#include "signal/src/square_root.h" + +namespace tflite { +namespace tflm_signal { + +uint32_t Sqrt64(uint64_t num) { + // Take a shortcut and just use 32 bit operations if the upper word is all + // clear. This will cause a slight off by one issue for numbers close to 2^32, + // but it probably isn't going to matter (and gives us a big performance win). + if ((num >> 32) == 0) { + return Sqrt32(static_cast<uint32_t>(num)); + } + uint64_t res = 0; + int max_bit_number = 64 - MostSignificantBit64(num); + max_bit_number |= 1; + uint64_t bit = UINT64_C(1) << (63 - max_bit_number); + int iterations = (63 - max_bit_number) / 2 + 1; + while (iterations--) { + if (num >= res + bit) { + num -= res + bit; + res = (res >> 1U) + bit; + } else { + res >>= 1U; + } + bit >>= 2U; + } + // Do rounding - if we have the bits. + if (num > res && res != 0xFFFFFFFFLL) ++res; + return res; +} + +} // namespace tflm_signal +} // namespace tflite
diff --git a/tensorflow/lite/micro/kernels/Makefile.inc b/tensorflow/lite/micro/kernels/Makefile.inc index e64c0f4..384fcb3 100644 --- a/tensorflow/lite/micro/kernels/Makefile.inc +++ b/tensorflow/lite/micro/kernels/Makefile.inc
@@ -69,6 +69,24 @@ $(TENSORFLOW_ROOT)signal/micro/kernels/framer_flexbuffers_generated_data.cc, \ $(TENSORFLOW_ROOT)signal/micro/kernels/framer_flexbuffers_generated_data.h)) +$(eval $(call microlite_test,kernel_signal_filter_bank_test,\ + $(TENSORFLOW_ROOT)signal/micro/kernels/filter_bank_test.cc \ + $(TENSORFLOW_ROOT)signal/micro/kernels/filter_bank_flexbuffers_generated_data.cc, \ + $(TENSORFLOW_ROOT)signal/micro/kernels/filter_bank_flexbuffers_generated_data.h)) + +$(eval $(call microlite_test,kernel_signal_filter_bank_log_test,\ + $(TENSORFLOW_ROOT)signal/micro/kernels/filter_bank_log_test.cc \ + $(TENSORFLOW_ROOT)signal/micro/kernels/filter_bank_log_flexbuffers_generated_data.cc, \ + $(TENSORFLOW_ROOT)signal/micro/kernels/filter_bank_log_flexbuffers_generated_data.h)) + +$(eval $(call microlite_test,kernel_signal_filter_bank_square_root_test,\ + $(TENSORFLOW_ROOT)signal/micro/kernels/filter_bank_square_root_test.cc)) + +$(eval $(call microlite_test,kernel_signal_filter_bank_spectral_subtraction_test,\ + $(TENSORFLOW_ROOT)signal/micro/kernels/filter_bank_spectral_subtraction_test.cc \ + $(TENSORFLOW_ROOT)signal/micro/kernels/filter_bank_spectral_subtraction_flexbuffers_generated_data.cc, \ + $(TENSORFLOW_ROOT)signal/micro/kernels/filter_bank_spectral_subtraction_flexbuffers_generated_data.h)) + $(eval $(call microlite_test,kernel_signal_overlap_add_test,\ $(TENSORFLOW_ROOT)signal/micro/kernels/overlap_add_test.cc \ $(TENSORFLOW_ROOT)signal/micro/kernels/overlap_add_flexbuffers_generated_data.cc, \
diff --git a/tensorflow/lite/micro/kernels/micro_ops.h b/tensorflow/lite/micro/kernels/micro_ops.h index e14d327..070f4bb 100644 --- a/tensorflow/lite/micro/kernels/micro_ops.h +++ b/tensorflow/lite/micro/kernels/micro_ops.h
@@ -135,6 +135,10 @@ // TODO(b/160234179): Change custom OPs to also return by value. namespace tflm_signal { TFLMRegistration* Register_DELAY(); +TFLMRegistration* Register_FILTER_BANK(); +TFLMRegistration* Register_FILTER_BANK_LOG(); +TFLMRegistration* Register_FILTER_BANK_SPECTRAL_SUBTRACTION(); +TFLMRegistration* Register_FILTER_BANK_SQUARE_ROOT(); TFLMRegistration* Register_ENERGY(); TFLMRegistration* Register_FRAMER(); TFLMRegistration* Register_OVERLAP_ADD();
diff --git a/tensorflow/lite/micro/micro_mutable_op_resolver.h b/tensorflow/lite/micro/micro_mutable_op_resolver.h index 09cd25d..31ee7c5 100644 --- a/tensorflow/lite/micro/micro_mutable_op_resolver.h +++ b/tensorflow/lite/micro/micro_mutable_op_resolver.h
@@ -258,6 +258,31 @@ return AddBuiltin(BuiltinOperator_FILL, tflite::Register_FILL(), ParseFill); } + TfLiteStatus AddFilterBank() { + // TODO(b/286250473): change back name to "FilterBank" and remove namespace + return AddCustom("SignalFilterBank", + tflite::tflm_signal::Register_FILTER_BANK()); + } + TfLiteStatus AddFilterBankLog() { + // TODO(b/286250473): change back name to "FilterBankLog" and remove + // namespace + return AddCustom("SignalFilterBankLog", + tflite::tflm_signal::Register_FILTER_BANK_LOG()); + } + TfLiteStatus AddFilterBankSquareRoot() { + // TODO(b/286250473): change back name to "FilterBankSquareRoot" and remove + // namespace + return AddCustom("SignalFilterBankSquareRoot", + tflite::tflm_signal::Register_FILTER_BANK_SQUARE_ROOT()); + } + TfLiteStatus AddFilterBankSpectralSubtraction() { + // TODO(b/286250473): change back name to "FilterBankSpectralSubtraction" + // and remove namespace + return AddCustom( + "SignalAddFilterBankSpectralSubtraction", + tflite::tflm_signal::Register_FILTER_BANK_SPECTRAL_SUBTRACTION()); + } + TfLiteStatus AddFloor() { return AddBuiltin(BuiltinOperator_FLOOR, Register_FLOOR(), ParseFloor); }
diff --git a/tensorflow/lite/micro/tools/make/Makefile b/tensorflow/lite/micro/tools/make/Makefile index def1874..8264d8d 100644 --- a/tensorflow/lite/micro/tools/make/Makefile +++ b/tensorflow/lite/micro/tools/make/Makefile
@@ -314,6 +314,10 @@ MICROLITE_CC_KERNEL_SRCS := \ $(TENSORFLOW_ROOT)signal/micro/kernels/delay.cc \ $(TENSORFLOW_ROOT)signal/micro/kernels/energy.cc \ +$(TENSORFLOW_ROOT)signal/micro/kernels/filter_bank.cc \ +$(TENSORFLOW_ROOT)signal/micro/kernels/filter_bank_log.cc \ +$(TENSORFLOW_ROOT)signal/micro/kernels/filter_bank_square_root.cc \ +$(TENSORFLOW_ROOT)signal/micro/kernels/filter_bank_spectral_subtraction.cc \ $(TENSORFLOW_ROOT)signal/micro/kernels/framer.cc \ $(TENSORFLOW_ROOT)signal/micro/kernels/rfft.cc \ $(TENSORFLOW_ROOT)signal/micro/kernels/stacker.cc \ @@ -321,10 +325,19 @@ $(TENSORFLOW_ROOT)signal/micro/kernels/window.cc \ $(TENSORFLOW_ROOT)signal/src/circular_buffer.cc \ $(TENSORFLOW_ROOT)signal/src/energy.cc \ +$(TENSORFLOW_ROOT)signal/src/filter_bank.cc \ +$(TENSORFLOW_ROOT)signal/src/filter_bank_log.cc \ +$(TENSORFLOW_ROOT)signal/src/filter_bank_square_root.cc \ +$(TENSORFLOW_ROOT)signal/src/filter_bank_spectral_subtraction.cc \ +$(TENSORFLOW_ROOT)signal/src/log.cc \ +$(TENSORFLOW_ROOT)signal/src/msb_32.cc \ +$(TENSORFLOW_ROOT)signal/src/msb_64.cc \ $(TENSORFLOW_ROOT)signal/src/overlap_add.cc \ $(TENSORFLOW_ROOT)signal/src/rfft_float.cc \ $(TENSORFLOW_ROOT)signal/src/rfft_int16.cc \ $(TENSORFLOW_ROOT)signal/src/rfft_int32.cc \ +$(TENSORFLOW_ROOT)signal/src/square_root_32.cc \ +$(TENSORFLOW_ROOT)signal/src/square_root_64.cc \ $(TENSORFLOW_ROOT)signal/src/window.cc \ $(TENSORFLOW_ROOT)tensorflow/lite/micro/kernels/activations.cc \ $(TENSORFLOW_ROOT)tensorflow/lite/micro/kernels/activations_common.cc \