Debian Codegraph @

fon/AmplitudeTier.cpp

raw symbols SCIP
1 /* AmplitudeTier.cpp
2 *
3 * Copyright (C) 2003-2012,2014-2022,2024,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 "AmplitudeTier.h"
20
21 Thing_implement (AmplitudeTier, RealTier, 0);
22
23 autoAmplitudeTier AmplitudeTier_create (double tmin, double tmax) {
24 try {
25 autoAmplitudeTier me = Thing_new (AmplitudeTier);
26 RealTier_init (me.get(), tmin, tmax);
27 return me;
28 } catch (MelderError) {
29 Melder_throw (U"AmplitudeTier not created.");
30 }
31 }
32
33 void AmplitudeTier_draw (AmplitudeTier me, Graphics g, double tmin, double tmax,
34 double ymin, double ymax, conststring32 method, bool garnish)
35 {
36 RealTier_draw (me, g, tmin, tmax, ymin, ymax, garnish, method, U"Sound pressure (Pa)");
37 }
38
39 autoAmplitudeTier PointProcess_upto_AmplitudeTier (PointProcess me, double soundPressure) {
40 try {
41 autoAmplitudeTier thee = PointProcess_upto_RealTier (me, soundPressure, classAmplitudeTier).static_cast_move <structAmplitudeTier>();
42 return thee;
43 } catch (MelderError) {
44 Melder_throw (me, U": not converted to AmplitudeTier.");
45 }
46 }
47
48 autoAmplitudeTier RealTier_to_AmplitudeTier (RealTier me) {
49 try {
50 autoAmplitudeTier thee = Thing_new (AmplitudeTier);
51 my structRealTier :: v1_copy (thee.get());
52 return thee;
53 } catch (MelderError) {
54 Melder_throw (me, U": not converted to AmplitudeTier.");
55 }
56 }
57
58 autoAmplitudeTier IntensityTier_to_AmplitudeTier (IntensityTier me) {
59 try {
60 autoAmplitudeTier thee = Thing_new (AmplitudeTier);
61 my structRealTier :: v1_copy (thee.get());
62 for (integer i = 1; i <= thy points.size; i ++) {
63 const mutableRealPoint point = thy points.at [i];
64 point -> value = pow (10.0, point -> value / 20.0) * 2.0e-5;
65 }
66 return thee;
67 } catch (MelderError) {
68 Melder_throw (me, U": not converted to AmplitudeTier.");
69 }
70 }
71
72 autoIntensityTier AmplitudeTier_to_IntensityTier (AmplitudeTier me, double threshold_dB) {
73 try {
74 const double threshold_Pa = pow (10.0, threshold_dB / 20.0) * 2.0e-5; // often zero!
75 autoIntensityTier thee = Thing_new (IntensityTier);
76 my structRealTier :: v1_copy (thee.get());
77 for (integer i = 1; i <= thy points.size; i ++) {
78 const mutableRealPoint point = thy points.at [i];
79 const double absoluteValue = fabs (point -> value);
80 point -> value = ( absoluteValue <= threshold_Pa ? threshold_dB : 20.0 * log10 (absoluteValue / 2.0e-5) );
81 }
82 return thee;
83 } catch (MelderError) {
84 Melder_throw (me, U": not converted to IntensityTier.");
85 }
86 }
87
88 autoTableOfReal AmplitudeTier_downto_TableOfReal (AmplitudeTier me) {
89 return RealTier_downto_TableOfReal (me, U"Time (s)", U"Sound pressure (Pa)");
90 }
91
92 void Sound_AmplitudeTier_multiply_inplace (Sound me, AmplitudeTier amplitude) {
93 if (amplitude -> points.size == 0)
94 return;
95 for (integer isamp = 1; isamp <= my nx; isamp ++) {
96 const double t = my x1 + (isamp - 1) * my dx;
97 const double factor = RealTier_getValueAtTime (amplitude, t);
98 for (integer channel = 1; channel <= my ny; channel ++)
99 my z [channel] [isamp] *= factor;
100 }
101 }
102
103 autoSound Sound_AmplitudeTier_multiply (Sound me, AmplitudeTier amplitude) {
104 try {
105 autoSound thee = Data_copy (me);
106 Sound_AmplitudeTier_multiply_inplace (thee.get(), amplitude);
107 Vector_scale (thee.get(), 0.9);
108 return thee;
109 } catch (MelderError) {
110 Melder_throw (me, U": not multiplied by ", amplitude, U".");
111 }
112 }
113
114 autoAmplitudeTier PointProcess_Sound_to_AmplitudeTier_point (PointProcess me, Sound you) {
115 try {
116 const MelderIntegerRange peaks = PointProcess_getWindowPoints (me, my xmin, my xmax);
117 if (peaks.size() < 3)
118 return autoAmplitudeTier();
119 autoAmplitudeTier him = AmplitudeTier_create (my xmin, my xmax);
120 for (integer i = peaks.first; i <= peaks.last; i ++) {
121 const double value = Vector_getValueAtX (you, my t [i], Vector_CHANNEL_AVERAGE, kVector_valueInterpolation :: SINC700);
122 if (isdefined (value))
123 RealTier_addPoint (him.get(), my t [i], value);
124 }
125 return him;
126 } catch (MelderError) {
127 Melder_throw (me, U" & ", you, U": not converted to AmplitudeTier.");
128 }
129 }
130 /*
131 static double Sound_getPeak (Sound me, double tmin, double tmax, integer channel) {
132 double *y = my z [channel];
133 integer imin, imax;
134 if (Sampled_getWindowSamples (me, tmin, tmax, & imin, & imax) < 3) return undefined;
135 double minimum = y [imin];
136 double maximum = y [imin];
137 integer sampleOfMinimum = imin;
138 integer sampleOfMaximum = imin;
139 for (integer i = imin + 1; i <= imax; i ++) {
140 if (y [i] < minimum) { minimum = y [i]; sampleOfMinimum = i; }
141 if (y [i] > maximum) { maximum = y [i]; sampleOfMaximum = i; }
142 }
143 double timeOfMinimum = my x1 + (sampleOfMinimum - 1) * my dx;
144 double timeOfMaximum = my x1 + (sampleOfMaximum - 1) * my dx;
145 Vector_getMinimumAndX (me, timeOfMinimum - my dx, timeOfMinimum + my dx, NUM_PEAK_INTERPOLATE_SINC70, & minimum, & timeOfMinimum);
146 Vector_getMaximumAndX (me, timeOfMaximum - my dx, timeOfMaximum + my dx, NUM_PEAK_INTERPOLATE_SINC70, & maximum, & timeOfMaximum);
147 return maximum - minimum;
148 }
149 */
150 static double Sound_getHannWindowedRms (Sound me, double tmid, double widthLeft, double widthRight) {
151 integer imin, imax;
152 if (Sampled_getWindowSamples (me, tmid - widthLeft, tmid + widthRight, & imin, & imax) < 3)
153 return undefined;
154 longdouble sumOfSquares = 0.0, windowSumOfSquares = 0.0;
155 for (integer i = imin; i <= imax; i ++) {
156 const double t = my x1 + (i - 1) * my dx;
157 const double width = t < tmid ? widthLeft : widthRight;
158 const double windowPhase = (t - tmid) / width; /* in [-1 .. 1] */
159 const double window = 0.5 + 0.5 * cos (NUMpi * windowPhase); // Hann
160 const double windowedValue = ( my ny == 1 ? my z [1] [i] : 0.5 * (my z [1] [i] + my z [2] [i]) ) * window;
161 sumOfSquares += sqr (windowedValue);
162 windowSumOfSquares += sqr (window);
163 }
164 return sqrt (double (sumOfSquares / windowSumOfSquares));
165 }
166 autoAmplitudeTier PointProcess_Sound_to_AmplitudeTier_period (PointProcess me, Sound you, double tmin, double tmax,
167 double pmin, double pmax, double maximumPeriodFactor)
168 {
169 try {
170 Function_unidirectionalAutowindow (me, & tmin, & tmax);
171 const MelderIntegerRange peaks = PointProcess_getWindowPoints (me, tmin, tmax);
172 if (peaks.size() < 3)
173 Melder_throw (U"Too few pulses between ", tmin, U" and ", tmax, U" seconds.");
174 autoAmplitudeTier him = AmplitudeTier_create (tmin, tmax);
175 for (integer i = peaks.first + 1; i < peaks.last; i ++) {
176 const double p1 = my t [i] - my t [i - 1];
177 const double p2 = my t [i + 1] - my t [i];
178 const double intervalFactor = ( p1 > p2 ? p1 / p2 : p2 / p1 );
179 if (pmin == pmax || (p1 >= pmin && p1 <= pmax && p2 >= pmin && p2 <= pmax && intervalFactor <= maximumPeriodFactor)) {
180 const double peak = Sound_getHannWindowedRms (you, my t [i], 0.2 * p1, 0.2 * p2);
181 if (isdefined (peak) && peak > 0.0)
182 RealTier_addPoint (him.get(), my t [i], peak);
183 }
184 }
185 return him;
186 } catch (MelderError) {
187 Melder_throw (me, U" & ", you, U": not converted to AmplitudeTier.");
188 }
189 }
190 double AmplitudeTier_getShimmer_local_u (AmplitudeTier me, double pmin, double pmax, double maximumAmplitudeFactor) {
191 /* mutable count */ integer numberOfPeaks = 0;
192 /* mutable accumulate */ longdouble numerator = 0.0, denominator = 0.0;
193 RealPoint *points = & my points.at [0];
194 for (integer i = 2; i <= my points.size; i ++) {
195 const double p = points [i] -> number - points [i - 1] -> number;
196 if (pmin == pmax || (p >= pmin && p <= pmax)) {
197 const double a1 = points [i - 1] -> value, a2 = points [i] -> value;
198 const double amplitudeFactor = ( a1 > a2 ? a1 / a2 : a2 / a1 );
199 if (amplitudeFactor <= maximumAmplitudeFactor) {
200 numerator += fabs (a1 - a2);
201 numberOfPeaks ++;
202 }
203 }
204 }
205 if (numberOfPeaks < 1)
206 return undefined;
207 numerator /= numberOfPeaks;
208 numberOfPeaks = 0;
209 for (integer i = 1; i < my points.size; i ++) {
210 denominator += points [i] -> value;
211 numberOfPeaks ++;
212 }
213 denominator /= numberOfPeaks;
214 if (denominator == 0.0)
215 return undefined;
216 return double (numerator / denominator);
217 }
218 double AmplitudeTier_getShimmer_local_dB_u (AmplitudeTier me, double pmin, double pmax, double maximumAmplitudeFactor) {
219 /* mutable count */ integer numberOfPeaks = 0;
220 /* mutable accumulate */ longdouble result = 0.0;
221 RealPoint *points = & my points.at [0];
222 for (integer i = 2; i <= my points.size; i ++) {
223 const double p = points [i] -> number - points [i - 1] -> number;
224 if (pmin == pmax || (p >= pmin && p <= pmax)) {
225 const double a1 = points [i - 1] -> value, a2 = points [i] -> value;
226 const double amplitudeFactor = ( a1 > a2 ? a1 / a2 : a2 / a1 );
227 if (amplitudeFactor <= maximumAmplitudeFactor) {
228 result += fabs (log10 (a1 / a2));
229 numberOfPeaks ++;
230 }
231 }
232 }
233 if (numberOfPeaks < 1)
234 return undefined;
235 result /= numberOfPeaks;
236 return double (20.0 * result);
237 }
238 double AmplitudeTier_getShimmer_apq3_u (AmplitudeTier me, double pmin, double pmax, double maximumAmplitudeFactor) {
239 /* mutable count */ integer numberOfPeaks = 0;
240 /* mutable accumulate */ longdouble numerator = 0.0, denominator = 0.0;
241 RealPoint *points = & my points.at [0];
242 for (integer i = 2; i <= my points.size - 1; i ++) {
243 const double p1 = points [i] -> number - points [i - 1] -> number;
244 const double p2 = points [i + 1] -> number - points [i] -> number;
245 if (pmin == pmax || (p1 >= pmin && p1 <= pmax && p2 >= pmin && p2 <= pmax)) {
246 const double a1 = points [i - 1] -> value;
247 const double a2 = points [i] -> value;
248 const double a3 = points [i + 1] -> value;
249 const double f1 = ( a1 > a2 ? a1 / a2 : a2 / a1 );
250 const double f2 = ( a2 > a3 ? a2 / a3 : a3 / a2 );
251 if (f1 <= maximumAmplitudeFactor && f2 <= maximumAmplitudeFactor) {
252 const double threePointAverage = (a1 + a2 + a3) / 3.0;
253 numerator += fabs (a2 - threePointAverage);
254 numberOfPeaks ++;
255 }
256 }
257 }
258 if (numberOfPeaks < 1)
259 return undefined;
260 numerator /= numberOfPeaks;
261 numberOfPeaks = 0;
262 for (integer i = 1; i < my points.size; i ++) {
263 denominator += points [i] -> value;
264 numberOfPeaks ++;
265 }
266 denominator /= numberOfPeaks;
267 if (denominator == 0.0)
268 return undefined;
269 return double (numerator / denominator);
270 }
271 double AmplitudeTier_getShimmer_apq5_u (AmplitudeTier me, double pmin, double pmax, double maximumAmplitudeFactor) {
272 /* mutable count */ integer numberOfPeaks = 0;
273 /* mutable accumulate */ longdouble numerator = 0.0, denominator = 0.0;
274 RealPoint *points = & my points.at [0];
275 for (integer i = 3; i <= my points.size - 2; i ++) {
276 const double p1 = points [i - 1] -> number - points [i - 2] -> number;
277 const double p2 = points [i] -> number - points [i - 1] -> number;
278 const double p3 = points [i + 1] -> number - points [i] -> number;
279 const double p4 = points [i + 2] -> number - points [i + 1] -> number;
280 if (pmin == pmax || (p1 >= pmin && p1 <= pmax && p2 >= pmin && p2 <= pmax
281 && p3 >= pmin && p3 <= pmax && p4 >= pmin && p4 <= pmax))
282 {
283 const double a1 = points [i - 2] -> value;
284 const double a2 = points [i - 1] -> value;
285 const double a3 = points [i] -> value;
286 const double a4 = points [i + 1] -> value;
287 const double a5 = points [i + 2] -> value;
288 const double f1 = ( a1 > a2 ? a1 / a2 : a2 / a1 );
289 const double f2 = ( a2 > a3 ? a2 / a3 : a3 / a2 );
290 const double f3 = ( a3 > a4 ? a3 / a4 : a4 / a3 );
291 const double f4 = ( a4 > a5 ? a4 / a5 : a5 / a4 );
292 if (f1 <= maximumAmplitudeFactor && f2 <= maximumAmplitudeFactor &&
293 f3 <= maximumAmplitudeFactor && f4 <= maximumAmplitudeFactor)
294 {
295 const double fivePointAverage = ((a1 + a2 + a3) + (a4 + a5)) / 5.0;
296 numerator += fabs (a3 - fivePointAverage);
297 numberOfPeaks ++;
298 }
299 }
300 }
301 if (numberOfPeaks < 1)
302 return undefined;
303 numerator /= numberOfPeaks;
304 numberOfPeaks = 0;
305 for (integer i = 1; i < my points.size; i ++) {
306 denominator += points [i] -> value;
307 numberOfPeaks ++;
308 }
309 denominator /= numberOfPeaks;
310 if (denominator == 0.0)
311 return undefined;
312 return double (numerator / denominator);
313 }
314 double AmplitudeTier_getShimmer_apq11_u (AmplitudeTier me, double pmin, double pmax, double maximumAmplitudeFactor) {
315 /* mutable count */ integer numberOfPeaks = 0;
316 /* mutable accumulate */ longdouble numerator = 0.0, denominator = 0.0;
317 RealPoint *points = & my points.at [0];
318 for (integer i = 6; i <= my points.size - 5; i ++) {
319 const double p1 = points [i - 4] -> number - points [i - 5] -> number;
320 const double p2 = points [i - 3] -> number - points [i - 4] -> number;
321 const double p3 = points [i - 2] -> number - points [i - 3] -> number;
322 const double p4 = points [i - 1] -> number - points [i - 2] -> number;
323 const double p5 = points [i] -> number - points [i - 1] -> number;
324 const double p6 = points [i + 1] -> number - points [i] -> number;
325 const double p7 = points [i + 2] -> number - points [i + 1] -> number;
326 const double p8 = points [i + 3] -> number - points [i + 2] -> number;
327 const double p9 = points [i + 4] -> number - points [i + 3] -> number;
328 const double p10 = points [i + 5] -> number - points [i + 4] -> number;
329 if (pmin == pmax || (p1 >= pmin && p1 <= pmax && p2 >= pmin && p2 <= pmax
330 && p3 >= pmin && p3 <= pmax && p4 >= pmin && p4 <= pmax && p5 >= pmin && p5 <= pmax
331 && p6 >= pmin && p6 <= pmax && p7 >= pmin && p7 <= pmax && p8 >= pmin && p8 <= pmax
332 && p9 >= pmin && p9 <= pmax && p10 >= pmin && p10 <= pmax))
333 {
334 const double a1 = points [i - 5] -> value;
335 const double a2 = points [i - 4] -> value;
336 const double a3 = points [i - 3] -> value;
337 const double a4 = points [i - 2] -> value;
338 const double a5 = points [i - 1] -> value;
339 const double a6 = points [i] -> value;
340 const double a7 = points [i + 1] -> value;
341 const double a8 = points [i + 2] -> value;
342 const double a9 = points [i + 3] -> value;
343 const double a10 = points [i + 4] -> value;
344 const double a11 = points [i + 5] -> value;
345 const double f1 = ( a1 > a2 ? a1 / a2 : a2 / a1 );
346 const double f2 = ( a2 > a3 ? a2 / a3 : a3 / a2 );
347 const double f3 = ( a3 > a4 ? a3 / a4 : a4 / a3 );
348 const double f4 = ( a4 > a5 ? a4 / a5 : a5 / a4 );
349 const double f5 = ( a5 > a6 ? a5 / a6 : a6 / a5 );
350 const double f6 = ( a6 > a7 ? a6 / a7 : a7 / a6 );
351 const double f7 = ( a7 > a8 ? a7 / a8 : a8 / a7 );
352 const double f8 = ( a8 > a9 ? a8 / a9 : a9 / a8 );
353 const double f9 = ( a9 > a10 ? a9 / a10 : a10 / a9 );
354 const double f10 = ( a10 > a11 ? a10 / a11 : a11 / a10 );
355 if (f1 <= maximumAmplitudeFactor && f2 <= maximumAmplitudeFactor &&
356 f3 <= maximumAmplitudeFactor && f4 <= maximumAmplitudeFactor &&
357 f5 <= maximumAmplitudeFactor && f6 <= maximumAmplitudeFactor &&
358 f7 <= maximumAmplitudeFactor && f8 <= maximumAmplitudeFactor &&
359 f9 <= maximumAmplitudeFactor && f10 <= maximumAmplitudeFactor)
360 {
361 const double elevenPointAverage = (((a1 + a2 + a3) + (a4 + a5 + a6)) + ((a7 + a8 + a9) + (a10 + a11))) / 11.0;
362 numerator += fabs (a6 - elevenPointAverage);
363 numberOfPeaks ++;
364 }
365 }
366 }
367 if (numberOfPeaks < 1)
368 return undefined;
369 numerator /= numberOfPeaks;
370 numberOfPeaks = 0;
371 for (integer i = 1; i < my points.size; i ++) {
372 denominator += points [i] -> value;
373 numberOfPeaks ++;
374 }
375 denominator /= numberOfPeaks;
376 if (denominator == 0.0)
377 return undefined;
378 return double (numerator / denominator);
379 }
380 double AmplitudeTier_getShimmer_dda_u (AmplitudeTier me, double pmin, double pmax, double maximumAmplitudeFactor) {
381 const double apq3 = AmplitudeTier_getShimmer_apq3_u (me, pmin, pmax, maximumAmplitudeFactor);
382 return ( isdefined (apq3) ? 3.0 * apq3 : undefined );
383 }
384
385 autoSound AmplitudeTier_to_Sound (AmplitudeTier me, double samplingFrequency, integer interpolationDepth) {
386 try {
387 const integer sound_nt = 1 + Melder_ifloor ((my xmax - my xmin) * samplingFrequency); // >= 1
388 const double dt = 1.0 / samplingFrequency;
389 const double tmid = (my xmin + my xmax) / 2;
390 const double t1 = tmid - 0.5 * (sound_nt - 1) * dt;
391 autoSound you = Sound_create (1, my xmin, my xmax, sound_nt, dt, t1);
392 VEC sound = your z.row (1);
393 for (integer it = 1; it <= my points.size; it ++) {
394 const constRealPoint point = my points.at [it];
395 const double t = point -> number;
396 const double amplitude = point -> value;
397 const integer mid = Sampled_xToNearestIndex (you.get(), t);
398 const integer begin = Melder_clippedLeft (1_integer, mid - interpolationDepth);
399 const integer end = Melder_clippedRight (mid + interpolationDepth, your nx);
400 /* mutable cycle */ double angle = NUMpi * (Sampled_indexToX (you.get(), begin) - t) / your dx;
401 /* mutable cycle */ double halfampsinangle = 0.5 * amplitude * sin (angle);
402 for (integer j = begin; j <= end; j ++) {
403 if (fabs (angle) < 1e-6)
404 sound [j] += amplitude;
405 else if (angle < 0.0)
406 sound [j] += halfampsinangle *
407 (1.0 + cos (angle / (mid - begin + 1))) / angle;
408 else
409 sound [j] += halfampsinangle *
410 (1.0 + cos (angle / (end - mid + 1))) / angle;
411 angle += NUMpi;
412 halfampsinangle = - halfampsinangle;
413 }
414 }
415 return you;
416 } catch (MelderError) {
417 Melder_throw (me, U": not converted to Sound.");
418 }
419 }
420
421 /* End of file AmplitudeTier.cpp */