1 | /* |
---|
2 | Copyright (C) 2003-2007 Paul Brossier <piem@piem.org> |
---|
3 | |
---|
4 | This program is free software; you can redistribute it and/or modify |
---|
5 | it under the terms of the GNU General Public License as published by |
---|
6 | the Free Software Foundation; either version 2 of the License, or |
---|
7 | (at your option) any later version. |
---|
8 | |
---|
9 | This program is distributed in the hope that it will be useful, |
---|
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | GNU General Public License for more details. |
---|
13 | |
---|
14 | You should have received a copy of the GNU General Public License |
---|
15 | along with this program; if not, write to the Free Software |
---|
16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
17 | |
---|
18 | */ |
---|
19 | |
---|
20 | #include "aubio_priv.h" |
---|
21 | #include "fvec.h" |
---|
22 | |
---|
23 | fvec_t * new_fvec( uint_t length, uint_t channels) { |
---|
24 | fvec_t * s = AUBIO_NEW(fvec_t); |
---|
25 | uint_t i,j; |
---|
26 | s->channels = channels; |
---|
27 | s->length = length; |
---|
28 | s->data = AUBIO_ARRAY(smpl_t*,s->channels); |
---|
29 | for (i=0; i< s->channels; i++) { |
---|
30 | s->data[i] = AUBIO_ARRAY(smpl_t, s->length); |
---|
31 | for (j=0; j< s->length; j++) { |
---|
32 | s->data[i][j]=0.; |
---|
33 | } |
---|
34 | } |
---|
35 | return s; |
---|
36 | } |
---|
37 | |
---|
38 | void del_fvec(fvec_t *s) { |
---|
39 | uint_t i; |
---|
40 | for (i=0; i<s->channels; i++) { |
---|
41 | AUBIO_FREE(s->data[i]); |
---|
42 | } |
---|
43 | AUBIO_FREE(s->data); |
---|
44 | AUBIO_FREE(s); |
---|
45 | } |
---|
46 | |
---|
47 | void fvec_write_sample(fvec_t *s, smpl_t data, uint_t channel, uint_t position) { |
---|
48 | s->data[channel][position] = data; |
---|
49 | } |
---|
50 | smpl_t fvec_read_sample(fvec_t *s, uint_t channel, uint_t position) { |
---|
51 | return s->data[channel][position]; |
---|
52 | } |
---|
53 | void fvec_put_channel(fvec_t *s, smpl_t * data, uint_t channel) { |
---|
54 | s->data[channel] = data; |
---|
55 | } |
---|
56 | smpl_t * fvec_get_channel(fvec_t *s, uint_t channel) { |
---|
57 | return s->data[channel]; |
---|
58 | } |
---|
59 | |
---|
60 | smpl_t ** fvec_get_data(fvec_t *s) { |
---|
61 | return s->data; |
---|
62 | } |
---|
63 | |
---|
64 | /* helper functions */ |
---|
65 | |
---|
66 | void fvec_print(fvec_t *s) { |
---|
67 | uint_t i,j; |
---|
68 | for (i=0; i< s->channels; i++) { |
---|
69 | for (j=0; j< s->length; j++) { |
---|
70 | AUBIO_MSG(AUBIO_SMPL_FMT " ", s->data[i][j]); |
---|
71 | } |
---|
72 | AUBIO_MSG("\n"); |
---|
73 | } |
---|
74 | } |
---|
75 | |
---|
76 | void fvec_set(fvec_t *s, smpl_t val) { |
---|
77 | uint_t i,j; |
---|
78 | for (i=0; i< s->channels; i++) { |
---|
79 | for (j=0; j< s->length; j++) { |
---|
80 | s->data[i][j] = val; |
---|
81 | } |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | void fvec_zeros(fvec_t *s) { |
---|
86 | fvec_set(s, 0.); |
---|
87 | } |
---|
88 | |
---|
89 | void fvec_ones(fvec_t *s) { |
---|
90 | fvec_set(s, 1.); |
---|
91 | } |
---|
92 | |
---|
93 | void fvec_rev(fvec_t *s) { |
---|
94 | uint_t i,j; |
---|
95 | for (i=0; i< s->channels; i++) { |
---|
96 | for (j=0; j< FLOOR(s->length/2); j++) { |
---|
97 | ELEM_SWAP(s->data[i][j], s->data[i][s->length-1-j]); |
---|
98 | } |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | void fvec_weight(fvec_t *s, fvec_t *weight) { |
---|
103 | uint_t i,j; |
---|
104 | uint_t length = MIN(s->length, weight->length); |
---|
105 | for (i=0; i< s->channels; i++) { |
---|
106 | for (j=0; j< length; j++) { |
---|
107 | s->data[i][j] *= weight->data[0][j]; |
---|
108 | } |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | void fvec_copy(fvec_t *s, fvec_t *t) { |
---|
113 | uint_t i,j; |
---|
114 | uint_t channels = MIN(s->channels, t->channels); |
---|
115 | uint_t length = MIN(s->length, t->length); |
---|
116 | if (s->channels != t->channels) { |
---|
117 | AUBIO_ERR("warning, trying to copy %d channels to %d channels\n", |
---|
118 | s->channels, t->channels); |
---|
119 | } |
---|
120 | if (s->length != t->length) { |
---|
121 | AUBIO_ERR("warning, trying to copy %d elements to %d elements \n", |
---|
122 | s->length, t->length); |
---|
123 | } |
---|
124 | for (i=0; i< channels; i++) { |
---|
125 | for (j=0; j< length; j++) { |
---|
126 | t->data[i][j] = s->data[i][j]; |
---|
127 | } |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|