3961d7468e75a0ec833e16828fb7b08342398308
[aubio.git] / src / fvec.c
1 /*
2   Copyright (C) 2003-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 "fvec.h"
23
24 fvec_t * new_fvec(uint_t length) {
25   fvec_t * s;
26   if ((sint_t)length <= 0) {
27     return NULL;
28   }
29   s = AUBIO_NEW(fvec_t);
30   s->length = length;
31   s->data = AUBIO_ARRAY(smpl_t, s->length);
32   return s;
33 }
34
35 void del_fvec(fvec_t *s) {
36   AUBIO_FREE(s->data);
37   AUBIO_FREE(s);
38 }
39
40 void fvec_set_sample(fvec_t *s, smpl_t data, uint_t position) {
41   s->data[position] = data;
42 }
43
44 smpl_t fvec_get_sample(const fvec_t *s, uint_t position) {
45   return s->data[position];
46 }
47
48 smpl_t * fvec_get_data(const fvec_t *s) {
49   return s->data;
50 }
51
52 /* helper functions */
53
54 void fvec_print(const fvec_t *s) {
55   uint_t j;
56   for (j=0; j< s->length; j++) {
57     AUBIO_MSG(AUBIO_SMPL_FMT " ", s->data[j]);
58   }
59   AUBIO_MSG("\n");
60 }
61
62 void fvec_set_all (fvec_t *s, smpl_t val) {
63 #if !defined(HAVE_ACCELERATE) && !defined(HAVE_ATLAS)
64   uint_t j;
65   for (j=0; j< s->length; j++) {
66     s->data[j] = val;
67   }
68 #elif defined(HAVE_ATLAS)
69   aubio_catlas_set(s->length, val, s->data, 1);
70 #elif defined(HAVE_ACCELERATE)
71   aubio_vDSP_vfill(&val, s->data, 1, s->length);
72 #endif
73 }
74
75 void fvec_zeros(fvec_t *s) {
76 #if !defined(HAVE_MEMCPY_HACKS) && !defined(HAVE_ACCELERATE)
77   fvec_set_all (s, 0.);
78 #else
79 #if defined(HAVE_MEMCPY_HACKS)
80   memset(s->data, 0, s->length * sizeof(smpl_t));
81 #else
82   aubio_vDSP_vclr(s->data, 1, s->length);
83 #endif
84 #endif
85 }
86
87 void fvec_ones(fvec_t *s) {
88   fvec_set_all (s, 1.);
89 }
90
91 void fvec_rev(fvec_t *s) {
92   uint_t j;
93   for (j=0; j< FLOOR((smpl_t)s->length/2); j++) {
94     ELEM_SWAP(s->data[j], s->data[s->length-1-j]);
95   }
96 }
97
98 void fvec_weight(fvec_t *s, const fvec_t *weight) {
99 #ifndef HAVE_ACCELERATE
100   uint_t j;
101   uint_t length = MIN(s->length, weight->length);
102   for (j=0; j< length; j++) {
103     s->data[j] *= weight->data[j];
104   }
105 #else
106   aubio_vDSP_vmul(s->data, 1, weight->data, 1, s->data, 1, s->length);
107 #endif /* HAVE_ACCELERATE */
108 }
109
110 void fvec_weighted_copy(const fvec_t *in, const fvec_t *weight, fvec_t *out) {
111 #ifndef HAVE_ACCELERATE
112   uint_t j;
113   uint_t length = MIN(out->length, weight->length);
114   for (j=0; j< length; j++) {
115     out->data[j] = in->data[j] * weight->data[j];
116   }
117 #else
118   aubio_vDSP_vmul(in->data, 1, weight->data, 1, out->data, 1, out->length);
119 #endif /* HAVE_ACCELERATE */
120 }
121
122 void fvec_copy(const fvec_t *s, fvec_t *t) {
123   if (s->length != t->length) {
124     AUBIO_ERR("trying to copy %d elements to %d elements \n",
125         s->length, t->length);
126     return;
127   }
128 #ifdef HAVE_NOOPT
129   uint_t j;
130   for (j=0; j< t->length; j++) {
131     t->data[j] = s->data[j];
132   }
133 #elif defined(HAVE_MEMCPY_HACKS)
134   memcpy(t->data, s->data, t->length * sizeof(smpl_t));
135 #elif defined(HAVE_ATLAS)
136   aubio_cblas_copy(s->length, s->data, 1, t->data, 1);
137 #elif defined(HAVE_ACCELERATE)
138   aubio_vDSP_mmov(s->data, t->data, 1, s->length, 1, 1);
139 #endif
140 }