2 Copyright (C) 2009 Paul Brossier <piem@aubio.org>
4 This file is part of aubio.
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.
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.
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/>.
21 #include "aubio_priv.h"
24 fmat_t * new_fmat (uint_t height, uint_t length) {
27 if ((sint_t)length <= 0 || (sint_t)height <= 0 ) {
30 s = AUBIO_NEW(fmat_t);
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++) {
43 void del_fmat (fmat_t *s) {
45 for (i=0; i<s->height; i++) {
46 AUBIO_FREE(s->data[i]);
52 void fmat_set_sample(fmat_t *s, smpl_t data, uint_t channel, uint_t position) {
53 s->data[channel][position] = data;
56 smpl_t fmat_get_sample(const fmat_t *s, uint_t channel, uint_t position) {
57 return s->data[channel][position];
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;
66 smpl_t * fmat_get_channel_data(const fmat_t *s, uint_t channel) {
67 return s->data[channel];
70 smpl_t ** fmat_get_data(const fmat_t *s) {
74 /* helper functions */
76 void fmat_print(const fmat_t *s) {
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]);
86 void fmat_set(fmat_t *s, smpl_t val) {
88 for (i=0; i< s->height; i++) {
89 for (j=0; j< s->length; j++) {
95 void fmat_zeros(fmat_t *s) {
96 #ifdef HAVE_MEMCPY_HACKS
98 for (i=0; i< s->height; i++) {
99 memset(s->data[i], 0, s->length * sizeof(smpl_t));
101 #else /* HAVE_MEMCPY_HACKS */
103 #endif /* HAVE_MEMCPY_HACKS */
106 void fmat_ones(fmat_t *s) {
110 void fmat_rev(fmat_t *s) {
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]);
119 void fmat_weight(fmat_t *s, const fmat_t *weight) {
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];
129 void fmat_copy(const fmat_t *s, fmat_t *t) {
131 #ifndef HAVE_MEMCPY_HACKS
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);
139 if (s->length != t->length) {
140 AUBIO_ERR("trying to copy %d columns to %d columns\n",
141 s->length, t->length);
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));
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];
154 #endif /* HAVE_MEMCPY_HACKS */
157 void fmat_vecmul(const fmat_t *s, const fvec_t *scale, fvec_t *output) {
160 assert(s->height == output->length);
161 assert(s->length == scale->length);
163 #if !defined(HAVE_ACCELERATE) && !defined(HAVE_BLAS)
166 for (j = 0; j < s->length; j++) {
167 for (k = 0; k < s->height; k++) {
168 output->data[k] += scale->data[j]
172 #elif defined(HAVE_BLAS)
173 for (k = 0; k < s->height; k++) {
174 output->data[k] = aubio_cblas_dot( s->length, scale->data, 1, s->data[k], 1);
176 #elif defined(HAVE_ACCELERATE)
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);
181 for (k = 0; k < s->height; k++) {
182 aubio_vDSP_dotpr( scale->data, 1, s->data[k], 1, &(output->data[k]), s->length);