| 1 |
/* Cepstrum_and_Spectrum.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1994-2020 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 20020812 GPL header |
| 21 |
djmw 20041124 Changed call to Sound_to_Spectrum. |
| 22 |
djmw 20070103 Sound interface changes |
| 23 |
djmw 20080122 float -> double |
| 24 |
djmw 20121015 |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "Cepstrum_and_Spectrum.h" |
| 28 |
#include "NUM2.h" |
| 29 |
#include "Spectrum_extensions.h" |
| 30 |
#include "Sound_and_Spectrum.h" |
| 31 |
|
| 32 |
autoPowerCepstrum Spectrum_to_PowerCepstrum (Spectrum me) { |
| 33 |
try { |
| 34 |
autoSpectrum dBspectrum = Data_copy (me); |
| 35 |
const VEC re = dBspectrum -> z.row (1), im = dBspectrum -> z.row (2); |
| 36 |
for (integer i = 1; i <= dBspectrum -> nx; i ++) { |
| 37 |
re [i] = log (re [i] * re [i] + im [i] * im [i] + 1e-300); |
| 38 |
im [i] = 0.0; |
| 39 |
} |
| 40 |
autoSound cepstrum = Spectrum_to_Sound (dBspectrum.get()); |
| 41 |
autoPowerCepstrum thee = PowerCepstrum_create (0.5 / my dx, my nx); |
| 42 |
for (integer i = 1; i <= thy nx; i ++) { |
| 43 |
const double val = cepstrum -> z [1] [i]; |
| 44 |
thy z [1] [i] = val * val; |
| 45 |
} |
| 46 |
return thee; |
| 47 |
} catch (MelderError) { |
| 48 |
Melder_throw (me, U": not converted to Sound."); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
autoSpectrum PowerCepstrum_to_Spectrum (PowerCepstrum me, bool randomPhases) { |
| 53 |
try { |
| 54 |
autoSound him = Sound_createSimple (1_integer, my xmax, 1.0 / my dx); |
| 55 |
for (integer i = 1; i <= my nx; i ++) |
| 56 |
his z [1] [i] = sqrt (my z [1] [i]); |
| 57 |
const double mean = NUMmean (his z.row(1)); |
| 58 |
his z.row(1) -= mean; |
| 59 |
autoSpectrum thee = Sound_to_Spectrum (him.get(), true); |
| 60 |
for (integer i = 1; i <= thy nx; i ++) { |
| 61 |
thy z [1] [i] = exp (0.5 * thy z [1] [i] / thy dx); |
| 62 |
thy z [2] [i] = 0.0; |
| 63 |
} |
| 64 |
if (randomPhases) |
| 65 |
for (integer i = 1; i <= thy nx; i ++) { |
| 66 |
double phase = NUMrandomUniform (0, NUMpi); |
| 67 |
thy z [2] [i] = thy z [1] [i] * sin (phase); |
| 68 |
thy z [1] [i] *= cos (phase); |
| 69 |
} |
| 70 |
return thee; |
| 71 |
} catch (MelderError) { |
| 72 |
Melder_throw (me, U": not converted to Spectrum."); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
autoCepstrum Spectrum_to_Cepstrum (Spectrum me) { |
| 77 |
try { |
| 78 |
autoSpectrum dBspectrum = Data_copy (me); |
| 79 |
const VEC re = dBspectrum -> z.row (1), im = dBspectrum -> z.row (2); |
| 80 |
for (integer i = 1; i <= dBspectrum -> nx; i ++) { |
| 81 |
re [i] = log (re [i] * re [i] + im [i] * im [i] + 1e-300); |
| 82 |
im [i] = 0.0; |
| 83 |
} |
| 84 |
autoSound cepstrum = Spectrum_to_Sound (dBspectrum.get()); |
| 85 |
autoCepstrum thee = Cepstrum_create (0.5 / my dx, my nx); |
| 86 |
thy z.row (1) <<= cepstrum -> z.row (1).part (1, thy nx); |
| 87 |
return thee; |
| 88 |
} catch (MelderError) { |
| 89 |
Melder_throw (me, U": not converted to Sound."); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
autoSpectrum Cepstrum_to_Spectrum (Cepstrum me) { //TODO power cepstrum |
| 94 |
try { |
| 95 |
autoCepstrum cepstrum = Data_copy (me); |
| 96 |
cepstrum -> z [1] [1] = my z [1] [1]; |
| 97 |
for (integer i = 2; i <= cepstrum -> nx; i ++) |
| 98 |
cepstrum -> z [1] [i] = 2.0 * my z [1] [i]; |
| 99 |
autoSpectrum thee = Sound_to_Spectrum ((Sound) cepstrum.get(), true); |
| 100 |
|
| 101 |
const VEC re = thy z.row (1), im = thy z.row (2); |
| 102 |
for (integer i = 1; i <= thy nx; i ++) { |
| 103 |
re [i] = exp (0.5 * re [i]); // i.e., sqrt (exp(re [i])) |
| 104 |
im [i] = 0.0; |
| 105 |
} |
| 106 |
return thee; |
| 107 |
} catch (MelderError) { |
| 108 |
Melder_throw (me, U": no Spectrum created."); |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
autoCepstrum Spectrum_to_Cepstrum_hillenbrand (Spectrum me) { |
| 113 |
try { |
| 114 |
// originalNumberOfSamplesProbablyOdd irrelevant |
| 115 |
Melder_require (my x1 == 0.0, |
| 116 |
U"A Fourier-transformable Spectrum should have a first frequency of 0 Hz, not ", my x1, U" Hz."); |
| 117 |
const integer numberOfSamples = my nx - 1; |
| 118 |
autoCepstrum thee = Cepstrum_create (0.5 / my dx, my nx); |
| 119 |
autoNUMFourierTable fftTable = NUMFourierTable_create (my nx); |
| 120 |
autoVEC amp = raw_VEC (my nx); |
| 121 |
for (integer i = 1; i <= my nx; i ++) |
| 122 |
amp [i] = my v_getValueAtSample (i, 0, 2); |
| 123 |
NUMfft_forward (fftTable.get(), amp.get()); |
| 124 |
|
| 125 |
for (integer i = 1; i <= my nx; i ++) { |
| 126 |
const double val = amp [i] / numberOfSamples;// scaling 1/n because ifft(fft(1))= n; |
| 127 |
thy z [1] [i] = val * val; // power cepstrum |
| 128 |
} |
| 129 |
return thee; |
| 130 |
} catch (MelderError) { |
| 131 |
Melder_throw (me, U": not converted to Sound."); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
/* End of file Cepstrum_and_Spectrum.cpp */ |