annotate dsp/phasevocoder/PhaseVocoder.cpp @ 340:c99d83236f0d

Do actual phase unwrapping in the phase vocoder!
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 02 Oct 2013 15:05:34 +0100
parents d5014ab8b0e5
children 2020c73dc997
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@340 7 This file 2005-2006 Christian Landone, copyright 2013 QMUL.
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@340 18 #include "maths/MathUtilities.h"
c@225 19 #include <math.h>
c@225 20
c@340 21 #include <iostream>
c@340 22 using std::cerr;
c@340 23 using std::endl;
c@225 24
c@340 25 PhaseVocoder::PhaseVocoder(int n, int hop) :
c@340 26 m_n(n),
c@340 27 m_hop(hop)
c@225 28 {
c@289 29 m_fft = new FFTReal(m_n);
c@340 30 m_real = new double[m_n];
c@340 31 m_imag = new double[m_n];
c@340 32 m_phase = new double[m_n/2 + 1];
c@340 33 m_unwrapped = new double[m_n/2 + 1];
c@340 34
c@340 35 for (int i = 0; i < m_n/2 + 1; ++i) {
c@340 36 m_phase[i] = 0.0;
c@340 37 m_unwrapped[i] = 0.0;
c@340 38 }
c@340 39
c@340 40 reset();
c@225 41 }
c@225 42
c@225 43 PhaseVocoder::~PhaseVocoder()
c@225 44 {
c@340 45 delete [] m_unwrapped;
c@340 46 delete [] m_phase;
c@340 47 delete [] m_real;
c@340 48 delete [] m_imag;
c@289 49 delete m_fft;
c@225 50 }
c@225 51
c@340 52 void PhaseVocoder::FFTShift(double *src)
c@225 53 {
c@340 54 const int hs = m_n/2;
c@280 55 for (int i = 0; i < hs; ++i) {
c@280 56 double tmp = src[i];
c@280 57 src[i] = src[i + hs];
c@280 58 src[i + hs] = tmp;
c@225 59 }
c@225 60 }
c@225 61
c@340 62 void PhaseVocoder::process(double *src, double *mag, double *theta,
c@340 63 double *unwrapped)
c@225 64 {
c@340 65 FFTShift(src);
c@289 66
c@340 67 m_fft->forward(src, m_real, m_imag);
c@225 68
c@340 69 getMagnitudes(mag);
c@340 70 getPhases(theta);
c@340 71 unwrapPhases(theta, unwrapped);
c@225 72 }
c@225 73
c@340 74 void PhaseVocoder::reset()
c@340 75 {
c@340 76 for (int i = 0; i < m_n/2 + 1; ++i) {
c@340 77 // m_phase stores the "previous" phase, so set to one step
c@340 78 // behind so that a signal with initial phase at zero matches
c@340 79 // the expected values. This is completely unnecessary for any
c@340 80 // analytical purpose, it's just tidier.
c@340 81 double omega = (2 * M_PI * m_hop * i) / m_n;
c@340 82 m_phase[i] = -omega;
c@340 83 m_unwrapped[i] = -omega;
c@225 84 }
c@225 85 }
c@225 86
c@340 87 void PhaseVocoder::getMagnitudes(double *mag)
c@340 88 {
c@340 89 for (int i = 0; i < m_n/2 + 1; i++) {
c@340 90 mag[i] = sqrt(m_real[i] * m_real[i] + m_imag[i] * m_imag[i]);
c@340 91 }
c@340 92 }
c@340 93
c@340 94 void PhaseVocoder::getPhases(double *theta)
c@225 95 {
c@340 96 for (int i = 0; i < m_n/2 + 1; i++) {
c@340 97 theta[i] = atan2(m_imag[i], m_real[i]);
c@225 98 }
c@225 99 }
c@340 100
c@340 101 void PhaseVocoder::unwrapPhases(double *theta, double *unwrapped)
c@340 102 {
c@340 103 cerr << "PhaseVocoder::unwrapPhases" << endl;
c@340 104
c@340 105 for (int i = 0; i < m_n/2 + 1; ++i) {
c@340 106
c@340 107 double omega = (2 * M_PI * m_hop * i) / m_n;
c@340 108 double expected = m_phase[i] + omega;
c@340 109 double error = MathUtilities::princarg(theta[i] - expected);
c@340 110
c@340 111 unwrapped[i] = m_unwrapped[i] + omega + error;
c@340 112
c@340 113 cerr << "i = " << i << ", instantaneous phase = " << theta[i] << ", prev phase = " << m_phase[i] << ", omega = " << omega << ", expected = " << expected << ", error = " << error << ", unwrapped = " << unwrapped[i] << endl;
c@340 114
c@340 115 m_phase[i] = theta[i];
c@340 116 m_unwrapped[i] = unwrapped[i];
c@340 117 }
c@340 118 }
c@340 119