shithub: aubio

Download patch

ref: 116bd1b775471efeec4a96d59d108c9c791fc89c
parent: d21a4b16afd6d962be45604f2681e2346695e159
author: Paul Brossier <piem@piem.org>
date: Tue Feb 16 12:47:43 EST 2016

src/mathutils.h: add fvec_ishift

--- a/src/mathutils.c
+++ b/src/mathutils.c
@@ -253,15 +253,41 @@
 void
 fvec_shift (fvec_t * s)
 {
+  uint_t half = s->length / 2, start = half;
+  // if length is odd, middle element is moved to the end
+  if (2 * half < s->length) start ++;
 #ifndef HAVE_ATLAS
-  uint_t j;
-  for (j = 0; j < s->length / 2; j++) {
-    ELEM_SWAP (s->data[j], s->data[j + s->length / 2]);
+  for (uint_t j = 0; j < half; j++) {
+    ELEM_SWAP (s->data[j], s->data[j + start]);
   }
 #else
-  uint_t half = s->length / 2;
-  aubio_cblas_swap(half, s->data, 1, s->data + half, 1);
+  aubio_cblas_swap(half, s->data, 1, s->data + start, 1);
 #endif
+  if (start != half) {
+    for (uint_t j = 0; j < half; j++) {
+      ELEM_SWAP (s->data[j + start - 1], s->data[j + start]);
+    }
+  }
+}
+
+void
+fvec_ishift (fvec_t * s)
+{
+  uint_t half = s->length / 2, start = half;
+  // if length is odd, middle element is moved to the beginning
+  if (2 * half < s->length) start ++;
+#ifndef HAVE_ATLAS
+  for (uint_t j = 0; j < half; j++) {
+    ELEM_SWAP (s->data[j], s->data[j + start]);
+  }
+#else
+  aubio_cblas_swap(half, s->data, 1, s->data + start, 1);
+#endif
+  if (start != half) {
+    for (uint_t j = 0; j < half; j++) {
+      ELEM_SWAP (s->data[half], s->data[j]);
+    }
+  }
 }
 
 smpl_t
--- a/src/mathutils.h
+++ b/src/mathutils.h
@@ -99,6 +99,24 @@
 */
 void fvec_shift (fvec_t * v);
 
+/** swap the left and right halves of a vector
+
+  This function swaps the left part of the signal with the right part of the
+signal. Therefore
+
+  \f$ a[0], a[1], ..., a[\frac{N}{2}], a[\frac{N}{2}+1], ..., a[N-1], a[N] \f$
+
+  becomes
+
+  \f$ a[\frac{N}{2}+1], ..., a[N-1], a[N], a[0], a[1], ..., a[\frac{N}{2}] \f$
+
+  This operation, known as 'ifftshift' in the Matlab Signal Processing Toolbox,
+can be used after computing the inverse FFT to simplify the phase relationship
+of the resulting spectrum. See Amalia de Götzen's paper referred to above.
+
+*/
+void fvec_ishift (fvec_t * v);
+
 /** compute the sum of all elements of a vector
 
   \param v vector to compute the sum of