| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| #include <math.h> |
| #include <stddef.h> |
| #include <stdint.h> |
| #include <string.h> |
|
|
| #include <algorithm> |
| #include <complex> |
|
|
| #include "edge-impulse-sdk/tensorflow/lite/c/builtin_op_data.h" |
| #include "edge-impulse-sdk/tensorflow/lite/c/common.h" |
| #include "edge-impulse-sdk/tensorflow/lite/kernels/internal/tensor_ctypes.h" |
| #include "edge-impulse-sdk/tensorflow/lite/kernels/kernel_util.h" |
| #include "edge-impulse-sdk/tensorflow/lite/micro/kernels/kernel_util.h" |
| #include "edge-impulse-sdk/tensorflow/lite/micro/micro_utils.h" |
|
|
| namespace tflite { |
| namespace ops { |
| namespace micro { |
| namespace complex_abs { |
|
|
| using std::complex; |
|
|
| constexpr int kInputTensor = 0; |
| constexpr int kOutputTensor = 0; |
|
|
| 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); |
|
|
| if (input->type != kTfLiteComplex64 || output->type != kTfLiteFloat32) { |
| TF_LITE_KERNEL_LOG(context, "Types input %s (%d), output %s (%d) not supported.", |
| TfLiteTypeGetName(input->type), input->type, |
| TfLiteTypeGetName(output->type), output->type); |
| return kTfLiteError; |
| } |
|
|
| micro_context->DeallocateTempTfLiteTensor(input); |
| micro_context->DeallocateTempTfLiteTensor(output); |
|
|
| return kTfLiteOk; |
| } |
|
|
| TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) { |
|
|
| const TfLiteEvalTensor* input = |
| tflite::micro::GetEvalInput(context, node, kInputTensor); |
| TfLiteEvalTensor* output = |
| tflite::micro::GetEvalOutput(context, node, kOutputTensor); |
| TF_LITE_ENSURE(context, input != nullptr); |
| TF_LITE_ENSURE(context, output != nullptr); |
|
|
| size_t total_input_els = 1; |
| for (size_t dim_ix = 0; dim_ix < input->dims->size; dim_ix++) { |
| total_input_els *= input->dims->data[dim_ix]; |
| } |
|
|
| for (size_t ix = 0; ix < total_input_els; ix++) { |
| output->data.f[ix] = sqrt(pow(input->data.c64[ix].re, 2) + pow(input->data.c64[ix].im, 2)); |
| } |
|
|
| return kTfLiteOk; |
| } |
|
|
| } |
| } |
| } |
|
|
| TfLiteRegistration Register_COMPLEX_ABS() { |
| return {nullptr, |
| nullptr, |
| ops::micro::complex_abs::Prepare, |
| ops::micro::complex_abs::Eval, |
| nullptr, |
| 0, |
| nullptr, |
| 0}; |
| } |
|
|
| } |
|
|