Debian Codegraph @

fon/Vector.cpp

raw symbols SCIP
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"cxx . . $ `<file>/fon/Vector.h`/
20
21 #include "enums_getText.h"cxx . . $ `<file>/melder/enums_getText.h`/
22 #include "Vector_enums.h"cxx . . $ `<file>/fon/Vector_enums.h`/
23 #include "enums_getValue.h"cxx . . $ `<file>/melder/enums_getValue.h`/
24 #include "Vector_enums.h"cxx . . $ `<file>/fon/Vector_enums.h`/
25
26 Thing_implementcxx . . $ `sys/Thing.h:65:9`! (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 structVectorcxx . . $ structVector# :: v_getVectorcxx . . $ structVector#v_getVector(f7a1f362ba849873). (integercxx . . $ integer# rowNumber, integercxx . . $ integer# columnNumber) const {
34 if (columnNumber < 1 || columnNumber > ourcxx . . $ `melder/melder.h:42:9`! nxcxx . . $ structSampled#nx.)
35 return 0.0;
36 if (ourcxx . . $ `melder/melder.h:42:9`! nycxx . . $ structSampledXY#ny. == 1)
37 return ourcxx . . $ `melder/melder.h:42:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).columnNumber]; // optimization
38 if (rowNumber == 0) {
39 if (ourcxx . . $ `melder/melder.h:42:9`! nycxx . . $ structSampledXY#ny. == 2)
40 return 0.5 * (ourcxx . . $ `melder/melder.h:42:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).columnNumber] + ourcxx . . $ `melder/melder.h:42:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).columnNumber]); // optimization
41 longdoublecxx . . $ longdouble# sum = 0.0;
42 for (integercxx . . $ integer# channel = 1; channel <= ourcxx . . $ `melder/melder.h:42:9`! nycxx . . $ structSampledXY#ny.; channel ++)
43 sum += ourcxx . . $ `melder/melder.h:42:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).channel] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).columnNumber];
44 return double (sum / ourcxx . . $ `melder/melder.h:42:9`! nycxx . . $ structSampledXY#ny.);
45 }
46 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (rowNumber > 0 && rowNumber <= ourcxx . . $ `melder/melder.h:42:9`! ny);
47 return ourcxx . . $ `melder/melder.h:42:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).rowNumber] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).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 structVectorcxx . . $ structVector# :: v_getFunction1cxx . . $ structVector#v_getFunction1(6525b878db7c2448). (integercxx . . $ integer# rowNumber, double x) const {
56 const double columnNumber_real = (x - ourcxx . . $ `melder/melder.h:42:9`! x1cxx . . $ structSampled#x1.) / ourcxx . . $ `melder/melder.h:42:9`! dxcxx . . $ structSampled#dx. + 1.0;
57 const integercxx . . $ integer# leftColumnNumber = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (columnNumber_real);
58 const double columnNumber_phase = columnNumber_real - leftColumnNumber;
59 double leftValue;
60 if (leftColumnNumber < 1 || leftColumnNumber > ourcxx . . $ `melder/melder.h:42:9`! nxcxx . . $ structSampled#nx.) {
61 leftValue = 0.0; // outside the definition region, Formula is expected to return zero
62 } else if (ourcxx . . $ `melder/melder.h:42:9`! nycxx . . $ structSampledXY#ny. == 1) {
63 leftValue = zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).leftColumnNumber]; // optimization
64 } else if (rowNumber == 0) {
65 if (ourcxx . . $ `melder/melder.h:42:9`! nycxx . . $ structSampledXY#ny. == 2) {
66 leftValue = 0.5 * (ourcxx . . $ `melder/melder.h:42:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).leftColumnNumber] + ourcxx . . $ `melder/melder.h:42:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).leftColumnNumber]); // optimization
67 } else {
68 longdoublecxx . . $ longdouble# sum = 0.0;
69 for (integercxx . . $ integer# channel = 1; channel <= nycxx . . $ structSampledXY#ny.; channel ++)
70 sum += ourcxx . . $ `melder/melder.h:42:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).channel] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).leftColumnNumber];
71 leftValue = double (sum / ourcxx . . $ `melder/melder.h:42:9`! nycxx . . $ structSampledXY#ny.);
72 }
73 } else {
74 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (rowNumber > 0 && rowNumber <= ourcxx . . $ `melder/melder.h:42:9`! ny);
75 leftValue = ourcxx . . $ `melder/melder.h:42:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).rowNumber] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).leftColumnNumber];
76 }
77 const integercxx . . $ integer# rightColumnNumber = leftColumnNumber + 1;
78 double rightValue;
79 if (rightColumnNumber < 1 || rightColumnNumber > ourcxx . . $ `melder/melder.h:42:9`! nxcxx . . $ structSampled#nx.) {
80 rightValue = 0.0; // outside the definition region, Formula is expected to return zero
81 } else if (ourcxx . . $ `melder/melder.h:42:9`! nycxx . . $ structSampledXY#ny. == 1) {
82 rightValue = zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).rightColumnNumber]; // optimization
83 } else if (rowNumber == 0) {
84 if (ourcxx . . $ `melder/melder.h:42:9`! nycxx . . $ structSampledXY#ny. == 2) {
85 rightValue = 0.5 * (ourcxx . . $ `melder/melder.h:42:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).rightColumnNumber] + ourcxx . . $ `melder/melder.h:42:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).rightColumnNumber]); // optimization
86 } else {
87 longdoublecxx . . $ longdouble# sum = 0.0;
88 for (integercxx . . $ integer# channel = 1; channel <= ourcxx . . $ `melder/melder.h:42:9`! nycxx . . $ structSampledXY#ny.; channel ++)
89 sum += ourcxx . . $ `melder/melder.h:42:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).channel] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).rightColumnNumber];
90 rightValue = double (sum / ourcxx . . $ `melder/melder.h:42:9`! nycxx . . $ structSampledXY#ny.);
91 }
92 } else {
93 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (rowNumber > 0 && rowNumber <= ourcxx . . $ `melder/melder.h:42:9`! ny);
94 rightValue = zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).rowNumber] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).rightColumnNumber];
95 }
96 return (1.0 - columnNumber_phase) * leftValue + columnNumber_phase * rightValue;
97 }
98
99 double structVectorcxx . . $ structVector# :: v_getValueAtSamplecxx . . $ structVector#v_getValueAtSample(8bbfc6ce471470c9). (integercxx . . $ integer# isamp, integercxx . . $ 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_AVERAGEcxx . . $ `fon/Vector.h:49:9`!) {
107 value = ourcxx . . $ `melder/melder.h:42:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ilevel] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).isamp];
108 } else if (ourcxx . . $ `melder/melder.h:42:9`! nycxx . . $ structSampledXY#ny. == 1) {
109 value = ourcxx . . $ `melder/melder.h:42:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).isamp]; // optimization
110 } else if (ourcxx . . $ `melder/melder.h:42:9`! nycxx . . $ structSampledXY#ny. == 2) {
111 value = 0.5 * (ourcxx . . $ `melder/melder.h:42:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).isamp] + ourcxx . . $ `melder/melder.h:42:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).isamp]); // optimization
112 } else {
113 longdoublecxx . . $ longdouble# sum = 0.0;
114 for (integercxx . . $ integer# channel = 1; channel <= ourcxx . . $ `melder/melder.h:42:9`! nycxx . . $ structSampledXY#ny.; channel ++)
115 sum += ourcxx . . $ `melder/melder.h:42:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).channel] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).isamp];
116 value = double (sum / ourcxx . . $ `melder/melder.h:42:9`! nycxx . . $ structSampledXY#ny.);
117 }
118 return isdefinedcxx . . $ isdefined(f8197d536af80c96). (value) ? ourcxx . . $ `melder/melder.h:42:9`! v_convertStandardToSpecialUnitcxx . . $ structFunction#v_convertStandardToSpecialUnit(d783654ce2b2ffd0). (value, ilevel, unit) : undefinedcxx . . $ undefined.;
119 }
120
121 /***** Get content. *****/
122
123 integercxx . . $ integer# kVector_valueInterpolation_to_interpolationDepthcxx . . $ kVector_valueInterpolation_to_interpolationDepth(42b64d17316aa70a). (kVector_valueInterpolationcxx . . $ kVector_valueInterpolation# valueInterpolationType) {
124 switch (valueInterpolationType) {
125 case kVector_valueInterpolationcxx . . $ kVector_valueInterpolation# :: UNDEFINEDcxx . . $ kVector_valueInterpolation#UNDEFINED.:
126 return NUM_VALUE_INTERPOLATE_LINEARcxx . . $ `melder/NUMinterpol.h:25:9`!;
127 case kVector_valueInterpolationcxx . . $ kVector_valueInterpolation# :: NEARESTcxx . . $ kVector_valueInterpolation#NEAREST.:
128 return NUM_VALUE_INTERPOLATE_NEARESTcxx . . $ `melder/NUMinterpol.h:24:9`!;
129 case kVector_valueInterpolationcxx . . $ kVector_valueInterpolation# :: LINEARcxx . . $ kVector_valueInterpolation#LINEAR.:
130 return NUM_VALUE_INTERPOLATE_LINEARcxx . . $ `melder/NUMinterpol.h:25:9`!;
131 case kVector_valueInterpolationcxx . . $ kVector_valueInterpolation# :: CUBICcxx . . $ kVector_valueInterpolation#CUBIC.:
132 return NUM_VALUE_INTERPOLATE_CUBICcxx . . $ `melder/NUMinterpol.h:26:9`!;
133 case kVector_valueInterpolationcxx . . $ kVector_valueInterpolation# :: SINC70cxx . . $ kVector_valueInterpolation#SINC70.:
134 return NUM_VALUE_INTERPOLATE_SINC70cxx . . $ `melder/NUMinterpol.h:28:9`!;
135 case kVector_valueInterpolationcxx . . $ kVector_valueInterpolation# :: SINC700cxx . . $ kVector_valueInterpolation#SINC700.:
136 return NUM_VALUE_INTERPOLATE_SINC700cxx . . $ `melder/NUMinterpol.h:29:9`!;
137 }
138 return 0; // never reached
139 }
140
141 integercxx . . $ integer# kVector_peakInterpolation_to_interpolationDepthcxx . . $ kVector_peakInterpolation_to_interpolationDepth(5f6fa5b1fce060b0). (kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# peakInterpolationType) {
142 switch (peakInterpolationType) {
143 case kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# :: UNDEFINEDcxx . . $ kVector_peakInterpolation#UNDEFINED.:
144 return NUM_PEAK_INTERPOLATE_PARABOLICcxx . . $ `melder/NUMinterpol.h:33:9`!;
145 case kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# :: NONEcxx . . $ kVector_peakInterpolation#NONE.:
146 return NUM_PEAK_INTERPOLATE_NONEcxx . . $ `melder/NUMinterpol.h:32:9`!;
147 case kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# :: PARABOLICcxx . . $ kVector_peakInterpolation#PARABOLIC.:
148 return NUM_PEAK_INTERPOLATE_PARABOLICcxx . . $ `melder/NUMinterpol.h:33:9`!;
149 case kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# :: CUBICcxx . . $ kVector_peakInterpolation#CUBIC.:
150 return NUM_PEAK_INTERPOLATE_CUBICcxx . . $ `melder/NUMinterpol.h:34:9`!;
151 case kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# :: SINC70cxx . . $ kVector_peakInterpolation#SINC70.:
152 return NUM_PEAK_INTERPOLATE_SINC70cxx . . $ `melder/NUMinterpol.h:35:9`!;
153 case kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# :: SINC700cxx . . $ kVector_peakInterpolation#SINC700.:
154 return NUM_PEAK_INTERPOLATE_SINC700cxx . . $ `melder/NUMinterpol.h:36:9`!;
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_getValueAtXcxx . . $ Vector_getValueAtX(1bfdbe9f8adea74f). (constVectorcxx . . $ constVector# me, double x, integercxx . . $ integer# ilevel, kVector_valueInterpolationcxx . . $ kVector_valueInterpolation# valueInterpolationType) {
164 const volatile double leftEdge = mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1. - 0.5 * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., rightEdge = leftEdge + mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.; // Sound_to_Polygon depends on this weird computation
165 if (x < leftEdge || x > rightEdge)
166 return undefinedcxx . . $ undefined.;
167 const integercxx . . $ integer# interpolationDepth = kVector_valueInterpolation_to_interpolationDepthcxx . . $ kVector_valueInterpolation_to_interpolationDepth(42b64d17316aa70a). (valueInterpolationType);
168 if (ilevel > Vector_CHANNEL_AVERAGEcxx . . $ `fon/Vector.h:49:9`!) {
169 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (ilevel <= mycxx . . $ `melder/melder.h:44:9`! ny);
170 const double index_real = (x - mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1.) / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx. + 1.0;
171 return NUM_interpolate_sinccxx . . $ NUM_interpolate_sinc(3bff97c7d2c43d6f). (mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (ilevel), index_real, interpolationDepth);
172 }
173 longdoublecxx . . $ longdouble# sum = 0.0;
174 for (integercxx . . $ integer# ichan = 1; ichan <= mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.; ichan ++) {
175 const double index_real = (x - mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1.) / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx. + 1.0;
176 sum += NUM_interpolate_sinccxx . . $ NUM_interpolate_sinc(3bff97c7d2c43d6f). (mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (ichan), index_real, interpolationDepth);
177 }
178 return double (sum / mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.);
179 }
180
181 /***** Get shape. *****/
182
183 void Vector_getMinimumAndXcxx . . $ Vector_getMinimumAndX(5da84364dcb96b40). (constVectorcxx . . $ constVector# me, double xmin, double xmax, integercxx . . $ integer# channelNumber, kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# peakInterpolationType,
184 double *out_minimum, double *out_xOfMinimum)
185 {
186 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (channelNumber >= 1 && channelNumber <= mycxx . . $ `melder/melder.h:44:9`! ny);
187 constVECcxx . . $ constVEC# y = mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (channelNumber);
188 double minimum, x;
189 Function_unidirectionalAutowindowcxx . . $ Function_unidirectionalAutowindow(88b7840c59a57eaf). (me, & xmin, & xmax);
190 integercxx . . $ integer# imin, imax;
191 if (! Sampled_getWindowSamplescxx . . $ Sampled_getWindowSamples(fe99094ed5bb3f6a). (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_valueInterpolationcxx . . $ kVector_valueInterpolation# valueInterpolationType = ( peakInterpolationType > kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# :: NONEcxx . . $ kVector_peakInterpolation#NONE. ?
197 kVector_valueInterpolationcxx . . $ kVector_valueInterpolation# :: LINEARcxx . . $ kVector_valueInterpolation#LINEAR. : kVector_valueInterpolationcxx . . $ kVector_valueInterpolation# :: NEARESTcxx . . $ kVector_valueInterpolation#NEAREST. );
198 const double yleft = Vector_getValueAtXcxx . . $ Vector_getValueAtX(1bfdbe9f8adea74f). (me, xmin, channelNumber, valueInterpolationType);
199 const double yright = Vector_getValueAtXcxx . . $ Vector_getValueAtX(1bfdbe9f8adea74f). (me, xmax, channelNumber, valueInterpolationType);
200 minimum = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a). (yleft, yright);
201 x = ( yleft == yright ? (xmin + xmax) / 2.0 : yleft < yright ? xmin : xmax );
202 } else {
203 minimum = y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).imin];
204 x = imin;
205 if (y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).imax] < minimum) {
206 minimum = y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).imax];
207 x = imax;
208 }
209 if (imin == 1)
210 imin ++;
211 if (imax == mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.)
212 imax --;
213 for (integercxx . . $ integer# i = imin; i <= imax; i ++) {
214 if (y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i] < y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i - 1] && y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i] <= y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i + 1]) {
215 double i_real;
216 const double localMinimum = NUMimproveMinimumcxx . . $ NUMimproveMinimum(c86a71e1ecac36cd). (y, i, kVector_peakInterpolation_to_interpolationDepthcxx . . $ kVector_peakInterpolation_to_interpolationDepth(5f6fa5b1fce060b0). (peakInterpolationType), & i_real);
217 if (localMinimum < minimum) {
218 minimum = localMinimum;
219 x = i_real;
220 }
221 }
222 }
223 x = mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1. + (x - 1) * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.; // convert sample to x
224 Melder_clipcxx . . $ Melder_clip(971c667ea412f82c). (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_getMinimumAndXAndChannelcxx . . $ Vector_getMinimumAndXAndChannel(d40985585eba717). (constVectorcxx . . $ constVector# me, double xmin, double xmax, kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# peakInterpolationType,
233 double *out_minimum, double *out_xOfMinimum, integercxx . . $ integer# *out_channelOfMinimum)
234 {
235 double minimum, xOfMinimum;
236 integercxx . . $ integer# channelOfMinimum = 1;
237 Vector_getMinimumAndXcxx . . $ Vector_getMinimumAndX(5da84364dcb96b40). (me, xmin, xmax, 1, peakInterpolationType, & minimum, & xOfMinimum);
238 for (integercxx . . $ integer# channel = 2; channel <= mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.; channel ++) {
239 double minimumOfChannel, xOfMinimumOfChannel;
240 Vector_getMinimumAndXcxx . . $ Vector_getMinimumAndX(5da84364dcb96b40). (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_getMinimumcxx . . $ Vector_getMinimum(9e5d98b21939784c). (constVectorcxx . . $ constVector# me, double xmin, double xmax, kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# peakInterpolationType) {
256 double minimum;
257 Vector_getMinimumAndXAndChannelcxx . . $ Vector_getMinimumAndXAndChannel(d40985585eba717). (me, xmin, xmax, peakInterpolationType, & minimum, nullptr, nullptr);
258 return minimum;
259 }
260
261 double Vector_getXOfMinimumcxx . . $ Vector_getXOfMinimum(9e5d98b21939784c). (constVectorcxx . . $ constVector# me, double xmin, double xmax, kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# peakInterpolationType) {
262 double xOfMinimum;
263 Vector_getMinimumAndXAndChannelcxx . . $ Vector_getMinimumAndXAndChannel(d40985585eba717). (me, xmin, xmax, peakInterpolationType, nullptr, & xOfMinimum, nullptr);
264 return xOfMinimum;
265 }
266
267 integercxx . . $ integer# Vector_getChannelOfMinimumcxx . . $ Vector_getChannelOfMinimum(4252c3d829fb2dbf). (constVectorcxx . . $ constVector# me, double xmin, double xmax, kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# peakInterpolationType) {
268 integercxx . . $ integer# channelOfMinimum;
269 Vector_getMinimumAndXAndChannelcxx . . $ Vector_getMinimumAndXAndChannel(d40985585eba717). (me, xmin, xmax, peakInterpolationType, nullptr, nullptr, & channelOfMinimum);
270 return channelOfMinimum;
271 }
272
273 void Vector_getMaximumAndXcxx . . $ Vector_getMaximumAndX(5da84364dcb96b40). (constVectorcxx . . $ constVector# me, double xmin, double xmax, integercxx . . $ integer# channelNumber, kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# peakInterpolationType,
274 double *out_maximum, double *out_xOfMaximum)
275 {
276 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (channelNumber >= 1 && channelNumber <= mycxx . . $ `melder/melder.h:44:9`! ny);
277 constVECcxx . . $ constVEC# y = mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (channelNumber);
278 double maximum, x;
279 Function_unidirectionalAutowindowcxx . . $ Function_unidirectionalAutowindow(88b7840c59a57eaf). (me, & xmin, & xmax);
280 integercxx . . $ integer# imin, imax, i;
281 if (! Sampled_getWindowSamplescxx . . $ Sampled_getWindowSamples(fe99094ed5bb3f6a). (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_valueInterpolationcxx . . $ kVector_valueInterpolation# valueInterpolationType = ( peakInterpolationType > kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# :: NONEcxx . . $ kVector_peakInterpolation#NONE. ?
287 kVector_valueInterpolationcxx . . $ kVector_valueInterpolation# :: LINEARcxx . . $ kVector_valueInterpolation#LINEAR. : kVector_valueInterpolationcxx . . $ kVector_valueInterpolation# :: NEARESTcxx . . $ kVector_valueInterpolation#NEAREST. );
288 const double yleft = Vector_getValueAtXcxx . . $ Vector_getValueAtX(1bfdbe9f8adea74f). (me, xmin, channelNumber, valueInterpolationType);
289 const double yright = Vector_getValueAtXcxx . . $ Vector_getValueAtX(1bfdbe9f8adea74f). (me, xmax, channelNumber, valueInterpolationType);
290 maximum = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a). (yleft, yright);
291 x = ( yleft == yright ? (xmin + xmax) / 2.0 : yleft > yright ? xmin : xmax );
292 } else {
293 maximum = y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).imin];
294 x = imin;
295 if (y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).imax] > maximum) {
296 maximum = y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).imax];
297 x = imax;
298 }
299 if (imin == 1)
300 imin ++;
301 if (imax == mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.)
302 imax --;
303 for (i = imin; i <= imax; i ++) {
304 if (y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i] > y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i - 1] && y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i] >= y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i + 1]) {
305 double i_real;
306 const double localMaximum = NUMimproveMaximumcxx . . $ NUMimproveMaximum(c86a71e1ecac36cd). (y, i, kVector_peakInterpolation_to_interpolationDepthcxx . . $ kVector_peakInterpolation_to_interpolationDepth(5f6fa5b1fce060b0). (peakInterpolationType), & i_real);
307 if (localMaximum > maximum) {
308 maximum = localMaximum;
309 x = i_real;
310 }
311 }
312 }
313 x = mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1. + (x - 1) * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.; // convert sample to x
314 Melder_clipcxx . . $ Melder_clip(971c667ea412f82c). (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_getMaximumAndXAndChannelcxx . . $ Vector_getMaximumAndXAndChannel(d40985585eba717). (constVectorcxx . . $ constVector# me, double xmin, double xmax, kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# peakInterpolationType,
323 double *out_maximum, double *out_xOfMaximum, integercxx . . $ integer# *out_channelOfMaximum)
324 {
325 double maximum, xOfMaximum;
326 integercxx . . $ integer# channelOfMaximum = 1;
327 Vector_getMaximumAndXcxx . . $ Vector_getMaximumAndX(5da84364dcb96b40). (me, xmin, xmax, 1, peakInterpolationType, & maximum, & xOfMaximum);
328 for (integercxx . . $ integer# channel = 2; channel <= mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.; channel ++) {
329 double maximumOfChannel, xOfMaximumOfChannel;
330 Vector_getMaximumAndXcxx . . $ Vector_getMaximumAndX(5da84364dcb96b40). (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_getMaximumcxx . . $ Vector_getMaximum(9e5d98b21939784c). (constVectorcxx . . $ constVector# me, double xmin, double xmax, kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# peakInterpolationType) {
346 double maximum;
347 Vector_getMaximumAndXAndChannelcxx . . $ Vector_getMaximumAndXAndChannel(d40985585eba717). (me, xmin, xmax, peakInterpolationType, & maximum, nullptr, nullptr);
348 return maximum;
349 }
350
351 double Vector_getXOfMaximumcxx . . $ Vector_getXOfMaximum(9e5d98b21939784c). (constVectorcxx . . $ constVector# me, double xmin, double xmax, kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# peakInterpolationType) {
352 double xOfMaximum;
353 Vector_getMaximumAndXAndChannelcxx . . $ Vector_getMaximumAndXAndChannel(d40985585eba717). (me, xmin, xmax, peakInterpolationType, nullptr, & xOfMaximum, nullptr);
354 return xOfMaximum;
355 }
356
357 integercxx . . $ integer# Vector_getChannelOfMaximumcxx . . $ Vector_getChannelOfMaximum(4252c3d829fb2dbf). (constVectorcxx . . $ constVector# me, double xmin, double xmax, kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# peakInterpolationType) {
358 integercxx . . $ integer# channelOfMaximum;
359 Vector_getMaximumAndXAndChannelcxx . . $ Vector_getMaximumAndXAndChannel(d40985585eba717). (me, xmin, xmax, peakInterpolationType, nullptr, nullptr, & channelOfMaximum);
360 return channelOfMaximum;
361 }
362
363 double Vector_getAbsoluteExtremumcxx . . $ Vector_getAbsoluteExtremum(9e5d98b21939784c). (constVectorcxx . . $ constVector# me, double xmin, double xmax, kVector_peakInterpolationcxx . . $ kVector_peakInterpolation# peakInterpolationType) {
364 const double minimum = Vector_getMinimumcxx . . $ Vector_getMinimum(9e5d98b21939784c). (me, xmin, xmax, peakInterpolationType);
365 const double maximum = Vector_getMaximumcxx . . $ Vector_getMaximum(9e5d98b21939784c). (me, xmin, xmax, peakInterpolationType);
366 return stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a). (fabscxx . . $ fabs(fee4f1d22bc8e617). (minimum), fabscxx . . $ fabs(fee4f1d22bc8e617). (maximum));
367 }
368
369 /***** Get statistics. *****/
370
371 double Vector_getMeancxx . . $ Vector_getMean(8ea9441cb87dc78e). (constVectorcxx . . $ constVector# me, double xmin, double xmax, integercxx . . $ integer# channel) {
372 return Sampled_getMeancxx . . $ Sampled_getMean(2a8b0d40a92e6734). (me, xmin, xmax, channel, 0, true);
373 }
374
375 double Vector_getStandardDeviationcxx . . $ Vector_getStandardDeviation(8ea9441cb87dc78e). (constVectorcxx . . $ constVector# me, double xmin, double xmax, integercxx . . $ integer# ilevel) {
376 Function_unidirectionalAutowindowcxx . . $ Function_unidirectionalAutowindow(88b7840c59a57eaf). (me, & xmin, & xmax);
377 integercxx . . $ integer# imin, imax, n = Sampled_getWindowSamplescxx . . $ Sampled_getWindowSamples(fe99094ed5bb3f6a). (me, xmin, xmax, & imin, & imax);
378 if (n < 2)
379 return undefinedcxx . . $ undefined.;
380 if (ilevel == Vector_CHANNEL_AVERAGEcxx . . $ `fon/Vector.h:49:9`!) {
381 longdoublecxx . . $ longdouble# sum2 = 0.0;
382 for (integercxx . . $ integer# channel = 1; channel <= mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.; channel ++) {
383 const double mean = Vector_getMeancxx . . $ Vector_getMean(8ea9441cb87dc78e). (me, xmin, xmax, channel);
384 for (integercxx . . $ integer# i = imin; i <= imax; i ++) {
385 const double diff = mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).channel] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] - mean;
386 sum2 += diff * diff;
387 }
388 }
389 return sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (double (sum2 / (n * mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny. - mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#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_getMeancxx . . $ Vector_getMean(8ea9441cb87dc78e). (me, xmin, xmax, ilevel);
394 longdoublecxx . . $ longdouble# sum2 = 0.0;
395 for (integercxx . . $ integer# i = imin; i <= imax; i ++) {
396 const double diff = mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ilevel] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] - mean;
397 sum2 += diff * diff;
398 }
399 return sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (double (sum2 / (n - 1)));
400 }
401
402 /***** Modify. *****/
403
404 void Vector_addScalarcxx . . $ Vector_addScalar(54f31cdb89d1acde). (mutableVectorcxx . . $ mutableVector# me, double scalar) {
405 for (integercxx . . $ integer# ichan = 1; ichan <= mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.; ichan ++)
406 mycxx . . $ `melder/melder.h:44:9`! channelcxx . . $ structVector#channel(516a12db1224da40). (ichan) +=cxx . . $ `operator+=`(c555d170d72ff256). scalar;
407 }
408
409 void Vector_subtractMeancxx . . $ Vector_subtractMean(8ab8c8e01f03cabf). (mutableVectorcxx . . $ mutableVector# me) {
410 for (integercxx . . $ integer# ichan = 1; ichan <= mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.; ichan ++)
411 centre_VEC_inoutcxx . . $ centre_VEC_inout(b915798f9bafc8a). (mycxx . . $ `melder/melder.h:44:9`! channelcxx . . $ structVector#channel(516a12db1224da40). (ichan));
412 }
413
414 void Vector_multiplyByScalarcxx . . $ Vector_multiplyByScalar(54f31cdb89d1acde). (mutableVectorcxx . . $ mutableVector# me, double scalar) {
415 for (integercxx . . $ integer# ichan = 1; ichan <= mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.; ichan ++)
416 mycxx . . $ `melder/melder.h:44:9`! channelcxx . . $ structVector#channel(516a12db1224da40). (ichan) *=cxx . . $ `operator*=`(c555d170d72ff256). scalar;
417 }
418
419 void Vector_scalecxx . . $ Vector_scale(54f31cdb89d1acde). (mutableVectorcxx . . $ mutableVector# me, double scale) {
420 const double extremum = NUMextremum_ecxx . . $ NUMextremum_e(7a3744b654164b28). (mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z..getcxx . . $ automatrix#get(b2b81450b5bdda66).());
421 if (extremum != 0.0)
422 Vector_multiplyByScalarcxx . . $ Vector_multiplyByScalar(54f31cdb89d1acde). (me, scale / extremum);
423 }
424
425 /***** Graphics. *****/
426
427 void Vector_drawcxx . . $ Vector_draw(ac83553bcb211d2d). (constVectorcxx . . $ constVector# me, Graphicscxx . . $ Graphics# graphics, double *pxmin, double *pxmax, double *pymin, double *pymax,
428 double defaultDy, conststring32cxx . . $ conststring32# method)
429 {
430 const bool xreversed = ( *pxmin > *pxmax ), yreversed = ( *pymin > *pymax );
431 if (xreversed)
432 stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06). (*pxmin, *pxmax);
433 if (yreversed)
434 stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06). (*pymin, *pymax);
435 Function_bidirectionalAutowindowcxx . . $ Function_bidirectionalAutowindow(88b7840c59a57eaf). (me, pxmin, pxmax);
436 /*
437 Domain expressed in sample numbers.
438 */
439 integercxx . . $ integer# ixmin, ixmax;
440 const integercxx . . $ integer# n = Matrix_getWindowSamplesXcxx . . $ Matrix_getWindowSamplesX(c738979be11bd51a). (me, *pxmin, *pxmax, & ixmin, & ixmax);
441 if (n < 1)
442 return;
443 /*
444 Automatic vertical range.
445 */
446 if (*pymin == *pymax) {
447 Matrix_getWindowExtremacxx . . $ Matrix_getWindowExtrema(d50591dd897af896). (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_setInnercxx . . $ Graphics_setInner(2d838ed765120c5d). (graphics);
457 Graphics_setWindowcxx . . $ Graphics_setWindow(72bdb7937dbb13da). (graphics, ( xreversed ? *pxmax : *pxmin ), ( xreversed ? *pxmin : *pxmax ),
458 ( yreversed ? *pymax : *pymin ), ( yreversed ? *pymin : *pymax ));
459 if (str32strcxx . . $ str32str(2de17cd8f7c584a6). (method, U"bars") || str32strcxx . . $ str32str(2de17cd8f7c584a6). (method, U"Bars")) {
460 for (integercxx . . $ integer# ix = ixmin; ix <= ixmax; ix ++) {
461 const double x = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (me, ix);
462 double y = mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ix];
463 double left = x - 0.5 * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., right = x + 0.5 * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.;
464 Melder_clipRightcxx . . $ Melder_clipRight(5b63ce711df1ade7). (& y, *pymax);
465 Melder_clipLeftcxx . . $ Melder_clipLeft(6245cd023a7d1552). (*pxmin, & left);
466 Melder_clipRightcxx . . $ Melder_clipRight(5b63ce711df1ade7). (& right, *pxmax);
467 if (y > *pymin) {
468 Graphics_linecxx . . $ Graphics_line(72bdb7937dbb13da). (graphics, left, y, right, y);
469 Graphics_linecxx . . $ Graphics_line(72bdb7937dbb13da). (graphics, left, y, left, *pymin);
470 Graphics_linecxx . . $ Graphics_line(72bdb7937dbb13da). (graphics, right, y, right, *pymin);
471 }
472 }
473 } else if (str32strcxx . . $ str32str(2de17cd8f7c584a6). (method, U"poles") || str32strcxx . . $ str32str(2de17cd8f7c584a6). (method, U"Poles")) {
474 for (integercxx . . $ integer# ix = ixmin; ix <= ixmax; ix ++) {
475 const double x = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (me, ix);
476 Graphics_linecxx . . $ Graphics_line(72bdb7937dbb13da). (graphics, x, 0.0, x, mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ix]);
477 }
478 } else if (str32strcxx . . $ str32str(2de17cd8f7c584a6). (method, U"speckles") || str32strcxx . . $ str32str(2de17cd8f7c584a6). (method, U"Speckles")) {
479 for (integercxx . . $ integer# ix = ixmin; ix <= ixmax; ix ++) {
480 const double x = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (me, ix);
481 Graphics_specklecxx . . $ Graphics_speckle(c84ba9158791aef6). (graphics, x, mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ix]);
482 }
483 } else {
484 /*
485 The default: draw as a curve.
486 */
487 Graphics_functioncxx . . $ Graphics_function(1c6279e2c8b8beb1). (graphics, & mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).0], ixmin, ixmax,
488 Matrix_columnToXcxx . . $ Matrix_columnToX(89d6d8659270b8b7). (me, ixmin), Matrix_columnToXcxx . . $ Matrix_columnToX(89d6d8659270b8b7). (me, ixmax));
489 }
490 Graphics_unsetInnercxx . . $ Graphics_unsetInner(2d838ed765120c5d). (graphics);
491 }
492
493 /* End of file Vector.cpp */