c@225: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ c@225: c@225: /* c@225: QM DSP Library c@225: c@225: Centre for Digital Music, Queen Mary, University of London. c@309: This file 2005-2006 Christian Landone. c@309: c@309: This program is free software; you can redistribute it and/or c@309: modify it under the terms of the GNU General Public License as c@309: published by the Free Software Foundation; either version 2 of the c@309: License, or (at your option) any later version. See the file c@309: COPYING included with this distribution for more information. c@225: */ c@225: c@225: #include "PhaseVocoder.h" c@225: #include "dsp/transforms/FFT.h" c@225: #include c@225: c@225: ////////////////////////////////////////////////////////////////////// c@225: // Construction/Destruction c@225: ////////////////////////////////////////////////////////////////////// c@225: c@289: PhaseVocoder::PhaseVocoder(unsigned int n) : c@289: m_n(n) c@225: { c@289: m_fft = new FFTReal(m_n); c@289: m_realOut = new double[m_n]; c@289: m_imagOut = new double[m_n]; c@225: } c@225: c@225: PhaseVocoder::~PhaseVocoder() c@225: { c@289: delete [] m_realOut; c@289: delete [] m_imagOut; c@289: delete m_fft; c@225: } c@225: c@225: void PhaseVocoder::FFTShift(unsigned int size, double *src) c@225: { c@280: const int hs = size/2; c@280: for (int i = 0; i < hs; ++i) { c@280: double tmp = src[i]; c@280: src[i] = src[i + hs]; c@280: src[i + hs] = tmp; c@225: } c@225: } c@225: c@289: void PhaseVocoder::process(double *src, double *mag, double *theta) c@225: { c@289: FFTShift( m_n, src); c@289: c@289: m_fft->process(0, src, m_realOut, m_imagOut); c@225: c@289: getMagnitude( m_n/2, mag, m_realOut, m_imagOut); c@289: getPhase( m_n/2, theta, m_realOut, m_imagOut); c@225: } c@225: c@225: void PhaseVocoder::getMagnitude(unsigned int size, double *mag, double *real, double *imag) c@225: { c@225: unsigned int j; c@225: c@225: for( j = 0; j < size; j++) c@225: { c@225: mag[ j ] = sqrt( real[ j ] * real[ j ] + imag[ j ] * imag[ j ]); c@225: } c@225: } c@225: c@225: void PhaseVocoder::getPhase(unsigned int size, double *theta, double *real, double *imag) c@225: { c@225: unsigned int k; c@225: c@225: // Phase Angle "matlab" style c@225: //Watch out for quadrant mapping !!! c@225: for( k = 0; k < size; k++) c@225: { c@225: theta[ k ] = atan2( -imag[ k ], real[ k ]); c@225: } c@225: }