shithub: aubio

Download patch

ref: 3b2d32cd30a888729cc855eb94787767e5c82621
parent: ae4d5de65d7b89f4010cef3a6f57d81a71f27b16
author: Paul Brossier <[email protected]>
date: Sat Sep 12 10:28:00 EDT 2009

src/fvec.{c,h}: add fvec_set _zeros _ones _rev _weight and _copy utilities functions

--- a/src/fvec.c
+++ b/src/fvec.c
@@ -61,6 +61,8 @@
   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 @@
     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];
+    }
+  }
+}
+
--- a/src/fvec.h
+++ b/src/fvec.h
@@ -120,6 +120,54 @@
 */
 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