Debian Codegraph @

fon/PointProcess.cpp

raw
1 /* PointProcess.cpp
2 *
3 * Copyright (C) 1992-2012,2014-2022,2024 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 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.
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 "PointProcess.h"
20 #include "VoiceAnalysis.h"
21
22 #include "oo_DESTROY.h"
23 #include "PointProcess_def.h"
24 #include "oo_COPY.h"
25 #include "PointProcess_def.h"
26 #include "oo_EQUAL.h"
27 #include "PointProcess_def.h"
28 #include "oo_CAN_WRITE_AS_ENCODING.h"
29 #include "PointProcess_def.h"
30 #include "oo_WRITE_TEXT.h"
31 #include "PointProcess_def.h"
32 #include "oo_READ_TEXT.h"
33 #include "PointProcess_def.h"
34 #include "oo_WRITE_BINARY.h"
35 #include "PointProcess_def.h"
36 #include "oo_READ_BINARY.h"
37 #include "PointProcess_def.h"
38 #include "oo_DESCRIPTION.h"
39 #include "PointProcess_def.h"
40
41 Thing_implement (PointProcess, Function, 0);
42
43 static void infoPeriods (PointProcess me, double shortestPeriod, double longestPeriod, double maximumPeriodFactor, int precision) {
44 const integer numberOfPeriods = PointProcess_getNumberOfPeriods (me, 0.0, 0.0, shortestPeriod, longestPeriod, maximumPeriodFactor);
45 const double meanPeriod = PointProcess_getMeanPeriod (me, 0.0, 0.0, shortestPeriod, longestPeriod, maximumPeriodFactor);
46 const double stdevPeriod = PointProcess_getStdevPeriod (me, 0.0, 0.0, shortestPeriod, longestPeriod, maximumPeriodFactor);
47 const double jitter_local = PointProcess_getJitter_local (me, 0.0, 0.0, shortestPeriod, longestPeriod, maximumPeriodFactor);
48 const double jitter_local_absolute = PointProcess_getJitter_local_absolute (me, 0.0, 0.0, shortestPeriod, longestPeriod, maximumPeriodFactor);
49 const double jitter_rap = PointProcess_getJitter_rap (me, 0.0, 0.0, shortestPeriod, longestPeriod, maximumPeriodFactor);
50 const double jitter_ppq5 = PointProcess_getJitter_ppq5 (me, 0.0, 0.0, shortestPeriod, longestPeriod, maximumPeriodFactor);
51 const double jitter_ddp = PointProcess_getJitter_ddp (me, 0.0, 0.0, shortestPeriod, longestPeriod, maximumPeriodFactor);
52 MelderInfo_writeLine (U" Number of periods: ", numberOfPeriods);
53 MelderInfo_writeLine (U" Mean period: ", meanPeriod, U" seconds");
54 MelderInfo_writeLine (U" Stdev period: ", stdevPeriod, U" seconds");
55 MelderInfo_writeLine (U" Jitter (local): ", Melder_percent (jitter_local, precision));
56 MelderInfo_writeLine (U" Jitter (local, absolute): ", Melder_fixedExponent (jitter_local_absolute, -6, precision), U" seconds");
57 MelderInfo_writeLine (U" Jitter (rap): ", Melder_percent (jitter_rap, precision));
58 MelderInfo_writeLine (U" Jitter (ppq5): ", Melder_percent (jitter_ppq5, precision));
59 MelderInfo_writeLine (U" Jitter (ddp): ", Melder_percent (jitter_ddp, precision));
60 }
61
62 void structPointProcess :: v1_info () {
63 structDaata :: v1_info ();
64 MelderInfo_writeLine (U"Time domain:");
65 MelderInfo_writeLine (U" Start time: ", xmin, U" seconds");
66 MelderInfo_writeLine (U" End time: ", xmax, U" seconds");
67 MelderInfo_writeLine (U" Total duration: ", xmax - xmin, U" seconds");
68 MelderInfo_writeLine (U"Number of times: ", nt);
69 if (nt) {
70 MelderInfo_writeLine (U"First time: ", t [1], U" seconds");
71 MelderInfo_writeLine (U"Last time: ", t [nt], U" seconds");
72 }
73 MelderInfo_writeLine (U"Periods between 0.1 ms and 20 ms (pitch between 50 and 10000 Hz),");
74 MelderInfo_writeLine (U"with a maximum \"period factor\" of 1.3:");
75 infoPeriods (this, 1e-4, 20e-3, 1.3, 3);
76 MelderInfo_writeLine (U"All periods:");
77 infoPeriods (this, 0.0, 0.0, 1e308, 6);
78 }
79
80 void structPointProcess :: v_shiftX (double xfrom, double xto) {
81 PointProcess_Parent :: v_shiftX (xfrom, xto);
82 for (integer i = 1; i <= nt; i ++)
83 NUMshift (& t [i], xfrom, xto);
84 }
85
86 void structPointProcess :: v_scaleX (double xminfrom, double xmaxfrom, double xminto, double xmaxto) {
87 PointProcess_Parent :: v_scaleX (xminfrom, xmaxfrom, xminto, xmaxto);
88 for (integer i = 1; i <= nt; i ++)
89 NUMscale (& t [i], xminfrom, xmaxfrom, xminto, xmaxto);
90 }
91
92 void PointProcess_init (PointProcess me, double tmin, double tmax, integer initialMaxnt) {
93 Function_init (me, tmin, tmax);
94 my t.initWithCapacity (initialMaxnt);
95 my nt = 0; // maintain invariant
96 }
97
98 autoPointProcess PointProcess_create (double tmin, double tmax, integer initialMaxnt) {
99 try {
100 autoPointProcess me = Thing_new (PointProcess);
101 PointProcess_init (me.get(), tmin, tmax, initialMaxnt);
102 return me;
103 } catch (MelderError) {
104 Melder_throw (U"PointProcess not created.");
105 }
106 }
107
108 autoPointProcess PointProcess_createPoissonProcess (double startingTime, double finishingTime, double density) {
109 try {
110 autoPointProcess me = PointProcess_create (startingTime, finishingTime, 0);
111 const integer numberOfPoints = (integer) NUMrandomPoisson ((finishingTime - startingTime) * density);
112 my t = randomUniform_VEC (numberOfPoints, startingTime, finishingTime);
113 my nt = numberOfPoints; // maintain invariant
114 sort_e_VEC_inout (my t.get());
115 return me;
116 } catch (MelderError) {
117 Melder_throw (U"PointProcess (Poisson process) not created.");
118 }
119 }
120
121 integer PointProcess_getLowIndex (PointProcess me, double t) {
122 if (my nt == 0 || t < my t [1])
123 return 0;
124 if (t >= my t [my nt]) // special case that often occurs in practice
125 return my nt;
126 Melder_assert (my nt != 1); // may fail if t or my t [1] is NaN
127 /*
128 Start binary search.
129 */
130 integer left = 1, right = my nt;
131 while (left < right - 1) {
132 const integer mid = (left + right) / 2;
133 if (t >= my t [mid])
134 left = mid;
135 else
136 right = mid;
137 }
138 Melder_assert (right == left + 1);
139 return left;
140 }
141
142 integer PointProcess_getHighIndex (PointProcess me, double t) {
143 if (my nt == 0)
144 return 0;
145 if (t <= my t [1])
146 return 1;
147 if (t > my t [my nt])
148 return my nt + 1;
149 /*
150 Start binary search.
151 */
152 integer left = 1, right = my nt;
153 while (left < right - 1) {
154 const integer mid = (left + right) / 2;
155 if (t > my t [mid])
156 left = mid;
157 else
158 right = mid;
159 }
160 Melder_assert (right == left + 1);
161 return right;
162 }
163
164 integer PointProcess_getNearestIndex (PointProcess me, double t) {
165 if (my nt == 0)
166 return 0;
167 if (t <= my t [1])
168 return 1;
169 if (t >= my t [my nt])
170 return my nt;
171 /*
172 Start binary search.
173 */
174 integer left = 1, right = my nt;
175 while (left < right - 1) {
176 const integer mid = (left + right) / 2;
177 if (t >= my t [mid])
178 left = mid;
179 else
180 right = mid;
181 }
182 Melder_assert (right == left + 1);
183 return t - my t [left] < my t [right] - t ? left : right;
184 }
185
186 void PointProcess_addPoint (PointProcess me, double t) {
187 try {
188 Melder_require (isdefined (t),
189 U"Cannot add a point at an undefined time.");
190 const integer newNumberOfPoints = my nt + 1;
191 my t. resize (newNumberOfPoints);
192 if (my nt == 0 || t >= my t [my nt]) { // special case that often occurs in practice
193 my nt = newNumberOfPoints; // maintain invariant
194 my t [newNumberOfPoints] = t;
195 } else {
196 const integer left = PointProcess_getLowIndex (me, t);
197 if (left == 0 || my t [left] != t) {
198 for (integer i = my nt; i > left; i --)
199 my t [i + 1] = my t [i];
200 my nt = newNumberOfPoints; // maintain invariant
201 my t [left + 1] = t;
202 }
203 }
204 } catch (MelderError) {
205 Melder_throw (me, U": point not added.");
206 }
207 }
208
209 void PointProcess_addPoints (PointProcess me, constVECVU const& times) {
210 try {
211 const integer newNumberOfPoints = my nt + times.size;
212 my t. resize (newNumberOfPoints);
213 my t.part (my nt + 1, newNumberOfPoints) <<= times;
214 my nt = newNumberOfPoints; // maintain invariant
215 sort_e_VEC_inout (my t.get());
216 } catch (MelderError) {
217 Melder_throw (me, U": points not added.");
218 }
219 }
220
221 void PointProcess_removePoint (PointProcess me, integer pointNumber) {
222 if (pointNumber < 1 || pointNumber > my nt) return;
223 /*
224 First shift, then resize.
225 */
226 for (integer i = pointNumber; i < my nt; i ++)
227 my t [i] = my t [i + 1];
228 const integer newNumberOfPoints = my nt - 1;
229 my t. resize (newNumberOfPoints);
230 my nt = newNumberOfPoints; // maintain invariant
231 }
232
233 void PointProcess_removePointNear (PointProcess me, double time) {
234 PointProcess_removePoint (me, PointProcess_getNearestIndex (me, time));
235 }
236
237 void PointProcess_removePoints (PointProcess me, integer first, integer last) {
238 Melder_clipLeft (1_integer, & first);
239 Melder_clipRight (& last, my nt);
240 const integer distance = last - first + 1;
241 if (distance <= 0)
242 return;
243 for (integer i = first + distance; i <= my nt; i ++)
244 my t [i - distance] = my t [i];
245 const integer newNumberOfPoints = my nt - distance;
246 my t. resize (newNumberOfPoints);
247 my nt = newNumberOfPoints; // maintain invariant
248 }
249
250 void PointProcess_removePointsBetween (PointProcess me, double tmin, double tmax) {
251 PointProcess_removePoints (me, PointProcess_getHighIndex (me, tmin), PointProcess_getLowIndex (me, tmax));
252 }
253
254 void PointProcess_draw (PointProcess me, Graphics g, double tmin, double tmax, bool garnish) {
255 Function_unidirectionalAutowindow (me, & tmin, & tmax);
256 Graphics_setWindow (g, tmin, tmax, -1.0, 1.0);
257 if (my nt) {
258 const integer imin = PointProcess_getHighIndex (me, tmin);
259 const integer imax = PointProcess_getLowIndex (me, tmax);
260 const int lineType = Graphics_inqLineType (g);
261 Graphics_setLineType (g, Graphics_DOTTED);
262 Graphics_setInner (g);
263 for (integer i = imin; i <= imax; i ++)
264 Graphics_line (g, my t [i], -1.0, my t [i], 1.0);
265 Graphics_setLineType (g, lineType);
266 Graphics_unsetInner (g);
267 }
268 if (garnish) {
269 Graphics_drawInnerBox (g);
270 Graphics_textBottom (g, true, U"Time (s)");
271 Graphics_marksBottom (g, 2, true, true, false);
272 }
273 }
274
275 double PointProcess_getInterval (PointProcess me, double t) {
276 const integer ileft = PointProcess_getLowIndex (me, t);
277 if (ileft <= 0 || ileft >= my nt)
278 return undefined;
279 return my t [ileft + 1] - my t [ileft];
280 }
281
282 autoPointProcess PointProcesses_union (PointProcess me, PointProcess thee) {
283 try {
284 autoPointProcess him = Data_copy (me);
285 if (thy xmin < my xmin)
286 his xmin = thy xmin;
287 if (thy xmax > my xmax)
288 his xmax = thy xmax;
289 for (integer i = 1; i <= thy nt; i ++)
290 PointProcess_addPoint (him.get(), thy t [i]);
291 return him;
292 } catch (MelderError) {
293 Melder_throw (me, U" & ", thee, U": union not computed.");
294 }
295 }
296
297 integer PointProcess_findPoint (PointProcess me, double t) {
298 integer left = 1, right = my nt;
299 if (my nt == 0)
300 return 0;
301 if (t < my t [left] || t > my t [right])
302 return 0;
303 while (left < right - 1) {
304 integer mid = (left + right) / 2; // tleft <= t <= tright
305 if (t == my t [mid])
306 return mid;
307 if (t > my t [mid])
308 left = mid;
309 else
310 right = mid;
311 }
312 if (t == my t [left])
313 return left;
314 if (t == my t [right])
315 return right;
316 return 0;
317 }
318
319 autoPointProcess PointProcesses_intersection (PointProcess me, PointProcess thee) {
320 try {
321 autoPointProcess him = Data_copy (me);
322 if (thy xmin > my xmin)
323 his xmin = thy xmin;
324 if (thy xmax < my xmax)
325 his xmax = thy xmax;
326 for (integer i = my nt; i >= 1; i --)
327 if (! PointProcess_findPoint (thee, my t [i]))
328 PointProcess_removePoint (him.get(), i);
329 return him;
330 } catch (MelderError) {
331 Melder_throw (me, U" & ", thee, U": intersection not computed.");
332 }
333 }
334
335 autoPointProcess PointProcesses_difference (PointProcess me, PointProcess thee) {
336 try {
337 autoPointProcess him = Data_copy (me);
338 for (integer i = my nt; i >= 1; i --)
339 if (PointProcess_findPoint (thee, my t [i]))
340 PointProcess_removePoint (him.get(), i);
341 return him;
342 } catch (MelderError) {
343 Melder_throw (me, U" & ", thee, U": difference not computed.");
344 }
345 }
346
347 void PointProcess_fill (PointProcess me, double tmin, double tmax, double period) {
348 try {
349 Function_unidirectionalAutowindow (me, & tmin, & tmax);
350 const integer n = Melder_ifloor ((tmax - tmin) / period);
351 double t = 0.5 * (tmin + tmax - n * period);
352 for (integer i = 1; i <= n; i ++, t += period)
353 PointProcess_addPoint (me, t);
354 } catch (MelderError) {
355 Melder_throw (me, U": not filled.");
356 }
357 }
358
359 void PointProcess_voice (PointProcess me, double period, double maxT) {
360 try {
361 integer ipointright;
362 double beginVoiceless = my xmin, endVoiceless;
363 for (integer ipointleft = 1; ipointleft <= my nt; ipointleft = ipointright + 1) {
364 endVoiceless = my t [ipointleft];
365 PointProcess_fill (me, beginVoiceless, endVoiceless, period);
366 for (ipointright = ipointleft + 1; ipointright <= my nt; ipointright ++)
367 if (my t [ipointright] - my t [ipointright - 1] > maxT)
368 break;
369 ipointright --;
370 beginVoiceless = my t [ipointright] + 0.005;
371 }
372 endVoiceless = my xmax;
373 PointProcess_fill (me, beginVoiceless, endVoiceless, period);
374 } catch (MelderError) {
375 Melder_throw (me, U": not voiced.");
376 }
377 }
378
379 MelderIntegerRange PointProcess_getWindowPoints (PointProcess me, double tmin, double tmax) {
380 return { PointProcess_getHighIndex (me, tmin), PointProcess_getLowIndex (me, tmax) };
381 }
382
383 static bool PointProcess_isPeriod (PointProcess me, integer ileft, double minimumPeriod, double maximumPeriod, double maximumPeriodFactor) {
384 /*
385 This function answers the question: is the interval from point 'ileft' to point 'ileft+1' a period?
386 */
387 const integer iright = ileft + 1;
388 /*
389 Period condition 1: both 'ileft' and 'iright' have to be within the point process.
390 */
391 if (ileft < 1 || iright > my nt)
392 return false;
393 /*
394 Period condition 2: the interval has to be within the boundaries, if specified.
395 */
396 if (minimumPeriod == maximumPeriod) // special input setting (typically both zero)
397 return true; // all intervals count as periods, irrespective of absolute size and relative size
398 const double interval = my t [iright] - my t [ileft];
399 if (interval <= 0.0 || interval < minimumPeriod || interval > maximumPeriod)
400 return false;
401 if (isundef (maximumPeriodFactor) || maximumPeriodFactor < 1.0)
402 return true;
403 /*
404 Period condition 3: the interval cannot be too different from both of its neigbours, if any.
405 */
406 const double previousInterval = ( ileft <= 1 ? undefined : my t [ileft] - my t [ileft - 1] );
407 const double nextInterval = ( iright >= my nt ? undefined : my t [iright + 1] - my t [iright] );
408 double previousIntervalFactor =
409 ( isdefined (previousInterval) && previousInterval > 0.0 ? interval / previousInterval : undefined );
410 double nextIntervalFactor =
411 ( isdefined (nextInterval) && nextInterval > 0.0 ? interval / nextInterval : undefined );
412 if (isundef (previousIntervalFactor) && isundef (nextIntervalFactor))
413 return true; // no neighbours: this is a period
414 if (isdefined (previousIntervalFactor) && previousIntervalFactor > 0.0 && previousIntervalFactor < 1.0)
415 previousIntervalFactor = 1.0 / previousIntervalFactor;
416 if (isdefined (nextIntervalFactor) && nextIntervalFactor > 0.0 && nextIntervalFactor < 1.0)
417 nextIntervalFactor = 1.0 / nextIntervalFactor;
418 if (isdefined (previousIntervalFactor) && previousIntervalFactor > maximumPeriodFactor &&
419 isdefined (nextIntervalFactor) && nextIntervalFactor > maximumPeriodFactor)
420 return false;
421 return true;
422 }
423
424 integer PointProcess_getNumberOfPeriods (PointProcess me, double tmin, double tmax,
425 double minimumPeriod, double maximumPeriod, double maximumPeriodFactor)
426 {
427 Function_unidirectionalAutowindow (me, & tmin, & tmax);
428 const MelderIntegerRange pointNumbers = PointProcess_getWindowPoints (me, tmin, tmax);
429 integer numberOfPeriods = 0;
430 for (integer ipoint = pointNumbers.first; ipoint < pointNumbers.last; ipoint ++)
431 numberOfPeriods += PointProcess_isPeriod (me, ipoint, minimumPeriod, maximumPeriod, maximumPeriodFactor);
432 return numberOfPeriods;
433 }
434
435 double PointProcess_getMeanPeriod (PointProcess me, double tmin, double tmax,
436 double minimumPeriod, double maximumPeriod, double maximumPeriodFactor)
437 {
438 Function_unidirectionalAutowindow (me, & tmin, & tmax);
439 const MelderIntegerRange pointNumbers = PointProcess_getWindowPoints (me, tmin, tmax);
440 integer numberOfPeriods = 0;
441 longdouble sum = 0.0;
442 for (integer ipoint = pointNumbers.first; ipoint < pointNumbers.last; ipoint ++) {
443 if (PointProcess_isPeriod (me, ipoint, minimumPeriod, maximumPeriod, maximumPeriodFactor)) {
444 numberOfPeriods ++;
445 sum += my t [ipoint + 1] - my t [ipoint];
446 }
447 }
448 return numberOfPeriods > 0 ? double (sum / numberOfPeriods) : undefined;
449 }
450
451 double PointProcess_getStdevPeriod (PointProcess me, double tmin, double tmax,
452 double minimumPeriod, double maximumPeriod, double maximumPeriodFactor)
453 {
454 Function_unidirectionalAutowindow (me, & tmin, & tmax);
455 const MelderIntegerRange pointNumbers = PointProcess_getWindowPoints (me, tmin, tmax);
456 integer numberOfPeriods = 0;
457 /*
458 Compute mean.
459 */
460 longdouble sum = 0.0;
461 for (integer ipoint = pointNumbers.first; ipoint < pointNumbers.last; ipoint ++) {
462 if (PointProcess_isPeriod (me, ipoint, minimumPeriod, maximumPeriod, maximumPeriodFactor)) {
463 numberOfPeriods ++;
464 sum += my t [ipoint + 1] - my t [ipoint];
465 }
466 }
467 constexpr integer minimumNumberOfDatapointsToComputeAStandardDeviation = 2;
468 if (numberOfPeriods < minimumNumberOfDatapointsToComputeAStandardDeviation)
469 return undefined;
470 const double mean = double (sum / numberOfPeriods);
471 /*
472 Compute sum of squares.
473 */
474 longdouble sum2 = 0.0;
475 for (integer ipoint = pointNumbers.first; ipoint < pointNumbers.last; ipoint ++) {
476 if (PointProcess_isPeriod (me, ipoint, minimumPeriod, maximumPeriod, maximumPeriodFactor)) {
477 const double dperiod = my t [ipoint + 1] - my t [ipoint] - mean;
478 sum2 += dperiod * dperiod;
479 }
480 }
481 /*
482 Compute standard deviation.
483 */
484 return sqrt (double (sum2 / (numberOfPeriods - 1)));
485 }
486
487 MelderCountAndFraction PointProcess_getCountAndFractionOfVoiceBreaks (PointProcess me,
488 double tmin, double tmax, double maximumPeriod)
489 {
490 MelderCountAndFraction result;
491 const MelderIntegerRange pointNumbers = PointProcess_getWindowPoints (me, tmin, tmax);
492 if (pointNumbers.size() > 1) {
493 result.denominator = tmax - tmin;
494 bool previousPeriodVoiced = true;
495 for (integer ipoint = pointNumbers.first + 1; ipoint < pointNumbers.last; ipoint ++) {
496 const double period = my t [ipoint] - my t [ipoint - 1];
497 if (period > maximumPeriod) {
498 result.numerator += period;
499 if (previousPeriodVoiced) {
500 result.count ++;
501 previousPeriodVoiced = false;
502 }
503 } else {
504 previousPeriodVoiced = true;
505 }
506 }
507 }
508 return result;
509 }
510
511 /* End of file PointProcess.cpp */