add queen mary DSP library
[ardour2.git] / libs / qm-dsp / dsp / phasevocoder / PhaseVocoder.cpp
blob71865267a4b4b247f9c7c9eb55e3f8674b46bf6d
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
3 /*
4 QM DSP Library
6 Centre for Digital Music, Queen Mary, University of London.
7 This file 2005-2006 Christian Landone.
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version. See the file
13 COPYING included with this distribution for more information.
16 #include "PhaseVocoder.h"
17 #include "dsp/transforms/FFT.h"
18 #include <math.h>
20 //////////////////////////////////////////////////////////////////////
21 // Construction/Destruction
22 //////////////////////////////////////////////////////////////////////
24 PhaseVocoder::PhaseVocoder(unsigned int n) :
25 m_n(n)
27 m_fft = new FFTReal(m_n);
28 m_realOut = new double[m_n];
29 m_imagOut = new double[m_n];
32 PhaseVocoder::~PhaseVocoder()
34 delete [] m_realOut;
35 delete [] m_imagOut;
36 delete m_fft;
39 void PhaseVocoder::FFTShift(unsigned int size, double *src)
41 const int hs = size/2;
42 for (int i = 0; i < hs; ++i) {
43 double tmp = src[i];
44 src[i] = src[i + hs];
45 src[i + hs] = tmp;
49 void PhaseVocoder::process(double *src, double *mag, double *theta)
51 FFTShift( m_n, src);
53 m_fft->process(0, src, m_realOut, m_imagOut);
55 getMagnitude( m_n/2, mag, m_realOut, m_imagOut);
56 getPhase( m_n/2, theta, m_realOut, m_imagOut);
59 void PhaseVocoder::getMagnitude(unsigned int size, double *mag, double *real, double *imag)
61 unsigned int j;
63 for( j = 0; j < size; j++)
65 mag[ j ] = sqrt( real[ j ] * real[ j ] + imag[ j ] * imag[ j ]);
69 void PhaseVocoder::getPhase(unsigned int size, double *theta, double *real, double *imag)
71 unsigned int k;
73 // Phase Angle "matlab" style
74 //Watch out for quadrant mapping !!!
75 for( k = 0; k < size; k++)
77 theta[ k ] = atan2( -imag[ k ], real[ k ]);