| 1 |
/* LPC_to_Spectrum.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1994-2019 David Weenink |
| 4 |
* |
| 5 |
* This code is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
/* |
| 20 |
djmw 20020529 GPL header |
| 21 |
djmw 20020529 Changed NUMrealft to NUMforwardRealFastFourierTransform |
| 22 |
djmw 20030708 Added NUM2.h |
| 23 |
djmw 20080122 float -> double |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "LPC_to_Spectrum.h" |
| 27 |
#include "NUM2.h" |
| 28 |
|
| 29 |
/* |
| 30 |
PSD(f) = (sigma^2 T) /|1 + Sum (k=1..p, a [k] exp(-2 pi i f k T))|^2, |
| 31 |
where sigma^2 == gain, T is samplinginterval |
| 32 |
|
| 33 |
LPC-spectrum is approximately 20 dB too high (w.r.t. 25 ms spectrum from Sound) |
| 34 |
*/ |
| 35 |
|
| 36 |
void LPC_Frame_into_Spectrum (LPC_Frame me, Spectrum thee, double bandwidthReduction, double deEmphasisFrequency) { |
| 37 |
Melder_assert (my nCoefficients == my a.size); // check invariant |
| 38 |
if (my nCoefficients == 0) { |
| 39 |
thy z.all() <<= 0.0; |
| 40 |
return; |
| 41 |
} |
| 42 |
/* |
| 43 |
When deEmphasisFrequency is effective we need 1 extra position in the fftbuffer. |
| 44 |
*/ |
| 45 |
const integer nfft = 2 * (thy nx - 1); |
| 46 |
integer ndata = my nCoefficients + 1; |
| 47 |
double scale = 1.0 / sqrt (2.0 * thy xmax * thy dx); |
| 48 |
if (ndata >= nfft - 1 && (deEmphasisFrequency < thy xmax || ndata > nfft)) |
| 49 |
Melder_throw (U"Spectrum size not large enough."); |
| 50 |
|
| 51 |
autoVEC fftbuffer = zero_VEC (nfft); |
| 52 |
/* |
| 53 |
Copy 1.0, a [1], ... a [p] into fftbuffer |
| 54 |
*/ |
| 55 |
fftbuffer [1] = 1.0; |
| 56 |
fftbuffer.part (2, ndata) <<= my a.get(); |
| 57 |
|
| 58 |
if (deEmphasisFrequency < thy xmax) { |
| 59 |
/* |
| 60 |
Multiply (1, a [1] z^-1, ... a [p] z^-p) by (1 - b z^-1) |
| 61 |
*/ |
| 62 |
const double b = exp (- NUM2pi * deEmphasisFrequency / thy xmax); |
| 63 |
ndata ++; |
| 64 |
for (integer i = ndata; i > 1; i--) |
| 65 |
fftbuffer [i] -= b * fftbuffer [i - 1]; |
| 66 |
} |
| 67 |
/* |
| 68 |
Calculate sum (k=0..ndata; a [k] (z)^-k) along a contour with radius r: |
| 69 |
sum (k=0..ndata; a [k] (rz)^-k) = sum (k=0..ndata; (a [k]r^-k) z^-k) |
| 70 |
*/ |
| 71 |
const double g = exp (NUMpi * bandwidthReduction / (thy dx * nfft)); /* r = 1/g */ |
| 72 |
for (integer i = 2; i <= ndata; i ++) |
| 73 |
fftbuffer [i] *= pow (g, i - 1); |
| 74 |
/* |
| 75 |
Perform the fft. |
| 76 |
The LPC spectrum is obtained by inverting this spectrum. |
| 77 |
The imaginary parts of the frequencies 0 and Nyquist are 0. |
| 78 |
*/ |
| 79 |
NUMforwardRealFastFourierTransform (fftbuffer.get()); |
| 80 |
if (my gain > 0.0) |
| 81 |
scale *= sqrt (my gain); |
| 82 |
thy z [1] [1] = scale / fftbuffer [1]; |
| 83 |
thy z [2] [1] = 0.0; |
| 84 |
for (integer i = 2; i <= nfft / 2; i ++) { |
| 85 |
/* |
| 86 |
We use: 1 / (a + ib) = (a - ib) / (a^2 + b^2) |
| 87 |
*/ |
| 88 |
const double re = fftbuffer [i + i - 1], im = fftbuffer [i + i]; |
| 89 |
const double invSquared = scale / (re * re + im * im); |
| 90 |
thy z [1] [i] = re * invSquared; |
| 91 |
thy z [2] [i] = -im * invSquared; |
| 92 |
} |
| 93 |
thy z [1] [thy nx] = scale / fftbuffer [2]; |
| 94 |
thy z [2] [thy nx] = 0.0; |
| 95 |
} |
| 96 |
|
| 97 |
autoSpectrum LPC_to_Spectrum (LPC me, double t, double dfMin, double bandwidthReduction, double deEmphasisFrequency) { |
| 98 |
try { |
| 99 |
Melder_assert (my samplingPeriod > 0.0); |
| 100 |
const double samplingFrequency = 1.0 / my samplingPeriod; |
| 101 |
Melder_assert (my nx >= 1); // preparing for the assertion in Melder_clipped |
| 102 |
const integer index = Melder_clipped (1_integer, Sampled_xToNearestIndex (me, t), my nx); |
| 103 |
integer nfft = 2; |
| 104 |
if (dfMin <= 0.0) { |
| 105 |
nfft = 512; |
| 106 |
dfMin = samplingFrequency / nfft; |
| 107 |
} |
| 108 |
while (samplingFrequency / nfft > dfMin || nfft <= my d_frames [index].nCoefficients) |
| 109 |
nfft *= 2; |
| 110 |
|
| 111 |
autoSpectrum thee = Spectrum_create (samplingFrequency / 2.0, nfft / 2 + 1); |
| 112 |
LPC_Frame_into_Spectrum (& my d_frames [index], thee.get(), bandwidthReduction, deEmphasisFrequency); |
| 113 |
return thee; |
| 114 |
} catch (MelderError) { |
| 115 |
Melder_throw (me, U": no Spectrum created."); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
/* End of file LPC_to_Spectrum.cpp */ |