1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
6 Centre for Digital Music, Queen Mary, University of London.
7 This file 2005-2006 Christian Landone, copyright 2013 QMUL.
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 "maths/MathUtilities.h"
27 PhaseVocoder::PhaseVocoder(int n, int hop) :
31 m_fft = new FFTReal(m_n);
32 m_time = new double[m_n];
33 m_real = new double[m_n];
34 m_imag = new double[m_n];
35 m_phase = new double[m_n/2 + 1];
36 m_unwrapped = new double[m_n/2 + 1];
38 for (int i = 0; i < m_n/2 + 1; ++i) {
46 PhaseVocoder::~PhaseVocoder()
56 void PhaseVocoder::FFTShift(double *src)
59 for (int i = 0; i < hs; ++i) {
66 void PhaseVocoder::processTimeDomain(const double *src,
67 double *mag, double *theta,
70 for (int i = 0; i < m_n; ++i) {
74 m_fft->forward(m_time, m_real, m_imag);
77 unwrapPhases(theta, unwrapped);
80 void PhaseVocoder::processFrequencyDomain(const double *reals,
82 double *mag, double *theta,
85 for (int i = 0; i < m_n/2 + 1; ++i) {
91 unwrapPhases(theta, unwrapped);
94 void PhaseVocoder::reset()
96 for (int i = 0; i < m_n/2 + 1; ++i) {
97 // m_phase stores the "previous" phase, so set to one step
98 // behind so that a signal with initial phase at zero matches
99 // the expected values. This is completely unnecessary for any
100 // analytical purpose, it's just tidier.
101 double omega = (2 * M_PI * m_hop * i) / m_n;
103 m_unwrapped[i] = -omega;
107 void PhaseVocoder::getMagnitudes(double *mag)
109 for (int i = 0; i < m_n/2 + 1; i++) {
110 mag[i] = sqrt(m_real[i] * m_real[i] + m_imag[i] * m_imag[i]);
114 void PhaseVocoder::getPhases(double *theta)
116 for (int i = 0; i < m_n/2 + 1; i++) {
117 theta[i] = atan2(m_imag[i], m_real[i]);
121 void PhaseVocoder::unwrapPhases(double *theta, double *unwrapped)
123 for (int i = 0; i < m_n/2 + 1; ++i) {
125 double omega = (2 * M_PI * m_hop * i) / m_n;
126 double expected = m_phase[i] + omega;
127 double error = MathUtilities::princarg(theta[i] - expected);
129 unwrapped[i] = m_unwrapped[i] + omega + error;
131 m_phase[i] = theta[i];
132 m_unwrapped[i] = unwrapped[i];