Debian Codegraph @

fon/VoiceAnalysis.cpp

raw symbols SCIP
1 /* VoiceAnalysis.cpp
2 *
3 * Copyright (C) 1992-2007,2011,2012,2015-2020,2025,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 #include "VoiceAnalysis.h"
20 #include "AmplitudeTier.h"
21 #include "H1minusH2Tier.h"
22
23 double PointProcess_getJitter_local (PointProcess me, double tmin, double tmax,
24 double pmin, double pmax, double maximumPeriodFactor)
25 {
26 Function_unidirectionalAutowindow (me, & tmin, & tmax);
27 const MelderIntegerRange pointNumbers = PointProcess_getWindowPoints (me, tmin, tmax);
28 /* mutable adjust */ integer numberOfPeriods = pointNumbers.size() - 1;
29 if (numberOfPeriods < 2)
30 return undefined;
31 longdouble sum = 0.0;
32 for (integer i = pointNumbers.first + 1; i < pointNumbers.last; i ++) {
33 const double p1 = my t [i] - my t [i - 1], p2 = my t [i + 1] - my t [i];
34 const double intervalFactor = ( p1 > p2 ? p1 / p2 : p2 / p1 );
35 if (pmin == pmax || (p1 >= pmin && p1 <= pmax && p2 >= pmin && p2 <= pmax && intervalFactor <= maximumPeriodFactor)) {
36 sum += fabs (p1 - p2);
37 } else {
38 numberOfPeriods --;
39 }
40 }
41 if (numberOfPeriods < 2)
42 return undefined;
43 return double (sum / (numberOfPeriods - 1)) / PointProcess_getMeanPeriod (me, tmin, tmax, pmin, pmax, maximumPeriodFactor);
44 }
45
46 double PointProcess_getJitter_local_absolute (PointProcess me, double tmin, double tmax,
47 double pmin, double pmax, double maximumPeriodFactor)
48 {
49 Function_unidirectionalAutowindow (me, & tmin, & tmax);
50 const MelderIntegerRange pointNumbers = PointProcess_getWindowPoints (me, tmin, tmax);
51 /* mutable adjust */ integer numberOfPeriods = pointNumbers.size() - 1;
52 if (numberOfPeriods < 2)
53 return undefined;
54 longdouble sum = 0.0;
55 for (integer i = pointNumbers.first + 1; i < pointNumbers.last; i ++) {
56 const double p1 = my t [i] - my t [i - 1];
57 const double p2 = my t [i + 1] - my t [i];
58 const double intervalFactor = ( p1 > p2 ? p1 / p2 : p2 / p1 );
59 if (pmin == pmax || (p1 >= pmin && p1 <= pmax && p2 >= pmin && p2 <= pmax && intervalFactor <= maximumPeriodFactor)) {
60 sum += fabs (p1 - p2);
61 } else {
62 numberOfPeriods --;
63 }
64 }
65 if (numberOfPeriods < 2)
66 return undefined;
67 return double (sum / (numberOfPeriods - 1));
68 }
69
70 double PointProcess_getJitter_rap (PointProcess me, double tmin, double tmax,
71 double pmin, double pmax, double maximumPeriodFactor)
72 {
73 Function_unidirectionalAutowindow (me, & tmin, & tmax);
74 const MelderIntegerRange pointNumbers = PointProcess_getWindowPoints (me, tmin, tmax);
75 /* mutable adjust */ integer numberOfPeriods = pointNumbers.size() - 1;
76 if (numberOfPeriods < 3)
77 return undefined;
78 longdouble sum = 0.0;
79 for (integer i = pointNumbers.first + 2; i < pointNumbers.last; i ++) {
80 const double p1 = my t [i - 1] - my t [i - 2];
81 const double p2 = my t [i] - my t [i - 1];
82 const double p3 = my t [i + 1] - my t [i];
83 const double intervalFactor1 = ( p1 > p2 ? p1 / p2 : p2 / p1 );
84 const double intervalFactor2 = ( p2 > p3 ? p2 / p3 : p3 / p2 );
85 if (pmin == pmax || (p1 >= pmin && p1 <= pmax && p2 >= pmin && p2 <= pmax && p3 >= pmin && p3 <= pmax
86 && intervalFactor1 <= maximumPeriodFactor && intervalFactor2 <= maximumPeriodFactor))
87 {
88 sum += fabs (p2 - (p1 + p2 + p3) / 3.0);
89 } else {
90 numberOfPeriods --;
91 }
92 }
93 if (numberOfPeriods < 3)
94 return undefined;
95 return double (sum / (numberOfPeriods - 2)) / PointProcess_getMeanPeriod (me, tmin, tmax, pmin, pmax, maximumPeriodFactor);
96 }
97
98 double PointProcess_getJitter_ppq5 (PointProcess me, double tmin, double tmax,
99 double pmin, double pmax, double maximumPeriodFactor)
100 {
101 Function_unidirectionalAutowindow (me, & tmin, & tmax);
102 const MelderIntegerRange pointNumbers = PointProcess_getWindowPoints (me, tmin, tmax);
103 /* mutable adjust */ integer numberOfPeriods = pointNumbers.size() - 1;
104 if (numberOfPeriods < 5)
105 return undefined;
106 longdouble sum = 0.0;
107 for (integer i = pointNumbers.first + 5; i <= pointNumbers.last; i ++) {
108 const double p1 = my t [i - 4] - my t [i - 5];
109 const double p2 = my t [i - 3] - my t [i - 4];
110 const double p3 = my t [i - 2] - my t [i - 3];
111 const double p4 = my t [i - 1] - my t [i - 2];
112 const double p5 = my t [i] - my t [i - 1];
113 const double f1 = ( p1 > p2 ? p1 / p2 : p2 / p1 );
114 const double f2 = ( p2 > p3 ? p2 / p3 : p3 / p2 );
115 const double f3 = ( p3 > p4 ? p3 / p4 : p4 / p3 );
116 const double f4 = ( p4 > p5 ? p4 / p5 : p5 / p4 );
117 if (pmin == pmax || (p1 >= pmin && p1 <= pmax && p2 >= pmin && p2 <= pmax && p3 >= pmin && p3 <= pmax &&
118 p4 >= pmin && p4 <= pmax && p5 >= pmin && p5 <= pmax &&
119 f1 <= maximumPeriodFactor && f2 <= maximumPeriodFactor && f3 <= maximumPeriodFactor && f4 <= maximumPeriodFactor))
120 {
121 sum += fabs (p3 - (p1 + p2 + p3 + p4 + p5) / 5.0);
122 } else {
123 numberOfPeriods --;
124 }
125 }
126 if (numberOfPeriods < 5)
127 return undefined;
128 return double (sum / (numberOfPeriods - 4)) / PointProcess_getMeanPeriod (me, tmin, tmax, pmin, pmax, maximumPeriodFactor);
129 }
130
131 double PointProcess_getJitter_ddp (PointProcess me, double tmin, double tmax,
132 double pmin, double pmax, double maximumPeriodFactor)
133 {
134 const double rap = PointProcess_getJitter_rap (me, tmin, tmax, pmin, pmax, maximumPeriodFactor);
135 return ( isdefined (rap) ? 3.0 * rap : undefined );
136 }
137
138 double PointProcess_Sound_getShimmer_local (PointProcess me, Sound thee, double tmin, double tmax,
139 double pmin, double pmax, double maximumPeriodFactor, double maximumAmplitudeFactor)
140 {
141 try {
142 Function_unidirectionalAutowindow (me, & tmin, & tmax);
143 autoAmplitudeTier peaks = PointProcess_Sound_to_AmplitudeTier_period (me, thee, tmin, tmax, pmin, pmax, maximumPeriodFactor);
144 return AmplitudeTier_getShimmer_local_u (peaks.get(), pmin, pmax, maximumAmplitudeFactor);
145 } catch (MelderError) {
146 if (Melder_hasError (U"Too few pulses between ")) {
147 Melder_clearError ();
148 return undefined;
149 } else {
150 Melder_throw (me, U" & ", thee, U": shimmer (local) not computed.");
151 }
152 }
153 }
154
155 double PointProcess_Sound_getShimmer_local_dB (PointProcess me, Sound thee, double tmin, double tmax,
156 double pmin, double pmax, double maximumPeriodFactor, double maximumAmplitudeFactor)
157 {
158 try {
159 Function_unidirectionalAutowindow (me, & tmin, & tmax);
160 autoAmplitudeTier peaks = PointProcess_Sound_to_AmplitudeTier_period (me, thee, tmin, tmax, pmin, pmax, maximumPeriodFactor);
161 return AmplitudeTier_getShimmer_local_dB_u (peaks.get(), pmin, pmax, maximumAmplitudeFactor);
162 } catch (MelderError) {
163 if (Melder_hasError (U"Too few pulses between ")) {
164 Melder_clearError ();
165 return undefined;
166 } else {
167 Melder_throw (me, U" & ", thee, U": shimmer (local, dB) not computed.");
168 }
169 }
170 }
171
172 double PointProcess_Sound_getShimmer_apq3 (PointProcess me, Sound thee, double tmin, double tmax,
173 double pmin, double pmax, double maximumPeriodFactor, double maximumAmplitudeFactor)
174 {
175 try {
176 Function_unidirectionalAutowindow (me, & tmin, & tmax);
177 autoAmplitudeTier peaks = PointProcess_Sound_to_AmplitudeTier_period (me, thee, tmin, tmax, pmin, pmax, maximumPeriodFactor);
178 return AmplitudeTier_getShimmer_apq3_u (peaks.get(), pmin, pmax, maximumAmplitudeFactor);
179 } catch (MelderError) {
180 if (Melder_hasError (U"Too few pulses between ")) {
181 Melder_clearError ();
182 return undefined;
183 } else {
184 Melder_throw (me, U" & ", thee, U": shimmer (apq3) not computed.");
185 }
186 }
187 }
188
189 double PointProcess_Sound_getShimmer_apq5 (PointProcess me, Sound thee, double tmin, double tmax,
190 double pmin, double pmax, double maximumPeriodFactor, double maximumAmplitudeFactor)
191 {
192 try {
193 Function_unidirectionalAutowindow (me, & tmin, & tmax);
194 autoAmplitudeTier peaks = PointProcess_Sound_to_AmplitudeTier_period (me, thee, tmin, tmax, pmin, pmax, maximumPeriodFactor);
195 return AmplitudeTier_getShimmer_apq5_u (peaks.get(), pmin, pmax, maximumAmplitudeFactor);
196 } catch (MelderError) {
197 if (Melder_hasError (U"Too few pulses between ")) {
198 Melder_clearError ();
199 return undefined;
200 } else {
201 Melder_throw (me, U" & ", thee, U": shimmer (apq5) not computed.");
202 }
203 }
204 }
205
206 double PointProcess_Sound_getShimmer_apq11 (PointProcess me, Sound thee, double tmin, double tmax,
207 double pmin, double pmax, double maximumPeriodFactor, double maximumAmplitudeFactor)
208 {
209 try {
210 Function_unidirectionalAutowindow (me, & tmin, & tmax);
211 autoAmplitudeTier peaks = PointProcess_Sound_to_AmplitudeTier_period (me, thee, tmin, tmax, pmin, pmax, maximumPeriodFactor);
212 return AmplitudeTier_getShimmer_apq11_u (peaks.get(), pmin, pmax, maximumAmplitudeFactor);
213 } catch (MelderError) {
214 if (Melder_hasError (U"Too few pulses between ")) {
215 Melder_clearError ();
216 return undefined;
217 } else {
218 Melder_throw (me, U" & ", thee, U": shimmer (apq11) not computed.");
219 }
220 }
221 }
222
223 double PointProcess_Sound_getShimmer_dda (PointProcess me, Sound thee, double tmin, double tmax,
224 double pmin, double pmax, double maximumPeriodFactor, double maximumAmplitudeFactor)
225 {
226 try {
227 Function_unidirectionalAutowindow (me, & tmin, & tmax);
228 autoAmplitudeTier peaks = PointProcess_Sound_to_AmplitudeTier_period (me, thee, tmin, tmax, pmin, pmax, maximumPeriodFactor);
229 const double apq3 = AmplitudeTier_getShimmer_apq3_u (peaks.get(), pmin, pmax, maximumAmplitudeFactor);
230 return ( isdefined (apq3) ? 3.0 * apq3 : undefined );
231 } catch (MelderError) {
232 if (Melder_hasError (U"Too few pulses between ")) {
233 Melder_clearError ();
234 return undefined;
235 } else {
236 Melder_throw (me, U" & ", thee, U": shimmer (dda) not computed.");
237 }
238 }
239 }
240
241 void PointProcess_Sound_getShimmer_multi (PointProcess me, Sound thee, double tmin, double tmax,
242 double pmin, double pmax, double maximumPeriodFactor, double maximumAmplitudeFactor,
243 double *local, double *local_dB, double *apq3, double *apq5, double *apq11, double *dda)
244 {
245 try {
246 Function_unidirectionalAutowindow (me, & tmin, & tmax);
247 autoAmplitudeTier peaks = PointProcess_Sound_to_AmplitudeTier_period (me, thee, tmin, tmax, pmin, pmax, maximumPeriodFactor);
248 if (local) *local = AmplitudeTier_getShimmer_local_u (peaks.get(), pmin, pmax, maximumAmplitudeFactor);
249 if (local_dB) *local_dB = AmplitudeTier_getShimmer_local_dB_u (peaks.get(), pmin, pmax, maximumAmplitudeFactor);
250 if (apq3) *apq3 = AmplitudeTier_getShimmer_apq3_u (peaks.get(), pmin, pmax, maximumAmplitudeFactor);
251 if (apq5) *apq5 = AmplitudeTier_getShimmer_apq5_u (peaks.get(), pmin, pmax, maximumAmplitudeFactor);
252 if (apq11) *apq11 = AmplitudeTier_getShimmer_apq11_u (peaks.get(), pmin, pmax, maximumAmplitudeFactor);
253 if (dda) *dda = 3.0 * AmplitudeTier_getShimmer_apq3_u (peaks.get(), pmin, pmax, maximumAmplitudeFactor);
254 } catch (MelderError) {
255 if (Melder_hasError (U"Too few pulses between ")) {
256 Melder_clearError ();
257 if (local) *local = undefined;
258 if (local_dB) *local_dB = undefined;
259 if (apq3) *apq3 = undefined;
260 if (apq5) *apq5 = undefined;
261 if (apq11) *apq11 = undefined;
262 if (dda) *dda = undefined;
263 } else {
264 Melder_throw (me, U" & ", thee, U": shimmer measures not computed.");
265 }
266 }
267 }
268
269 void Sound_Pitch_PointProcess_voiceReport (Sound sound, Pitch pitch, PointProcess pulses, double tmin, double tmax,
270 double floor, double ceiling, double maximumPeriodFactor, double maximumAmplitudeFactor, double silenceThreshold, double voicingThreshold)
271 {
272 try {
273 Function_unidirectionalAutowindow (sound, & tmin, & tmax);
274 /*
275 Time domain. Should be preceded by something like "Time range of SELECTION:" or so.
276 */
277 MelderInfo_writeLine (U" From ", Melder_fixed (tmin, 6), U" to ", Melder_fixed (tmax, 6), U" seconds",
278 U" (duration: ", Melder_fixed (tmax - tmin, 6), U" seconds)"
279 );
280 /*
281 Pitch statistics.
282 */
283 const double medianPitch = Pitch_getQuantile (pitch, tmin, tmax, 0.50, kPitch_unit::HERTZ);
284 const double meanPitch = Pitch_getMean (pitch, tmin, tmax, kPitch_unit::HERTZ);
285 const double stdevPitch = Pitch_getStandardDeviation (pitch, tmin, tmax, kPitch_unit::HERTZ);
286 const double minimumPitch = Pitch_getMinimum (pitch, tmin, tmax, kPitch_unit::HERTZ, 1);
287 const double maximumPitch = Pitch_getMaximum (pitch, tmin, tmax, kPitch_unit::HERTZ, 1);
288 MelderInfo_writeLine (U"Pitch:");
289 MelderInfo_writeLine (U" Median pitch: ", Melder_fixed (medianPitch, 3), U" Hz");
290 MelderInfo_writeLine (U" Mean pitch: ", Melder_fixed (meanPitch, 3), U" Hz");
291 MelderInfo_writeLine (U" Standard deviation: ", Melder_fixed (stdevPitch, 3), U" Hz");
292 MelderInfo_writeLine (U" Minimum pitch: ", Melder_fixed (minimumPitch, 3), U" Hz");
293 MelderInfo_writeLine (U" Maximum pitch: ", Melder_fixed (maximumPitch, 3), U" Hz");
294 /*
295 Pulses statistics.
296 */
297 const double pmin = 0.8 / ceiling, pmax = 1.25 / floor; // minimum period, maximum period (abbreviated for space)
298 const MelderIntegerRange pulseNumbers = PointProcess_getWindowPoints (pulses, tmin, tmax);
299 const integer numberOfPeriods = PointProcess_getNumberOfPeriods (pulses, tmin, tmax, pmin, pmax, maximumPeriodFactor);
300 const double meanPeriod = PointProcess_getMeanPeriod (pulses, tmin, tmax, pmin, pmax, maximumPeriodFactor);
301 const double stdevPeriod = PointProcess_getStdevPeriod (pulses, tmin, tmax, pmin, pmax, maximumPeriodFactor);
302 MelderInfo_writeLine (U"Pulses:");
303 MelderInfo_writeLine (U" Number of pulses: ", pulseNumbers.size());
304 MelderInfo_writeLine (U" Number of periods: ", numberOfPeriods);
305 MelderInfo_writeLine (U" Mean period: ", Melder_fixedExponent (meanPeriod, -3, 6), U" seconds");
306 MelderInfo_writeLine (U" Standard deviation of period: ", Melder_fixedExponent (stdevPeriod, -3, 6), U" seconds");
307 /*
308 Voicing.
309 */
310 const MelderFraction unvoicedFraction =
311 Pitch_getFractionOfLocallyUnvoicedFrames (pitch, tmin, tmax, ceiling, silenceThreshold, voicingThreshold);
312 const MelderCountAndFraction breaks =
313 PointProcess_getCountAndFractionOfVoiceBreaks (pulses, tmin, tmax, pmax);
314 MelderInfo_writeLine (U"Voicing:");
315 MelderInfo_writeLine (U" Fraction of locally unvoiced frames: ", Melder_percent (unvoicedFraction.get(), 3),
316 U" (", unvoicedFraction.numerator, U" / ", unvoicedFraction.denominator, U")"
317 );
318 MelderInfo_writeLine (U" Number of voice breaks: ", breaks.count);
319 MelderInfo_writeLine (U" Degree of voice breaks: ", Melder_percent (breaks.getFraction (), 3),
320 U" (", Melder_fixed (breaks.numerator, 6), U" seconds / ", Melder_fixed (breaks.denominator, 6), U" seconds)"
321 );
322 /*
323 Jitter.
324 */
325 const double jitterLocal = PointProcess_getJitter_local (pulses, tmin, tmax, pmin, pmax, maximumPeriodFactor);
326 const double jitterLocal_s = PointProcess_getJitter_local_absolute (pulses, tmin, tmax, pmin, pmax, maximumPeriodFactor);
327 const double rap = PointProcess_getJitter_rap (pulses, tmin, tmax, pmin, pmax, maximumPeriodFactor);
328 const double ppq5 = PointProcess_getJitter_ppq5 (pulses, tmin, tmax, pmin, pmax, maximumPeriodFactor);
329 const double ddp = PointProcess_getJitter_ddp (pulses, tmin, tmax, pmin, pmax, maximumPeriodFactor);
330 MelderInfo_writeLine (U"Jitter:");
331 MelderInfo_writeLine (U" Jitter (local): ", Melder_percent (jitterLocal, 3));
332 MelderInfo_writeLine (U" Jitter (local, absolute): ", Melder_fixedExponent (jitterLocal_s, -6, 3), U" seconds");
333 MelderInfo_writeLine (U" Jitter (rap): ", Melder_percent (rap, 3));
334 MelderInfo_writeLine (U" Jitter (ppq5): ", Melder_percent (ppq5, 3));
335 MelderInfo_writeLine (U" Jitter (ddp): ", Melder_percent (ddp, 3));
336 /*
337 Shimmer.
338 */
339 double shimmerLocal, shimmerLocal_dB, apq3, apq5, apq11, dda;
340 PointProcess_Sound_getShimmer_multi (pulses, sound, tmin, tmax, pmin, pmax,
341 maximumPeriodFactor, maximumAmplitudeFactor,
342 & shimmerLocal, & shimmerLocal_dB, & apq3, & apq5, & apq11, & dda
343 );
344 MelderInfo_writeLine (U"Shimmer:");
345 MelderInfo_writeLine (U" Shimmer (local): ", Melder_percent (shimmerLocal, 3));
346 MelderInfo_writeLine (U" Shimmer (local, dB): ", Melder_fixed (shimmerLocal_dB, 3), U" dB");
347 MelderInfo_writeLine (U" Shimmer (apq3): ", Melder_percent (apq3, 3));
348 MelderInfo_writeLine (U" Shimmer (apq5): ", Melder_percent (apq5, 3));
349 MelderInfo_writeLine (U" Shimmer (apq11): ", Melder_percent (apq11, 3));
350 MelderInfo_writeLine (U" Shimmer (dda): ", Melder_percent (dda, 3));
351 /*
352 Harmonicity.
353 */
354 const double meanAutocorrelation = Pitch_getMeanStrength (pitch, tmin, tmax, Pitch_STRENGTH_UNIT_AUTOCORRELATION);
355 const double meanNHR = Pitch_getMeanStrength (pitch, tmin, tmax, Pitch_STRENGTH_UNIT_NOISE_HARMONICS_RATIO);
356 const double meanHNR_dB = Pitch_getMeanStrength (pitch, tmin, tmax, Pitch_STRENGTH_UNIT_HARMONICS_NOISE_DB);
357 MelderInfo_writeLine (U"Harmonicity of the voiced parts only:");
358 MelderInfo_writeLine (U" Mean autocorrelation: ", Melder_fixed (meanAutocorrelation, 6));
359 MelderInfo_writeLine (U" Mean noise-to-harmonics ratio: ", Melder_fixed (meanNHR, 6));
360 MelderInfo_writeLine (U" Mean harmonics-to-noise ratio: ", Melder_fixed (meanHNR_dB, 3), U" dB");
361 /*
362 H1 minus H2.
363 */
364 autoH1minusH2Tier h1minusH2 = PointProcess_Sound_to_H1minusH2Tier_u (pulses, sound, tmin, tmax, pmin, pmax, maximumPeriodFactor);
365 const double meanOfThePoints = RealTier_getMean_points_u (h1minusH2.get(), tmin, tmax);
366 const double meanOfTheCurve = RealTier_getMean_curve_u (h1minusH2.get(), tmin, tmax);
367 MelderInfo_writeLine (U"H1-minus-H2:");
368 MelderInfo_writeLine (U" Mean of the points: ", Melder_fixed (meanOfThePoints, 3), U" dB");
369 MelderInfo_writeLine (U" Mean of the curve: ", Melder_fixed (meanOfTheCurve, 3), U" dB");
370 } catch (MelderError) {
371 Melder_throw (sound, U" & ", pitch, U" & ", pulses, U": voice report not computed.");
372 }
373 }
374
375 /* End of file VoiceAnalysis.cpp */