PointProcess.cpp 18 KB

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