src/cvec.h: improve cvec_t api
authorPaul Brossier <piem@piem.org>
Tue, 26 Nov 2013 19:09:06 +0000 (20:09 +0100)
committerPaul Brossier <piem@piem.org>
Tue, 26 Nov 2013 19:09:06 +0000 (20:09 +0100)
src/cvec.c
src/cvec.h
tests/src/spectral/test-mfcc.c
tests/src/test-cvec.c

index 825b034..efc89f8 100644 (file)
@@ -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);
+}
index 0e344e4..7968ce6 100644 (file)
@@ -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
 }
index 2584ec6..69829d6 100644 (file)
@@ -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);
 
index a7d8042..51bbc8a 100644 (file)
@@ -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;