| 1 |
/* Sound_and_Cepstrum.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 20020516 GPL header |
| 21 |
djmw 20020529 changed NUMrealft to NUM...RealFastFourierTransform_f |
| 22 |
djmw 20041124 Changed call to Sound_to_Spectrum. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include "Sound_and_Cepstrum.h" |
| 26 |
#include "Sound_and_Spectrum.h" |
| 27 |
#include "Cepstrum_and_Spectrum.h" |
| 28 |
#include "NUM2.h" |
| 29 |
|
| 30 |
/* |
| 31 |
Algorithm: |
| 32 |
J.B. Bednar & T.L. Watt (1985), Calculating the Complex Cepstrum |
| 33 |
without Phase Unwrapping or Integration, IEEE Trans. on ASSP 33, |
| 34 |
1014-1017 (Does not work yet). |
| 35 |
*/ |
| 36 |
autoCepstrum Sound_to_Cepstrum_bw (Sound me) { |
| 37 |
try { |
| 38 |
const integer nfft = Melder_clippedLeft (2_integer, Melder_iroundUpToPowerOfTwo (my nx)); // TODO: explain edge case |
| 39 |
|
| 40 |
const double qmax = (my xmax - my xmin) * nfft / my nx; |
| 41 |
autoCepstrum thee = Cepstrum_create (qmax, nfft); |
| 42 |
|
| 43 |
autoVEC x = raw_VEC (nfft); |
| 44 |
autoVEC nx = raw_VEC (nfft); |
| 45 |
|
| 46 |
for (integer i = 1; i <= my nx; i ++) { |
| 47 |
x [i] = my z [1] [i]; |
| 48 |
nx [i] = (i - 1) * x [i]; |
| 49 |
} |
| 50 |
/* |
| 51 |
Step 1: Fourier transform x(n) -> X(f) |
| 52 |
and n*x(n) -> NX(f) |
| 53 |
*/ |
| 54 |
NUMforwardRealFastFourierTransform (x.get()); |
| 55 |
NUMforwardRealFastFourierTransform (nx.get()); |
| 56 |
/* |
| 57 |
Step 2: Multiply {X^*(f) * NX(f)} / |X(f)|^2 |
| 58 |
Compute Avg (ln |X(f)|) as Avg (ln |X(f)|^2) / 2. |
| 59 |
Treat i=1 separately: x [1] * nx [1] / |x [1]|^2 |
| 60 |
*/ |
| 61 |
double lnxa = 0.0; |
| 62 |
if (x [1] != 0.0) { |
| 63 |
lnxa = 2.0 * log (fabs (x [1])); |
| 64 |
x [1] = nx [1] / x [1]; |
| 65 |
} |
| 66 |
if (x [2] != 0.0) { |
| 67 |
lnxa = 2.0 * log (fabs (x [2])); |
| 68 |
x [2] = nx [2] / x [2]; |
| 69 |
} |
| 70 |
|
| 71 |
for (integer i = 3; i < nfft; i += 2) { |
| 72 |
const double xr = x [i], nxr = nx [i]; |
| 73 |
const double xi = x [i + 1], nxi = nx [i + 1]; |
| 74 |
const double xa = xr * xr + xi * xi; |
| 75 |
if (xa > 0.0) { |
| 76 |
x [i] = (xr * nxr + xi * nxi) / xa; |
| 77 |
x [i + 1] = (xr * nxi - xi * nxr) / xa; |
| 78 |
lnxa += log (xa); |
| 79 |
} else { |
| 80 |
x [i] = x [i + 1] = 0.0; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
lnxa /= 2.0 * nfft / 2.0; // TODO |
| 85 |
/* |
| 86 |
Step 4: Inverse transform of complex array x |
| 87 |
results in: n * xhat (n) |
| 88 |
*/ |
| 89 |
NUMreverseRealFastFourierTransform (x.get()); |
| 90 |
/* |
| 91 |
Step 5: Inverse fft-correction factor: 1/nfftd2 |
| 92 |
Divide n * xhat (n) by n |
| 93 |
*/ |
| 94 |
for (integer i = 2; i <= my nx; i++) |
| 95 |
thy z [1] [i] = x [i] / ( (i - 1) * nfft); |
| 96 |
/* |
| 97 |
Step 6: xhat [0] = Avg (ln |X(f)|) |
| 98 |
*/ |
| 99 |
thy z [1] [1] = lnxa; |
| 100 |
return thee; |
| 101 |
} catch (MelderError) { |
| 102 |
Melder_throw (me, U": no Cepstrum created."); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/* Zijn nog niet elkaars inverse!!!!*/ |
| 107 |
|
| 108 |
autoCepstrum Sound_to_Cepstrum (Sound me) { |
| 109 |
try { |
| 110 |
autoSpectrum spectrum = Sound_to_Spectrum (me, true); |
| 111 |
autoCepstrum thee = Spectrum_to_Cepstrum (spectrum.get()); |
| 112 |
return thee; |
| 113 |
} catch (MelderError) { |
| 114 |
Melder_throw (me, U": no Cepstrum calculated."); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
autoSound Cepstrum_to_Sound (Cepstrum me) { |
| 119 |
try { |
| 120 |
autoSpectrum sx = Cepstrum_to_Spectrum (me); |
| 121 |
autoSound thee = Spectrum_to_Sound (sx.get()); |
| 122 |
return thee; |
| 123 |
} catch (MelderError) { |
| 124 |
Melder_throw (me, U": no Sound calculated."); |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
/* End of file Sound_and_Cepstrum.cpp */ |