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"
27 #include "LPC_and_Formant.h"
28 #include "LPC_to_Spectrum.h"
29 #include "SpectrumTier.h"
30 #include "VocalTract_to_Spectrum.h"
31 #include "NUM2.h"
32
33 // IEEE: Programs fo digital signal processing section 4.3 LPTRN
34
35 void LPC_Frame_into_Tube_Frame_rc (LPC_Frame me, Tube_Frame thee) {
36 Melder_assert (my nCoefficients == my a.size); // check invariant
37 thy c.resize (my nCoefficients);
38 thy numberOfSegments = thy c.size; // maintain invariant
39 VECrc_from_lpc (thy c.get(), my a.get());
40 }
41
42 void LPC_Frame_into_Tube_Frame_area (LPC_Frame me, Tube_Frame thee) {
43 VECarea_from_lpc (thy c.part (1, my nCoefficients), my a.part (1, my nCoefficients));
44 }
45
46 double VocalTract_LPC_Frame_getMatchingLength (VocalTract me, 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 integer numberOfFrequencies = 1000;
52 const double maximumFrequency = 5000.0;
53 autoSpectrum vts = VocalTract_to_Spectrum (me, numberOfFrequencies, maximumFrequency, glottalDamping, radiationDamping, internalDamping);
54 const double samplingFrequency = 1000.0 * my nx;
55 autoSpectrum lps = Spectrum_create (0.5 * samplingFrequency, numberOfFrequencies);
56 LPC_Frame_into_Spectrum (thee, lps.get(), 0, 50);
57 autoSpectrumTier vtst = Spectrum_to_SpectrumTier_peaks (vts.get());
58 autoSpectrumTier lpst = Spectrum_to_SpectrumTier_peaks (lps.get());
59 double length = undefined;
60 if (vtst -> points.size > 1 && lpst -> points.size > 1) {
61 const double vt_f1 = vtst -> points.at [1] -> number, vt_f2 = vtst -> points.at [2] -> number;
62 const double lp_f1 = lpst -> points.at [1] -> number, lp_f2 = lpst -> points.at [2] -> 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 = my dx * my nx * (1 + dl);
66 }
67 return length;
68 } catch (MelderError) {
69 Melder_throw (U"Length could not be determined from VocalTract and LPC_Frame.");
70 }
71 }
72
73 double LPC_Frame_getVTL_wakita (LPC_Frame me, double samplingPeriod, double refLength) {
74 struct structLPC_Frame lpc_struct;
75 const LPC_Frame lpc = & lpc_struct;
76 struct structFormant_Frame f_struct;
77 const Formant_Frame f = & f_struct;
78 struct structTube_Frame rc_struct, af_struct;
79 const Tube_Frame rc = & rc_struct, af = & af_struct;
80 try {
81 const integer numberOfCoefficients = my nCoefficients;
82 const double dlength = 0.001;
83 double length, wakita_length = 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_init (lpc, numberOfCoefficients);
92 Tube_Frame_init (rc, numberOfCoefficients, refLength);
93 Tube_Frame_init (af, numberOfCoefficients, refLength);
94 /*
95 Step 2
96 */
97 LPC_Frame_into_Formant_Frame (me, f, samplingPeriod, 0.0);
98 /*
99 LPC_Frame_into_Formant_Frame performs the Formant_Frame_init !!
100 */
101 Melder_require (f -> numberOfFormants > 0,
102 U"Not enough formants.");
103 VEC area = af -> c.get(); // 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 (integer i = 1; i <= f -> numberOfFormants; i ++) {
112 f -> formant [i]. frequency *= fscale;
113 f -> formant [i]. 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_Frame (f, lpc, samplingPeriod);
129 /*
130 Step 5
131 */
132 rc -> length = length;
133 LPC_Frame_into_Tube_Frame_rc (lpc, rc);
134 /*
135 Step 6.1
136 */
137 Tube_Frames_rc_into_area (rc, af);
138 /*
139 Step 6.2 Log(areas)
140 */
141 double logSum = 0.0;
142 for (integer i = 1; i <= af -> numberOfSegments; i ++) {
143 area [i] = log (area [i]);
144 logSum += area [i];
145 }
146 /*
147 Steps 6.3 and 7
148 */
149 double var = 0.0;
150 for (integer i = 1; i <= af -> numberOfSegments; i ++) {
151 const double delta = area [i] - logSum / af -> 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 -> destroy ();
164 lpc -> destroy ();
165 rc -> destroy ();
166 af -> destroy ();
167 return wakita_length;
168 } catch (MelderError) {
169 f -> destroy ();
170 lpc -> destroy ();
171 rc -> destroy ();
172 af -> destroy ();
173 return undefined;
174 }
175 }
176
177 int Tube_Frame_into_LPC_Frame_area (Tube_Frame me, LPC_Frame thee) {
178 (void) me;
179 (void) thee;
180 return 0;
181 }
182
183 int Tube_Frame_into_LPC_Frame_rc (Tube_Frame me, LPC_Frame thee) {
184 (void) me;
185 (void) thee;
186 return 0;
187 }
188
189 void VocalTract_setLength (VocalTract me, double newLength) {
190 my xmax = newLength;
191 my dx = newLength / my nx;
192 my x1 = 0.5 * my dx;
193 }
194
195 autoVocalTract LPC_to_VocalTract_slice_special (LPC me, double time, double glottalDamping, bool radiationDamping, bool internalDamping) {
196 try {
197 integer frameNumber = Sampled_xToNearestIndex (me, time);
198 Melder_clip (1_integer, & frameNumber, my nx); // constant extrapolation
199 LPC_Frame lpc = & my d_frames [frameNumber];
200 autoVocalTract thee = LPC_Frame_to_VocalTract (lpc, 0.17);
201 const double length = VocalTract_LPC_Frame_getMatchingLength (thee.get(), lpc, glottalDamping, radiationDamping, internalDamping);
202 VocalTract_setLength (thee.get(), length);
203 return thee;
204 } catch (MelderError) {
205 Melder_throw (me, U": no VocalTract created.");
206 }
207 }
208
209 autoVocalTract LPC_Frame_to_VocalTract (LPC_Frame me, double length) {
210 try {
211 const integer m = my nCoefficients;
212 autoVocalTract thee = VocalTract_create (m, length / m);
213 VECarea_from_lpc (thy z.row (1), my a.part (1, m));
214 // area [lips..glottis] (m^2) to VocalTract [glottis..lips] (m^2)
215
216 for (integer i = 1; i <= m / 2; i ++)
217 std::swap (thy z [1] [i], thy z [1] [m + 1 - i]);
218 return thee;
219 } catch (MelderError) {
220 Melder_throw (U"No VocalTract created from LPC_Frame.");
221 }
222 }
223
224 autoVocalTract LPC_to_VocalTract_slice (LPC me, double time, double length) {
225 try {
226 integer frameNumber = Sampled_xToNearestIndex (me, time);
227 Melder_clip (1_integer, & frameNumber, my nx); // constant extrapolation
228 const LPC_Frame lpc = & my d_frames [frameNumber];
229 autoVocalTract thee = LPC_Frame_to_VocalTract (lpc, length);
230 return thee;
231 } catch (MelderError) {
232 Melder_throw (me, U": no VocalTract created.");
233 }
234 }
235
236 /* End of file LPC_and_Tube.cpp */