From: Paul Brossier Date: Sat, 12 Sep 2009 12:28:00 +0000 (+0200) Subject: src/fvec.{c,h}: add fvec_set _zeros _ones _rev _weight and _copy utilities functions X-Git-Tag: bzr2git~363 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=3b2d32cd30a888729cc855eb94787767e5c82621;p=aubio.git src/fvec.{c,h}: add fvec_set _zeros _ones _rev _weight and _copy utilities functions --- diff --git a/src/fvec.c b/src/fvec.c index 49eda085..6ba0c333 100644 --- a/src/fvec.c +++ b/src/fvec.c @@ -61,6 +61,8 @@ smpl_t ** fvec_get_data(fvec_t *s) { return s->data; } +/* helper functions */ + void fvec_print(fvec_t *s) { uint_t i,j; for (i=0; i< s->channels; i++) { @@ -70,3 +72,59 @@ void fvec_print(fvec_t *s) { AUBIO_MSG("\n"); } } + +void fvec_set(fvec_t *s, smpl_t val) { + uint_t i,j; + for (i=0; i< s->channels; i++) { + for (j=0; j< s->length; j++) { + s->data[i][j] = val; + } + } +} + +void fvec_zeros(fvec_t *s) { + fvec_set(s, 0.); +} + +void fvec_ones(fvec_t *s) { + fvec_set(s, 1.); +} + +void fvec_rev(fvec_t *s) { + uint_t i,j; + for (i=0; i< s->channels; i++) { + for (j=0; j< FLOOR(s->length/2); j++) { + ELEM_SWAP(s->data[i][j], s->data[i][s->length-1-j]); + } + } +} + +void fvec_weight(fvec_t *s, fvec_t *weight) { + uint_t i,j; + uint_t length = MIN(s->length, weight->length); + for (i=0; i< s->channels; i++) { + for (j=0; j< length; j++) { + s->data[i][j] *= weight->data[0][j]; + } + } +} + +void fvec_copy(fvec_t *s, fvec_t *t) { + uint_t i,j; + uint_t channels = MIN(s->channels, t->channels); + uint_t length = MIN(s->length, t->length); + if (s->channels != t->channels) { + AUBIO_ERR("warning, trying to copy %d channels to %d channels\n", + s->channels, t->channels); + } + if (s->length != t->length) { + AUBIO_ERR("warning, trying to copy %d elements to %d elements \n", + s->length, t->length); + } + for (i=0; i< channels; i++) { + for (j=0; j< length; j++) { + t->data[i][j] = s->data[i][j]; + } + } +} + diff --git a/src/fvec.h b/src/fvec.h index c3f99c14..be2c07b7 100644 --- a/src/fvec.h +++ b/src/fvec.h @@ -120,6 +120,54 @@ smpl_t ** fvec_get_data(fvec_t *s); */ void fvec_print(fvec_t *s); +/** set all elements to a given value + + \param s vector to modify + \param val value to set elements to + +*/ +void fvec_set(fvec_t *s, smpl_t val); + +/** set all elements to zero + + \param s vector to modify + +*/ +void fvec_zeros(fvec_t *s); + +/** set all elements to ones + + \param s vector to modify + +*/ +void fvec_ones(fvec_t *s); + +/** revert order of vector elements + + \param s vector to revert + +*/ +void fvec_rev(fvec_t *s); + +/** apply weight to vector + + If the weight vector is longer than s, only the first elements are used. If + the weight vector is shorter than s, the last elements of s are not weighted. + + \param s vector to weight + \param weight weighting coefficients + +*/ +void fvec_weight(fvec_t *s, fvec_t *weight); + +/** make a copy of a vector + + \param s source vector + \param t vector to copy to + +*/ +void fvec_copy(fvec_t *s, fvec_t *t); + #ifdef __cplusplus } #endif