1 | #include "aubio_priv.h" |
---|
2 | #include "fmat.h" |
---|
3 | #include "tensor.h" |
---|
4 | |
---|
5 | aubio_tensor_t *new_aubio_tensor(uint_t ndim, uint_t *shape) |
---|
6 | { |
---|
7 | aubio_tensor_t *c = AUBIO_NEW(aubio_tensor_t); |
---|
8 | uint_t items_per_row = 1; |
---|
9 | uint_t i; |
---|
10 | |
---|
11 | if ((sint_t)ndim <= 0) goto failure; |
---|
12 | for (i = 0; i < ndim; i++) { |
---|
13 | if ((sint_t)shape[i] <= 0) goto failure; |
---|
14 | } |
---|
15 | |
---|
16 | c->ndim = ndim; |
---|
17 | c->shape[0] = shape[0]; |
---|
18 | for (i = 1; i < ndim; i++) { |
---|
19 | c->shape[i] = shape[i]; |
---|
20 | items_per_row *= shape[i]; |
---|
21 | } |
---|
22 | c->size = items_per_row * shape[0]; |
---|
23 | c->buffer = AUBIO_ARRAY(smpl_t, c->size); |
---|
24 | c->data = AUBIO_ARRAY(smpl_t*, shape[0]); |
---|
25 | for (i = 0; i < c->shape[0]; i++) { |
---|
26 | c->data[i] = c->buffer + i * items_per_row; |
---|
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) { |
---|
48 | if (!c || !o) return AUBIO_FAIL; |
---|
49 | o->length = c->size; |
---|
50 | o->data = c->data[0]; |
---|
51 | return AUBIO_OK; |
---|
52 | } |
---|
53 | |
---|
54 | uint_t aubio_fvec_as_tensor(fvec_t *o, aubio_tensor_t *c) { |
---|
55 | if (!o || !c) return AUBIO_FAIL; |
---|
56 | c->ndim = 1; |
---|
57 | c->shape[0] = o->length; |
---|
58 | c->data = &o->data; |
---|
59 | c->buffer = o->data; |
---|
60 | c->size = o->length; |
---|
61 | return AUBIO_OK; |
---|
62 | } |
---|
63 | |
---|
64 | uint_t aubio_tensor_as_fmat(aubio_tensor_t *c, fmat_t *o) { |
---|
65 | if (!c || !o) return AUBIO_FAIL; |
---|
66 | o->height = c->shape[0]; |
---|
67 | o->length = c->size / c->shape[0]; |
---|
68 | o->data = c->data; |
---|
69 | return AUBIO_OK; |
---|
70 | } |
---|
71 | |
---|
72 | uint_t aubio_fmat_as_tensor(fmat_t *o, aubio_tensor_t *c) { |
---|
73 | if (!o || !c) return AUBIO_FAIL; |
---|
74 | c->ndim = 2; |
---|
75 | c->shape[0] = o->height; |
---|
76 | c->shape[1] = o->length; |
---|
77 | c->size = o->height * o->length; |
---|
78 | c->data = o->data; |
---|
79 | c->buffer = o->data[0]; |
---|
80 | return AUBIO_OK; |
---|
81 | } |
---|
82 | |
---|
83 | uint_t aubio_tensor_get_subtensor(aubio_tensor_t *t, uint_t i, |
---|
84 | aubio_tensor_t *st) |
---|
85 | { |
---|
86 | uint_t j; |
---|
87 | if (!t || !st) return AUBIO_FAIL; |
---|
88 | if (i >= t->shape[0]) { |
---|
89 | AUBIO_ERR("tensor: index %d out of range, only %d subtensors\n", |
---|
90 | i, t->shape[0]); |
---|
91 | return AUBIO_FAIL; |
---|
92 | } |
---|
93 | if(t->ndim > 1) { |
---|
94 | st->ndim = t->ndim - 1; |
---|
95 | for (j = 0; j < st->ndim; j++) { |
---|
96 | st->shape[j] = t->shape[j + 1]; |
---|
97 | } |
---|
98 | for (j = st->ndim; j < AUBIO_TENSOR_MAXDIM; j++) { |
---|
99 | st->shape[j] = 0; |
---|
100 | } |
---|
101 | st->size = t->size / t->shape[0]; |
---|
102 | } else { |
---|
103 | st->ndim = 1; |
---|
104 | st->shape[0] = 1; |
---|
105 | st->size = 1; |
---|
106 | } |
---|
107 | // st was allocated on the stack, row indices are lost |
---|
108 | st->data = NULL; |
---|
109 | st->buffer = &t->buffer[0] + st->size * i; |
---|
110 | return AUBIO_OK; |
---|
111 | } |
---|
112 | |
---|
113 | smpl_t aubio_tensor_max(aubio_tensor_t *t) |
---|
114 | { |
---|
115 | uint_t i; |
---|
116 | smpl_t max = t->buffer[0]; |
---|
117 | for (i = 0; i < t->size; i++) { |
---|
118 | max = MAX(t->buffer[i], max); |
---|
119 | } |
---|
120 | return max; |
---|
121 | } |
---|
122 | |
---|
123 | const char_t *aubio_tensor_get_shape_string(aubio_tensor_t *t) { |
---|
124 | uint_t i; |
---|
125 | if (!t) return NULL; |
---|
126 | size_t offset = 2; |
---|
127 | static char_t shape_str[STRN_LENGTH]; |
---|
128 | char_t shape_str_previous[STRN_LENGTH] = "("; |
---|
129 | for (i = 0; i < t->ndim; i++) { |
---|
130 | int len = snprintf(shape_str, STRN_LENGTH, "%s%d%s", |
---|
131 | shape_str_previous, t->shape[i], (i == t->ndim - 1) ? "" : ", "); |
---|
132 | strncpy(shape_str_previous, shape_str, len); |
---|
133 | } |
---|
134 | snprintf(shape_str, strnlen(shape_str, STRN_LENGTH - offset - 1) + offset, |
---|
135 | "%s)", shape_str_previous); |
---|
136 | return shape_str; |
---|
137 | } |
---|