Debian Codegraph @

LPC/Cepstrum_and_Spectrum.cpp

raw symbols SCIP
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"cxx . . $ `<file>/LPC/Cepstrum_and_Spectrum.h`/
28 #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/
29 #include "Spectrum_extensions.h"cxx . . $ `<file>/dwtools/Spectrum_extensions.h`/
30 #include "Sound_and_Spectrum.h"cxx . . $ `<file>/fon/Sound_and_Spectrum.h`/
31
32 autoPowerCepstrumcxx . . $ autoPowerCepstrum# Spectrum_to_PowerCepstrumcxx . . $ Spectrum_to_PowerCepstrum(8c5431a838c59772). (Spectrumcxx . . $ Spectrum# me) {
33 try {
34 autoSpectrumcxx . . $ autoSpectrum# dBspectrum = Data_copycxx . . $ Data_copy(97a542a26794c80e). (me);
35 const VECcxx . . $ VEC# re = dBspectrum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1), im = dBspectrum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (2);
36 for (integercxx . . $ integer# i = 1; i <= dBspectrum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). nxcxx . . $ structSampled#nx.; i ++) {
37 re [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = logcxx . . $ log(fee4f1d22bc8e617). (re [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * re [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] + im [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * im [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] + 1e-300);
38 im [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = 0.0;
39 }
40 autoSoundcxx . . $ autoSound# cepstrum = Spectrum_to_Soundcxx . . $ Spectrum_to_Sound(b9577246d0f6b8d7). (dBspectrum.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).());
41 autoPowerCepstrumcxx . . $ autoPowerCepstrum# thee = PowerCepstrum_createcxx . . $ PowerCepstrum_create(65f6cc037cbc1c8a). (0.5 / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.);
42 for (integercxx . . $ integer# i = 1; i <= thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.; i ++) {
43 const double val = cepstrum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i];
44 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = val * val;
45 }
46 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
47 } catch (MelderErrorcxx . . $ MelderError#) {
48 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to Sound.");
49 }
50 }
51
52 autoSpectrumcxx . . $ autoSpectrum# PowerCepstrum_to_Spectrumcxx . . $ PowerCepstrum_to_Spectrum(a6b89be38024dc67). (PowerCepstrumcxx . . $ PowerCepstrum# me, bool randomPhases) {
53 try {
54 autoSoundcxx . . $ autoSound# him = Sound_createSimplecxx . . $ Sound_createSimple(fcb03a673f737ce). (1_integercxx . . $ `operator""_integer`(85240eb457c3721a)., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., 1.0 / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.);
55 for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; i ++)
56 hiscxx . . $ `melder/melder.h:47:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i]);
57 const double mean = NUMmeancxx . . $ NUMmean(4bb9c3e3a8dd5442). (hiscxx . . $ `melder/melder.h:47:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666).(1));
58 hiscxx . . $ `melder/melder.h:47:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666).(1) -=cxx . . $ `operator-=`(c555d170d72ff256). mean;
59 autoSpectrumcxx . . $ autoSpectrum# thee = Sound_to_Spectrumcxx . . $ Sound_to_Spectrum(e96f4d575d8eeb8c). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), true);
60 for (integercxx . . $ integer# i = 1; i <= thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.; i ++) {
61 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = expcxx . . $ exp(fee4f1d22bc8e617). (0.5 * thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] / thycxx . . $ `melder/melder.h:45:9`! dxcxx . . $ structSampled#dx.);
62 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = 0.0;
63 }
64 if (randomPhases)
65 for (integercxx . . $ integer# i = 1; i <= thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.; i ++) {
66 double phase = NUMrandomUniformcxx . . $ NUMrandomUniform(d8e7e0733fa41c87). (0, NUMpicxx . . $ NUMpi.);
67 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * sincxx . . $ sin(fee4f1d22bc8e617). (phase);
68 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] *= coscxx . . $ cos(fee4f1d22bc8e617). (phase);
69 }
70 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
71 } catch (MelderErrorcxx . . $ MelderError#) {
72 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to Spectrum.");
73 }
74 }
75
76 autoCepstrumcxx . . $ autoCepstrum# Spectrum_to_Cepstrumcxx . . $ Spectrum_to_Cepstrum(169ebe48529da5fe). (Spectrumcxx . . $ Spectrum# me) {
77 try {
78 autoSpectrumcxx . . $ autoSpectrum# dBspectrum = Data_copycxx . . $ Data_copy(97a542a26794c80e). (me);
79 const VECcxx . . $ VEC# re = dBspectrum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1), im = dBspectrum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (2);
80 for (integercxx . . $ integer# i = 1; i <= dBspectrum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). nxcxx . . $ structSampled#nx.; i ++) {
81 re [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = logcxx . . $ log(fee4f1d22bc8e617). (re [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * re [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] + im [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * im [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] + 1e-300);
82 im [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = 0.0;
83 }
84 autoSoundcxx . . $ autoSound# cepstrum = Spectrum_to_Soundcxx . . $ Spectrum_to_Sound(b9577246d0f6b8d7). (dBspectrum.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).());
85 autoCepstrumcxx . . $ autoCepstrum# thee = Cepstrum_createcxx . . $ Cepstrum_create(2cbed51e64e74b54). (0.5 / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.);
86 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1) <<=cxx . . $ `operator<<=`(adea042513329390). cepstrumcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1).partcxx . . $ vector#part(ae267db9e2c04be7). (1, thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.);
87 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
88 } catch (MelderErrorcxx . . $ MelderError#) {
89 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to Sound.");
90 }
91 }
92
93 autoSpectrumcxx . . $ autoSpectrum# Cepstrum_to_Spectrumcxx . . $ Cepstrum_to_Spectrum(66251bf87992ca02). (Cepstrumcxx . . $ Cepstrum# me) { //TODO power cepstrum
94 try {
95 autoCepstrumcxx . . $ autoCepstrum# cepstrum = Data_copycxx . . $ Data_copy(97a542a26794c80e). (me);
96 cepstrum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1];
97 for (integercxx . . $ integer# i = 2; i <= cepstrum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). nxcxx . . $ structSampled#nx.; i ++)
98 cepstrum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = 2.0 * mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i];
99 autoSpectrumcxx . . $ autoSpectrum# thee = Sound_to_Spectrumcxx . . $ Sound_to_Spectrum(e96f4d575d8eeb8c). ((Soundcxx . . $ Sound#) cepstrum.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), true);
100
101 const VECcxx . . $ VEC# re = thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1), im = thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (2);
102 for (integercxx . . $ integer# i = 1; i <= thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.; i ++) {
103 re [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = expcxx . . $ exp(fee4f1d22bc8e617). (0.5 * re [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i]); // i.e., sqrt (exp(re [i]))
104 im [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = 0.0;
105 }
106 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
107 } catch (MelderErrorcxx . . $ MelderError#) {
108 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no Spectrum created.");
109 }
110 }
111
112 autoCepstrumcxx . . $ autoCepstrum# Spectrum_to_Cepstrum_hillenbrandcxx . . $ Spectrum_to_Cepstrum_hillenbrand(169ebe48529da5fe). (Spectrumcxx . . $ Spectrum# me) {
113 try {
114 // originalNumberOfSamplesProbablyOdd irrelevant
115 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! x1 == 0.0,
116 U"A Fourier-transformable Spectrum should have a first frequency of 0 Hz, not ", mycxx . . $ `melder/melder.h:44:9`! x1, U" Hz.");
117 const integercxx . . $ integer# numberOfSamples = mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. - 1;
118 autoCepstrumcxx . . $ autoCepstrum# thee = Cepstrum_createcxx . . $ Cepstrum_create(2cbed51e64e74b54). (0.5 / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.);
119 autoNUMFourierTablecxx . . $ autoNUMFourierTable# fftTable = NUMFourierTable_createcxx . . $ NUMFourierTable_create(10012c71e147318e). (mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.);
120 autoVECcxx . . $ autoVEC# amp = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.);
121 for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; i ++)
122 amp [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = mycxx . . $ `melder/melder.h:44:9`! v_getValueAtSamplecxx . . $ structSpectrum#v_getValueAtSample(8bbfc6ce471470c9). (i, 0, 2);
123 NUMfft_forwardcxx . . $ NUMfft_forward(aea41fd94d07eee). (fftTable.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), amp.getcxx . . $ autovector#get(e8d1da7fe68a6585).());
124
125 for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; i ++) {
126 const double val = amp [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] / numberOfSamples;// scaling 1/n because ifft(fft(1))= n;
127 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = val * val; // power cepstrum
128 }
129 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
130 } catch (MelderErrorcxx . . $ MelderError#) {
131 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to Sound.");
132 }
133 }
134
135 /* End of file Cepstrum_and_Spectrum.cpp */