Debian Codegraph @

fon/Formant.cpp

raw
1 /* Formant.cpp
2 *
3 * Copyright (C) 1992-2009,2011,2012,2014-2020,2022-2025 Paul Boersma
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 3 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.
13 * See the GNU 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 #include "Formant.h"
20
21 #include "oo_DESTROY.h"
22 #include "Formant_def.h"
23 #include "oo_COPY.h"
24 #include "Formant_def.h"
25 #include "oo_EQUAL.h"
26 #include "Formant_def.h"
27 #include "oo_CAN_WRITE_AS_ENCODING.h"
28 #include "Formant_def.h"
29 #include "oo_READ_TEXT.h"
30 #include "Formant_def.h"
31 #include "oo_WRITE_TEXT.h"
32 #include "Formant_def.h"
33 #include "oo_READ_BINARY.h"
34 #include "Formant_def.h"
35 #include "oo_WRITE_BINARY.h"
36 #include "Formant_def.h"
37 #include "oo_DESCRIPTION.h"
38 #include "Formant_def.h"
39
40 #include "enums_getText.h"
41 #include "Formant_enums.h"
42 #include "enums_getValue.h"
43 #include "Formant_enums.h"
44
45 Thing_implement (Formant, Sampled, 2); // version 1 = with intensity, 2 = double
46
47 void structFormant :: v1_info () {
48 structDaata :: v1_info ();
49 MelderInfo_writeLine (U"Time domain:");
50 MelderInfo_writeLine (U" Start time: ", xmin, U" seconds");
51 MelderInfo_writeLine (U" End time: ", xmax, U" seconds");
52 MelderInfo_writeLine (U" Total duration: ", xmax - xmin, U" seconds");
53 MelderInfo_writeLine (U"Time sampling:");
54 MelderInfo_writeLine (U" Number of frames: ", nx);
55 MelderInfo_writeLine (U" Time step: ", dx, U" seconds");
56 MelderInfo_writeLine (U" First frame centred at: ", x1, U" seconds");
57 }
58
59 double structFormant :: v_getValueAtSample (integer iframe, integer which, int units) const {
60 const Formant_Frame frame = & our frames [iframe];
61 const integer iformant = which >> 1;
62 if (iformant < 1 || iformant > frame -> numberOfFormants)
63 return undefined;
64 const double frequency = frame -> formant [iformant]. frequency;
65 if ((which & 1) == 0) {
66 return ( units ? NUMhertzToBark (frequency) : frequency );
67 } else {
68 const double bandwidth = frame -> formant [iformant]. bandwidth;
69 if (units) {
70 double fleft = frequency - 0.5 * bandwidth, fright = frequency + 0.5 * bandwidth;
71 fleft = ( fleft <= 0.0 ? 0.0 : NUMhertzToBark (fleft) ); // prevent undefined
72 fright = NUMhertzToBark (fright);
73 return fright - fleft;
74 }
75 return bandwidth;
76 }
77 return undefined;
78 }
79
80 autoFormant Formant_create (double tmin, double tmax, integer nt, double dt, double t1,
81 integer maximumNumberOfFormants)
82 {
83 try {
84 autoFormant me = Thing_new (Formant);
85 Sampled_init (me.get(), tmin, tmax, nt, dt, t1);
86 my frames = newvectorzero <structFormant_Frame> (nt);
87 my maxnFormants = maximumNumberOfFormants;
88 return me;
89 } catch (MelderError) {
90 Melder_throw (U"Formant object not created.");
91 }
92 }
93
94 integer Formant_getMinNumFormants (Formant me) {
95 integer minNumFormants = 100000000;
96 for (integer iframe = 1; iframe <= my nx; iframe ++)
97 if (my frames [iframe]. numberOfFormants < minNumFormants)
98 minNumFormants = my frames [iframe]. numberOfFormants;
99 return minNumFormants;
100 }
101
102 integer Formant_getMaxNumFormants (Formant me) {
103 integer maxNumFormants = 0;
104 for (integer iframe = 1; iframe <= my nx; iframe ++)
105 if (my frames [iframe]. numberOfFormants > maxNumFormants)
106 maxNumFormants = my frames [iframe]. numberOfFormants;
107 return maxNumFormants;
108 }
109
110 void Formant_drawTracks (Formant me, Graphics g, double tmin, double tmax, double fmax, bool garnish) {
111 const integer ntrack = Formant_getMinNumFormants (me);
112 Function_unidirectionalAutowindow (me, & tmin, & tmax);
113 integer itmin, itmax;
114 if (! Sampled_getWindowSamples (me, tmin, tmax, & itmin, & itmax))
115 return;
116 Graphics_setInner (g);
117 Graphics_setWindow (g, tmin, tmax, 0.0, fmax);
118 for (integer itrack = 1; itrack <= ntrack; itrack ++) {
119 for (integer iframe = itmin; iframe < itmax; iframe ++) {
120 const Formant_Frame curFrame = & my frames [iframe], nextFrame = & my frames [iframe + 1];
121 const double x1 = Sampled_indexToX (me, iframe), x2 = Sampled_indexToX (me, iframe + 1);
122 const double f1 = curFrame -> formant [itrack]. frequency;
123 const double f2 = nextFrame -> formant [itrack]. frequency;
124 if (isdefined (x1) && isdefined (f1) && isdefined (x2) && isdefined (f2))
125 Graphics_line (g, x1, f1, x2, f2);
126 }
127 }
128 Graphics_unsetInner (g);
129 if (garnish) {
130 Graphics_drawInnerBox (g);
131 Graphics_textBottom (g, true, U"Time (s)");
132 Graphics_textLeft (g, true, U"Formant frequency (Hz)");
133 Graphics_marksBottom (g, 2, true, true, false);
134 Graphics_marksLeftEvery (g, 1.0, 1000.0, true, true, true);
135 }
136 }
137
138 void Formant_drawSpeckles_inside (Formant me, Graphics g, double tmin, double tmax, double fmin, double fmax,
139 double suppress_dB, MelderColour oddColour, MelderColour evenColour, bool drawWithContrast)
140 {
141 double maximumIntensity = 0.0, minimumIntensity;
142 Function_unidirectionalAutowindow (me, & tmin, & tmax);
143 integer itmin, itmax;
144 if (! Sampled_getWindowSamples (me, tmin, tmax, & itmin, & itmax))
145 return;
146 Graphics_setWindow (g, tmin, tmax, fmin, fmax);
147
148 for (integer iframe = itmin; iframe <= itmax; iframe ++) {
149 const Formant_Frame frame = & my frames [iframe];
150 if (frame -> intensity > maximumIntensity)
151 maximumIntensity = frame -> intensity;
152 }
153 if (maximumIntensity == 0.0 || suppress_dB <= 0.0)
154 minimumIntensity = 0.0; // ignore
155 else
156 minimumIntensity = maximumIntensity / pow (10.0, suppress_dB / 10.0);
157
158 for (integer iframe = itmin; iframe <= itmax; iframe ++) {
159 const Formant_Frame frame = & my frames [iframe];
160 const double x = Sampled_indexToX (me, iframe);
161 if (frame -> intensity < minimumIntensity)
162 continue;
163 for (integer iformant = 1; iformant <= frame -> numberOfFormants; iformant ++) {
164 const double frequency = frame -> formant [iformant]. frequency;
165 if (frequency >= fmin && frequency <= fmax) {
166 if (drawWithContrast) {
167 #if macintosh
168 const double original_speckleSize = Graphics_inqSpeckleSize (g);
169 Graphics_setSpeckleSize (g, 1.111 * original_speckleSize);
170 Graphics_setColour (g, iformant % 2 == 1 ? evenColour : oddColour);
171 Graphics_speckle (g, x, frequency);
172 Graphics_setSpeckleSize (g, 0.900 * original_speckleSize);
173 Graphics_setColour (g, iformant % 2 == 1 ? oddColour : evenColour);
174 Graphics_speckle (g, x, frequency);
175 Graphics_setSpeckleSize (g, original_speckleSize);
176 #else
177 const double original_lineWidth = Graphics_inqLineWidth (g);
178 const bool even = ( iformant % 2 == 0 );
179 const double speckleSize = Graphics_inqSpeckleSize (g);
180 Graphics_setLineWidth (g, 1.0);
181 if (even) {
182 Graphics_setColour (g, evenColour);
183 Graphics_fillCircle_mm (g, x, frequency, speckleSize * 1.4);
184 Graphics_setColour (g, oddColour);
185 Graphics_circle_mm (g, x, frequency, speckleSize * 1.4);
186 } else {
187 Graphics_setColour (g, oddColour);
188 Graphics_fillCircle_mm (g, x, frequency, speckleSize * 1.15);
189 Graphics_setColour (g, evenColour);
190 Graphics_circle_mm (g, x, frequency, speckleSize * 1.15);
191 }
192 Graphics_setLineWidth (g, original_lineWidth);
193 #endif
194 } else {
195 Graphics_setColour (g, iformant % 2 == 1 ? oddColour : evenColour);
196 Graphics_speckle (g, x, frequency);
197 }
198 }
199 }
200 }
201 }
202
203 void Formant_drawSpeckles_inside (Formant me, Graphics g, double tmin, double tmax, double fmin, double fmax,
204 double suppress_dB)
205 {
206 Formant_drawSpeckles_inside (me, g, tmin, tmax, fmin, fmax, suppress_dB, Graphics_inqColour (g), Graphics_inqColour (g), false);
207 }
208
209 void Formant_drawSpeckles (Formant me, Graphics g, double tmin, double tmax, double fmax, double suppress_dB,
210 bool garnish)
211 {
212 Graphics_setInner (g);
213 Formant_drawSpeckles_inside (me, g, tmin, tmax, 0.0, fmax, suppress_dB);
214 Graphics_unsetInner (g);
215 if (garnish) {
216 Graphics_drawInnerBox (g);
217 Graphics_textBottom (g, true, U"Time (s)");
218 Graphics_textLeft (g, true, U"Formant frequency (Hz)");
219 Graphics_marksBottom (g, 2, true, true, false);
220 Graphics_marksLeftEvery (g, 1.0, 1000.0, true, true, true);
221 }
222 }
223
224 void Formant_formula_bandwidths (Formant me, conststring32 formula, Interpreter interpreter) {
225 try {
226 const integer nrow = Formant_getMaxNumFormants (me);
227 if (nrow < 1)
228 Melder_throw (U"No formants available.");
229 autoMatrix mat = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 0.5, nrow + 0.5, nrow, 1.0, 1.0);
230 for (integer iframe = 1; iframe <= my nx; iframe ++) {
231 const Formant_Frame frame = & my frames [iframe];
232 for (integer iformant = 1; iformant <= frame -> numberOfFormants; iformant ++)
233 mat -> z [iformant] [iframe] = frame -> formant [iformant]. bandwidth;
234 }
235 Matrix_formula (mat.get(), formula, interpreter, nullptr);
236 for (integer iframe = 1; iframe <= my nx; iframe ++) {
237 const Formant_Frame frame = & my frames [iframe];
238 for (integer iformant = 1; iformant <= frame -> numberOfFormants; iformant ++)
239 frame -> formant [iformant]. bandwidth = mat -> z [iformant] [iframe];
240 }
241 } catch (MelderError) {
242 Melder_throw (me, U": bandwidth formula not executed.");
243 }
244 }
245
246 void Formant_formula_frequencies (Formant me, conststring32 formula, Interpreter interpreter) {
247 try {
248 const integer nrow = Formant_getMaxNumFormants (me);
249 if (nrow < 1)
250 Melder_throw (U"No formants available.");
251 autoMatrix mat = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 0.5, nrow + 0.5, nrow, 1.0, 1.0);
252 for (integer iframe = 1; iframe <= my nx; iframe ++) {
253 const Formant_Frame frame = & my frames [iframe];
254 for (integer iformant = 1; iformant <= frame -> numberOfFormants; iformant ++)
255 mat -> z [iformant] [iframe] = frame -> formant [iformant]. frequency;
256 }
257 Matrix_formula (mat.get(), formula, interpreter, nullptr);
258 for (integer iframe = 1; iframe <= my nx; iframe ++) {
259 const Formant_Frame frame = & my frames [iframe];
260 for (integer iformant = 1; iformant <= frame -> numberOfFormants; iformant ++)
261 frame -> formant [iformant]. frequency = mat -> z [iformant] [iframe];
262 }
263 } catch (MelderError) {
264 Melder_throw (me, U": frequency formula not executed.");
265 }
266 }
267
268 void Formant_getExtrema (Formant me, integer iformant, double tmin, double tmax, double *fmin, double *fmax) {
269 if (fmin)
270 *fmin = 0.0;
271 if (fmax)
272 *fmax = 0.0;
273 if (iformant < 1)
274 return;
275 Function_unidirectionalAutowindow (me, & tmin, & tmax);
276 integer itmin, itmax;
277 if (! Sampled_getWindowSamples (me, tmin, tmax, & itmin, & itmax))
278 return;
279 for (integer iframe = itmin; iframe <= itmax; iframe ++) {
280 const Formant_Frame frame = & my frames [iframe];
281 if (iformant > frame -> numberOfFormants)
282 continue;
283 const double f = frame -> formant [iformant]. frequency;
284 if (f == 0.0)
285 continue;
286 if (fmin && (f < *fmin || *fmin == 0.0))
287 *fmin = f;
288 if (fmax && f > *fmax)
289 *fmax = f;
290 }
291 }
292
293 void Formant_getMinimumAndTime (Formant me, integer iformant, double tmin, double tmax, kFormant_unit unit, int interpolate,
294 double *return_minimum, double *return_timeOfMinimum)
295 {
296 Sampled_getMinimumAndX (me, tmin, tmax, iformant << 1, (int) unit, interpolate, return_minimum, return_timeOfMinimum);
297 if (return_minimum && *return_minimum <= 0.0)
298 *return_minimum = undefined;
299 }
300
301 double Formant_getMinimum (Formant me, integer iformant, double tmin, double tmax, kFormant_unit unit, int interpolate) {
302 double minimum;
303 Formant_getMinimumAndTime (me, iformant, tmin, tmax, unit, interpolate, & minimum, nullptr);
304 return minimum;
305 }
306
307 double Formant_getTimeOfMinimum (Formant me, integer iformant, double tmin, double tmax, kFormant_unit unit, int interpolate) {
308 double time;
309 Formant_getMinimumAndTime (me, iformant, tmin, tmax, unit, interpolate, nullptr, & time);
310 return time;
311 }
312
313 void Formant_getMaximumAndTime (Formant me, integer iformant, double tmin, double tmax, kFormant_unit unit, int interpolate,
314 double *return_maximum, double *return_timeOfMaximum)
315 {
316 Sampled_getMaximumAndX (me, tmin, tmax, iformant << 1, (int) unit, interpolate, return_maximum, return_timeOfMaximum);
317 if (return_maximum && *return_maximum <= 0.0)
318 *return_maximum = undefined; // unlikely
319 }
320
321 double Formant_getMaximum (Formant me, integer iformant, double tmin, double tmax, kFormant_unit unit, int interpolate) {
322 double maximum;
323 Formant_getMaximumAndTime (me, iformant, tmin, tmax, unit, interpolate, & maximum, nullptr);
324 return maximum;
325 }
326
327 double Formant_getTimeOfMaximum (Formant me, integer iformant, double tmin, double tmax, kFormant_unit unit, int interpolate) {
328 double time;
329 Formant_getMaximumAndTime (me, iformant, tmin, tmax, unit, interpolate, nullptr, & time);
330 return time;
331 }
332
333 double Formant_getQuantile (Formant me, integer iformant, double quantile, double tmin, double tmax, kFormant_unit unit) {
334 return Sampled_getQuantile (me, tmin, tmax, quantile, iformant << 1, (int) unit);
335 }
336
337 double Formant_getMean (Formant me, integer iformant, double tmin, double tmax, kFormant_unit unit) {
338 return Sampled_getMean (me, tmin, tmax, iformant << 1, (int) unit, true);
339 }
340
341 double Formant_getStandardDeviation (Formant me, integer iformant, double tmin, double tmax, kFormant_unit unit) {
342 if (iformant < 1 || isundef (tmin) || isundef (tmax))
343 return undefined;
344 Function_unidirectionalAutowindow (me, & tmin, & tmax);
345 integer itmin, itmax;
346 if (! Sampled_getWindowSamples (me, tmin, tmax, & itmin, & itmax))
347 return undefined;
348 const double mean = Formant_getMean (me, iformant, tmin, tmax, unit);
349 longdouble sum = 0.0;
350 integer n = 0;
351 for (integer iframe = itmin; iframe <= itmax; iframe ++) {
352 const Formant_Frame frame = & my frames [iframe];
353 if (iformant > frame -> numberOfFormants)
354 continue;
355 double f = frame -> formant [iformant]. frequency;
356 if (f == 0.0)
357 continue;
358 if (unit == kFormant_unit::BARK)
359 f = NUMhertzToBark (f);
360 n += 1;
361 sum += (f - mean) * (f - mean);
362 }
363 if (n > 1)
364 return sqrt (double (sum) / (n - 1));
365 return undefined;
366 }
367
368 double Formant_getValueAtTime (Formant me, integer iformant, double time, kFormant_unit unit) {
369 return Sampled_getValueAtX (me, time, iformant << 1, (int) unit, true);
370 }
371
372 double Formant_getBandwidthAtTime (Formant me, integer iformant, double time, kFormant_unit unit) {
373 return Sampled_getValueAtX (me, time, (iformant << 1) + 1, (int) unit, true);
374 }
375
376 double Formant_getQuantileOfBandwidth (Formant me, integer iformant, double quantile, double tmin, double tmax, kFormant_unit unit) {
377 return Sampled_getQuantile (me, tmin, tmax, quantile, (iformant << 1) + 1, (int) unit);
378 }
379
380 void Formant_scatterPlot (Formant me, Graphics g, double tmin, double tmax,
381 integer iformant1, double fmin1, double fmax1, integer iformant2, double fmin2, double fmax2,
382 double size_mm, conststring32 mark, bool garnish)
383 {
384 if (iformant1 < 1 || iformant2 < 1)
385 return;
386 Function_unidirectionalAutowindow (me, & tmin, & tmax);
387 integer itmin, itmax;
388 if (! Sampled_getWindowSamples (me, tmin, tmax, & itmin, & itmax))
389 return;
390 if (fmax1 == fmin1)
391 Formant_getExtrema (me, iformant1, tmin, tmax, & fmin1, & fmax1);
392 if (fmax1 == fmin1)
393 return;
394 if (fmax2 == fmin2)
395 Formant_getExtrema (me, iformant2, tmin, tmax, & fmin2, & fmax2);
396 if (fmax2 == fmin2)
397 return;
398 Graphics_setInner (g);
399 Graphics_setWindow (g, fmin1, fmax1, fmin2, fmax2);
400 for (integer iframe = itmin; iframe <= itmax; iframe ++) {
401 const Formant_Frame frame = & my frames [iframe];
402 if (iformant1 > frame -> numberOfFormants || iformant2 > frame -> numberOfFormants)
403 continue;
404 const double x = frame -> formant [iformant1]. frequency;
405 const double y = frame -> formant [iformant2]. frequency;
406 if (x == 0.0 || y == 0.0)
407 continue;
408 Graphics_mark (g, x, y, size_mm, mark);
409 }
410 Graphics_unsetInner (g);
411 if (garnish) {
412 Graphics_drawInnerBox (g);
413 Graphics_textBottom (g, true, Melder_cat (U"%%F_", iformant1, U" (Hz)"));
414 Graphics_textLeft (g, true, Melder_cat (U"%%F_", iformant2, U" (Hz)"));
415 Graphics_marksBottom (g, 2, true, true, false);
416 Graphics_marksLeft (g, 2, true, true, false);
417 }
418 }
419
420 autoMatrix Formant_to_Matrix (Formant me, integer iformant) {
421 try {
422 autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 1.0, 1.0, 1, 1.0, 1.0);
423 for (integer iframe = 1; iframe <= my nx; iframe ++) {
424 const Formant_Frame frame = & my frames [iframe];
425 thy z [1] [iframe] = ( iformant <= frame -> numberOfFormants ? frame -> formant [iformant]. frequency : 0.0 );
426 }
427 return thee;
428 } catch (MelderError) {
429 Melder_throw (me, U": frequencies of formant ", iformant, U" not converted to Matrix.");
430 }
431 }
432
433 autoMatrix Formant_to_Matrix_bandwidths (Formant me, integer iformant) {
434 try {
435 autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 1.0, 1.0, 1, 1.0, 1.0);
436 for (integer iframe = 1; iframe <= my nx; iframe ++) {
437 const Formant_Frame frame = & my frames [iframe];
438 thy z [1] [iframe] = ( iformant <= frame -> numberOfFormants ? frame -> formant [iformant]. bandwidth : 0.0 );
439 }
440 return thee;
441 } catch (MelderError) {
442 Melder_throw (me, U": bandwidths of formant ", iformant, U" not converted to Matrix.");
443 }
444 }
445
446 /*** Viterbi methods. ***/
447
448 struct fparm { Formant me, thee; double dfCost, bfCost, octaveJumpCost, refF [1 + 5]; };
449
450 static double getLocalCost (integer iframe, integer icand, integer itrack, void *closure) {
451 struct fparm *me = (struct fparm *) closure;
452 const Formant_Frame frame = & my my frames [iframe];
453 if (icand > frame -> numberOfFormants)
454 return 1e30;
455 Formant_Formant candidate = & frame -> formant [icand];
456 /*if (candidate -> frequency <= 0.0) candidate -> frequency = 0.001;
457 Melder_crash (U"Weird formant frequency ", candidate -> frequency, U" Hz.")*/;
458 Melder_assert (candidate -> bandwidth > 0.0);
459 Melder_assert (itrack > 0 && itrack <= 5);
460 return my dfCost * fabs (candidate -> frequency - my refF [itrack]) +
461 my bfCost * candidate -> bandwidth / candidate -> frequency;
462 }
463 static double getTransitionCost (integer iframe, integer icand1, integer icand2, integer /* itrack */, void *closure) {
464 struct fparm *me = (struct fparm *) closure;
465 const Formant_Frame prevFrame = & my my frames [iframe - 1], curFrame = & my my frames [iframe];
466 if (icand1 > prevFrame -> numberOfFormants || icand2 > curFrame -> numberOfFormants)
467 return 1e30;
468 const double f1 = prevFrame -> formant [icand1]. frequency;
469 const double f2 = curFrame -> formant [icand2]. frequency;
470 /*Melder_assert (f1 > 0.0);*/
471 /*Melder_assert (f2 > 0.0);*/
472 return my octaveJumpCost * fabs (NUMlog2 (f1 / f2));
473 }
474 static void putResult (integer iframe, integer place, integer itrack, void *closure) {
475 struct fparm *me = (struct fparm *) closure;
476 Melder_assert (iframe > 0 && iframe <= my my nx);
477 Melder_assert (itrack > 0 && itrack <= 5);
478 Melder_assert (place > 0);
479 Melder_assert (place <= my my frames [iframe]. numberOfFormants);
480 my thy frames [iframe]. formant [itrack] = my my frames [iframe]. formant [place];
481 }
482
483 autoFormant Formant_tracker (Formant me, integer ntrack,
484 double refF1, double refF2, double refF3, double refF4, double refF5,
485 double dfCost, /* Per kHz. */
486 double bfCost, double octaveJumpCost)
487 {
488 try {
489 integer nformmin = Formant_getMinNumFormants (me);
490 struct fparm parm;
491 Melder_require (ntrack <= nformmin,
492 U"Number of tracks (", ntrack, U") should not exceed minimum number of formants (", nformmin, U").");
493 autoFormant thee = Formant_create (my xmin, my xmax, my nx, my dx, my x1, ntrack);
494 for (integer iframe = 1; iframe <= thy nx; iframe ++) {
495 thy frames [iframe]. formant = newvectorzero <structFormant_Formant> (ntrack);
496 thy frames [iframe]. numberOfFormants = ntrack; // maintain invariant
497 thy frames [iframe]. intensity = my frames [iframe]. intensity;
498 }
499 /* BUG: limit costs to 1e10 or so */
500 parm.me = me;
501 parm.thee = thee.get();
502 parm.dfCost = dfCost / 1000.0; // per Hz
503 parm.bfCost = bfCost;
504 parm.octaveJumpCost = octaveJumpCost;
505 parm.refF [1] = refF1;
506 parm.refF [2] = refF2;
507 parm.refF [3] = refF3;
508 parm.refF [4] = refF4;
509 parm.refF [5] = refF5;
510 NUM_viterbi_multi (my nx, my maxnFormants, ntrack,
511 getLocalCost, getTransitionCost, putResult, & parm);
512 return thee;
513 } catch (MelderError) {
514 Melder_throw (me, U": not tracked.");
515 }
516 }
517
518 autoTable Formant_downto_Table (Formant me, bool includeFrameNumbers,
519 bool includeTimes, integer timeDecimals,
520 bool includeIntensity, integer intensityDecimals,
521 bool includeNumberOfFormants, integer frequencyDecimals,
522 bool includeBandwidths)
523 {
524 try {
525 autoTable thee = Table_createWithoutColumnNames (my nx, includeFrameNumbers + includeTimes + includeIntensity +
526 includeNumberOfFormants + my maxnFormants * (1 + includeBandwidths));
527 integer icol = 0;
528 if (includeFrameNumbers)
529 Table_renameColumn_e (thee.get(), ++ icol, U"frame");
530 if (includeTimes)
531 Table_renameColumn_e (thee.get(), ++ icol, U"time(s)");
532 if (includeIntensity)
533 Table_renameColumn_e (thee.get(), ++ icol, U"intensity");
534 if (includeNumberOfFormants)
535 Table_renameColumn_e (thee.get(), ++ icol, U"nformants");
536 for (integer iformant = 1; iformant <= my maxnFormants; iformant ++) {
537 Table_renameColumn_e (thee.get(), ++ icol, Melder_cat (U"F", iformant, U"(Hz)"));
538 if (includeBandwidths)
539 Table_renameColumn_e (thee.get(), ++ icol, Melder_cat (U"B", iformant, U"(Hz)"));
540 }
541 for (integer iframe = 1; iframe <= my nx; iframe ++) {
542 icol = 0;
543 if (includeFrameNumbers)
544 Table_setNumericValue (thee.get(), iframe, ++ icol, iframe);
545 if (includeTimes)
546 Table_setStringValue (thee.get(), iframe, ++ icol, Melder_fixed (my x1 + (iframe - 1) * my dx, timeDecimals));
547 const Formant_Frame frame = & my frames [iframe];
548 if (includeIntensity)
549 Table_setStringValue (thee.get(), iframe, ++ icol, Melder_fixed (frame -> intensity, intensityDecimals));
550 if (includeNumberOfFormants)
551 Table_setNumericValue (thee.get(), iframe, ++ icol, frame -> numberOfFormants);
552 for (integer iformant = 1; iformant <= frame -> numberOfFormants; iformant ++) {
553 const Formant_Formant formant = & frame -> formant [iformant];
554 Table_setStringValue (thee.get(), iframe, ++ icol, Melder_fixed (formant -> frequency, frequencyDecimals));
555 if (includeBandwidths)
556 Table_setStringValue (thee.get(), iframe, ++ icol, Melder_fixed (formant -> bandwidth, frequencyDecimals));
557 }
558 for (integer iformant = frame -> numberOfFormants + 1; iformant <= my maxnFormants; iformant ++) {
559 Table_setNumericValue (thee.get(), iframe, ++ icol, undefined);
560 if (includeBandwidths)
561 Table_setNumericValue (thee.get(), iframe, ++ icol, undefined);
562 }
563 }
564 return thee;
565 } catch (MelderError) {
566 Melder_throw (me, U": not converted to Table.");
567 }
568 }
569
570 void Formant_list (
571 const Formant me,
572 const bool includeFrameNumbers,
573 const bool includeTimes,
574 const integer timeDecimals,
575 const bool includeIntensity,
576 const integer intensityDecimals,
577 const bool includeNumberOfFormants,
578 const integer frequencyDecimals,
579 const bool includeBandwidths
580 ) {
581 try {
582 autoTable table = Formant_downto_Table (me, includeFrameNumbers,
583 includeTimes, timeDecimals,
584 includeIntensity, intensityDecimals,
585 includeNumberOfFormants, frequencyDecimals, includeBandwidths
586 );
587 Table_list (table.get(), false);
588 } catch (MelderError) {
589 Melder_throw (me, U": not listed.");
590 }
591 }
592
593 /* End of file Formant.cpp */