[1fe822d] | 1 | #include "aubio_priv.h" |
---|
| 2 | #include "fmat.h" |
---|
| 3 | #include "tensor.h" |
---|
| 4 | |
---|
[b508ba6] | 5 | aubio_tensor_t *new_aubio_tensor(uint_t ndim, uint_t *shape) |
---|
[1fe822d] | 6 | { |
---|
| 7 | aubio_tensor_t *c = AUBIO_NEW(aubio_tensor_t); |
---|
| 8 | uint_t i; |
---|
| 9 | |
---|
[b508ba6] | 10 | if ((sint_t)ndim <= 0) goto failure; |
---|
| 11 | for (i = 0; i < ndim; i++) { |
---|
| 12 | if ((sint_t)shape[i] <= 0) goto failure; |
---|
[1fe822d] | 13 | } |
---|
| 14 | |
---|
[b508ba6] | 15 | c->ndim = ndim; |
---|
[83ec87f] | 16 | uint_t items_per_row = 1; |
---|
[b508ba6] | 17 | //c->shape = AUBIO_ARRAY(uint_t, ndim); |
---|
| 18 | c->shape[0] = shape[0]; |
---|
| 19 | for (i = 1; i < ndim; i++) { |
---|
| 20 | c->shape[i] = shape[i]; |
---|
[83ec87f] | 21 | items_per_row *= shape[i]; |
---|
[1fe822d] | 22 | } |
---|
[83ec87f] | 23 | c->size = items_per_row * shape[0]; |
---|
[b508ba6] | 24 | c->data = AUBIO_ARRAY(smpl_t*, shape[0]); |
---|
[83ec87f] | 25 | c->data[0] = AUBIO_ARRAY(smpl_t, c->size); |
---|
[b508ba6] | 26 | for (i = 1; i < c->shape[0]; i++) { |
---|
[83ec87f] | 27 | c->data[i] = c->data[0] + i * items_per_row; |
---|
[1fe822d] | 28 | } |
---|
| 29 | |
---|
| 30 | return c; |
---|
| 31 | |
---|
| 32 | failure: |
---|
| 33 | del_aubio_tensor(c); |
---|
| 34 | return NULL; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | void del_aubio_tensor(aubio_tensor_t *c) |
---|
| 38 | { |
---|
| 39 | AUBIO_ASSERT(c); |
---|
| 40 | if (c->data) { |
---|
| 41 | if (c->data[0]) { |
---|
| 42 | AUBIO_FREE(c->data[0]); |
---|
| 43 | } |
---|
| 44 | AUBIO_FREE(c->data); |
---|
| 45 | } |
---|
[b508ba6] | 46 | //if (c->shape) |
---|
| 47 | // AUBIO_FREE(c->shape); |
---|
[1fe822d] | 48 | AUBIO_FREE(c); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | uint_t aubio_tensor_as_fvec(aubio_tensor_t *c, fvec_t *o) { |
---|
[b508ba6] | 52 | if (c->ndim != 1) return AUBIO_FAIL; |
---|
| 53 | if (c->shape[0] <= 0) return AUBIO_FAIL; |
---|
| 54 | o->length = c->shape[0]; |
---|
[1fe822d] | 55 | o->data = c->data[0]; |
---|
| 56 | return AUBIO_OK; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | uint_t aubio_fvec_as_tensor(fvec_t *o, aubio_tensor_t *c) { |
---|
| 60 | if (o == NULL) return AUBIO_FAIL; |
---|
[b508ba6] | 61 | c->ndim = 1; |
---|
| 62 | c->shape[0] = o->length; |
---|
[1fe822d] | 63 | c->data = &o->data; |
---|
[a5199ad] | 64 | c->size = o->length; |
---|
[1fe822d] | 65 | return AUBIO_OK; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | uint_t aubio_tensor_as_fmat(aubio_tensor_t *c, fmat_t *o) { |
---|
[b508ba6] | 69 | if (c->ndim != 2) return AUBIO_FAIL; |
---|
| 70 | if (c->shape[0] <= 0) return AUBIO_FAIL; |
---|
| 71 | if (c->shape[1] <= 0) return AUBIO_FAIL; |
---|
| 72 | o->height = c->shape[0]; |
---|
| 73 | o->length = c->shape[1]; |
---|
[1fe822d] | 74 | o->data = c->data; |
---|
| 75 | return AUBIO_OK; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | uint_t aubio_fmat_as_tensor(fmat_t *o, aubio_tensor_t *c) { |
---|
| 79 | if (o == NULL) return AUBIO_FAIL; |
---|
| 80 | if (c == NULL) return AUBIO_FAIL; |
---|
[6ad67a5] | 81 | c->ndim = 2; |
---|
| 82 | c->shape[0] = o->height; |
---|
| 83 | c->shape[1] = o->length; |
---|
[a5199ad] | 84 | c->size = o->height * o->length; |
---|
[1fe822d] | 85 | c->data = o->data; |
---|
| 86 | return AUBIO_OK; |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | smpl_t aubio_tensor_max(aubio_tensor_t *t) |
---|
| 90 | { |
---|
| 91 | uint_t i; |
---|
| 92 | smpl_t max = -1000000; |
---|
[83ec87f] | 93 | for (i = 0; i < t->size; i++) { |
---|
[1fe822d] | 94 | max = MAX(t->data[0][i], max); |
---|
| 95 | } |
---|
| 96 | return max; |
---|
| 97 | } |
---|