Debian Codegraph @

LPC/LPC_and_Tube.cpp

raw symbols SCIP
1 /* LPC_and_Tube.cpp
2 *
3 * Copyright (C) 1993-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 20020612 GPL header
21 djmw 20041020 struct Tube_Frame -> struct structTube_Frame; struct LPC_Frame -> struct structLPC_Frame;
22 struct Formant_Frame->struct structFormant_Frame
23 djmw 20051005 Always make a VocalTract with length 0.01 m when isundef(wakita_length).
24 */
25
26 #include "LPC_and_Tube.h"cxx . . $ `<file>/LPC/LPC_and_Tube.h`/
27 #include "LPC_and_Formant.h"cxx . . $ `<file>/LPC/LPC_and_Formant.h`/
28 #include "LPC_to_Spectrum.h"cxx . . $ `<file>/LPC/LPC_to_Spectrum.h`/
29 #include "SpectrumTier.h"cxx . . $ `<file>/fon/SpectrumTier.h`/
30 #include "VocalTract_to_Spectrum.h"cxx . . $ `<file>/fon/VocalTract_to_Spectrum.h`/
31 #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/
32
33 // IEEE: Programs fo digital signal processing section 4.3 LPTRN
34
35 void LPC_Frame_into_Tube_Frame_rccxx . . $ LPC_Frame_into_Tube_Frame_rc(17070d9ea1b892). (LPC_Framecxx . . $ LPC_Frame# me, Tube_Framecxx . . $ Tube_Frame# thee) {
36 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (mycxx . . $ `melder/melder.h:44:9`! nCoefficients == mycxx . . $ `melder/melder.h:44:9`! a.size); // check invariant
37 thycxx . . $ `melder/melder.h:45:9`! ccxx . . $ structTube_Frame#c..resizecxx . . $ autovector#resize(d91e89fbd57cd1). (mycxx . . $ `melder/melder.h:44:9`! nCoefficientscxx . . $ structLPC_Frame#nCoefficients.);
38 thycxx . . $ `melder/melder.h:45:9`! numberOfSegmentscxx . . $ structTube_Frame#numberOfSegments. = thycxx . . $ `melder/melder.h:45:9`! ccxx . . $ structTube_Frame#c..sizecxx . . $ autovector#size.; // maintain invariant
39 VECrc_from_lpccxx . . $ VECrc_from_lpc(9ec4162bb4ce9309). (thycxx . . $ `melder/melder.h:45:9`! ccxx . . $ structTube_Frame#c..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), mycxx . . $ `melder/melder.h:44:9`! acxx . . $ structLPC_Frame#a..getcxx . . $ autovector#get(e8d1da7fe68a6585).());
40 }
41
42 void LPC_Frame_into_Tube_Frame_areacxx . . $ LPC_Frame_into_Tube_Frame_area(17070d9ea1b892). (LPC_Framecxx . . $ LPC_Frame# me, Tube_Framecxx . . $ Tube_Frame# thee) {
43 VECarea_from_lpccxx . . $ VECarea_from_lpc(9ec4162bb4ce9309). (thycxx . . $ `melder/melder.h:45:9`! ccxx . . $ structTube_Frame#c..partcxx . . $ autovector#part(2d8d48654ca64e70). (1, mycxx . . $ `melder/melder.h:44:9`! nCoefficientscxx . . $ structLPC_Frame#nCoefficients.), mycxx . . $ `melder/melder.h:44:9`! acxx . . $ structLPC_Frame#a..partcxx . . $ autovector#part(2d8d48654ca64e70). (1, mycxx . . $ `melder/melder.h:44:9`! nCoefficientscxx . . $ structLPC_Frame#nCoefficients.));
44 }
45
46 double VocalTract_LPC_Frame_getMatchingLengthcxx . . $ VocalTract_LPC_Frame_getMatchingLength(9c05a7c6fd4ad9af). (VocalTractcxx . . $ VocalTract# me, LPC_Framecxx . . $ LPC_Frame# thee, double glottalDamping, bool radiationDamping, bool internalDamping) {
47 try {
48 /*
49 Match the average distance between the first two formants in the VocaTract and the LPC spectrum
50 */
51 constexpr integercxx . . $ integer# numberOfFrequencies = 1000;
52 const double maximumFrequency = 5000.0;
53 autoSpectrumcxx . . $ autoSpectrum# vts = VocalTract_to_Spectrumcxx . . $ VocalTract_to_Spectrum(bc47d1a1ea8004d6). (me, numberOfFrequencies, maximumFrequency, glottalDamping, radiationDamping, internalDamping);
54 const double samplingFrequency = 1000.0 * mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.;
55 autoSpectrumcxx . . $ autoSpectrum# lps = Spectrum_createcxx . . $ Spectrum_create(a338b9c326abb5ed). (0.5 * samplingFrequency, numberOfFrequencies);
56 LPC_Frame_into_Spectrumcxx . . $ LPC_Frame_into_Spectrum(a6f57fd19bde75a1). (thee, lps.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 0, 50);
57 autoSpectrumTiercxx . . $ autoSpectrumTier# vtst = Spectrum_to_SpectrumTier_peakscxx . . $ Spectrum_to_SpectrumTier_peaks(b46e03f03e7d3bf6). (vts.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).());
58 autoSpectrumTiercxx . . $ autoSpectrumTier# lpst = Spectrum_to_SpectrumTier_peakscxx . . $ Spectrum_to_SpectrumTier_peaks(b46e03f03e7d3bf6). (lps.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).());
59 double length = undefinedcxx . . $ undefined.;
60 if (vtst ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). pointscxx . . $ structRealTier#points..sizecxx . . $ CollectionOf#size. > 1 && lpst ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). pointscxx . . $ structRealTier#points..sizecxx . . $ CollectionOf#size. > 1) {
61 const double vt_f1 = vtst ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). pointscxx . . $ structRealTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).1] -> numbercxx . . $ structSimpleDouble#number., vt_f2 = vtst ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). pointscxx . . $ structRealTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).2] -> numbercxx . . $ structSimpleDouble#number.;
62 const double lp_f1 = lpst ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). pointscxx . . $ structRealTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).1] -> numbercxx . . $ structSimpleDouble#number., lp_f2 = lpst ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). pointscxx . . $ structRealTier#points..atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).2] -> numbercxx . . $ structSimpleDouble#number.;
63 const double df1 = lp_f1 - vt_f1, df2 = lp_f2 - vt_f2, df = 0.5 * (df1 + df2);
64 const double dl = - df / lp_f2;
65 length = mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx. * mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. * (1 + dl);
66 }
67 return length;
68 } catch (MelderErrorcxx . . $ MelderError#) {
69 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Length could not be determined from VocalTract and LPC_Frame.");
70 }
71 }
72
73 double LPC_Frame_getVTL_wakitacxx . . $ LPC_Frame_getVTL_wakita(71c2c15b6c996a67). (LPC_Framecxx . . $ LPC_Frame# me, double samplingPeriod, double refLength) {
74 struct structLPC_Framecxx . . $ structLPC_Frame# lpc_struct;
75 const LPC_Framecxx . . $ LPC_Frame# lpc = & lpc_struct;
76 struct structFormant_Framecxx . . $ structFormant_Frame# f_struct;
77 const Formant_Framecxx . . $ Formant_Frame# f = & f_struct;
78 struct structTube_Framecxx . . $ structTube_Frame# rc_struct, af_struct;
79 const Tube_Framecxx . . $ Tube_Frame# rc = & rc_struct, af = & af_struct;
80 try {
81 const integercxx . . $ integer# numberOfCoefficients = mycxx . . $ `melder/melder.h:44:9`! nCoefficientscxx . . $ structLPC_Frame#nCoefficients.;
82 const double dlength = 0.001;
83 double length, wakita_length = undefinedcxx . . $ undefined.;
84 double varMin = 1e308;
85
86 memset (& lpc_struct, 0, sizeof (lpc_struct));
87 memset (& f_struct, 0, sizeof (f_struct));
88 memset (& rc_struct, 0, sizeof (rc_struct));
89 memset (& af_struct, 0, sizeof (af_struct));
90
91 LPC_Frame_initcxx . . $ LPC_Frame_init(36b24d3ac8bf631d). (lpc, numberOfCoefficients);
92 Tube_Frame_initcxx . . $ Tube_Frame_init(8c3bc4dceaf08dbf). (rc, numberOfCoefficients, refLength);
93 Tube_Frame_initcxx . . $ Tube_Frame_init(8c3bc4dceaf08dbf). (af, numberOfCoefficients, refLength);
94 /*
95 Step 2
96 */
97 LPC_Frame_into_Formant_Framecxx . . $ LPC_Frame_into_Formant_Frame(dfd7bec558386286). (me, f, samplingPeriod, 0.0);
98 /*
99 LPC_Frame_into_Formant_Frame performs the Formant_Frame_init !!
100 */
101 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (f -> numberOfFormants > 0,
102 U"Not enough formants.");
103 VECcxx . . $ VEC# area = af -> ccxx . . $ structTube_Frame#c..getcxx . . $ autovector#get(e8d1da7fe68a6585).(); // TODO
104 double lmin = length = 0.10;
105 double plength = refLength;
106 while (length <= 0.25) {
107 /*
108 Step 3
109 */
110 const double fscale = plength / length;
111 for (integercxx . . $ integer# i = 1; i <= f -> numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants.; i ++) {
112 f -> formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. frequencycxx . . $ structFormant_Formant#frequency. *= fscale;
113 f -> formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. bandwidthcxx . . $ structFormant_Formant#bandwidth. *= fscale;
114 }
115 /*
116 20000125: Bovenstaande schaling van f1/b1 kan ook gedaan worden door
117 MGfb_to_a (f, b, nf, samplingFrequency*length/refLength, a1)
118 De berekening is extreem gevoelig voor de samplefrequentie: een zelfde
119 stel f,b waardes geven andere lengtes afhankelijk van Fs. Ook het
120 weglaten van een hogere formant heeft consekwenties.
121 De refLength zou eigenlijk vast moeten liggen op
122 refLength=c*aantalFormanten/Fs waarbij c=340 m/s (geluidssnelheid).
123 Bij Fs=10000 zou aantalFormanten=5 zijn en refLength -> 0.17 m
124 */
125 /*
126 Step 4
127 */
128 Formant_Frame_into_LPC_Framecxx . . $ Formant_Frame_into_LPC_Frame(95fe1d925a21a5f7). (f, lpc, samplingPeriod);
129 /*
130 Step 5
131 */
132 rc -> lengthcxx . . $ structTube_Frame#length. = length;
133 LPC_Frame_into_Tube_Frame_rccxx . . $ LPC_Frame_into_Tube_Frame_rc(17070d9ea1b892). (lpc, rc);
134 /*
135 Step 6.1
136 */
137 Tube_Frames_rc_into_areacxx . . $ Tube_Frames_rc_into_area(e5bdc26047605541). (rc, af);
138 /*
139 Step 6.2 Log(areas)
140 */
141 double logSum = 0.0;
142 for (integercxx . . $ integer# i = 1; i <= af -> numberOfSegmentscxx . . $ structTube_Frame#numberOfSegments.; i ++) {
143 area [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = logcxx . . $ log(fee4f1d22bc8e617). (area [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i]);
144 logSum += area [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i];
145 }
146 /*
147 Steps 6.3 and 7
148 */
149 double var = 0.0;
150 for (integercxx . . $ integer# i = 1; i <= af -> numberOfSegmentscxx . . $ structTube_Frame#numberOfSegments.; i ++) {
151 const double delta = area [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] - logSum / af -> numberOfSegmentscxx . . $ structTube_Frame#numberOfSegments.;
152 var += delta * delta;
153 }
154 if (var < varMin) {
155 lmin = length;
156 varMin = var;
157 }
158 plength = length;
159 length += dlength;
160 }
161
162 wakita_length = lmin;
163 f -> destroycxx . . $ structFormant_Frame#destroy(49f6e7a06ebc5aa8). ();
164 lpc -> destroycxx . . $ structLPC_Frame#destroy(49f6e7a06ebc5aa8). ();
165 rc -> destroycxx . . $ structTube_Frame#destroy(49f6e7a06ebc5aa8). ();
166 af -> destroycxx . . $ structTube_Frame#destroy(49f6e7a06ebc5aa8). ();
167 return wakita_length;
168 } catch (MelderErrorcxx . . $ MelderError#) {
169 f -> destroycxx . . $ structFormant_Frame#destroy(49f6e7a06ebc5aa8). ();
170 lpc -> destroycxx . . $ structLPC_Frame#destroy(49f6e7a06ebc5aa8). ();
171 rc -> destroycxx . . $ structTube_Frame#destroy(49f6e7a06ebc5aa8). ();
172 af -> destroycxx . . $ structTube_Frame#destroy(49f6e7a06ebc5aa8). ();
173 return undefinedcxx . . $ undefined.;
174 }
175 }
176
177 int Tube_Frame_into_LPC_Frame_areacxx . . $ Tube_Frame_into_LPC_Frame_area(70dc84e662fca4f6). (Tube_Framecxx . . $ Tube_Frame# me, LPC_Framecxx . . $ LPC_Frame# thee) {
178 (void) me;
179 (void) thee;
180 return 0;
181 }
182
183 int Tube_Frame_into_LPC_Frame_rccxx . . $ Tube_Frame_into_LPC_Frame_rc(70dc84e662fca4f6). (Tube_Framecxx . . $ Tube_Frame# me, LPC_Framecxx . . $ LPC_Frame# thee) {
184 (void) me;
185 (void) thee;
186 return 0;
187 }
188
189 void VocalTract_setLengthcxx . . $ VocalTract_setLength(2aaec95368b3ed64). (VocalTractcxx . . $ VocalTract# me, double newLength) {
190 mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. = newLength;
191 mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx. = newLength / mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.;
192 mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1. = 0.5 * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.;
193 }
194
195 autoVocalTractcxx . . $ autoVocalTract# LPC_to_VocalTract_slice_specialcxx . . $ LPC_to_VocalTract_slice_special(166a58b450d33722). (LPCcxx . . $ LPC# me, double time, double glottalDamping, bool radiationDamping, bool internalDamping) {
196 try {
197 integercxx . . $ integer# frameNumber = Sampled_xToNearestIndexcxx . . $ Sampled_xToNearestIndex(405c2491bc3636e9). (me, time);
198 Melder_clipcxx . . $ Melder_clip(971c667ea412f82c). (1_integercxx . . $ `operator""_integer`(85240eb457c3721a)., & frameNumber, mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.); // constant extrapolation
199 LPC_Framecxx . . $ LPC_Frame# lpc = & mycxx . . $ `melder/melder.h:44:9`! d_framescxx . . $ structLPC#d_frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).frameNumber];
200 autoVocalTractcxx . . $ autoVocalTract# thee = LPC_Frame_to_VocalTractcxx . . $ LPC_Frame_to_VocalTract(47c13f181dc166cb). (lpc, 0.17);
201 const double length = VocalTract_LPC_Frame_getMatchingLengthcxx . . $ VocalTract_LPC_Frame_getMatchingLength(9c05a7c6fd4ad9af). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), lpc, glottalDamping, radiationDamping, internalDamping);
202 VocalTract_setLengthcxx . . $ VocalTract_setLength(2aaec95368b3ed64). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), length);
203 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
204 } catch (MelderErrorcxx . . $ MelderError#) {
205 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no VocalTract created.");
206 }
207 }
208
209 autoVocalTractcxx . . $ autoVocalTract# LPC_Frame_to_VocalTractcxx . . $ LPC_Frame_to_VocalTract(47c13f181dc166cb). (LPC_Framecxx . . $ LPC_Frame# me, double length) {
210 try {
211 const integercxx . . $ integer# m = mycxx . . $ `melder/melder.h:44:9`! nCoefficientscxx . . $ structLPC_Frame#nCoefficients.;
212 autoVocalTractcxx . . $ autoVocalTract# thee = VocalTract_createcxx . . $ VocalTract_create(a5d85e6da9521b1d). (m, length / m);
213 VECarea_from_lpccxx . . $ VECarea_from_lpc(9ec4162bb4ce9309). (thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1), mycxx . . $ `melder/melder.h:44:9`! acxx . . $ structLPC_Frame#a..partcxx . . $ autovector#part(2d8d48654ca64e70). (1, m));
214 // area [lips..glottis] (m^2) to VocalTract [glottis..lips] (m^2)
215
216 for (integercxx . . $ integer# i = 1; i <= m / 2; i ++)
217 stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06). (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`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).m + 1 - i]);
218 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
219 } catch (MelderErrorcxx . . $ MelderError#) {
220 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"No VocalTract created from LPC_Frame.");
221 }
222 }
223
224 autoVocalTractcxx . . $ autoVocalTract# LPC_to_VocalTract_slicecxx . . $ LPC_to_VocalTract_slice(fbef0bff810c5f6f). (LPCcxx . . $ LPC# me, double time, double length) {
225 try {
226 integercxx . . $ integer# frameNumber = Sampled_xToNearestIndexcxx . . $ Sampled_xToNearestIndex(405c2491bc3636e9). (me, time);
227 Melder_clipcxx . . $ Melder_clip(971c667ea412f82c). (1_integercxx . . $ `operator""_integer`(85240eb457c3721a)., & frameNumber, mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.); // constant extrapolation
228 const LPC_Framecxx . . $ LPC_Frame# lpc = & mycxx . . $ `melder/melder.h:44:9`! d_framescxx . . $ structLPC#d_frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).frameNumber];
229 autoVocalTractcxx . . $ autoVocalTract# thee = LPC_Frame_to_VocalTractcxx . . $ LPC_Frame_to_VocalTract(47c13f181dc166cb). (lpc, length);
230 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
231 } catch (MelderErrorcxx . . $ MelderError#) {
232 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no VocalTract created.");
233 }
234 }
235
236 /* End of file LPC_and_Tube.cpp */