| 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"cxx . . $ `<file>/LPC/Sound_and_Cepstrum.h`/ |
| 26 | #include "Sound_and_Spectrum.h"cxx . . $ `<file>/fon/Sound_and_Spectrum.h`/ |
| 27 | #include "Cepstrum_and_Spectrum.h"cxx . . $ `<file>/LPC/Cepstrum_and_Spectrum.h`/ |
| 28 | #include "NUM2.h"cxx . . $ `<file>/dwsys/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 | autoCepstrumcxx . . $ autoCepstrum# Sound_to_Cepstrum_bwcxx . . $ Sound_to_Cepstrum_bw(e9fe026e24be0430). (Soundcxx . . $ Sound# me) { |
| 37 | try { |
| 38 | const integercxx . . $ integer# nfft = Melder_clippedLeftcxx . . $ Melder_clippedLeft(f80f7d6260b3ee3a). (2_integercxx . . $ `operator""_integer`(85240eb457c3721a)., Melder_iroundUpToPowerOfTwocxx . . $ Melder_iroundUpToPowerOfTwo(8ce0d1b217db7fd1). (mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.)); // TODO: explain edge case |
| 39 | |
| 40 | const double qmax = (mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. - mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin.) * nfft / mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; |
| 41 | autoCepstrumcxx . . $ autoCepstrum# thee = Cepstrum_createcxx . . $ Cepstrum_create(2cbed51e64e74b54). (qmax, nfft); |
| 42 | |
| 43 | autoVECcxx . . $ autoVEC# x = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (nfft); |
| 44 | autoVECcxx . . $ autoVEC# nx = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (nfft); |
| 45 | |
| 46 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; i ++) { |
| 47 | x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i]; |
| 48 | nx [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = (i - 1) * x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 49 | } |
| 50 | /* |
| 51 | Step 1: Fourier transform x(n) -> X(f) |
| 52 | and n*x(n) -> NX(f) |
| 53 | */ |
| 54 | NUMforwardRealFastFourierTransformcxx . . $ NUMforwardRealFastFourierTransform(33ae648fa537298c). (x.getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 55 | NUMforwardRealFastFourierTransformcxx . . $ NUMforwardRealFastFourierTransform(33ae648fa537298c). (nx.getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 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 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] != 0.0) { |
| 63 | lnxa = 2.0 * logcxx . . $ log(fee4f1d22bc8e617). (fabscxx . . $ fabs(fee4f1d22bc8e617). (x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1])); |
| 64 | x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] = nx [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] / x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]; |
| 65 | } |
| 66 | if (x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] != 0.0) { |
| 67 | lnxa = 2.0 * logcxx . . $ log(fee4f1d22bc8e617). (fabscxx . . $ fabs(fee4f1d22bc8e617). (x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2])); |
| 68 | x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] = nx [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] / x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2]; |
| 69 | } |
| 70 | |
| 71 | for (integercxx . . $ integer# i = 3; i < nfft; i += 2) { |
| 72 | const double xr = x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i], nxr = nx [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 73 | const double xi = x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i + 1], nxi = nx [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i + 1]; |
| 74 | const double xa = xr * xr + xi * xi; |
| 75 | if (xa > 0.0) { |
| 76 | x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = (xr * nxr + xi * nxi) / xa; |
| 77 | x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i + 1] = (xr * nxi - xi * nxr) / xa; |
| 78 | lnxa += logcxx . . $ log(fee4f1d22bc8e617). (xa); |
| 79 | } else { |
| 80 | x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).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 | NUMreverseRealFastFourierTransformcxx . . $ NUMreverseRealFastFourierTransform(33ae648fa537298c). (x.getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 90 | /* |
| 91 | Step 5: Inverse fft-correction factor: 1/nfftd2 |
| 92 | Divide n * xhat (n) by n |
| 93 | */ |
| 94 | for (integercxx . . $ integer# i = 2; i <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; i++) |
| 95 | thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] / ( (i - 1) * nfft); |
| 96 | /* |
| 97 | Step 6: xhat [0] = Avg (ln |X(f)|) |
| 98 | */ |
| 99 | thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = lnxa; |
| 100 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 101 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 102 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no Cepstrum created."); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /* Zijn nog niet elkaars inverse!!!!*/ |
| 107 | |
| 108 | autoCepstrumcxx . . $ autoCepstrum# Sound_to_Cepstrumcxx . . $ Sound_to_Cepstrum(e9fe026e24be0430). (Soundcxx . . $ Sound# me) { |
| 109 | try { |
| 110 | autoSpectrumcxx . . $ autoSpectrum# spectrum = Sound_to_Spectrumcxx . . $ Sound_to_Spectrum(e96f4d575d8eeb8c). (me, true); |
| 111 | autoCepstrumcxx . . $ autoCepstrum# thee = Spectrum_to_Cepstrumcxx . . $ Spectrum_to_Cepstrum(169ebe48529da5fe). (spectrum.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 112 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 113 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 114 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no Cepstrum calculated."); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | autoSoundcxx . . $ autoSound# Cepstrum_to_Soundcxx . . $ Cepstrum_to_Sound(9871125382097136). (Cepstrumcxx . . $ Cepstrum# me) { |
| 119 | try { |
| 120 | autoSpectrumcxx . . $ autoSpectrum# sx = Cepstrum_to_Spectrumcxx . . $ Cepstrum_to_Spectrum(66251bf87992ca02). (me); |
| 121 | autoSoundcxx . . $ autoSound# thee = Spectrum_to_Soundcxx . . $ Spectrum_to_Sound(b9577246d0f6b8d7). (sx.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 122 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 123 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 124 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no Sound calculated."); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | /* End of file Sound_and_Cepstrum.cpp */ |