From 39a7b26d00a5898b87643043304c634b66d1e7ad Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Tue, 26 Nov 2013 20:09:06 +0100 Subject: [PATCH] src/cvec.h: improve cvec_t api --- src/cvec.c | 55 ++++++++++++++++++++++++++++++++++++++---- src/cvec.h | 49 ++++++++++++++++++++++++++++++++----- tests/src/spectral/test-mfcc.c | 4 +-- tests/src/test-cvec.c | 12 +++++++-- 4 files changed, 105 insertions(+), 15 deletions(-) diff --git a/src/cvec.c b/src/cvec.c index 825b0345..efc89f87 100644 --- a/src/cvec.c +++ b/src/cvec.c @@ -70,18 +70,63 @@ void cvec_print(cvec_t *s) { AUBIO_MSG("\n"); } -void cvec_set(cvec_t *s, smpl_t val) { +void cvec_copy(cvec_t *s, cvec_t *t) { + if (s->length != t->length) { + AUBIO_ERR("trying to copy %d elements to %d elements \n", + s->length, t->length); + return; + } +#if HAVE_MEMCPY_HACKS + memcpy(t->norm, s->norm, t->length * sizeof(smpl_t)); + memcpy(t->phas, s->phas, t->length * sizeof(smpl_t)); +#else + uint_t j; + for (j=0; j< t->length; j++) { + t->norm[j] = s->norm[j]; + t->phas[j] = s->phas[j]; + } +#endif +} + +void cvec_set_all_norm(cvec_t *s, smpl_t val) { uint_t j; for (j=0; j< s->length; j++) { s->norm[j] = val; } } -void cvec_zeros(cvec_t *s) { - cvec_set(s, 0.); +void cvec_zeros_norm(cvec_t *s) { +#if HAVE_MEMCPY_HACKS + memset(s->norm, 0, s->length * sizeof(smpl_t)); +#else + cvec_set_all_norm(s, 0.); +#endif } -void cvec_ones(cvec_t *s) { - cvec_set(s, 1.); +void cvec_ones_norm(cvec_t *s) { + cvec_set_all_norm(s, 1.); } +void cvec_set_all_phas(cvec_t *s, smpl_t val) { + uint_t j; + for (j=0; j< s->length; j++) { + s->phas[j] = val; + } +} + +void cvec_zeros_phas(cvec_t *s) { +#if HAVE_MEMCPY_HACKS + memset(s->phas, 0, s->length * sizeof(smpl_t)); +#else + cvec_set_all_phas(s, 0.); +#endif +} + +void cvec_ones_phas(cvec_t *s) { + cvec_set_all_phas(s, 1.); +} + +void cvec_zeros(cvec_t *s) { + cvec_zeros_norm(s); + cvec_zeros_phas(s); +} diff --git a/src/cvec.h b/src/cvec.h index 0e344e40..7968ce65 100644 --- a/src/cvec.h +++ b/src/cvec.h @@ -158,27 +158,64 @@ smpl_t * cvec_get_phas(cvec_t *s); */ void cvec_print(cvec_t *s); -/** set all elements to a given value +/** make a copy of a vector + + \param s source vector + \param t vector to copy to + +*/ +void cvec_copy(cvec_t *s, cvec_t *t); + +/** set all norm elements to a given value \param s vector to modify \param val value to set elements to */ -void cvec_set(cvec_t *s, smpl_t val); +void cvec_set_all_norm(cvec_t *s, smpl_t val); -/** set all elements to zero +/** set all norm elements to zero \param s vector to modify */ -void cvec_zeros(cvec_t *s); +void cvec_zeros_norm(cvec_t *s); + +/** set all norm elements to one + + \param s vector to modify + +*/ +void cvec_ones_norm(cvec_t *s); -/** set all elements to ones +/** set all phase elements to a given value \param s vector to modify + \param val value to set elements to */ -void cvec_ones(cvec_t *s); +void cvec_set_all_phas(cvec_t *s, smpl_t val); + +/** set all phase elements to zero + + \param s vector to modify + +*/ +void cvec_zeros_phas(cvec_t *s); + +/** set all phase elements to one + + \param s vector to modify + +*/ +void cvec_ones_phas(cvec_t *s); + +/** set all norm and phas elements to zero + + \param s vector to modify + +*/ +void cvec_zeros(cvec_t *s); #ifdef __cplusplus } diff --git a/tests/src/spectral/test-mfcc.c b/tests/src/spectral/test-mfcc.c index 2584ec6e..69829d6e 100644 --- a/tests/src/spectral/test-mfcc.c +++ b/tests/src/spectral/test-mfcc.c @@ -12,11 +12,11 @@ int main () // create mfcc object aubio_mfcc_t *o = new_aubio_mfcc (win_s, n_filters, n_coefs, samplerate); - cvec_set (in, 1.); + cvec_set_all_norm (in, 1.); aubio_mfcc_do (o, in, out); fvec_print (out); - cvec_set (in, .5); + cvec_set_all_norm (in, .5); aubio_mfcc_do (o, in, out); fvec_print (out); diff --git a/tests/src/test-cvec.c b/tests/src/test-cvec.c index a7d80427..51bbc8a5 100644 --- a/tests/src/test-cvec.c +++ b/tests/src/test-cvec.c @@ -19,7 +19,7 @@ int main () } // set all vector elements to `0` - cvec_zeros(complex_vector); + cvec_zeros_norm(complex_vector); for ( i = 0; i < complex_vector->length; i++ ) { assert( complex_vector->norm[i] == 0. ); // assert( complex_vector->phas[i] == 0 ); @@ -27,12 +27,20 @@ int main () cvec_print(complex_vector); // set all vector elements to `1` - cvec_ones(complex_vector); + cvec_ones_norm(complex_vector); for ( i = 0; i < complex_vector->length; i++ ) { assert( complex_vector->norm[i] == 1. ); // assert( complex_vector->phas[i] == 0 ); } cvec_print(complex_vector); + + cvec_zeros(complex_vector); + cvec_zeros_phas(complex_vector); + cvec_zeros_norm(complex_vector); + cvec_ones_norm(complex_vector); + cvec_ones_phas(complex_vector); + cvec_copy(complex_vector, complex_vector); + // destroy it del_cvec(complex_vector); return 0; -- 2.11.0