Debian Codegraph @

fon/Vector.cpp

raw
1 /* Vector.cpp
2 *
3 * Copyright (C) 1992-2009,2011,2012,2014-2021,2023,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 "Vector.h"
20
21 #include "enums_getText.h"
22 #include "Vector_enums.h"
23 #include "enums_getValue.h"
24 #include "Vector_enums.h"
25
26 Thing_implement (Vector, Matrix, 2);
27
28 /*
29 Vector::v_getVector () returns the value in channel `rowNumber`,
30 or if `rowNumber` == 0, then the average value of all the channels.
31 The value is assumed to be zero in columns outside the domain.
32 */
33 double structVector :: v_getVector (integer rowNumber, integer columnNumber) const {
34 if (columnNumber < 1 || columnNumber > our nx)
35 return 0.0;
36 if (our ny == 1)
37 return our z [1] [columnNumber]; // optimization
38 if (rowNumber == 0) {
39 if (our ny == 2)
40 return 0.5 * (our z [1] [columnNumber] + our z [2] [columnNumber]); // optimization
41 longdouble sum = 0.0;
42 for (integer channel = 1; channel <= our ny; channel ++)
43 sum += our z [channel] [columnNumber];
44 return double (sum / our ny);
45 }
46 Melder_assert (rowNumber > 0 && rowNumber <= our ny);
47 return our z [rowNumber] [columnNumber];
48 }
49
50 /*
51 Vector::v_getFunction1 () returns a channel or the average of all the channels.
52 There is linear interpolation between columns.
53 The value is assumed to be zero in columns outside the domain.
54 */
55 double structVector :: v_getFunction1 (integer rowNumber, double x) const {
56 const double columnNumber_real = (x - our x1) / our dx + 1.0;
57 const integer leftColumnNumber = Melder_ifloor (columnNumber_real);
58 const double columnNumber_phase = columnNumber_real - leftColumnNumber;
59 double leftValue;
60 if (leftColumnNumber < 1 || leftColumnNumber > our nx) {
61 leftValue = 0.0; // outside the definition region, Formula is expected to return zero
62 } else if (our ny == 1) {
63 leftValue = z [1] [leftColumnNumber]; // optimization
64 } else if (rowNumber == 0) {
65 if (our ny == 2) {
66 leftValue = 0.5 * (our z [1] [leftColumnNumber] + our z [2] [leftColumnNumber]); // optimization
67 } else {
68 longdouble sum = 0.0;
69 for (integer channel = 1; channel <= ny; channel ++)
70 sum += our z [channel] [leftColumnNumber];
71 leftValue = double (sum / our ny);
72 }
73 } else {
74 Melder_assert (rowNumber > 0 && rowNumber <= our ny);
75 leftValue = our z [rowNumber] [leftColumnNumber];
76 }
77 const integer rightColumnNumber = leftColumnNumber + 1;
78 double rightValue;
79 if (rightColumnNumber < 1 || rightColumnNumber > our nx) {
80 rightValue = 0.0; // outside the definition region, Formula is expected to return zero
81 } else if (our ny == 1) {
82 rightValue = z [1] [rightColumnNumber]; // optimization
83 } else if (rowNumber == 0) {
84 if (our ny == 2) {
85 rightValue = 0.5 * (our z [1] [rightColumnNumber] + our z [2] [rightColumnNumber]); // optimization
86 } else {
87 longdouble sum = 0.0;
88 for (integer channel = 1; channel <= our ny; channel ++)
89 sum += our z [channel] [rightColumnNumber];
90 rightValue = double (sum / our ny);
91 }
92 } else {
93 Melder_assert (rowNumber > 0 && rowNumber <= our ny);
94 rightValue = z [rowNumber] [rightColumnNumber];
95 }
96 return (1.0 - columnNumber_phase) * leftValue + columnNumber_phase * rightValue;
97 }
98
99 double structVector :: v_getValueAtSample (integer isamp, integer ilevel, int unit) const {
100 /*
101 Preconditions:
102 1 <= isamp <= my nx
103 0 <= ilevel <= my ny
104 */
105 double value;
106 if (ilevel > Vector_CHANNEL_AVERAGE) {
107 value = our z [ilevel] [isamp];
108 } else if (our ny == 1) {
109 value = our z [1] [isamp]; // optimization
110 } else if (our ny == 2) {
111 value = 0.5 * (our z [1] [isamp] + our z [2] [isamp]); // optimization
112 } else {
113 longdouble sum = 0.0;
114 for (integer channel = 1; channel <= our ny; channel ++)
115 sum += our z [channel] [isamp];
116 value = double (sum / our ny);
117 }
118 return isdefined (value) ? our v_convertStandardToSpecialUnit (value, ilevel, unit) : undefined;
119 }
120
121 /***** Get content. *****/
122
123 integer kVector_valueInterpolation_to_interpolationDepth (kVector_valueInterpolation valueInterpolationType) {
124 switch (valueInterpolationType) {
125 case kVector_valueInterpolation :: UNDEFINED:
126 return NUM_VALUE_INTERPOLATE_LINEAR;
127 case kVector_valueInterpolation :: NEAREST:
128 return NUM_VALUE_INTERPOLATE_NEAREST;
129 case kVector_valueInterpolation :: LINEAR:
130 return NUM_VALUE_INTERPOLATE_LINEAR;
131 case kVector_valueInterpolation :: CUBIC:
132 return NUM_VALUE_INTERPOLATE_CUBIC;
133 case kVector_valueInterpolation :: SINC70:
134 return NUM_VALUE_INTERPOLATE_SINC70;
135 case kVector_valueInterpolation :: SINC700:
136 return NUM_VALUE_INTERPOLATE_SINC700;
137 }
138 return 0; // never reached
139 }
140
141 integer kVector_peakInterpolation_to_interpolationDepth (kVector_peakInterpolation peakInterpolationType) {
142 switch (peakInterpolationType) {
143 case kVector_peakInterpolation :: UNDEFINED:
144 return NUM_PEAK_INTERPOLATE_PARABOLIC;
145 case kVector_peakInterpolation :: NONE:
146 return NUM_PEAK_INTERPOLATE_NONE;
147 case kVector_peakInterpolation :: PARABOLIC:
148 return NUM_PEAK_INTERPOLATE_PARABOLIC;
149 case kVector_peakInterpolation :: CUBIC:
150 return NUM_PEAK_INTERPOLATE_CUBIC;
151 case kVector_peakInterpolation :: SINC70:
152 return NUM_PEAK_INTERPOLATE_SINC70;
153 case kVector_peakInterpolation :: SINC700:
154 return NUM_PEAK_INTERPOLATE_SINC700;
155 }
156 return 0; // never reached
157 }
158
159 /*
160 Vector_getValueAtX () returns the interpolated value in channel `ilevel`,
161 or if `ilevel` == 0, then the average of all the interpolated channels.
162 */
163 double Vector_getValueAtX (constVector me, double x, integer ilevel, kVector_valueInterpolation valueInterpolationType) {
164 const volatile double leftEdge = my x1 - 0.5 * my dx, rightEdge = leftEdge + my nx * my dx; // Sound_to_Polygon depends on this weird computation
165 if (x < leftEdge || x > rightEdge)
166 return undefined;
167 const integer interpolationDepth = kVector_valueInterpolation_to_interpolationDepth (valueInterpolationType);
168 if (ilevel > Vector_CHANNEL_AVERAGE) {
169 Melder_assert (ilevel <= my ny);
170 const double index_real = (x - my x1) / my dx + 1.0;
171 return NUM_interpolate_sinc (my z.row (ilevel), index_real, interpolationDepth);
172 }
173 longdouble sum = 0.0;
174 for (integer ichan = 1; ichan <= my ny; ichan ++) {
175 const double index_real = (x - my x1) / my dx + 1.0;
176 sum += NUM_interpolate_sinc (my z.row (ichan), index_real, interpolationDepth);
177 }
178 return double (sum / my ny);
179 }
180
181 /***** Get shape. *****/
182
183 void Vector_getMinimumAndX (constVector me, double xmin, double xmax, integer channelNumber, kVector_peakInterpolation peakInterpolationType,
184 double *out_minimum, double *out_xOfMinimum)
185 {
186 Melder_assert (channelNumber >= 1 && channelNumber <= my ny);
187 constVEC y = my z.row (channelNumber);
188 double minimum, x;
189 Function_unidirectionalAutowindow (me, & xmin, & xmax);
190 integer imin, imax;
191 if (! Sampled_getWindowSamples (me, xmin, xmax, & imin, & imax)) {
192 /*
193 No samples between xmin and xmax.
194 Try to return the lesser of the values at these two points.
195 */
196 kVector_valueInterpolation valueInterpolationType = ( peakInterpolationType > kVector_peakInterpolation :: NONE ?
197 kVector_valueInterpolation :: LINEAR : kVector_valueInterpolation :: NEAREST );
198 const double yleft = Vector_getValueAtX (me, xmin, channelNumber, valueInterpolationType);
199 const double yright = Vector_getValueAtX (me, xmax, channelNumber, valueInterpolationType);
200 minimum = std::min (yleft, yright);
201 x = ( yleft == yright ? (xmin + xmax) / 2.0 : yleft < yright ? xmin : xmax );
202 } else {
203 minimum = y [imin];
204 x = imin;
205 if (y [imax] < minimum) {
206 minimum = y [imax];
207 x = imax;
208 }
209 if (imin == 1)
210 imin ++;
211 if (imax == my nx)
212 imax --;
213 for (integer i = imin; i <= imax; i ++) {
214 if (y [i] < y [i - 1] && y [i] <= y [i + 1]) {
215 double i_real;
216 const double localMinimum = NUMimproveMinimum (y, i, kVector_peakInterpolation_to_interpolationDepth (peakInterpolationType), & i_real);
217 if (localMinimum < minimum) {
218 minimum = localMinimum;
219 x = i_real;
220 }
221 }
222 }
223 x = my x1 + (x - 1) * my dx; // convert sample to x
224 Melder_clip (xmin, & x, xmax);
225 }
226 if (out_minimum)
227 *out_minimum = minimum;
228 if (out_xOfMinimum)
229 *out_xOfMinimum = x;
230 }
231
232 void Vector_getMinimumAndXAndChannel (constVector me, double xmin, double xmax, kVector_peakInterpolation peakInterpolationType,
233 double *out_minimum, double *out_xOfMinimum, integer *out_channelOfMinimum)
234 {
235 double minimum, xOfMinimum;
236 integer channelOfMinimum = 1;
237 Vector_getMinimumAndX (me, xmin, xmax, 1, peakInterpolationType, & minimum, & xOfMinimum);
238 for (integer channel = 2; channel <= my ny; channel ++) {
239 double minimumOfChannel, xOfMinimumOfChannel;
240 Vector_getMinimumAndX (me, xmin, xmax, channel, peakInterpolationType, & minimumOfChannel, & xOfMinimumOfChannel);
241 if (minimumOfChannel < minimum) {
242 minimum = minimumOfChannel;
243 xOfMinimum = xOfMinimumOfChannel;
244 channelOfMinimum = channel;
245 }
246 }
247 if (out_minimum)
248 *out_minimum = minimum;
249 if (out_xOfMinimum)
250 *out_xOfMinimum = xOfMinimum;
251 if (out_channelOfMinimum)
252 *out_channelOfMinimum = channelOfMinimum;
253 }
254
255 double Vector_getMinimum (constVector me, double xmin, double xmax, kVector_peakInterpolation peakInterpolationType) {
256 double minimum;
257 Vector_getMinimumAndXAndChannel (me, xmin, xmax, peakInterpolationType, & minimum, nullptr, nullptr);
258 return minimum;
259 }
260
261 double Vector_getXOfMinimum (constVector me, double xmin, double xmax, kVector_peakInterpolation peakInterpolationType) {
262 double xOfMinimum;
263 Vector_getMinimumAndXAndChannel (me, xmin, xmax, peakInterpolationType, nullptr, & xOfMinimum, nullptr);
264 return xOfMinimum;
265 }
266
267 integer Vector_getChannelOfMinimum (constVector me, double xmin, double xmax, kVector_peakInterpolation peakInterpolationType) {
268 integer channelOfMinimum;
269 Vector_getMinimumAndXAndChannel (me, xmin, xmax, peakInterpolationType, nullptr, nullptr, & channelOfMinimum);
270 return channelOfMinimum;
271 }
272
273 void Vector_getMaximumAndX (constVector me, double xmin, double xmax, integer channelNumber, kVector_peakInterpolation peakInterpolationType,
274 double *out_maximum, double *out_xOfMaximum)
275 {
276 Melder_assert (channelNumber >= 1 && channelNumber <= my ny);
277 constVEC y = my z.row (channelNumber);
278 double maximum, x;
279 Function_unidirectionalAutowindow (me, & xmin, & xmax);
280 integer imin, imax, i;
281 if (! Sampled_getWindowSamples (me, xmin, xmax, & imin, & imax)) {
282 /*
283 No samples between xmin and xmax.
284 Try to return the greater of the values at these two points.
285 */
286 kVector_valueInterpolation valueInterpolationType = ( peakInterpolationType > kVector_peakInterpolation :: NONE ?
287 kVector_valueInterpolation :: LINEAR : kVector_valueInterpolation :: NEAREST );
288 const double yleft = Vector_getValueAtX (me, xmin, channelNumber, valueInterpolationType);
289 const double yright = Vector_getValueAtX (me, xmax, channelNumber, valueInterpolationType);
290 maximum = std::max (yleft, yright);
291 x = ( yleft == yright ? (xmin + xmax) / 2.0 : yleft > yright ? xmin : xmax );
292 } else {
293 maximum = y [imin];
294 x = imin;
295 if (y [imax] > maximum) {
296 maximum = y [imax];
297 x = imax;
298 }
299 if (imin == 1)
300 imin ++;
301 if (imax == my nx)
302 imax --;
303 for (i = imin; i <= imax; i ++) {
304 if (y [i] > y [i - 1] && y [i] >= y [i + 1]) {
305 double i_real;
306 const double localMaximum = NUMimproveMaximum (y, i, kVector_peakInterpolation_to_interpolationDepth (peakInterpolationType), & i_real);
307 if (localMaximum > maximum) {
308 maximum = localMaximum;
309 x = i_real;
310 }
311 }
312 }
313 x = my x1 + (x - 1) * my dx; // convert sample to x
314 Melder_clip (xmin, & x, xmax);
315 }
316 if (out_maximum)
317 *out_maximum = maximum;
318 if (out_xOfMaximum)
319 *out_xOfMaximum = x;
320 }
321
322 void Vector_getMaximumAndXAndChannel (constVector me, double xmin, double xmax, kVector_peakInterpolation peakInterpolationType,
323 double *out_maximum, double *out_xOfMaximum, integer *out_channelOfMaximum)
324 {
325 double maximum, xOfMaximum;
326 integer channelOfMaximum = 1;
327 Vector_getMaximumAndX (me, xmin, xmax, 1, peakInterpolationType, & maximum, & xOfMaximum);
328 for (integer channel = 2; channel <= my ny; channel ++) {
329 double maximumOfChannel, xOfMaximumOfChannel;
330 Vector_getMaximumAndX (me, xmin, xmax, channel, peakInterpolationType, & maximumOfChannel, & xOfMaximumOfChannel);
331 if (maximumOfChannel > maximum) {
332 maximum = maximumOfChannel;
333 xOfMaximum = xOfMaximumOfChannel;
334 channelOfMaximum = channel;
335 }
336 }
337 if (out_maximum)
338 *out_maximum = maximum;
339 if (out_xOfMaximum)
340 *out_xOfMaximum = xOfMaximum;
341 if (out_channelOfMaximum)
342 *out_channelOfMaximum = channelOfMaximum;
343 }
344
345 double Vector_getMaximum (constVector me, double xmin, double xmax, kVector_peakInterpolation peakInterpolationType) {
346 double maximum;
347 Vector_getMaximumAndXAndChannel (me, xmin, xmax, peakInterpolationType, & maximum, nullptr, nullptr);
348 return maximum;
349 }
350
351 double Vector_getXOfMaximum (constVector me, double xmin, double xmax, kVector_peakInterpolation peakInterpolationType) {
352 double xOfMaximum;
353 Vector_getMaximumAndXAndChannel (me, xmin, xmax, peakInterpolationType, nullptr, & xOfMaximum, nullptr);
354 return xOfMaximum;
355 }
356
357 integer Vector_getChannelOfMaximum (constVector me, double xmin, double xmax, kVector_peakInterpolation peakInterpolationType) {
358 integer channelOfMaximum;
359 Vector_getMaximumAndXAndChannel (me, xmin, xmax, peakInterpolationType, nullptr, nullptr, & channelOfMaximum);
360 return channelOfMaximum;
361 }
362
363 double Vector_getAbsoluteExtremum (constVector me, double xmin, double xmax, kVector_peakInterpolation peakInterpolationType) {
364 const double minimum = Vector_getMinimum (me, xmin, xmax, peakInterpolationType);
365 const double maximum = Vector_getMaximum (me, xmin, xmax, peakInterpolationType);
366 return std::max (fabs (minimum), fabs (maximum));
367 }
368
369 /***** Get statistics. *****/
370
371 double Vector_getMean (constVector me, double xmin, double xmax, integer channel) {
372 return Sampled_getMean (me, xmin, xmax, channel, 0, true);
373 }
374
375 double Vector_getStandardDeviation (constVector me, double xmin, double xmax, integer ilevel) {
376 Function_unidirectionalAutowindow (me, & xmin, & xmax);
377 integer imin, imax, n = Sampled_getWindowSamples (me, xmin, xmax, & imin, & imax);
378 if (n < 2)
379 return undefined;
380 if (ilevel == Vector_CHANNEL_AVERAGE) {
381 longdouble sum2 = 0.0;
382 for (integer channel = 1; channel <= my ny; channel ++) {
383 const double mean = Vector_getMean (me, xmin, xmax, channel);
384 for (integer i = imin; i <= imax; i ++) {
385 const double diff = my z [channel] [i] - mean;
386 sum2 += diff * diff;
387 }
388 }
389 return sqrt (double (sum2 / (n * my ny - my ny))); // The number of constraints equals the number of channels,
390 // because from every channel its own mean was subtracted.
391 // Corollary: a two-channel mono sound will have the same stdev as the corresponding one-channel sound.
392 }
393 const double mean = Vector_getMean (me, xmin, xmax, ilevel);
394 longdouble sum2 = 0.0;
395 for (integer i = imin; i <= imax; i ++) {
396 const double diff = my z [ilevel] [i] - mean;
397 sum2 += diff * diff;
398 }
399 return sqrt (double (sum2 / (n - 1)));
400 }
401
402 /***** Modify. *****/
403
404 void Vector_addScalar (mutableVector me, double scalar) {
405 for (integer ichan = 1; ichan <= my ny; ichan ++)
406 my channel (ichan) += scalar;
407 }
408
409 void Vector_subtractMean (mutableVector me) {
410 for (integer ichan = 1; ichan <= my ny; ichan ++)
411 centre_VEC_inout (my channel (ichan));
412 }
413
414 void Vector_multiplyByScalar (mutableVector me, double scalar) {
415 for (integer ichan = 1; ichan <= my ny; ichan ++)
416 my channel (ichan) *= scalar;
417 }
418
419 void Vector_scale (mutableVector me, double scale) {
420 const double extremum = NUMextremum_e (my z.get());
421 if (extremum != 0.0)
422 Vector_multiplyByScalar (me, scale / extremum);
423 }
424
425 /***** Graphics. *****/
426
427 void Vector_draw (constVector me, Graphics graphics, double *pxmin, double *pxmax, double *pymin, double *pymax,
428 double defaultDy, conststring32 method)
429 {
430 const bool xreversed = ( *pxmin > *pxmax ), yreversed = ( *pymin > *pymax );
431 if (xreversed)
432 std::swap (*pxmin, *pxmax);
433 if (yreversed)
434 std::swap (*pymin, *pymax);
435 Function_bidirectionalAutowindow (me, pxmin, pxmax);
436 /*
437 Domain expressed in sample numbers.
438 */
439 integer ixmin, ixmax;
440 const integer n = Matrix_getWindowSamplesX (me, *pxmin, *pxmax, & ixmin, & ixmax);
441 if (n < 1)
442 return;
443 /*
444 Automatic vertical range.
445 */
446 if (*pymin == *pymax) {
447 Matrix_getWindowExtrema (me, ixmin, ixmax, 1, 1, pymin, pymax);
448 if (*pymin == *pymax) {
449 *pymin -= defaultDy;
450 *pymax += defaultDy;
451 }
452 }
453 /*
454 Set coordinates for drawing.
455 */
456 Graphics_setInner (graphics);
457 Graphics_setWindow (graphics, ( xreversed ? *pxmax : *pxmin ), ( xreversed ? *pxmin : *pxmax ),
458 ( yreversed ? *pymax : *pymin ), ( yreversed ? *pymin : *pymax ));
459 if (str32str (method, U"bars") || str32str (method, U"Bars")) {
460 for (integer ix = ixmin; ix <= ixmax; ix ++) {
461 const double x = Sampled_indexToX (me, ix);
462 double y = my z [1] [ix];
463 double left = x - 0.5 * my dx, right = x + 0.5 * my dx;
464 Melder_clipRight (& y, *pymax);
465 Melder_clipLeft (*pxmin, & left);
466 Melder_clipRight (& right, *pxmax);
467 if (y > *pymin) {
468 Graphics_line (graphics, left, y, right, y);
469 Graphics_line (graphics, left, y, left, *pymin);
470 Graphics_line (graphics, right, y, right, *pymin);
471 }
472 }
473 } else if (str32str (method, U"poles") || str32str (method, U"Poles")) {
474 for (integer ix = ixmin; ix <= ixmax; ix ++) {
475 const double x = Sampled_indexToX (me, ix);
476 Graphics_line (graphics, x, 0.0, x, my z [1] [ix]);
477 }
478 } else if (str32str (method, U"speckles") || str32str (method, U"Speckles")) {
479 for (integer ix = ixmin; ix <= ixmax; ix ++) {
480 const double x = Sampled_indexToX (me, ix);
481 Graphics_speckle (graphics, x, my z [1] [ix]);
482 }
483 } else {
484 /*
485 The default: draw as a curve.
486 */
487 Graphics_function (graphics, & my z [1] [0], ixmin, ixmax,
488 Matrix_columnToX (me, ixmin), Matrix_columnToX (me, ixmax));
489 }
490 Graphics_unsetInner (graphics);
491 }
492
493 /* End of file Vector.cpp */