Debian Codegraph @

fon/Pitch_to_PointProcess.cpp

raw
1 /* Pitch_to_PointProcess.cpp
2 *
3 * Copyright (C) 1992-2005,2007,2008,2011,2014-2018,2020,2021,2024-2026 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 /*
20 * pb 2002/07/16 GPL
21 * pb 2003/02/26 Sound_Pitch_to_PointProcess_peaks
22 * pb 2003/05/17 introduced silence threshold
23 * pb 2003/05/20 removed bug in global peak
24 * pb 2003/05/22 changed 1.2 to 1.25
25 * pb 2004/05/11 undefined pitch is `undefined` rather than 0.0
26 * pb 2004/11/01 Pitch_getVoicedIntervalAfter clips to my xmax
27 * pb 2004/11/28 repaired memory leak in Pitch_to_PointProcess
28 * pb 2004/11/28 truncated tleft in Pitch_getVoicedIntervalAfter to my xmin (otherwise, getValue can crash)
29 * pb 2005/06/16 units
30 * pb 2007/01/26 compatible with stereo sounds
31 * pb 2008/01/19 double
32 * pb 2011/06/04 C++
33 */
34
35 #include "Pitch_to_PointProcess.h"
36 #include "PitchTier_to_PointProcess.h"
37 #include "Pitch_to_PitchTier.h"
38
39 autoPointProcess Pitch_to_PointProcess (Pitch pitch) {
40 try {
41 autoPitchTier pitchTier = Pitch_to_PitchTier (pitch);
42 autoPointProcess point = PitchTier_Pitch_to_PointProcess (pitchTier.get(), pitch);
43 return point;
44 } catch (MelderError) {
45 Melder_throw (pitch, U": not converted to PointProcess.");
46 }
47 }
48
49 static int Pitch_getVoicedIntervalAfter (Pitch me, double after, double *tleft, double *tright) {
50 integer ileft = Sampled_xToHighIndex (me, after), iright;
51 if (ileft > my nx)
52 return 0; // offright
53 if (ileft < 1)
54 ileft = 1; // offleft
55
56 /*
57 Search for first voiced frame.
58 */
59 for (; ileft <= my nx; ileft ++)
60 if (Pitch_isVoiced_i (me, ileft))
61 break;
62 if (ileft > my nx)
63 return 0; // offright
64
65 /*
66 Search for last voiced frame.
67 */
68 for (iright = ileft; iright <= my nx; iright ++)
69 if (! Pitch_isVoiced_i (me, iright))
70 break;
71 iright --;
72
73 *tleft = Sampled_indexToX (me, ileft) - 0.5 * my dx; // the whole frame is considered voiced
74 *tright = Sampled_indexToX (me, iright) + 0.5 * my dx;
75 if (*tleft >= my xmax - 0.5 * my dx)
76 return 0;
77 Melder_clipLeft (my xmin, tleft);
78 Melder_clipRight (tright, my xmax);
79 if (*tright <= after)
80 return 0;
81 return 1;
82 }
83
84 static double findExtremum_3 (double *channel1_base, double *channel2_base, integer d, integer n, bool includeMaxima, bool includeMinima) {
85 const double *const channel1 = channel1_base + d;
86 const double *const channel2 = ( channel2_base ? channel2_base + d : nullptr );
87 bool includeAll = ( includeMaxima == includeMinima );
88 integer imin = 1, imax = 1, i;
89 double minimum, maximum;
90 if (n < 3) {
91 if (n <= 0)
92 return 0.0; // outside
93 else if (n == 1)
94 return 1.0;
95 else { // n == 2
96 const double x1 = ( channel2 ? 0.5 * (channel1 [1] + channel2 [1]) : channel1 [1] );
97 const double x2 = ( channel2 ? 0.5 * (channel1 [2] + channel2 [2]) : channel1 [2] );
98 const double xleft = ( includeAll ? fabs (x1) : includeMaxima ? x1 : - x1 );
99 const double xright = ( includeAll ? fabs (x2) : includeMaxima ? x2 : - x2 );
100 if (xleft > xright)
101 return 1.0;
102 else if (xleft < xright)
103 return 2.0;
104 else
105 return 1.5;
106 }
107 }
108 minimum = maximum = ( channel2 ? 0.5 * (channel1 [1] + channel2 [1]) : channel1 [1] );
109 for (i = 2; i <= n; i ++) {
110 const double value = ( channel2 ? 0.5 * (channel1 [i] + channel2 [i]) : channel1 [i] );
111 if (value < minimum) {
112 minimum = value;
113 imin = i;
114 }
115 if (value > maximum) {
116 maximum = value;
117 imax = i;
118 }
119 }
120 if (minimum == maximum)
121 return 0.5 * (n + 1.0); // all equal
122 const integer iextr = includeAll ? ( fabs (minimum) > fabs (maximum) ? imin : imax ) : includeMaxima ? imax : imin;
123 if (iextr == 1)
124 return 1.0;
125 if (iextr == n)
126 return (double) n;
127 /*
128 Parabolic interpolation.
129 We do NOT need fabs here: we look for a genuine extremum.
130 */
131 const double valueMid = ( channel2 ? 0.5 * (channel1 [iextr] + channel2 [iextr]) : channel1 [iextr] );
132 const double valueLeft = ( channel2 ? 0.5 * (channel1 [iextr - 1] + channel2 [iextr - 1]) : channel1 [iextr - 1] );
133 const double valueRight = ( channel2 ? 0.5 * (channel1 [iextr + 1] + channel2 [iextr + 1]) : channel1 [iextr + 1] );
134 return iextr + 0.5 * (valueRight - valueLeft) / (2 * valueMid - valueLeft - valueRight);
135 }
136
137 static double Sound_findExtremum (Sound me, double tmin, double tmax, bool includeMaxima, bool includeMinima) {
138 Melder_assert (isdefined (tmin));
139 Melder_assert (isdefined (tmax));
140 const integer imin = Melder_clippedLeft (1_integer, Sampled_xToLowIndex (me, tmin));
141 const integer imax = Melder_clippedRight (Sampled_xToHighIndex (me, tmax), my nx); // SMELL: check the logic
142 const double iextremum = findExtremum_3 (& my z [1] [0], my ny > 1 ? & my z [2] [0] : nullptr, imin - 1, imax - imin + 1, includeMaxima, includeMinima);
143 if (iextremum != 0.0)
144 return my x1 + (imin - 1 + iextremum - 1) * my dx;
145 else
146 return 0.5 * (tmin + tmax);
147 }
148
149 /*
150 Function `Sound_findMaximumCorrelation`
151
152 Determines at what time point between `tmin2` and `tmax2` the signal is most similar to the signal at `t1`.
153 What is actually compared is the window that runs from
154 t1 - windowLength / 2
155 to
156 t1 + windowLength / 2
157 with the window that runs from
158 t - windowLength / 2
159 to
160 t + windowLength / 2
161 for all `t` between `tmin2` and `tmax2`.
162 */
163 static double Sound_findMaximumCorrelation (Sound me, double t1, double windowLength, double tmin2, double tmax2, double *tout, double *peak) {
164 double maximumCorrelation = -1.0; // smart 'impossible' starting value
165 double r1_best = undefined, r3_best = undefined, ir = undefined; // assignments not necessary, but extra safe
166 /*
167 We hold three consecutive correlation values in a self-cycling ring buffer.
168 */
169 double r1 = 0.0, r2 = 0.0, r3 = 0.0;
170
171 const double halfWindowLength = 0.5 * windowLength;
172 const integer ileft1 = Sampled_xToNearestIndex (me, t1 - halfWindowLength);
173 const integer iright1 = Sampled_xToNearestIndex (me, t1 + halfWindowLength);
174 const integer ileft2min = Sampled_xToLowIndex (me, tmin2 - halfWindowLength);
175 const integer ileft2max = Sampled_xToHighIndex (me, tmax2 - halfWindowLength);
176 *peak = 0.0; // default
177 Melder_assert (ileft2max >= ileft2min); // if the loop is never executed, the result will be garbage
178 for (integer ileft2 = ileft2min; ileft2 <= ileft2max; ileft2 ++) {
179 longdouble norm1 = 0.0, norm2 = 0.0, product = 0.0;
180 double localPeak = 0.0;
181 for (integer ichan = 1; ichan <= my ny; ichan ++) {
182 for (integer i1 = ileft1, i2 = ileft2; i1 <= iright1; i1 ++, i2 ++) {
183 if (i1 < 1 || i1 > my nx || i2 < 1 || i2 > my nx)
184 continue;
185 const double amp1 = my z [ichan] [i1], amp2 = my z [ichan] [i2];
186 norm1 += amp1 * amp1;
187 norm2 += amp2 * amp2;
188 product += amp1 * amp2;
189 if (fabs (amp2) > localPeak)
190 localPeak = fabs (amp2);
191 }
192 }
193
194 /*
195 Cycle the ring buffer.
196 */
197 r1 = r2;
198 r2 = r3;
199 /*
200 r3 is the new autocorrelation value. It can be positive, negative, or zero.
201 If `amp1` or `amp2` is silent (i.e. all zeroes, i.e. `norm1` or `norm2` is zero),
202 then we'll safely define `r3` as 0.0.
203 */
204 if (norm1 == 0.0 || norm2 == 0.0) // one or both signals are silent
205 r3 = 0.0;
206 else
207 r3 = double (product) / sqrt (double (norm1 * norm2)); // well-defined correlation
208
209 if (r2 > maximumCorrelation /* true on first test */ && r2 >= r1 && r2 >= r3) {
210 r1_best = r1;
211 maximumCorrelation = r2;
212 r3_best = r3;
213 ir = ileft2 - 1;
214 *peak = localPeak;
215 }
216 }
217 /*
218 Improve the result by means of parabolic interpolation.
219 */
220 if (maximumCorrelation > -1.0) { // was maximumCorrelation ever assigned to?...
221 // ...then r1_best and r3_best and ir must also have been assigned to:
222 Melder_assert (isdefined (r1_best) && isdefined (r3_best) && isdefined (ir));
223 double interpolatedPeakHeight = undefined;
224 const double d2r = (maximumCorrelation - r1_best) + (maximumCorrelation - r3_best); // careful prevention of rounding error
225 if (d2r != 0.0 && ! (Melder_debug == 57)) {
226 const double dr = 0.5 * (r3_best - r1_best);
227 interpolatedPeakHeight = maximumCorrelation + 0.5 * dr * dr / d2r;
228 ir += dr / d2r;
229 }
230 const double peakTime = t1 + (ir - ileft1) * my dx; // can be interpolated or not
231 if (peakTime >= tmin2 && peakTime <= tmax2) { // NaN-safe
232 *tout = peakTime;
233 if (isdefined (interpolatedPeakHeight))
234 maximumCorrelation = interpolatedPeakHeight;
235 } else {
236 const double middleDistanceToT1 = NUMsqrt_a ((tmin2 - t1) * (tmax2 - t1)); // e.g. for 0.8*F0 and 1.25*F0 it's precisely F0
237 // crash if tmin2 and tmax are not on the same side of t1
238 const double middleTime = ( tmin2 < t1 ? t1 - middleDistanceToT1 : t1 + middleDistanceToT1 );
239 *tout = middleTime;
240 //Melder_casual (U"Sound_findMaximumCorrelation: fell back to middle time ", middleTime, U"; correlation last ",
241 // U" ", r1, U" ", r2, U" ", r3, U" best ", r1_best, U" ", maximumCorrelation, U" ", r3_best,
242 // U" interpolated ", interpolatedPeakHeight);
243 if (r3 > maximumCorrelation)
244 maximumCorrelation = r3; // HACK
245 }
246 }
247 return maximumCorrelation;
248 }
249
250 autoPointProcess Sound_Pitch_to_PointProcess_cc (Sound sound, Pitch pitch) {
251 try {
252 autoPointProcess point = PointProcess_create (sound -> xmin, sound -> xmax, 10);
253 double t = pitch -> xmin;
254 double addedRight = -1e308;
255 const double globalPeak = Vector_getAbsoluteExtremum (sound, sound -> xmin, sound -> xmax, kVector_peakInterpolation :: NONE);
256
257 /*
258 Cycle over all voiced intervals.
259 */
260 autoMelderProgress progress (U"Sound & Pitch: To PointProcess (cc)...");
261 for (;;) {
262 double tleft, tright;
263 if (! Pitch_getVoicedIntervalAfter (pitch, t, & tleft, & tright))
264 break;
265 Melder_assert (tright > t);
266
267 /*
268 Go to the middle of the voiced interval.
269 */
270 const double tmiddle = 0.5 * (tleft + tright);
271 Melder_progress ((tmiddle - sound -> xmin) / (sound -> xmax - sound -> xmin), U"Sound & Pitch to PointProcess");
272 const double f0middle = Pitch_getValueAtTime (pitch, tmiddle, kPitch_unit::HERTZ, Pitch_LINEAR);
273
274 /*
275 Our first point is near this middle.
276 */
277 if (isundef (f0middle)) {
278 Melder_crash (U"Sound_Pitch_to_PointProcess_cc:"
279 U" tleft ", tleft,
280 U", tright ", tright,
281 U", f0middle ", f0middle
282 );
283 }
284 double tmax = Sound_findExtremum (sound, tmiddle - 0.5 / f0middle, tmiddle + 0.5 / f0middle, true, true);
285 Melder_assert (isdefined (tmax));
286 PointProcess_addPoint (point.get(), tmax);
287
288 //TRACE
289 double tsave = tmax;
290 for (;;) {
291 const double f0 = Pitch_getValueAtTime (pitch, tmax, kPitch_unit::HERTZ, Pitch_LINEAR);
292 trace (U"1 F0: ", f0);
293 trace (U"1 tmax: ", tmax);
294 if (isundef (f0))
295 break;
296 double peak;
297 const double correlation = Sound_findMaximumCorrelation (sound, tmax, 1.0 / f0, tmax - 1.25 / f0, tmax - 0.8 / f0, & tmax, & peak);
298 trace (U"1 new tmax: ", tmax);
299 if (correlation == -1.0)
300 /*break*/ tmax -= 1.0 / f0; // this one period will drop out
301 if (tmax < tleft) {
302 if (correlation > 0.7 && peak > 0.023333 * globalPeak && tmax - addedRight > 0.8 / f0)
303 PointProcess_addPoint (point.get(), tmax);
304 break;
305 }
306 if (correlation > 0.3 && (peak == 0.0 || peak > 0.01 * globalPeak)) {
307 if (tmax - addedRight > 0.8 / f0) { // do not fill in a short originally unvoiced interval twice
308 trace (U"1 adding point at: ", tmax);
309 PointProcess_addPoint (point.get(), tmax);
310 }
311 }
312 }
313 tmax = tsave;
314 for (;;) {
315 const double f0 = Pitch_getValueAtTime (pitch, tmax, kPitch_unit::HERTZ, Pitch_LINEAR);
316 trace (U"2 F0: ", f0);
317 trace (U"2 tmax: ", tmax);
318 if (isundef (f0))
319 break;
320 double peak;
321 const double correlation = Sound_findMaximumCorrelation (sound, tmax, 1.0 / f0, tmax + 0.8 / f0, tmax + 1.25 / f0, & tmax, & peak);
322 trace (U"2 new tmax: ", tmax);
323 if (correlation == -1.0)
324 /*break*/ tmax += 1.0 / f0;
325 if (tmax > tright) {
326 if (correlation > 0.7 && peak > 0.023333 * globalPeak) {
327 PointProcess_addPoint (point.get(), tmax);
328 addedRight = tmax;
329 }
330 break;
331 }
332 if (correlation > 0.3 && (peak == 0.0 || peak > 0.01 * globalPeak)) {
333 trace (U"2 adding point at: ", tmax);
334 PointProcess_addPoint (point.get(), tmax);
335 addedRight = tmax;
336 }
337 }
338 t = tright;
339 }
340 return point;
341 } catch (MelderError) {
342 Melder_throw (sound, U" & ", pitch, U": not converted to PointProcess (cc).");
343 }
344 }
345
346 autoPointProcess Sound_Pitch_to_PointProcess_peaks (Sound sound, Pitch pitch, int includeMaxima, int includeMinima) {
347 try {
348 autoPointProcess point = PointProcess_create (sound -> xmin, sound -> xmax, 10);
349 double t = pitch -> xmin;
350 double addedRight = -1e308;
351 /*
352 Cycle over all voiced intervals.
353 */
354
355 autoMelderProgress progress (U"Sound & Pitch: To PointProcess (peaks)...");
356 for (;;) {
357 double tleft, tright;
358 if (! Pitch_getVoicedIntervalAfter (pitch, t, & tleft, & tright))
359 break;
360 /*
361 Go to the middle of the voiced interval.
362 */
363 const double tmiddle = 0.5 * (tleft + tright);
364 Melder_progress ((tmiddle - sound -> xmin) / (sound -> xmax - sound -> xmin), U"Sound & Pitch: To PointProcess");
365 const double f0middle = Pitch_getValueAtTime (pitch, tmiddle, kPitch_unit::HERTZ, Pitch_LINEAR);
366
367 /*
368 Our first point is near this middle.
369 */
370 Melder_assert (isdefined (f0middle));
371 double tmax = Sound_findExtremum (sound, tmiddle - 0.5 / f0middle, tmiddle + 0.5 / f0middle, includeMaxima, includeMinima);
372 Melder_assert (isdefined (tmax));
373 PointProcess_addPoint (point.get(), tmax);
374
375 double tsave = tmax;
376 for (;;) {
377 const double f0 = Pitch_getValueAtTime (pitch, tmax, kPitch_unit::HERTZ, Pitch_LINEAR);
378 if (isundef (f0))
379 break;
380 tmax = Sound_findExtremum (sound, tmax - 1.25 / f0, tmax - 0.8 / f0, includeMaxima, includeMinima);
381 if (tmax < tleft) {
382 if (tmax - addedRight > 0.8 / f0)
383 PointProcess_addPoint (point.get(), tmax);
384 break;
385 }
386 if (tmax - addedRight > 0.8 / f0) // do not fill in a short originally unvoiced interval twice
387 PointProcess_addPoint (point.get(), tmax);
388 }
389 tmax = tsave;
390 for (;;) {
391 const double f0 = Pitch_getValueAtTime (pitch, tmax, kPitch_unit::HERTZ, Pitch_LINEAR);
392 if (isundef (f0))
393 break;
394 tmax = Sound_findExtremum (sound, tmax + 0.8 / f0, tmax + 1.25 / f0, includeMaxima, includeMinima);
395 if (tmax > tright) {
396 PointProcess_addPoint (point.get(), tmax);
397 addedRight = tmax;
398 break;
399 }
400 PointProcess_addPoint (point.get(), tmax);
401 addedRight = tmax;
402 }
403 t = tright;
404 }
405 return point;
406 } catch (MelderError) {
407 Melder_throw (sound, U" & ", pitch, U": not converted to PointProcess (peaks).");
408 }
409 }
410
411 /* End of file Pitch_to_PointProcess.cpp */