[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] | 24 | fmat_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 | |
---|
| 41 | void 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] | 50 | void 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] | 54 | smpl_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] | 58 | void 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] | 64 | smpl_t * fmat_get_channel_data(const fmat_t *s, uint_t channel) { |
---|
[1ece4f8] | 65 | return s->data[channel]; |
---|
| 66 | } |
---|
| 67 | |
---|
[1120f86] | 68 | smpl_t ** fmat_get_data(const fmat_t *s) { |
---|
[c7860af] | 69 | return s->data; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | /* helper functions */ |
---|
| 73 | |
---|
[1120f86] | 74 | void 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 | |
---|
| 84 | void 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 | |
---|
| 93 | void 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 | |
---|
| 104 | void fmat_ones(fmat_t *s) { |
---|
| 105 | fmat_set(s, 1.); |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | void 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] | 117 | void 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] | 127 | void 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] | 155 | void fmat_vecmul(const fmat_t *s, const fvec_t *scale, fvec_t *output) { |
---|
[630191c] | 156 | #if !defined(HAVE_ACCELERATE) && !defined(HAVE_BLAS) |
---|
[70afae1] | 157 | uint_t j, k; |
---|
| 158 | AUBIO_ASSERT(s->height == output->length); |
---|
| 159 | AUBIO_ASSERT(s->length == scale->length); |
---|
[a7348ca5] | 160 | fvec_zeros(output); |
---|
| 161 | for (j = 0; j < s->length; j++) { |
---|
| 162 | for (k = 0; k < s->height; k++) { |
---|
[70afae1] | 163 | output->data[k] += scale->data[j] * s->data[k][j]; |
---|
[a7348ca5] | 164 | } |
---|
| 165 | } |
---|
[b8c6c1e] | 166 | #elif defined(HAVE_BLAS) |
---|
[70afae1] | 167 | #if 0 |
---|
[a7348ca5] | 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 | } |
---|
[70afae1] | 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 |
---|
[a7348ca5] | 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 |
---|
[70afae1] | 183 | uint_t k; |
---|
[a7348ca5] | 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 |
---|
[1f13e29] | 189 | } |
---|
| 190 | |
---|
| 191 | void 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); |
---|
[70afae1] | 223 | } |
---|
| 224 | #endif |
---|
| 225 | #endif |
---|
[a7348ca5] | 226 | } |
---|