source: src/fmat.c @ f4c5a95

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

[fmat] ensure contiguous memory

  • Property mode set to 100644
File size: 4.5 KB
Line 
1/*
2  Copyright (C) 2009 Paul Brossier <piem@aubio.org>
3
4  This file is part of aubio.
5
6  aubio is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10
11  aubio is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15
16  You should have received a copy of the GNU General Public License
17  along with aubio.  If not, see <http://www.gnu.org/licenses/>.
18
19*/
20
21#include "aubio_priv.h"
22#include "fmat.h"
23
24fmat_t * new_fmat (uint_t height, uint_t length) {
25  fmat_t * s;
26  uint_t i;
27  if ((sint_t)length <= 0 || (sint_t)height <= 0 ) {
28    return NULL;
29  }
30  s = AUBIO_NEW(fmat_t);
31  s->height = height;
32  s->length = length;
33  s->data = AUBIO_ARRAY(smpl_t*,s->height);
34  s->data[0] = AUBIO_ARRAY(smpl_t, s->length * s->height);
35  for (i=1; i< s->height; i++) {
36    s->data[i] = s->data[0] + i * s->length;
37  }
38  return s;
39}
40
41void del_fmat (fmat_t *s) {
42  AUBIO_ASSERT(s);
43  if (s->data[0])
44    AUBIO_FREE(s->data[0]);
45  if (s->data)
46    AUBIO_FREE(s->data);
47  AUBIO_FREE(s);
48}
49
50void fmat_set_sample(fmat_t *s, smpl_t data, uint_t channel, uint_t position) {
51  s->data[channel][position] = data;
52}
53
54smpl_t fmat_get_sample(const fmat_t *s, uint_t channel, uint_t position) {
55  return s->data[channel][position];
56}
57
58void fmat_get_channel(const fmat_t *s, uint_t channel, fvec_t *output) {
59  output->data = s->data[channel];
60  output->length = s->length;
61  return;
62}
63
64smpl_t * fmat_get_channel_data(const fmat_t *s, uint_t channel) {
65  return s->data[channel];
66}
67
68smpl_t ** fmat_get_data(const fmat_t *s) {
69  return s->data;
70}
71
72/* helper functions */
73
74void fmat_print(const fmat_t *s) {
75  uint_t i,j;
76  for (i=0; i< s->height; i++) {
77    for (j=0; j< s->length; j++) {
78      AUBIO_MSG(AUBIO_SMPL_FMT " ", s->data[i][j]);
79    }
80    AUBIO_MSG("\n");
81  }
82}
83
84void fmat_set(fmat_t *s, smpl_t val) {
85  uint_t i,j;
86  for (i=0; i< s->height; i++) {
87    for (j=0; j< s->length; j++) {
88      s->data[i][j] = val;
89    }
90  }
91}
92
93void fmat_zeros(fmat_t *s) {
94#ifdef HAVE_MEMCPY_HACKS
95  uint_t i;
96  for (i=0; i< s->height; i++) {
97    memset(s->data[i], 0, s->length * sizeof(smpl_t));
98  }
99#else /* HAVE_MEMCPY_HACKS */
100  fmat_set(s, 0.);
101#endif /* HAVE_MEMCPY_HACKS */
102}
103
104void fmat_ones(fmat_t *s) {
105  fmat_set(s, 1.);
106}
107
108void fmat_rev(fmat_t *s) {
109  uint_t i,j;
110  for (i=0; i< s->height; i++) {
111    for (j=0; j< FLOOR((smpl_t)s->length/2); j++) {
112      ELEM_SWAP(s->data[i][j], s->data[i][s->length-1-j]);
113    }
114  }
115}
116
117void fmat_weight(fmat_t *s, const fmat_t *weight) {
118  uint_t i,j;
119  uint_t length = MIN(s->length, weight->length);
120  for (i=0; i< s->height; i++) {
121    for (j=0; j< length; j++) {
122      s->data[i][j] *= weight->data[0][j];
123    }
124  }
125}
126
127void fmat_copy(const fmat_t *s, fmat_t *t) {
128  uint_t i;
129#ifndef HAVE_MEMCPY_HACKS
130  uint_t j;
131#endif /* HAVE_MEMCPY_HACKS */
132  if (s->height != t->height) {
133    AUBIO_ERR("trying to copy %d rows to %d rows \n",
134            s->height, t->height);
135    return;
136  }
137  if (s->length != t->length) {
138    AUBIO_ERR("trying to copy %d columns to %d columns\n",
139            s->length, t->length);
140    return;
141  }
142#ifdef HAVE_MEMCPY_HACKS
143  for (i=0; i< s->height; i++) {
144    memcpy(t->data[i], s->data[i], t->length * sizeof(smpl_t));
145  }
146#else /* HAVE_MEMCPY_HACKS */
147  for (i=0; i< t->height; i++) {
148    for (j=0; j< t->length; j++) {
149      t->data[i][j] = s->data[i][j];
150    }
151  }
152#endif /* HAVE_MEMCPY_HACKS */
153}
154
155void fmat_vecmul(const fmat_t *s, const fvec_t *scale, fvec_t *output) {
156  uint_t k;
157#if 0
158  assert(s->height == output->length);
159  assert(s->length == scale->length);
160#endif
161#if !defined(HAVE_ACCELERATE) && !defined(HAVE_BLAS)
162  uint_t j;
163  fvec_zeros(output);
164  for (j = 0; j < s->length; j++) {
165    for (k = 0; k < s->height; k++) {
166      output->data[k] += scale->data[j]
167          * s->data[k][j];
168    }
169  }
170#elif defined(HAVE_BLAS)
171  for (k = 0; k < s->height; k++) {
172    output->data[k] = aubio_cblas_dot( s->length, scale->data, 1, s->data[k], 1);
173  }
174#elif defined(HAVE_ACCELERATE)
175#if 0
176  // seems slower and less precise (and dangerous?)
177  vDSP_mmul (s->data[0], 1, scale->data, 1, output->data, 1, s->height, 1, s->length);
178#else
179  for (k = 0; k < s->height; k++) {
180    aubio_vDSP_dotpr( scale->data, 1, s->data[k], 1, &(output->data[k]), s->length);
181  }
182#endif
183#endif
184}
Note: See TracBrowser for help on using the repository browser.