source: src/fmat.c @ e75a48e

feature/crepe_org
Last change on this file since e75a48e was 147afba, checked in by Paul Brossier <piem@piem.org>, 6 years ago

[fmat] add fvec_matmul

  • Property mode set to 100644
File size: 5.7 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#if !defined(HAVE_ACCELERATE) && !defined(HAVE_BLAS)
157  uint_t j, k;
158  AUBIO_ASSERT(s->height == output->length);
159  AUBIO_ASSERT(s->length == scale->length);
160  fvec_zeros(output);
161  for (j = 0; j < s->length; j++) {
162    for (k = 0; k < s->height; k++) {
163      output->data[k] += scale->data[j] * s->data[k][j];
164    }
165  }
166#elif defined(HAVE_BLAS)
167#if 0
168  for (k = 0; k < s->height; k++) {
169    output->data[k] = aubio_cblas_dot( s->length, scale->data, 1, s->data[k], 1);
170  }
171#else
172  aubio_cblas__gemv(CblasColMajor, CblasTrans,
173      s->length, s->height, 1.,
174      s->data[0], s->length,
175      scale->data, 1, 0.,
176      output->data, 1);
177#endif
178#elif defined(HAVE_ACCELERATE)
179#if 0
180  // seems slower and less precise (and dangerous?)
181  vDSP_mmul (s->data[0], 1, scale->data, 1, output->data, 1, s->height, 1, s->length);
182#else
183  uint_t k;
184  for (k = 0; k < s->height; k++) {
185    aubio_vDSP_dotpr( scale->data, 1, s->data[k], 1, &(output->data[k]), s->length);
186  }
187#endif
188#endif
189}
190
191void fvec_matmul(const fvec_t *scale, const fmat_t *s, fvec_t *output) {
192  AUBIO_ASSERT(s->height == scale->length);
193  AUBIO_ASSERT(s->length == output->length);
194#if !defined(HAVE_ACCELERATE) && !defined(HAVE_BLAS)
195  uint_t j, k;
196  fvec_zeros(output);
197  for (k = 0; k < s->height; k++) {
198    for (j = 0; j < s->length; j++) {
199      output->data[j] += s->data[k][j] * scale->data[k];
200    }
201  }
202#elif defined(HAVE_BLAS)
203#if 0
204  for (k = 0; k < s->length; k++) {
205    output->data[k] = aubio_cblas_dot( scale->length, scale->data, 1,
206        &s->data[0][0] + k, s->length);
207  }
208#else
209  aubio_cblas__gemv(CblasColMajor, CblasNoTrans,
210      s->length, s->height, 1.,
211      s->data[0], s->length,
212      scale->data, 1, 0.,
213      output->data, 1);
214#endif
215#elif defined(HAVE_ACCELERATE)
216#if 0
217  // seems slower and less precise (and dangerous?)
218  vDSP_mmul (s->data[0], 1, scale->data, 1, output->data, 1, s->height, 1, s->length);
219#else
220  uint_t k;
221  for (k = 0; k < s->height; k++) {
222    aubio_vDSP_dotpr( scale->data, 1, s->data[k], 1, &(output->data[k]), scale->length);
223  }
224#endif
225#endif
226}
Note: See TracBrowser for help on using the repository browser.