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 | |
---|
24 | fmat_t * new_fmat (uint_t height, uint_t length) { |
---|
25 | fmat_t * s; |
---|
26 | uint_t i; |
---|
27 | if ((sint_t)height <= 0 || (sint_t)length <= 0 ) { |
---|
28 | return NULL; |
---|
29 | } |
---|
30 | s = AUBIO_NEW(fmat_t); |
---|
31 | s->height = height; |
---|
32 | s->length = length; |
---|
33 | // array of row pointers |
---|
34 | s->data = AUBIO_ARRAY(smpl_t*,s->height); |
---|
35 | // first row store the full height * length buffer |
---|
36 | s->data[0] = AUBIO_ARRAY(smpl_t, s->height * s->length); |
---|
37 | for (i=1; i< s->height; i++) { |
---|
38 | s->data[i] = s->data[0] + i * s->length; |
---|
39 | } |
---|
40 | return s; |
---|
41 | } |
---|
42 | |
---|
43 | void del_fmat (fmat_t *s) { |
---|
44 | AUBIO_ASSERT(s); |
---|
45 | if (s->data[0]) |
---|
46 | AUBIO_FREE(s->data[0]); |
---|
47 | if (s->data) |
---|
48 | AUBIO_FREE(s->data); |
---|
49 | AUBIO_FREE(s); |
---|
50 | } |
---|
51 | |
---|
52 | void fmat_set_sample(fmat_t *s, smpl_t data, uint_t channel, uint_t position) { |
---|
53 | s->data[channel][position] = data; |
---|
54 | } |
---|
55 | |
---|
56 | smpl_t fmat_get_sample(const fmat_t *s, uint_t channel, uint_t position) { |
---|
57 | return s->data[channel][position]; |
---|
58 | } |
---|
59 | |
---|
60 | void 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 | |
---|
66 | smpl_t * fmat_get_channel_data(const fmat_t *s, uint_t channel) { |
---|
67 | return s->data[channel]; |
---|
68 | } |
---|
69 | |
---|
70 | smpl_t ** fmat_get_data(const fmat_t *s) { |
---|
71 | return s->data; |
---|
72 | } |
---|
73 | |
---|
74 | /* helper functions */ |
---|
75 | |
---|
76 | void 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[0][i * s->length + j]); |
---|
81 | } |
---|
82 | AUBIO_MSG("\n"); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | void 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 | |
---|
95 | void 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 | |
---|
106 | void fmat_ones(fmat_t *s) { |
---|
107 | fmat_set(s, 1.); |
---|
108 | } |
---|
109 | |
---|
110 | void 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 | |
---|
119 | void 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 | |
---|
129 | void 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 | |
---|
157 | void fmat_vecmul(const fmat_t *s, const fvec_t *scale, fvec_t *output) { |
---|
158 | #if !defined(HAVE_ACCELERATE) && !defined(HAVE_BLAS) |
---|
159 | uint_t j, k; |
---|
160 | AUBIO_ASSERT(s->height == output->length); |
---|
161 | AUBIO_ASSERT(s->length == scale->length); |
---|
162 | fvec_zeros(output); |
---|
163 | for (j = 0; j < s->length; j++) { |
---|
164 | for (k = 0; k < s->height; k++) { |
---|
165 | output->data[k] += scale->data[j] * s->data[k][j]; |
---|
166 | } |
---|
167 | } |
---|
168 | #elif defined(HAVE_BLAS) |
---|
169 | #if 0 |
---|
170 | for (k = 0; k < s->height; k++) { |
---|
171 | output->data[k] = aubio_cblas_dot( s->length, scale->data, 1, s->data[k], 1); |
---|
172 | } |
---|
173 | #else |
---|
174 | aubio_cblas__gemv(CblasColMajor, CblasTrans, |
---|
175 | s->length, s->height, 1., |
---|
176 | s->data[0], s->length, |
---|
177 | scale->data, 1, 0., |
---|
178 | output->data, 1); |
---|
179 | #endif |
---|
180 | #elif defined(HAVE_ACCELERATE) |
---|
181 | #if 0 |
---|
182 | // seems slower and less precise (and dangerous?) |
---|
183 | vDSP_mmul (s->data[0], 1, scale->data, 1, output->data, 1, s->height, 1, s->length); |
---|
184 | #else |
---|
185 | uint_t k; |
---|
186 | for (k = 0; k < s->height; k++) { |
---|
187 | aubio_vDSP_dotpr( scale->data, 1, s->data[k], 1, &(output->data[k]), s->length); |
---|
188 | } |
---|
189 | #endif |
---|
190 | #endif |
---|
191 | } |
---|
192 | |
---|
193 | void fvec_matmul(const fvec_t *scale, const fmat_t *s, fvec_t *output) { |
---|
194 | AUBIO_ASSERT(s->height == scale->length); |
---|
195 | AUBIO_ASSERT(s->length == output->length); |
---|
196 | #if !defined(HAVE_ACCELERATE) && !defined(HAVE_BLAS) |
---|
197 | uint_t j, k; |
---|
198 | fvec_zeros(output); |
---|
199 | for (k = 0; k < s->height; k++) { |
---|
200 | for (j = 0; j < s->length; j++) { |
---|
201 | output->data[j] += s->data[k][j] * scale->data[k]; |
---|
202 | } |
---|
203 | } |
---|
204 | #elif defined(HAVE_BLAS) |
---|
205 | #if 0 |
---|
206 | for (k = 0; k < s->length; k++) { |
---|
207 | output->data[k] = aubio_cblas_dot( scale->length, scale->data, 1, |
---|
208 | &s->data[0][0] + k, s->length); |
---|
209 | } |
---|
210 | #else |
---|
211 | aubio_cblas__gemv(CblasColMajor, CblasNoTrans, |
---|
212 | s->length, s->height, 1., |
---|
213 | s->data[0], s->length, |
---|
214 | scale->data, 1, 0., |
---|
215 | output->data, 1); |
---|
216 | #endif |
---|
217 | #elif defined(HAVE_ACCELERATE) |
---|
218 | #if 0 |
---|
219 | // seems slower and less precise (and dangerous?) |
---|
220 | vDSP_mmul (s->data[0], 1, scale->data, 1, output->data, 1, s->height, 1, s->length); |
---|
221 | #else |
---|
222 | uint_t k; |
---|
223 | for (k = 0; k < s->height; k++) { |
---|
224 | aubio_vDSP_dotpr( scale->data, 1, s->data[k], 1, &(output->data[k]), scale->length); |
---|
225 | } |
---|
226 | #endif |
---|
227 | #endif |
---|
228 | } |
---|