source: src/fmat.c @ 965adee

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5
Last change on this file since 965adee was e141b23, checked in by Paul Brossier <piem@piem.org>, 8 years ago

src/{fmat,fvec}.c: avoid integer division

  • 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,j;
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  for (i=0; i< s->height; i++) {
35    s->data[i] = AUBIO_ARRAY(smpl_t, s->length);
36    for (j=0; j< s->length; j++) {
37      s->data[i][j]=0.;
38    }
39  }
40  return s;
41}
42
43void del_fmat (fmat_t *s) {
44  uint_t i;
45  for (i=0; i<s->height; i++) {
46    AUBIO_FREE(s->data[i]);
47  }
48  AUBIO_FREE(s->data);
49  AUBIO_FREE(s);
50}
51
52void fmat_set_sample(fmat_t *s, smpl_t data, uint_t channel, uint_t position) {
53  s->data[channel][position] = data;
54}
55
56smpl_t fmat_get_sample(const fmat_t *s, uint_t channel, uint_t position) {
57  return s->data[channel][position];
58}
59
60void fmat_get_channel(const fmat_t *s, uint_t channel, fvec_t *output) {
61  output->data = s->data[channel];
62  output->length = s->length;
63  return;
64}
65
66smpl_t * fmat_get_channel_data(const fmat_t *s, uint_t channel) {
67  return s->data[channel];
68}
69
70smpl_t ** fmat_get_data(const fmat_t *s) {
71  return s->data;
72}
73
74/* helper functions */
75
76void fmat_print(const fmat_t *s) {
77  uint_t i,j;
78  for (i=0; i< s->height; i++) {
79    for (j=0; j< s->length; j++) {
80      AUBIO_MSG(AUBIO_SMPL_FMT " ", s->data[i][j]);
81    }
82    AUBIO_MSG("\n");
83  }
84}
85
86void fmat_set(fmat_t *s, smpl_t val) {
87  uint_t i,j;
88  for (i=0; i< s->height; i++) {
89    for (j=0; j< s->length; j++) {
90      s->data[i][j] = val;
91    }
92  }
93}
94
95void fmat_zeros(fmat_t *s) {
96#ifdef HAVE_MEMCPY_HACKS
97  uint_t i;
98  for (i=0; i< s->height; i++) {
99    memset(s->data[i], 0, s->length * sizeof(smpl_t));
100  }
101#else /* HAVE_MEMCPY_HACKS */
102  fmat_set(s, 0.);
103#endif /* HAVE_MEMCPY_HACKS */
104}
105
106void fmat_ones(fmat_t *s) {
107  fmat_set(s, 1.);
108}
109
110void fmat_rev(fmat_t *s) {
111  uint_t i,j;
112  for (i=0; i< s->height; i++) {
113    for (j=0; j< FLOOR((smpl_t)s->length/2); j++) {
114      ELEM_SWAP(s->data[i][j], s->data[i][s->length-1-j]);
115    }
116  }
117}
118
119void fmat_weight(fmat_t *s, const fmat_t *weight) {
120  uint_t i,j;
121  uint_t length = MIN(s->length, weight->length);
122  for (i=0; i< s->height; i++) {
123    for (j=0; j< length; j++) {
124      s->data[i][j] *= weight->data[0][j];
125    }
126  }
127}
128
129void fmat_copy(const fmat_t *s, fmat_t *t) {
130  uint_t i;
131#ifndef HAVE_MEMCPY_HACKS
132  uint_t j;
133#endif /* HAVE_MEMCPY_HACKS */
134  if (s->height != t->height) {
135    AUBIO_ERR("trying to copy %d rows to %d rows \n",
136            s->height, t->height);
137    return;
138  }
139  if (s->length != t->length) {
140    AUBIO_ERR("trying to copy %d columns to %d columns\n",
141            s->length, t->length);
142    return;
143  }
144#ifdef HAVE_MEMCPY_HACKS
145  for (i=0; i< s->height; i++) {
146    memcpy(t->data[i], s->data[i], t->length * sizeof(smpl_t));
147  }
148#else /* HAVE_MEMCPY_HACKS */
149  for (i=0; i< t->height; i++) {
150    for (j=0; j< t->length; j++) {
151      t->data[i][j] = s->data[i][j];
152    }
153  }
154#endif /* HAVE_MEMCPY_HACKS */
155}
156
157void fmat_vecmul(const fmat_t *s, const fvec_t *scale, fvec_t *output) {
158  uint_t k;
159#if 0
160  assert(s->height == output->length);
161  assert(s->length == scale->length);
162#endif
163#if !defined(HAVE_ACCELERATE) && !defined(HAVE_ATLAS)
164  uint_t j;
165  fvec_zeros(output);
166  for (j = 0; j < s->length; j++) {
167    for (k = 0; k < s->height; k++) {
168      output->data[k] += scale->data[j]
169          * s->data[k][j];
170    }
171  }
172#elif defined(HAVE_ATLAS)
173  for (k = 0; k < s->height; k++) {
174    output->data[k] = aubio_cblas_dot( s->length, scale->data, 1, s->data[k], 1);
175  }
176#elif defined(HAVE_ACCELERATE)
177#if 0
178  // seems slower and less precise (and dangerous?)
179  vDSP_mmul (s->data[0], 1, scale->data, 1, output->data, 1, s->height, 1, s->length);
180#else
181  for (k = 0; k < s->height; k++) {
182    aubio_vDSP_dotpr( scale->data, 1, s->data[k], 1, &(output->data[k]), s->length);
183  }
184#endif
185#endif
186}
Note: See TracBrowser for help on using the repository browser.