[4c33f81] | 1 | #include "aubio_priv.h" |
---|
| 2 | #include "fmat.h" |
---|
| 3 | #include "tensor.h" |
---|
| 4 | |
---|
[5010e61] | 5 | aubio_tensor_t *new_aubio_tensor(uint_t ndim, uint_t *shape) |
---|
[4c33f81] | 6 | { |
---|
| 7 | aubio_tensor_t *c = AUBIO_NEW(aubio_tensor_t); |
---|
[f5ea4fb] | 8 | uint_t items_per_row = 1; |
---|
[4c33f81] | 9 | uint_t i; |
---|
| 10 | |
---|
[5010e61] | 11 | if ((sint_t)ndim <= 0) goto failure; |
---|
| 12 | for (i = 0; i < ndim; i++) { |
---|
| 13 | if ((sint_t)shape[i] <= 0) goto failure; |
---|
[4c33f81] | 14 | } |
---|
| 15 | |
---|
[5010e61] | 16 | c->ndim = ndim; |
---|
| 17 | c->shape[0] = shape[0]; |
---|
| 18 | for (i = 1; i < ndim; i++) { |
---|
| 19 | c->shape[i] = shape[i]; |
---|
[9ca7923] | 20 | items_per_row *= shape[i]; |
---|
[4c33f81] | 21 | } |
---|
[9ca7923] | 22 | c->size = items_per_row * shape[0]; |
---|
[f5ea4fb] | 23 | c->buffer = AUBIO_ARRAY(smpl_t, c->size); |
---|
[5010e61] | 24 | c->data = AUBIO_ARRAY(smpl_t*, shape[0]); |
---|
[f5ea4fb] | 25 | for (i = 0; i < c->shape[0]; i++) { |
---|
| 26 | c->data[i] = c->buffer + i * items_per_row; |
---|
[4c33f81] | 27 | } |
---|
| 28 | |
---|
| 29 | return c; |
---|
| 30 | |
---|
| 31 | failure: |
---|
| 32 | del_aubio_tensor(c); |
---|
| 33 | return NULL; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | void del_aubio_tensor(aubio_tensor_t *c) |
---|
| 37 | { |
---|
| 38 | if (c->data) { |
---|
| 39 | if (c->data[0]) { |
---|
| 40 | AUBIO_FREE(c->data[0]); |
---|
| 41 | } |
---|
| 42 | AUBIO_FREE(c->data); |
---|
| 43 | } |
---|
| 44 | AUBIO_FREE(c); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | uint_t aubio_tensor_as_fvec(aubio_tensor_t *c, fvec_t *o) { |
---|
[5010e61] | 48 | if (c->ndim != 1) return AUBIO_FAIL; |
---|
| 49 | if (c->shape[0] <= 0) return AUBIO_FAIL; |
---|
| 50 | o->length = c->shape[0]; |
---|
[4c33f81] | 51 | o->data = c->data[0]; |
---|
| 52 | return AUBIO_OK; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | uint_t aubio_fvec_as_tensor(fvec_t *o, aubio_tensor_t *c) { |
---|
| 56 | if (o == NULL) return AUBIO_FAIL; |
---|
[5010e61] | 57 | c->ndim = 1; |
---|
| 58 | c->shape[0] = o->length; |
---|
[4c33f81] | 59 | c->data = &o->data; |
---|
[849210c] | 60 | c->size = o->length; |
---|
[4c33f81] | 61 | return AUBIO_OK; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | uint_t aubio_tensor_as_fmat(aubio_tensor_t *c, fmat_t *o) { |
---|
[5010e61] | 65 | if (c->ndim != 2) return AUBIO_FAIL; |
---|
| 66 | if (c->shape[0] <= 0) return AUBIO_FAIL; |
---|
| 67 | if (c->shape[1] <= 0) return AUBIO_FAIL; |
---|
| 68 | o->height = c->shape[0]; |
---|
| 69 | o->length = c->shape[1]; |
---|
[4c33f81] | 70 | o->data = c->data; |
---|
| 71 | return AUBIO_OK; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | uint_t aubio_fmat_as_tensor(fmat_t *o, aubio_tensor_t *c) { |
---|
| 75 | if (o == NULL) return AUBIO_FAIL; |
---|
| 76 | if (c == NULL) return AUBIO_FAIL; |
---|
[9d35014] | 77 | c->ndim = 2; |
---|
| 78 | c->shape[0] = o->height; |
---|
| 79 | c->shape[1] = o->length; |
---|
[849210c] | 80 | c->size = o->height * o->length; |
---|
[4c33f81] | 81 | c->data = o->data; |
---|
| 82 | return AUBIO_OK; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | smpl_t aubio_tensor_max(aubio_tensor_t *t) |
---|
| 86 | { |
---|
| 87 | uint_t i; |
---|
| 88 | smpl_t max = -1000000; |
---|
[9ca7923] | 89 | for (i = 0; i < t->size; i++) { |
---|
[4c33f81] | 90 | max = MAX(t->data[0][i], max); |
---|
| 91 | } |
---|
| 92 | return max; |
---|
| 93 | } |
---|
[50d7afe] | 94 | |
---|
| 95 | const char_t *aubio_tensor_get_shape_string(aubio_tensor_t *t) { |
---|
| 96 | uint_t i; |
---|
| 97 | if (!t) return NULL; |
---|
| 98 | size_t offset = 2; |
---|
| 99 | static char_t shape_str[STRN_LENGTH]; |
---|
| 100 | char_t shape_str_previous[STRN_LENGTH] = "("; |
---|
| 101 | for (i = 0; i < t->ndim; i++) { |
---|
| 102 | int len = snprintf(shape_str, STRN_LENGTH, "%s%d%s", |
---|
| 103 | shape_str_previous, t->shape[i], (i == t->ndim - 1) ? "" : ", "); |
---|
| 104 | strncpy(shape_str_previous, shape_str, len); |
---|
| 105 | } |
---|
| 106 | snprintf(shape_str, strnlen(shape_str, STRN_LENGTH - offset - 1) + offset, |
---|
| 107 | "%s)", shape_str_previous); |
---|
| 108 | } |
---|