[96fb8ad] | 1 | /* |
---|
[a6db140] | 2 | Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org> |
---|
[96fb8ad] | 3 | |
---|
[a6db140] | 4 | This file is part of aubio. |
---|
[96fb8ad] | 5 | |
---|
[a6db140] | 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. |
---|
[96fb8ad] | 10 | |
---|
[a6db140] | 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/>. |
---|
[96fb8ad] | 18 | |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | #include "aubio_priv.h" |
---|
[6c7d49b] | 22 | #include "fvec.h" |
---|
[96fb8ad] | 23 | |
---|
[c7860af] | 24 | fvec_t * new_fvec( uint_t length) { |
---|
[767990e] | 25 | if ((sint_t)length <= 0) { |
---|
| 26 | return NULL; |
---|
| 27 | } |
---|
[96fb8ad] | 28 | fvec_t * s = AUBIO_NEW(fvec_t); |
---|
| 29 | s->length = length; |
---|
[c7860af] | 30 | s->data = AUBIO_ARRAY(smpl_t, s->length); |
---|
[96fb8ad] | 31 | return s; |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | void del_fvec(fvec_t *s) { |
---|
| 35 | AUBIO_FREE(s->data); |
---|
| 36 | AUBIO_FREE(s); |
---|
| 37 | } |
---|
| 38 | |
---|
[c7860af] | 39 | void fvec_write_sample(fvec_t *s, smpl_t data, uint_t position) { |
---|
| 40 | s->data[position] = data; |
---|
[96fb8ad] | 41 | } |
---|
[c7860af] | 42 | |
---|
| 43 | smpl_t fvec_read_sample(fvec_t *s, uint_t position) { |
---|
| 44 | return s->data[position]; |
---|
[96fb8ad] | 45 | } |
---|
| 46 | |
---|
[c7860af] | 47 | smpl_t * fvec_get_data(fvec_t *s) { |
---|
[96fb8ad] | 48 | return s->data; |
---|
| 49 | } |
---|
| 50 | |
---|
[3b2d32c] | 51 | /* helper functions */ |
---|
| 52 | |
---|
[32f5a01] | 53 | void fvec_print(fvec_t *s) { |
---|
[c7860af] | 54 | uint_t j; |
---|
| 55 | for (j=0; j< s->length; j++) { |
---|
| 56 | AUBIO_MSG(AUBIO_SMPL_FMT " ", s->data[j]); |
---|
[32f5a01] | 57 | } |
---|
[c7860af] | 58 | AUBIO_MSG("\n"); |
---|
[32f5a01] | 59 | } |
---|
[3b2d32c] | 60 | |
---|
| 61 | void fvec_set(fvec_t *s, smpl_t val) { |
---|
[c7860af] | 62 | uint_t j; |
---|
| 63 | for (j=0; j< s->length; j++) { |
---|
| 64 | s->data[j] = val; |
---|
[3b2d32c] | 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | void fvec_zeros(fvec_t *s) { |
---|
[923a7a8] | 69 | #if HAVE_MEMCPY_HACKS |
---|
| 70 | memset(s->data, 0, s->length * sizeof(smpl_t)); |
---|
| 71 | #else |
---|
[3b2d32c] | 72 | fvec_set(s, 0.); |
---|
[923a7a8] | 73 | #endif |
---|
[3b2d32c] | 74 | } |
---|
| 75 | |
---|
| 76 | void fvec_ones(fvec_t *s) { |
---|
| 77 | fvec_set(s, 1.); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | void fvec_rev(fvec_t *s) { |
---|
[c7860af] | 81 | uint_t j; |
---|
| 82 | for (j=0; j< FLOOR(s->length/2); j++) { |
---|
| 83 | ELEM_SWAP(s->data[j], s->data[s->length-1-j]); |
---|
[3b2d32c] | 84 | } |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | void fvec_weight(fvec_t *s, fvec_t *weight) { |
---|
[c7860af] | 88 | uint_t j; |
---|
[3b2d32c] | 89 | uint_t length = MIN(s->length, weight->length); |
---|
[c7860af] | 90 | for (j=0; j< length; j++) { |
---|
| 91 | s->data[j] *= weight->data[j]; |
---|
[3b2d32c] | 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | void fvec_copy(fvec_t *s, fvec_t *t) { |
---|
| 96 | if (s->length != t->length) { |
---|
[923a7a8] | 97 | AUBIO_ERR("trying to copy %d elements to %d elements \n", |
---|
[396103a] | 98 | s->length, t->length); |
---|
[923a7a8] | 99 | return; |
---|
[3b2d32c] | 100 | } |
---|
[923a7a8] | 101 | #if HAVE_MEMCPY_HACKS |
---|
| 102 | memcpy(t->data, s->data, t->length * sizeof(smpl_t)); |
---|
| 103 | #else |
---|
| 104 | uint_t j; |
---|
| 105 | for (j=0; j< t->length; j++) { |
---|
[c7860af] | 106 | t->data[j] = s->data[j]; |
---|
[3b2d32c] | 107 | } |
---|
[923a7a8] | 108 | #endif |
---|
[3b2d32c] | 109 | } |
---|