source: src/ai/tensor.c @ b508ba6

feature/cnnfeature/crepe
Last change on this file since b508ba6 was b508ba6, checked in by Paul Brossier <piem@piem.org>, 2 years ago

[tensor] rename attributes to ndim and shape[] to match numpy

  • Property mode set to 100644
File size: 2.1 KB
Line 
1#include "aubio_priv.h"
2#include "fmat.h"
3#include "tensor.h"
4
5aubio_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 i;
9
10  if ((sint_t)ndim <= 0) goto failure;
11  for (i = 0; i < ndim; i++) {
12    if ((sint_t)shape[i] <= 0) goto failure;
13  }
14
15  c->ndim = ndim;
16  c->items_per_row = 1;
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];
21    c->items_per_row *= shape[i];
22  }
23  c->n_items = c->items_per_row * shape[0];
24  c->data = AUBIO_ARRAY(smpl_t*, shape[0]);
25  c->data[0] = AUBIO_ARRAY(smpl_t, c->n_items);
26  for (i = 1; i < c->shape[0]; i++) {
27    c->data[i] = c->data[0] + i * c->items_per_row;
28  }
29
30  return c;
31
32failure:
33  del_aubio_tensor(c);
34  return NULL;
35}
36
37void 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  }
46  //if (c->shape)
47  //  AUBIO_FREE(c->shape);
48  AUBIO_FREE(c);
49}
50
51uint_t aubio_tensor_as_fvec(aubio_tensor_t *c, fvec_t *o) {
52  if (c->ndim  != 1) return AUBIO_FAIL;
53  if (c->shape[0] <= 0) return AUBIO_FAIL;
54  o->length = c->shape[0];
55  o->data = c->data[0];
56  return AUBIO_OK;
57}
58
59uint_t aubio_fvec_as_tensor(fvec_t *o, aubio_tensor_t *c) {
60  if (o == NULL) return AUBIO_FAIL;
61  c->ndim = 1;
62  c->shape[0] = o->length;
63  c->data = &o->data;
64  return AUBIO_OK;
65}
66
67uint_t aubio_tensor_as_fmat(aubio_tensor_t *c, fmat_t *o) {
68  if (c->ndim  != 2) return AUBIO_FAIL;
69  if (c->shape[0] <= 0) return AUBIO_FAIL;
70  if (c->shape[1] <= 0) return AUBIO_FAIL;
71  o->height = c->shape[0];
72  o->length = c->shape[1];
73  o->data = c->data;
74  return AUBIO_OK;
75}
76
77uint_t aubio_fmat_as_tensor(fmat_t *o, aubio_tensor_t *c) {
78  if (o == NULL) return AUBIO_FAIL;
79  if (c == NULL) return AUBIO_FAIL;
80  c->n_dims = 2;
81  c->dims[0] = o->height;
82  c->dims[1] = o->length;
83  c->data = o->data;
84  return AUBIO_OK;
85}
86
87smpl_t aubio_tensor_max(aubio_tensor_t *t)
88{
89  uint_t i;
90  smpl_t max = -1000000;
91  for (i = 0; i < t->n_items; i++) {
92    max = MAX(t->data[0][i], max);
93  }
94  return max;
95}
Note: See TracBrowser for help on using the repository browser.