From 116bd1b775471efeec4a96d59d108c9c791fc89c Mon Sep 17 00:00:00 2001 From: Paul Brossier Date: Tue, 16 Feb 2016 17:47:43 +0100 Subject: [PATCH] src/mathutils.h: add fvec_ishift --- src/mathutils.c | 36 +++++++++++++++++++++++++++++++----- src/mathutils.h | 18 ++++++++++++++++++ 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/mathutils.c b/src/mathutils.c index a1035173..7ea6dbfa 100644 --- a/src/mathutils.c +++ b/src/mathutils.c @@ -253,15 +253,41 @@ fvec_max_elem (fvec_t * s) 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 diff --git a/src/mathutils.h b/src/mathutils.h index 77cc7d50..ffa65563 100644 --- a/src/mathutils.h +++ b/src/mathutils.h @@ -99,6 +99,24 @@ resulting spectrum. See Amalia de Götzen's paper referred to above. */ 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 -- 2.11.0