source: src/fmat.c @ 567727d

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

[fmat] ensure contiguous memory

  • Property mode set to 100644
File size: 4.5 KB
RevLine 
[c7860af]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
[4ed0ed1]24fmat_t * new_fmat (uint_t height, uint_t length) {
[c21acb9]25  fmat_t * s;
[e11c14d4]26  uint_t i;
[767990e]27  if ((sint_t)length <= 0 || (sint_t)height <= 0 ) {
28    return NULL;
29  }
[c21acb9]30  s = AUBIO_NEW(fmat_t);
[c7860af]31  s->height = height;
32  s->length = length;
33  s->data = AUBIO_ARRAY(smpl_t*,s->height);
[e11c14d4]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;
[c7860af]37  }
38  return s;
39}
40
41void del_fmat (fmat_t *s) {
[e11c14d4]42  AUBIO_ASSERT(s);
43  if (s->data[0])
44    AUBIO_FREE(s->data[0]);
45  if (s->data)
46    AUBIO_FREE(s->data);
[c7860af]47  AUBIO_FREE(s);
48}
49
[1ece4f8]50void fmat_set_sample(fmat_t *s, smpl_t data, uint_t channel, uint_t position) {
[c7860af]51  s->data[channel][position] = data;
52}
[1ece4f8]53
[1120f86]54smpl_t fmat_get_sample(const fmat_t *s, uint_t channel, uint_t position) {
[c7860af]55  return s->data[channel][position];
56}
[1ece4f8]57
[1120f86]58void fmat_get_channel(const fmat_t *s, uint_t channel, fvec_t *output) {
[3cd9fdd]59  output->data = s->data[channel];
60  output->length = s->length;
61  return;
[c7860af]62}
63
[1120f86]64smpl_t * fmat_get_channel_data(const fmat_t *s, uint_t channel) {
[1ece4f8]65  return s->data[channel];
66}
67
[1120f86]68smpl_t ** fmat_get_data(const fmat_t *s) {
[c7860af]69  return s->data;
70}
71
72/* helper functions */
73
[1120f86]74void fmat_print(const fmat_t *s) {
[c7860af]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) {
[7585822]94#ifdef HAVE_MEMCPY_HACKS
[c666a18]95  uint_t i;
96  for (i=0; i< s->height; i++) {
97    memset(s->data[i], 0, s->length * sizeof(smpl_t));
98  }
[7585822]99#else /* HAVE_MEMCPY_HACKS */
[c7860af]100  fmat_set(s, 0.);
[7585822]101#endif /* HAVE_MEMCPY_HACKS */
[c7860af]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++) {
[e141b23]111    for (j=0; j< FLOOR((smpl_t)s->length/2); j++) {
[c7860af]112      ELEM_SWAP(s->data[i][j], s->data[i][s->length-1-j]);
113    }
114  }
115}
116
[1120f86]117void fmat_weight(fmat_t *s, const fmat_t *weight) {
[c7860af]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
[1120f86]127void fmat_copy(const fmat_t *s, fmat_t *t) {
[c21acb9]128  uint_t i;
[7585822]129#ifndef HAVE_MEMCPY_HACKS
[bc6c1a7]130  uint_t j;
[7585822]131#endif /* HAVE_MEMCPY_HACKS */
[c7860af]132  if (s->height != t->height) {
[923a7a8]133    AUBIO_ERR("trying to copy %d rows to %d rows \n",
[c7860af]134            s->height, t->height);
[923a7a8]135    return;
[c7860af]136  }
137  if (s->length != t->length) {
[923a7a8]138    AUBIO_ERR("trying to copy %d columns to %d columns\n",
[c7860af]139            s->length, t->length);
[923a7a8]140    return;
[c7860af]141  }
[7585822]142#ifdef HAVE_MEMCPY_HACKS
[83d2948]143  for (i=0; i< s->height; i++) {
144    memcpy(t->data[i], s->data[i], t->length * sizeof(smpl_t));
145  }
[7585822]146#else /* HAVE_MEMCPY_HACKS */
[923a7a8]147  for (i=0; i< t->height; i++) {
148    for (j=0; j< t->length; j++) {
[c7860af]149      t->data[i][j] = s->data[i][j];
150    }
151  }
[7585822]152#endif /* HAVE_MEMCPY_HACKS */
[c7860af]153}
154
[1120f86]155void fmat_vecmul(const fmat_t *s, const fvec_t *scale, fvec_t *output) {
[a7348ca5]156  uint_t k;
[240517e]157#if 0
[a7348ca5]158  assert(s->height == output->length);
159  assert(s->length == scale->length);
[240517e]160#endif
[630191c]161#if !defined(HAVE_ACCELERATE) && !defined(HAVE_BLAS)
[a7348ca5]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  }
[b8c6c1e]170#elif defined(HAVE_BLAS)
[a7348ca5]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.