Mercurial > hg > qm-dsp
comparison dsp/phasevocoder/PhaseVocoder.cpp @ 0:d7116e3183f8
* Queen Mary C++ DSP library
author | cannam |
---|---|
date | Wed, 05 Apr 2006 17:35:59 +0000 |
parents | |
children | 7fe29d8a7eaf |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d7116e3183f8 |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | |
2 | |
3 /* | |
4 QM DSP Library | |
5 | |
6 Centre for Digital Music, Queen Mary, University of London. | |
7 This file copyright 2005-2006 Christian Landone. | |
8 All rights reserved. | |
9 */ | |
10 | |
11 #include "PhaseVocoder.h" | |
12 #include "dsp/transforms/FFT.h" | |
13 #include <math.h> | |
14 | |
15 ////////////////////////////////////////////////////////////////////// | |
16 // Construction/Destruction | |
17 ////////////////////////////////////////////////////////////////////// | |
18 | |
19 PhaseVocoder::PhaseVocoder() | |
20 { | |
21 | |
22 } | |
23 | |
24 PhaseVocoder::~PhaseVocoder() | |
25 { | |
26 | |
27 } | |
28 | |
29 void PhaseVocoder::FFTShift(unsigned int size, double *src) | |
30 { | |
31 // IN-place Rotation of FFT arrays | |
32 unsigned int i; | |
33 | |
34 shiftBuffer = new double[size/2]; | |
35 | |
36 for( i = 0; i < size/2; i++) | |
37 { | |
38 shiftBuffer[ i ] = src[ i ]; | |
39 src[ i ] = src[ i + size/2]; | |
40 } | |
41 | |
42 for( i =size/2; i < size; i++) | |
43 { | |
44 src[ i ] = shiftBuffer[ i -(size/2)]; | |
45 } | |
46 | |
47 delete [] shiftBuffer; | |
48 | |
49 } | |
50 | |
51 void PhaseVocoder::process(unsigned int size, double *src, double *mag, double *theta) | |
52 { | |
53 | |
54 // Primary Interface to Phase Vocoder | |
55 realOut = new double[ size ]; | |
56 imagOut = new double[ size ]; | |
57 | |
58 FFTShift( size, src); | |
59 | |
60 coreFFT( size, src, 0, realOut, imagOut); | |
61 | |
62 getMagnitude( size/2, mag, realOut, imagOut); | |
63 getPhase( size/2, theta, realOut, imagOut); | |
64 | |
65 delete [] realOut; | |
66 delete [] imagOut; | |
67 } | |
68 | |
69 | |
70 void PhaseVocoder::coreFFT( unsigned int NumSamples, double *RealIn, double* ImagIn, double *RealOut, double *ImagOut) | |
71 { | |
72 // This function is taken from a standard freeware implementation defined in FFT.h | |
73 // TODO: Use FFTW | |
74 FFT::process( NumSamples,0, RealIn, ImagIn, RealOut, ImagOut ); | |
75 } | |
76 | |
77 void PhaseVocoder::getMagnitude(unsigned int size, double *mag, double *real, double *imag) | |
78 { | |
79 unsigned int j; | |
80 | |
81 for( j = 0; j < size; j++) | |
82 { | |
83 mag[ j ] = sqrt( real[ j ] * real[ j ] + imag[ j ] * imag[ j ]); | |
84 } | |
85 } | |
86 | |
87 void PhaseVocoder::getPhase(unsigned int size, double *theta, double *real, double *imag) | |
88 { | |
89 unsigned int k; | |
90 | |
91 // Phase Angle "matlab" style | |
92 //Watch out for quadrant mapping !!! | |
93 for( k = 0; k < size; k++) | |
94 { | |
95 theta[ k ] = atan2( -imag[ k ], real[ k ]); | |
96 } | |
97 } |