| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #ifndef _EIDSP_NUMPY_TYPES_H_ |
| #define _EIDSP_NUMPY_TYPES_H_ |
|
|
| #include <stdint.h> |
| #include <stdbool.h> |
| #include <string.h> |
| #include <stddef.h> |
| #ifdef __cplusplus |
| #include <functional> |
| #include "edge-impulse-sdk/dsp/ei_vector.h" |
| #ifdef __MBED__ |
| #include "mbed.h" |
| #endif |
| #endif |
| #include "config.hpp" |
| #include "edge-impulse-sdk/dsp/returntypes.h" |
|
|
| #if EIDSP_TRACK_ALLOCATIONS |
| #include "memory.hpp" |
| #endif |
|
|
| #ifdef __cplusplus |
| namespace ei { |
| #endif |
|
|
| typedef struct { |
| float r; |
| float i; |
| } fft_complex_t; |
|
|
| typedef struct { |
| int32_t r; |
| int32_t i; |
| } fft_complex_i32_t; |
| |
| |
| |
| |
| typedef struct ei_matrix { |
| float *buffer; |
| uint32_t rows; |
| uint32_t cols; |
| bool buffer_managed_by_me; |
|
|
| #if EIDSP_TRACK_ALLOCATIONS |
| const char *_fn; |
| const char *_file; |
| int _line; |
| uint32_t _originally_allocated_rows; |
| uint32_t _originally_allocated_cols; |
| #endif |
|
|
| #ifdef __cplusplus |
| |
| |
| |
| |
| |
| |
| ei_matrix( |
| uint32_t n_rows, |
| uint32_t n_cols, |
| float *a_buffer = NULL |
| #if EIDSP_TRACK_ALLOCATIONS |
| , |
| const char *fn = NULL, |
| const char *file = NULL, |
| int line = 0 |
| #endif |
| ) |
| { |
| if (a_buffer) { |
| buffer = a_buffer; |
| buffer_managed_by_me = false; |
| } |
| else { |
| buffer = (float*)ei_calloc(n_rows * n_cols * sizeof(float), 1); |
| buffer_managed_by_me = true; |
| } |
| rows = n_rows; |
| cols = n_cols; |
|
|
| if (!a_buffer) { |
| #if EIDSP_TRACK_ALLOCATIONS |
| _fn = fn; |
| _file = file; |
| _line = line; |
| _originally_allocated_rows = rows; |
| _originally_allocated_cols = cols; |
| if (_fn) { |
| ei_dsp_register_matrix_alloc_internal(fn, file, line, rows, cols, sizeof(float), buffer); |
| } |
| else { |
| ei_dsp_register_matrix_alloc(rows, cols, sizeof(float), buffer); |
| } |
| #endif |
| } |
| } |
|
|
| ~ei_matrix() { |
| if (buffer && buffer_managed_by_me) { |
| ei_free(buffer); |
|
|
| #if EIDSP_TRACK_ALLOCATIONS |
| if (_fn) { |
| ei_dsp_register_matrix_free_internal(_fn, _file, _line, _originally_allocated_rows, |
| _originally_allocated_cols, sizeof(float), buffer); |
| } |
| else { |
| ei_dsp_register_matrix_free(_originally_allocated_rows, _originally_allocated_cols, |
| sizeof(float), buffer); |
| } |
| #endif |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| float *get_row_ptr(size_t row) |
| { |
| return buffer + row * cols; |
| } |
|
|
| void* operator new(size_t size) { |
| return ei_malloc(size); |
| } |
|
|
| void operator delete(void* ptr) { |
| ei_free(ptr); |
| } |
|
|
| void* operator new[](size_t size) { |
| return ei_malloc(size); |
| } |
|
|
| void operator delete[](void* ptr) { |
| ei_free(ptr); |
| } |
|
|
| ei_matrix(ei_vector<float> &in) : ei_matrix(1, in.size(), in.data()) { |
| } |
| #endif |
| } matrix_t; |
|
|
|
|
| |
| |
| |
| |
| typedef struct ei_matrix_i8 { |
| int8_t *buffer; |
| uint32_t rows; |
| uint32_t cols; |
| bool buffer_managed_by_me; |
|
|
| #if EIDSP_TRACK_ALLOCATIONS |
| const char *_fn; |
| const char *_file; |
| int _line; |
| uint32_t _originally_allocated_rows; |
| uint32_t _originally_allocated_cols; |
| #endif |
|
|
| #ifdef __cplusplus |
| |
| |
| |
| |
| |
| |
| ei_matrix_i8( |
| uint32_t n_rows, |
| uint32_t n_cols, |
| int8_t *a_buffer = NULL |
| #if EIDSP_TRACK_ALLOCATIONS |
| , |
| const char *fn = NULL, |
| const char *file = NULL, |
| int line = 0 |
| #endif |
| ) |
| { |
| if (a_buffer) { |
| buffer = a_buffer; |
| buffer_managed_by_me = false; |
| } |
| else { |
| buffer = (int8_t*)ei_calloc(n_rows * n_cols * sizeof(int8_t), 1); |
| buffer_managed_by_me = true; |
| } |
| rows = n_rows; |
| cols = n_cols; |
|
|
| if (!a_buffer) { |
| #if EIDSP_TRACK_ALLOCATIONS |
| _fn = fn; |
| _file = file; |
| _line = line; |
| _originally_allocated_rows = rows; |
| _originally_allocated_cols = cols; |
| if (_fn) { |
| ei_dsp_register_matrix_alloc_internal(fn, file, line, rows, cols, sizeof(int8_t), buffer); |
| } |
| else { |
| ei_dsp_register_matrix_alloc(rows, cols, sizeof(int8_t), buffer); |
| } |
| #endif |
| } |
| } |
|
|
| ~ei_matrix_i8() { |
| if (buffer && buffer_managed_by_me) { |
| ei_free(buffer); |
|
|
| #if EIDSP_TRACK_ALLOCATIONS |
| if (_fn) { |
| ei_dsp_register_matrix_free_internal(_fn, _file, _line, _originally_allocated_rows, |
| _originally_allocated_cols, sizeof(int8_t), buffer); |
| } |
| else { |
| ei_dsp_register_matrix_free(_originally_allocated_rows, _originally_allocated_cols, |
| sizeof(int8_t), buffer); |
| } |
| #endif |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| int8_t *get_row_ptr(size_t row) |
| { |
| return buffer + row * cols; |
| } |
|
|
| void* operator new(size_t size) { |
| return ei_malloc(size); |
| } |
|
|
| void operator delete(void* ptr) { |
| ei_free(ptr); |
| } |
|
|
| void* operator new[](size_t size) { |
| return ei_malloc(size); |
| } |
|
|
| void operator delete[](void* ptr) { |
| ei_free(ptr); |
| } |
| #endif |
| } matrix_i8_t; |
|
|
| |
| |
| |
| |
| typedef struct ei_matrix_i32 { |
| int32_t *buffer; |
| uint32_t rows; |
| uint32_t cols; |
| bool buffer_managed_by_me; |
|
|
| #if EIDSP_TRACK_ALLOCATIONS |
| const char *_fn; |
| const char *_file; |
| int _line; |
| uint32_t _originally_allocated_rows; |
| uint32_t _originally_allocated_cols; |
| #endif |
|
|
| #ifdef __cplusplus |
| |
| |
| |
| |
| |
| |
| ei_matrix_i32( |
| uint32_t n_rows, |
| uint32_t n_cols, |
| int32_t *a_buffer = NULL |
| #if EIDSP_TRACK_ALLOCATIONS |
| , |
| const char *fn = NULL, |
| const char *file = NULL, |
| int line = 0 |
| #endif |
| ) |
| { |
| if (a_buffer) { |
| buffer = a_buffer; |
| buffer_managed_by_me = false; |
| } |
| else { |
| buffer = (int32_t*)ei_calloc(n_rows * n_cols * sizeof(int32_t), 1); |
| buffer_managed_by_me = true; |
| } |
| rows = n_rows; |
| cols = n_cols; |
|
|
| if (!a_buffer) { |
| #if EIDSP_TRACK_ALLOCATIONS |
| _fn = fn; |
| _file = file; |
| _line = line; |
| _originally_allocated_rows = rows; |
| _originally_allocated_cols = cols; |
| if (_fn) { |
| ei_dsp_register_matrix_alloc_internal(fn, file, line, rows, cols, sizeof(int32_t), buffer); |
| } |
| else { |
| ei_dsp_register_matrix_alloc(rows, cols, sizeof(int32_t), buffer); |
| } |
| #endif |
| } |
| } |
|
|
| ~ei_matrix_i32() { |
| if (buffer && buffer_managed_by_me) { |
| ei_free(buffer); |
|
|
| #if EIDSP_TRACK_ALLOCATIONS |
| if (_fn) { |
| ei_dsp_register_matrix_free_internal(_fn, _file, _line, _originally_allocated_rows, |
| _originally_allocated_cols, sizeof(int32_t), buffer); |
| } |
| else { |
| ei_dsp_register_matrix_free(_originally_allocated_rows, _originally_allocated_cols, |
| sizeof(int32_t), buffer); |
| } |
| #endif |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| int32_t *get_row_ptr(size_t row) |
| { |
| return buffer + row * cols; |
| } |
|
|
| void* operator new(size_t size) { |
| return ei_malloc(size); |
| } |
|
|
| void operator delete(void* ptr) { |
| ei_free(ptr); |
| } |
|
|
| void* operator new[](size_t size) { |
| return ei_malloc(size); |
| } |
|
|
| void operator delete[](void* ptr) { |
| ei_free(ptr); |
| } |
| #endif |
| } matrix_i32_t; |
|
|
| |
| |
| |
| |
| |
| typedef struct ei_quantized_matrix { |
| uint8_t *buffer; |
| uint32_t rows; |
| uint32_t cols; |
| bool buffer_managed_by_me; |
| #ifdef __MBED__ |
| mbed::Callback<float(uint8_t)> dequantization_fn; |
| #else |
| float (*dequantization_fn)(uint8_t); |
| #endif |
|
|
| #if EIDSP_TRACK_ALLOCATIONS |
| const char *_fn; |
| const char *_file; |
| int _line; |
| uint32_t _originally_allocated_rows; |
| uint32_t _originally_allocated_cols; |
| #endif |
|
|
| #ifdef __cplusplus |
| |
| |
| |
| |
| |
| |
| |
| ei_quantized_matrix(uint32_t n_rows, |
| uint32_t n_cols, |
| #ifdef __MBED__ |
| mbed::Callback<float(uint8_t)> a_dequantization_fn, |
| #else |
| float (*a_dequantization_fn)(uint8_t), |
| #endif |
| uint8_t *a_buffer = NULL |
| #if EIDSP_TRACK_ALLOCATIONS |
| , |
| const char *fn = NULL, |
| const char *file = NULL, |
| int line = 0 |
| #endif |
| ) |
| { |
| if (a_buffer) { |
| buffer = a_buffer; |
| buffer_managed_by_me = false; |
| } |
| else { |
| buffer = (uint8_t*)ei_calloc(n_rows * n_cols * sizeof(uint8_t), 1); |
| buffer_managed_by_me = true; |
| } |
| rows = n_rows; |
| cols = n_cols; |
| dequantization_fn = a_dequantization_fn; |
| if (!a_buffer) { |
| #if EIDSP_TRACK_ALLOCATIONS |
| _fn = fn; |
| _file = file; |
| _line = line; |
| _originally_allocated_rows = rows; |
| _originally_allocated_cols = cols; |
| if (_fn) { |
| ei_dsp_register_matrix_alloc_internal(fn, file, line, rows, cols, sizeof(uint8_t), buffer); |
| } |
| else { |
| ei_dsp_register_matrix_alloc(rows, cols, sizeof(uint8_t), buffer); |
| } |
| #endif |
| } |
| } |
|
|
| ~ei_quantized_matrix() { |
| if (buffer && buffer_managed_by_me) { |
| ei_free(buffer); |
|
|
| #if EIDSP_TRACK_ALLOCATIONS |
| if (_fn) { |
| ei_dsp_register_matrix_free_internal(_fn, _file, _line, _originally_allocated_rows, |
| _originally_allocated_cols, sizeof(uint8_t), buffer); |
| } |
| else { |
| ei_dsp_register_matrix_free(_originally_allocated_rows, _originally_allocated_cols, |
| sizeof(uint8_t), buffer); |
| } |
| #endif |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| uint8_t *get_row_ptr(size_t row) |
| { |
| return buffer + row * cols; |
| } |
|
|
| void* operator new(size_t size) { |
| return ei_malloc(size); |
| } |
|
|
| void operator delete(void* ptr) { |
| ei_free(ptr); |
| } |
|
|
| void* operator new[](size_t size) { |
| return ei_malloc(size); |
| } |
|
|
| void operator delete[](void* ptr) { |
| ei_free(ptr); |
| } |
| #endif |
| } quantized_matrix_t; |
|
|
| |
| |
| |
| |
| typedef struct ei_matrix_u8 { |
| uint8_t *buffer; |
| uint32_t rows; |
| uint32_t cols; |
| bool buffer_managed_by_me; |
|
|
| #if EIDSP_TRACK_ALLOCATIONS |
| const char *_fn; |
| const char *_file; |
| int _line; |
| uint32_t _originally_allocated_rows; |
| uint32_t _originally_allocated_cols; |
| #endif |
|
|
| #ifdef __cplusplus |
| |
| |
| |
| |
| |
| |
| ei_matrix_u8( |
| uint32_t n_rows, |
| uint32_t n_cols, |
| uint8_t *a_buffer = NULL |
| #if EIDSP_TRACK_ALLOCATIONS |
| , |
| const char *fn = NULL, |
| const char *file = NULL, |
| int line = 0 |
| #endif |
| ) |
| { |
| if (a_buffer) { |
| buffer = a_buffer; |
| buffer_managed_by_me = false; |
| } |
| else { |
| buffer = (uint8_t*)ei_calloc(n_rows * n_cols * sizeof(uint8_t), 1); |
| buffer_managed_by_me = true; |
| } |
| rows = n_rows; |
| cols = n_cols; |
|
|
| if (!a_buffer) { |
| #if EIDSP_TRACK_ALLOCATIONS |
| _fn = fn; |
| _file = file; |
| _line = line; |
| _originally_allocated_rows = rows; |
| _originally_allocated_cols = cols; |
| if (_fn) { |
| ei_dsp_register_matrix_alloc_internal(fn, file, line, rows, cols, sizeof(uint8_t), buffer); |
| } |
| else { |
| ei_dsp_register_matrix_alloc(rows, cols, sizeof(uint8_t), buffer); |
| } |
| #endif |
| } |
| } |
|
|
| ~ei_matrix_u8() { |
| if (buffer && buffer_managed_by_me) { |
| ei_free(buffer); |
|
|
| #if EIDSP_TRACK_ALLOCATIONS |
| if (_fn) { |
| ei_dsp_register_matrix_free_internal(_fn, _file, _line, _originally_allocated_rows, |
| _originally_allocated_cols, sizeof(uint8_t), buffer); |
| } |
| else { |
| ei_dsp_register_matrix_free(_originally_allocated_rows, _originally_allocated_cols, |
| sizeof(uint8_t), buffer); |
| } |
| #endif |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| uint8_t *get_row_ptr(size_t row) |
| { |
| return buffer + row * cols; |
| } |
|
|
| void* operator new(size_t size) { |
| return ei_malloc(size); |
| } |
|
|
| void operator delete(void* ptr) { |
| ei_free(ptr); |
| } |
|
|
| void* operator new[](size_t size) { |
| return ei_malloc(size); |
| } |
|
|
| void operator delete[](void* ptr) { |
| ei_free(ptr); |
| } |
| #endif |
| } matrix_u8_t; |
|
|
| |
| |
| |
| typedef struct { |
| uint32_t rows; |
| uint32_t cols; |
| } matrix_size_t; |
|
|
| typedef enum { |
| DCT_NORMALIZATION_NONE, |
| DCT_NORMALIZATION_ORTHO |
| } DCT_NORMALIZATION_MODE; |
|
|
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| typedef struct ei_signal_t { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #if EIDSP_SIGNAL_C_FN_POINTER == 1 |
| int __attribute__((aligned(32))) (*get_data)(size_t, size_t, float *); |
| #else |
| std::function<int(size_t offset, size_t length, float *out_ptr)> get_data; |
| #endif |
|
|
| |
| |
| |
| |
| |
| size_t total_length; |
| } signal_t; |
|
|
| |
|
|
| #ifdef __cplusplus |
| } |
| #endif |
|
|
| #ifdef __cplusplus |
| typedef struct ei_feature_t { |
| union { |
| ei::matrix_t* matrix; |
| ei::matrix_i8_t* matrix_i8; |
| ei::matrix_u8_t* matrix_u8; |
| }; |
| uint32_t blockId; |
|
|
| void* operator new(size_t size) { |
| return ei_malloc(size); |
| } |
|
|
| void operator delete(void* ptr) { |
| ei_free(ptr); |
| } |
|
|
| void* operator new[](size_t size) { |
| return ei_malloc(size); |
| } |
|
|
| void operator delete[](void* ptr) { |
| ei_free(ptr); |
| } |
| } feature_t; |
| #endif |
|
|
| |
| #if defined(__cplusplus) && defined(ARDUINO_NRF52_ADAFRUIT) |
| namespace std { |
| __attribute__((weak)) void __throw_bad_function_call() { while(1); }; |
| __attribute__((weak)) void __throw_length_error(char const*) { while(1); }; |
| } |
| #endif |
|
|
| #endif |
|
|