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_weight(fvec_t *s, const fvec_t *weight) { |
---|
102 | uint_t length = MIN(s->length, weight->length); |
---|
103 | #if defined(HAVE_INTEL_IPP) |
---|
104 | aubio_ippsMul(s->data, weight->data, s->data, (int)length); |
---|
105 | #elif defined(HAVE_ACCELERATE) |
---|
106 | aubio_vDSP_vmul( s->data, 1, weight->data, 1, s->data, 1, length ); |
---|
107 | #else |
---|
108 | uint_t j; |
---|
109 | for (j = 0; j < length; j++) { |
---|
110 | s->data[j] *= weight->data[j]; |
---|
111 | } |
---|
112 | #endif /* HAVE_ACCELERATE */ |
---|
113 | } |
---|
114 | |
---|
115 | void fvec_weighted_copy(const fvec_t *in, const fvec_t *weight, fvec_t *out) { |
---|
116 | uint_t length = MIN(in->length, MIN(out->length, weight->length)); |
---|
117 | #if defined(HAVE_INTEL_IPP) |
---|
118 | aubio_ippsMul(in->data, weight->data, out->data, (int)length); |
---|
119 | #elif defined(HAVE_ACCELERATE) |
---|
120 | aubio_vDSP_vmul(in->data, 1, weight->data, 1, out->data, 1, length); |
---|
121 | #else |
---|
122 | uint_t j; |
---|
123 | for (j = 0; j < length; j++) { |
---|
124 | out->data[j] = in->data[j] * weight->data[j]; |
---|
125 | } |
---|
126 | #endif |
---|
127 | } |
---|
128 | |
---|
129 | void fvec_copy(const fvec_t *s, fvec_t *t) { |
---|
130 | if (s->length != t->length) { |
---|
131 | AUBIO_ERR("trying to copy %d elements to %d elements \n", |
---|
132 | s->length, t->length); |
---|
133 | return; |
---|
134 | } |
---|
135 | #if defined(HAVE_INTEL_IPP) |
---|
136 | aubio_ippsCopy(s->data, t->data, (int)s->length); |
---|
137 | #elif defined(HAVE_ATLAS) |
---|
138 | aubio_cblas_copy(s->length, s->data, 1, t->data, 1); |
---|
139 | #elif defined(HAVE_ACCELERATE) |
---|
140 | aubio_vDSP_mmov(s->data, t->data, 1, s->length, 1, 1); |
---|
141 | #elif defined(HAVE_MEMCPY_HACKS) |
---|
142 | memcpy(t->data, s->data, t->length * sizeof(smpl_t)); |
---|
143 | #else |
---|
144 | uint_t j; |
---|
145 | for (j = 0; j < t->length; j++) { |
---|
146 | t->data[j] = s->data[j]; |
---|
147 | } |
---|
148 | #endif |
---|
149 | } |
---|