[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" |
---|
[d1ec8cb] | 22 | #include "cvec.h" |
---|
[96fb8ad] | 23 | |
---|
[66fb3ea] | 24 | cvec_t * new_cvec( uint_t length) { |
---|
[767990e] | 25 | if ((sint_t)length <= 0) { |
---|
| 26 | return NULL; |
---|
| 27 | } |
---|
[96fb8ad] | 28 | cvec_t * s = AUBIO_NEW(cvec_t); |
---|
| 29 | s->length = length/2 + 1; |
---|
[66fb3ea] | 30 | s->norm = AUBIO_ARRAY(smpl_t,s->length); |
---|
| 31 | s->phas = AUBIO_ARRAY(smpl_t,s->length); |
---|
[96fb8ad] | 32 | return s; |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | void del_cvec(cvec_t *s) { |
---|
| 36 | AUBIO_FREE(s->norm); |
---|
| 37 | AUBIO_FREE(s->phas); |
---|
| 38 | AUBIO_FREE(s); |
---|
| 39 | } |
---|
[7c206df] | 40 | |
---|
[66fb3ea] | 41 | void cvec_write_norm(cvec_t *s, smpl_t data, uint_t position) { |
---|
| 42 | s->norm[position] = data; |
---|
[7c206df] | 43 | } |
---|
[66fb3ea] | 44 | void cvec_write_phas(cvec_t *s, smpl_t data, uint_t position) { |
---|
| 45 | s->phas[position] = data; |
---|
[7c206df] | 46 | } |
---|
[66fb3ea] | 47 | smpl_t cvec_read_norm(cvec_t *s, uint_t position) { |
---|
| 48 | return s->norm[position]; |
---|
[7c206df] | 49 | } |
---|
[66fb3ea] | 50 | smpl_t cvec_read_phas(cvec_t *s, uint_t position) { |
---|
| 51 | return s->phas[position]; |
---|
[7c206df] | 52 | } |
---|
[66fb3ea] | 53 | smpl_t * cvec_get_norm(cvec_t *s) { |
---|
[7c206df] | 54 | return s->norm; |
---|
| 55 | } |
---|
[66fb3ea] | 56 | smpl_t * cvec_get_phas(cvec_t *s) { |
---|
[7c206df] | 57 | return s->phas; |
---|
| 58 | } |
---|
[d1ec8cb] | 59 | |
---|
[55b7cb4] | 60 | /* helper functions */ |
---|
| 61 | |
---|
| 62 | void cvec_print(cvec_t *s) { |
---|
[66fb3ea] | 63 | uint_t j; |
---|
| 64 | AUBIO_MSG("norm: "); |
---|
| 65 | for (j=0; j< s->length; j++) { |
---|
| 66 | AUBIO_MSG(AUBIO_SMPL_FMT " ", s->norm[j]); |
---|
| 67 | } |
---|
| 68 | AUBIO_MSG("\n"); |
---|
| 69 | AUBIO_MSG("phas: "); |
---|
| 70 | for (j=0; j< s->length; j++) { |
---|
| 71 | AUBIO_MSG(AUBIO_SMPL_FMT " ", s->phas[j]); |
---|
[55b7cb4] | 72 | } |
---|
[66fb3ea] | 73 | AUBIO_MSG("\n"); |
---|
[55b7cb4] | 74 | } |
---|
| 75 | |
---|
[39a7b26] | 76 | void cvec_copy(cvec_t *s, cvec_t *t) { |
---|
| 77 | if (s->length != t->length) { |
---|
| 78 | AUBIO_ERR("trying to copy %d elements to %d elements \n", |
---|
| 79 | s->length, t->length); |
---|
| 80 | return; |
---|
| 81 | } |
---|
| 82 | #if HAVE_MEMCPY_HACKS |
---|
| 83 | memcpy(t->norm, s->norm, t->length * sizeof(smpl_t)); |
---|
| 84 | memcpy(t->phas, s->phas, t->length * sizeof(smpl_t)); |
---|
| 85 | #else |
---|
| 86 | uint_t j; |
---|
| 87 | for (j=0; j< t->length; j++) { |
---|
| 88 | t->norm[j] = s->norm[j]; |
---|
| 89 | t->phas[j] = s->phas[j]; |
---|
| 90 | } |
---|
| 91 | #endif |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | void cvec_set_all_norm(cvec_t *s, smpl_t val) { |
---|
[66fb3ea] | 95 | uint_t j; |
---|
| 96 | for (j=0; j< s->length; j++) { |
---|
| 97 | s->norm[j] = val; |
---|
[012466b] | 98 | } |
---|
| 99 | } |
---|
| 100 | |
---|
[39a7b26] | 101 | void cvec_zeros_norm(cvec_t *s) { |
---|
| 102 | #if HAVE_MEMCPY_HACKS |
---|
| 103 | memset(s->norm, 0, s->length * sizeof(smpl_t)); |
---|
| 104 | #else |
---|
| 105 | cvec_set_all_norm(s, 0.); |
---|
| 106 | #endif |
---|
[012466b] | 107 | } |
---|
| 108 | |
---|
[39a7b26] | 109 | void cvec_ones_norm(cvec_t *s) { |
---|
| 110 | cvec_set_all_norm(s, 1.); |
---|
[012466b] | 111 | } |
---|
| 112 | |
---|
[39a7b26] | 113 | void cvec_set_all_phas(cvec_t *s, smpl_t val) { |
---|
| 114 | uint_t j; |
---|
| 115 | for (j=0; j< s->length; j++) { |
---|
| 116 | s->phas[j] = val; |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | void cvec_zeros_phas(cvec_t *s) { |
---|
| 121 | #if HAVE_MEMCPY_HACKS |
---|
| 122 | memset(s->phas, 0, s->length * sizeof(smpl_t)); |
---|
| 123 | #else |
---|
| 124 | cvec_set_all_phas(s, 0.); |
---|
| 125 | #endif |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | void cvec_ones_phas(cvec_t *s) { |
---|
| 129 | cvec_set_all_phas(s, 1.); |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | void cvec_zeros(cvec_t *s) { |
---|
| 133 | cvec_zeros_norm(s); |
---|
| 134 | cvec_zeros_phas(s); |
---|
| 135 | } |
---|