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_INTEL_IPP) |
---|
64 | aubio_ippsSet(val, s->data, (int)s->length); |
---|
65 | #elif defined(HAVE_ATLAS) |
---|
66 | aubio_catlas_set(s->length, val, s->data, 1); |
---|
67 | #elif defined(HAVE_ACCELERATE) |
---|
68 | aubio_vDSP_vfill(&val, s->data, 1, s->length); |
---|
69 | #else |
---|
70 | uint_t j; |
---|
71 | for ( j = 0; j< s->length; j++ ) |
---|
72 | { |
---|
73 | s->data[j] = val; |
---|
74 | } |
---|
75 | #endif |
---|
76 | } |
---|
77 | |
---|
78 | void fvec_zeros(fvec_t *s) { |
---|
79 | #if defined(HAVE_INTEL_IPP) |
---|
80 | aubio_ippsZero(s->data, (int)s->length); |
---|
81 | #elif defined(HAVE_ACCELERATE) |
---|
82 | aubio_vDSP_vclr(s->data, 1, s->length); |
---|
83 | #elif defined(HAVE_MEMCPY_HACKS) |
---|
84 | memset(s->data, 0, s->length * sizeof(smpl_t)); |
---|
85 | #else |
---|
86 | fvec_set_all(s, 0.); |
---|
87 | #endif |
---|
88 | } |
---|
89 | |
---|
90 | void fvec_ones(fvec_t *s) { |
---|
91 | fvec_set_all (s, 1.); |
---|
92 | } |
---|
93 | |
---|
94 | void fvec_rev(fvec_t *s) { |
---|
95 | uint_t j; |
---|
96 | for (j=0; j< FLOOR((smpl_t)s->length/2); j++) { |
---|
97 | ELEM_SWAP(s->data[j], s->data[s->length-1-j]); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | void fvec_vecadd(fvec_t *s, const fvec_t *bias) { |
---|
102 | uint_t j; |
---|
103 | uint_t length = MIN(s->length, bias->length); |
---|
104 | for (j = 0; j < length; j++) { |
---|
105 | s->data[j] += bias->data[j]; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | void fvec_weight(fvec_t *s, const fvec_t *weight) { |
---|
110 | uint_t length = MIN(s->length, weight->length); |
---|
111 | #if defined(HAVE_INTEL_IPP) |
---|
112 | aubio_ippsMul(s->data, weight->data, s->data, (int)length); |
---|
113 | #elif defined(HAVE_ACCELERATE) |
---|
114 | aubio_vDSP_vmul( s->data, 1, weight->data, 1, s->data, 1, length ); |
---|
115 | #else |
---|
116 | uint_t j; |
---|
117 | for (j = 0; j < length; j++) { |
---|
118 | s->data[j] *= weight->data[j]; |
---|
119 | } |
---|
120 | #endif /* HAVE_ACCELERATE */ |
---|
121 | } |
---|
122 | |
---|
123 | void fvec_weighted_copy(const fvec_t *in, const fvec_t *weight, fvec_t *out) { |
---|
124 | uint_t length = MIN(in->length, MIN(out->length, weight->length)); |
---|
125 | #if defined(HAVE_INTEL_IPP) |
---|
126 | aubio_ippsMul(in->data, weight->data, out->data, (int)length); |
---|
127 | #elif defined(HAVE_ACCELERATE) |
---|
128 | aubio_vDSP_vmul(in->data, 1, weight->data, 1, out->data, 1, length); |
---|
129 | #else |
---|
130 | uint_t j; |
---|
131 | for (j = 0; j < length; j++) { |
---|
132 | out->data[j] = in->data[j] * weight->data[j]; |
---|
133 | } |
---|
134 | #endif |
---|
135 | } |
---|
136 | |
---|
137 | void fvec_copy(const fvec_t *s, fvec_t *t) { |
---|
138 | if (s->length != t->length) { |
---|
139 | AUBIO_ERR("trying to copy %d elements to %d elements \n", |
---|
140 | s->length, t->length); |
---|
141 | return; |
---|
142 | } |
---|
143 | #if defined(HAVE_INTEL_IPP) |
---|
144 | aubio_ippsCopy(s->data, t->data, (int)s->length); |
---|
145 | #elif defined(HAVE_BLAS) |
---|
146 | aubio_cblas_copy(s->length, s->data, 1, t->data, 1); |
---|
147 | #elif defined(HAVE_ACCELERATE) |
---|
148 | aubio_vDSP_mmov(s->data, t->data, 1, s->length, 1, 1); |
---|
149 | #elif defined(HAVE_MEMCPY_HACKS) |
---|
150 | memcpy(t->data, s->data, t->length * sizeof(smpl_t)); |
---|
151 | #else |
---|
152 | uint_t j; |
---|
153 | for (j = 0; j < t->length; j++) { |
---|
154 | t->data[j] = s->data[j]; |
---|
155 | } |
---|
156 | #endif |
---|
157 | } |
---|