annotate dsp/phasevocoder/PhaseVocoder.cpp @ 309:d5014ab8b0e5

* Add GPL and README; some tidying
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 13 Dec 2010 14:55:28 +0000
parents befe5aa6b450
children f3c69325cca2
rev   line source
c@225 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@225 2
c@225 3 /*
c@225 4 QM DSP Library
c@225 5
c@225 6 Centre for Digital Music, Queen Mary, University of London.
c@309 7 This file 2005-2006 Christian Landone.
c@309 8
c@309 9 This program is free software; you can redistribute it and/or
c@309 10 modify it under the terms of the GNU General Public License as
c@309 11 published by the Free Software Foundation; either version 2 of the
c@309 12 License, or (at your option) any later version. See the file
c@309 13 COPYING included with this distribution for more information.
c@225 14 */
c@225 15
c@225 16 #include "PhaseVocoder.h"
c@225 17 #include "dsp/transforms/FFT.h"
c@225 18 #include <math.h>
c@225 19
c@225 20 //////////////////////////////////////////////////////////////////////
c@225 21 // Construction/Destruction
c@225 22 //////////////////////////////////////////////////////////////////////
c@225 23
c@289 24 PhaseVocoder::PhaseVocoder(unsigned int n) :
c@289 25 m_n(n)
c@225 26 {
c@289 27 m_fft = new FFTReal(m_n);
c@289 28 m_realOut = new double[m_n];
c@289 29 m_imagOut = new double[m_n];
c@225 30 }
c@225 31
c@225 32 PhaseVocoder::~PhaseVocoder()
c@225 33 {
c@289 34 delete [] m_realOut;
c@289 35 delete [] m_imagOut;
c@289 36 delete m_fft;
c@225 37 }
c@225 38
c@225 39 void PhaseVocoder::FFTShift(unsigned int size, double *src)
c@225 40 {
c@280 41 const int hs = size/2;
c@280 42 for (int i = 0; i < hs; ++i) {
c@280 43 double tmp = src[i];
c@280 44 src[i] = src[i + hs];
c@280 45 src[i + hs] = tmp;
c@225 46 }
c@225 47 }
c@225 48
c@289 49 void PhaseVocoder::process(double *src, double *mag, double *theta)
c@225 50 {
c@289 51 FFTShift( m_n, src);
c@289 52
c@289 53 m_fft->process(0, src, m_realOut, m_imagOut);
c@225 54
c@289 55 getMagnitude( m_n/2, mag, m_realOut, m_imagOut);
c@289 56 getPhase( m_n/2, theta, m_realOut, m_imagOut);
c@225 57 }
c@225 58
c@225 59 void PhaseVocoder::getMagnitude(unsigned int size, double *mag, double *real, double *imag)
c@225 60 {
c@225 61 unsigned int j;
c@225 62
c@225 63 for( j = 0; j < size; j++)
c@225 64 {
c@225 65 mag[ j ] = sqrt( real[ j ] * real[ j ] + imag[ j ] * imag[ j ]);
c@225 66 }
c@225 67 }
c@225 68
c@225 69 void PhaseVocoder::getPhase(unsigned int size, double *theta, double *real, double *imag)
c@225 70 {
c@225 71 unsigned int k;
c@225 72
c@225 73 // Phase Angle "matlab" style
c@225 74 //Watch out for quadrant mapping !!!
c@225 75 for( k = 0; k < size; k++)
c@225 76 {
c@225 77 theta[ k ] = atan2( -imag[ k ], real[ k ]);
c@225 78 }
c@225 79 }