| 1 |
/* RealTier.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1992-2012,2014-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 "RealTier.h" |
| 20 |
#include "Formula.h" |
| 21 |
|
| 22 |
#include "oo_DESTROY.h" |
| 23 |
#include "RealTier_def.h" |
| 24 |
#include "oo_COPY.h" |
| 25 |
#include "RealTier_def.h" |
| 26 |
#include "oo_EQUAL.h" |
| 27 |
#include "RealTier_def.h" |
| 28 |
#include "oo_CAN_WRITE_AS_ENCODING.h" |
| 29 |
#include "RealTier_def.h" |
| 30 |
#include "oo_WRITE_TEXT.h" |
| 31 |
#include "RealTier_def.h" |
| 32 |
#include "oo_READ_TEXT.h" |
| 33 |
#include "RealTier_def.h" |
| 34 |
#include "oo_WRITE_BINARY.h" |
| 35 |
#include "RealTier_def.h" |
| 36 |
#include "oo_READ_BINARY.h" |
| 37 |
#include "RealTier_def.h" |
| 38 |
#include "oo_DESCRIPTION.h" |
| 39 |
#include "RealTier_def.h" |
| 40 |
|
| 41 |
/********** class RealPoint **********/ |
| 42 |
|
| 43 |
Thing_implement (RealPoint, AnyPoint, 0); |
| 44 |
|
| 45 |
autoRealPoint RealPoint_create (double time, double value) { |
| 46 |
autoRealPoint me = Thing_new (RealPoint); |
| 47 |
my number = time; |
| 48 |
my value = value; |
| 49 |
return me; |
| 50 |
} |
| 51 |
|
| 52 |
/********** class RealTier **********/ |
| 53 |
|
| 54 |
void structRealTier :: v1_info () { |
| 55 |
structFunction :: v1_info (); |
| 56 |
MelderInfo_writeLine (U"Number of points: ", our points.size); |
| 57 |
MelderInfo_writeLine (U"Minimum value: ", RealTier_getMinimumValue (this)); |
| 58 |
MelderInfo_writeLine (U"Maximum value: ", RealTier_getMaximumValue (this)); |
| 59 |
} |
| 60 |
|
| 61 |
double structRealTier :: v_getVector (const integer /*irow*/, const integer icol) const { |
| 62 |
return RealTier_getValueAtIndex (this, icol); |
| 63 |
} |
| 64 |
|
| 65 |
double structRealTier :: v_getFunction1 (const integer /*irow*/, const double x) const { |
| 66 |
return RealTier_getValueAtTime (this, x); |
| 67 |
} |
| 68 |
|
| 69 |
Thing_implement (RealTier, AnyTier, 0); // the semantic superclass (see RealTier_def.h for explanation) |
| 70 |
|
| 71 |
void RealTier_init (RealTier me, double tmin, double tmax) { |
| 72 |
my xmin = tmin; |
| 73 |
my xmax = tmax; |
| 74 |
} |
| 75 |
|
| 76 |
autoRealTier RealTier_create (double tmin, double tmax) { |
| 77 |
try { |
| 78 |
autoRealTier me = Thing_new (RealTier); |
| 79 |
RealTier_init (me.get(), tmin, tmax); |
| 80 |
return me; |
| 81 |
} catch (MelderError) { |
| 82 |
Melder_throw (U"RealTier not created."); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
autoRealTier RealTier_createWithClass (double tmin, double tmax, ClassInfo klas) { |
| 87 |
try { |
| 88 |
autoRealTier me = Thing_newFromClass (klas).static_cast_move <structRealTier>(); |
| 89 |
RealTier_init (me.get(), tmin, tmax); |
| 90 |
return me; |
| 91 |
} catch (MelderError) { |
| 92 |
Melder_throw (klas -> className, U" not created."); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
template <typename T> autoSomeThing <T> Thing_create () { |
| 97 |
return Thing_newFromClass (nullptr); |
| 98 |
} |
| 99 |
|
| 100 |
template <> |
| 101 |
autoSomeThing <structRealTier> Thing_create <structRealTier> () { |
| 102 |
return Thing_newFromClass (classRealTier).static_cast_move <structRealTier>(); |
| 103 |
} |
| 104 |
|
| 105 |
template <typename structSomeRealTier> |
| 106 |
autoSomeThing <structSomeRealTier> SomeRealTier_create (const double tmin, const double tmax) { |
| 107 |
try { |
| 108 |
autoSomeThing <structSomeRealTier> me = Thing_create <structSomeRealTier> (); |
| 109 |
RealTier_init (me.get(), tmin, tmax); |
| 110 |
return me; |
| 111 |
} catch (MelderError) { |
| 112 |
Melder_throw (U"RealTier not created."); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
void RealTier_addPoint (const mutableRealTier me, const double t, const double value) { |
| 117 |
try { |
| 118 |
autoRealPoint point = RealPoint_create (t, value); |
| 119 |
my points. addItem_move (point.move()); |
| 120 |
} catch (MelderError) { |
| 121 |
Melder_throw (me, U": point not added."); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
double RealTier_getValueAtIndex (const constRealTier me, const integer i) { |
| 126 |
if (i < 1 || i > my points.size) |
| 127 |
return undefined; |
| 128 |
return my points.at [i] -> value; |
| 129 |
} |
| 130 |
|
| 131 |
double RealTier_getValueAtTime (const constRealTier me, const double t) { |
| 132 |
const integer n = my points.size; |
| 133 |
if (n == 0) |
| 134 |
return undefined; |
| 135 |
const constRealPoint firstPoint = my points.at [1]; |
| 136 |
if (t <= firstPoint -> number) |
| 137 |
return firstPoint -> value; // constant extrapolation |
| 138 |
const constRealPoint lastPoint = my points.at [n]; |
| 139 |
if (t >= lastPoint -> number) |
| 140 |
return lastPoint -> value; // constant extrapolation |
| 141 |
Melder_assert (n >= 2); |
| 142 |
const integer ileft = AnyTier_timeToLowIndex (me->asConstAnyTier(), t), iright = ileft + 1; |
| 143 |
Melder_assert (ileft >= 1 && iright <= n); |
| 144 |
const constRealPoint pointLeft = my points.at [ileft]; |
| 145 |
const constRealPoint pointRight = my points.at [iright]; |
| 146 |
const double tleft = pointLeft -> number, fleft = pointLeft -> value; |
| 147 |
const double tright = pointRight -> number, fright = pointRight -> value; |
| 148 |
return t == tright ? fright // be very accurate |
| 149 |
: tleft == tright ? 0.5 * (fleft + fright) // unusual, but possible; no preference |
| 150 |
: fleft + (t - tleft) * (fright - fleft) / (tright - tleft); // linear interpolation |
| 151 |
} |
| 152 |
|
| 153 |
double RealTier_getMaximumValue (const constRealTier me) { |
| 154 |
/* mutable */ double result = undefined; |
| 155 |
const integer n = my points.size; |
| 156 |
for (integer i = 1; i <= n; i ++) { |
| 157 |
const constRealPoint point = my points.at [i]; |
| 158 |
if (isundef (result) || point -> value > result) |
| 159 |
result = point -> value; |
| 160 |
} |
| 161 |
return result; |
| 162 |
} |
| 163 |
|
| 164 |
double RealTier_getMinimumValue (const constRealTier me) { |
| 165 |
Melder_assert (me); |
| 166 |
/* mutable */ double result = undefined; |
| 167 |
const integer n = my points.size; |
| 168 |
for (integer i = 1; i <= n; i ++) { |
| 169 |
Melder_assert (my points.at._elements); |
| 170 |
RealPoint point = my points.at [i]; |
| 171 |
Melder_assert (point); |
| 172 |
if (isundef (result) || point -> value < result) |
| 173 |
result = point -> value; |
| 174 |
} |
| 175 |
return result; |
| 176 |
} |
| 177 |
|
| 178 |
double RealTier_getArea (const constRealTier me, const double tmin, const double tmax) { |
| 179 |
const integer n = my points.size; |
| 180 |
if (n == 0) |
| 181 |
return undefined; |
| 182 |
if (n == 1) |
| 183 |
return (tmax - tmin) * my points.at [1] -> value; |
| 184 |
const integer imin = AnyTier_timeToLowIndex (me->asConstAnyTier(), tmin); |
| 185 |
if (imin == n) |
| 186 |
return (tmax - tmin) * my points.at [n] -> value; |
| 187 |
const integer imax = AnyTier_timeToHighIndex (me->asConstAnyTier(), tmax); |
| 188 |
if (imax == 1) |
| 189 |
return (tmax - tmin) * my points.at [1] -> value; |
| 190 |
Melder_assert (imin < n); |
| 191 |
Melder_assert (imax > 1); |
| 192 |
/* |
| 193 |
Sum the areas between the points. |
| 194 |
This works even if imin is 0 (offleft) and/or imax is n + 1 (offright). |
| 195 |
*/ |
| 196 |
/* mutable loop */ longdouble area = 0.0; |
| 197 |
for (integer i = imin; i < imax; i ++) { |
| 198 |
/* mutable conditional init */ double tleft, fleft, tright, fright; |
| 199 |
if (i == imin) { |
| 200 |
tleft = tmin; |
| 201 |
fleft = RealTier_getValueAtTime (me, tmin); |
| 202 |
} else { |
| 203 |
tleft = my points.at [i] -> number; |
| 204 |
fleft = my points.at [i] -> value; |
| 205 |
} |
| 206 |
if (i + 1 == imax) { |
| 207 |
tright = tmax; |
| 208 |
fright = RealTier_getValueAtTime (me, tmax); |
| 209 |
} else { |
| 210 |
tright = my points.at [i + 1] -> number; |
| 211 |
fright = my points.at [i + 1] -> value; |
| 212 |
} |
| 213 |
area += 0.5 * (fleft + fright) * (tright - tleft); |
| 214 |
} |
| 215 |
return (double) area; |
| 216 |
} |
| 217 |
|
| 218 |
double RealTier_getMean_curve_u (const constRealTier me, /* mutable */ double tmin, /* mutable */ double tmax) { |
| 219 |
Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 220 |
const double area = RealTier_getArea (me, tmin, tmax); |
| 221 |
if (isundef (area)) |
| 222 |
return undefined; |
| 223 |
return area / (tmax - tmin); |
| 224 |
} |
| 225 |
|
| 226 |
double RealTier_getStandardDeviation_curve_u (const constRealTier me, /* mutable */ double tmin, /* mutable */ double tmax) { |
| 227 |
Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 228 |
const integer n = my points.size; |
| 229 |
if (n == 0) |
| 230 |
return undefined; |
| 231 |
if (n == 1) |
| 232 |
return 0.0; |
| 233 |
const integer imin = AnyTier_timeToLowIndex (me->asConstAnyTier(), tmin); |
| 234 |
if (imin == n) |
| 235 |
return 0.0; |
| 236 |
const integer imax = AnyTier_timeToHighIndex (me->asConstAnyTier(), tmax); |
| 237 |
if (imax == 1) |
| 238 |
return 0.0; |
| 239 |
Melder_assert (imin < n); |
| 240 |
Melder_assert (imax > 1); |
| 241 |
/* |
| 242 |
Add the areas between the points. |
| 243 |
This works even if imin is 0 (offleft) and/or imax is n + 1 (offright). |
| 244 |
*/ |
| 245 |
const double mean = RealTier_getMean_curve_u (me, tmin, tmax); |
| 246 |
/* mutable loop */ longdouble integral = 0.0; |
| 247 |
for (integer i = imin; i < imax; i ++) { |
| 248 |
double tleft, fleft, tright, fright; |
| 249 |
if (i == imin) { |
| 250 |
tleft = tmin; |
| 251 |
fleft = RealTier_getValueAtTime (me, tmin); |
| 252 |
} else { |
| 253 |
tleft = my points.at [i] -> number; |
| 254 |
fleft = my points.at [i] -> value - mean; |
| 255 |
} |
| 256 |
if (i + 1 == imax) { |
| 257 |
tright = tmax; |
| 258 |
fright = RealTier_getValueAtTime (me, tmax); |
| 259 |
} else { |
| 260 |
tright = my points.at [i + 1] -> number; |
| 261 |
fright = my points.at [i + 1] -> value - mean; |
| 262 |
} |
| 263 |
/* |
| 264 |
* The area is integral dt f^2 |
| 265 |
* = integral dt [f1 + (f2-f1)/(t2-t1) (t-t1)]^2 |
| 266 |
* = int dt f1^2 + int dt 2 f1 (f2-f1)/(t2-t1) (t-t1) + int dt [(f2-f1)/(t2-t1)]^2 (t-t1)^2 |
| 267 |
* = f1^2 (t2-t1) + f1 (f2-f1)/(t2-t1) (t2-t1)^2 + 1/3 [(f2-f1)/(t2-t1)]^2 (t2-t1)^3 |
| 268 |
* = (t2-t1) [f1 f2 + 1/3 (f2-f1)^2] |
| 269 |
* = (t2-t1) (f1^2 + f2^2 + 1/3 f1 f2) |
| 270 |
* = (t2-t1) [1/4 (f1+f2)^2 + 1/12 (f1-f2)^2] |
| 271 |
* In the last expression, we have a sum of squares, which is computationally best. |
| 272 |
*/ |
| 273 |
const double sum = fleft + fright; |
| 274 |
const double diff = fleft - fright; |
| 275 |
integral += (sum * sum + (1.0/3.0) * diff * diff) * (tright - tleft); |
| 276 |
} |
| 277 |
return sqrt (0.25 * (double) integral / (tmax - tmin)); |
| 278 |
} |
| 279 |
|
| 280 |
double RealTier_getMean_points_u (const constRealTier me, /* mutable */ double tmin, /* mutable */ double tmax) { |
| 281 |
Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 282 |
/* mutable init */ integer imin, imax; |
| 283 |
const integer n = AnyTier_getWindowPoints (me->asConstAnyTier(), tmin, tmax, & imin, & imax); |
| 284 |
if (n == 0) |
| 285 |
return undefined; |
| 286 |
/* mutable loop */ longdouble sum = 0.0; |
| 287 |
for (integer i = imin; i <= imax; i ++) |
| 288 |
sum += my points.at [i] -> value; |
| 289 |
return (double) sum / n; |
| 290 |
} |
| 291 |
|
| 292 |
double RealTier_getStandardDeviation_points_u (const constRealTier me, /* mutable */ double tmin, /* mutable */ double tmax) { |
| 293 |
Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 294 |
/* mutable init */ integer imin, imax; |
| 295 |
const integer n = AnyTier_getWindowPoints (me->asConstAnyTier(), tmin, tmax, & imin, & imax); |
| 296 |
if (n < 2) |
| 297 |
return undefined; |
| 298 |
const double mean = RealTier_getMean_points_u (me, tmin, tmax); |
| 299 |
/* mutable loop */ longdouble sum = 0.0; |
| 300 |
for (integer i = imin; i <= imax; i ++) { |
| 301 |
const double diff = my points.at [i] -> value - mean; |
| 302 |
sum += diff * diff; |
| 303 |
} |
| 304 |
return sqrt ((double) sum / (n - 1)); |
| 305 |
} |
| 306 |
|
| 307 |
void RealTier_multiplyPart (RealTier me, double tmin, double tmax, double factor) { |
| 308 |
for (integer ipoint = 1; ipoint <= my points.size; ipoint ++) { |
| 309 |
const RealPoint point = my points.at [ipoint]; |
| 310 |
const double t = point -> number; |
| 311 |
if (t >= tmin && t <= tmax) |
| 312 |
point -> value *= factor; |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
void RealTier_draw ( |
| 317 |
const constRealTier me, |
| 318 |
const Graphics g, |
| 319 |
/* mutable */ double tmin, /* mutable */ double tmax, |
| 320 |
const double fmin, const double fmax, |
| 321 |
const bool garnish, |
| 322 |
const conststring32 method, |
| 323 |
const conststring32 quantity |
| 324 |
) { |
| 325 |
Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 326 |
const bool drawLines = str32str (method, U"lines") || str32str (method, U"Lines"); |
| 327 |
const bool drawSpeckles = str32str (method, U"speckles") || str32str (method, U"Speckles"); |
| 328 |
const integer n = my points.size; |
| 329 |
Graphics_setWindow (g, tmin, tmax, fmin, fmax); |
| 330 |
Graphics_setInner (g); |
| 331 |
const integer imin = AnyTier_timeToHighIndex (me->asConstAnyTier(), tmin); |
| 332 |
const integer imax = AnyTier_timeToLowIndex (me->asConstAnyTier(), tmax); |
| 333 |
if (n == 0) { |
| 334 |
} else if (imax < imin) { |
| 335 |
const double fleft = RealTier_getValueAtTime (me, tmin); |
| 336 |
const double fright = RealTier_getValueAtTime (me, tmax); |
| 337 |
if (drawLines) |
| 338 |
Graphics_line (g, tmin, fleft, tmax, fright); |
| 339 |
} else for (integer i = imin; i <= imax; i ++) { |
| 340 |
const constRealPoint point = my points.at [i]; |
| 341 |
const double t = point -> number, f = point -> value; |
| 342 |
if (drawSpeckles) |
| 343 |
Graphics_speckle (g, t, f); |
| 344 |
if (drawLines) { |
| 345 |
if (i == 1) |
| 346 |
Graphics_line (g, tmin, f, t, f); |
| 347 |
else if (i == imin) |
| 348 |
Graphics_line (g, t, f, tmin, RealTier_getValueAtTime (me, tmin)); |
| 349 |
if (i == n) |
| 350 |
Graphics_line (g, t, f, tmax, f); |
| 351 |
else if (i == imax) |
| 352 |
Graphics_line (g, t, f, tmax, RealTier_getValueAtTime (me, tmax)); |
| 353 |
else { |
| 354 |
const constRealPoint pointRight = my points.at [i + 1]; |
| 355 |
Graphics_line (g, t, f, pointRight -> number, pointRight -> value); |
| 356 |
} |
| 357 |
} |
| 358 |
} |
| 359 |
Graphics_unsetInner (g); |
| 360 |
if (garnish) { |
| 361 |
Graphics_drawInnerBox (g); |
| 362 |
Graphics_textBottom (g, true, my v_getUnitText (0, 0, 0)); |
| 363 |
Graphics_marksBottom (g, 2, true, true, false); |
| 364 |
Graphics_marksLeft (g, 2, true, true, false); |
| 365 |
if (quantity) |
| 366 |
Graphics_textLeft (g, true, quantity); |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
autoTableOfReal RealTier_downto_TableOfReal (const constRealTier me, const conststring32 timeLabel, const conststring32 valueLabel) { |
| 371 |
try { |
| 372 |
autoTableOfReal thee = TableOfReal_create (my points.size, 2); |
| 373 |
TableOfReal_setColumnLabel (thee.get(), 1, timeLabel); |
| 374 |
TableOfReal_setColumnLabel (thee.get(), 2, valueLabel); |
| 375 |
for (integer i = 1; i <= my points.size; i ++) { |
| 376 |
RealPoint point = my points.at [i]; |
| 377 |
thy data [i] [1] = point -> number; |
| 378 |
thy data [i] [2] = point -> value; |
| 379 |
} |
| 380 |
return thee; |
| 381 |
} catch (MelderError) { |
| 382 |
Melder_throw (me, U": not converted to TableOfReal."); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
void RealTier_interpolateQuadratically (const RealTier me, const integer numberOfPointsPerParabola, const bool logarithmically) { |
| 387 |
try { |
| 388 |
autoRealTier thee = Data_copy (me); |
| 389 |
for (integer ipoint = 1; ipoint < my points.size; ipoint ++) { |
| 390 |
const constRealPoint point1 = my points.at [ipoint], point2 = my points.at [ipoint + 1]; |
| 391 |
const double time1 = point1 -> number, time2 = point2 -> number, tmid = 0.5 * (time1 + time2); |
| 392 |
double value1 = point1 -> value, value2 = point2 -> value, valuemid; |
| 393 |
const double timeStep = (tmid - time1) / (numberOfPointsPerParabola + 1); |
| 394 |
if (logarithmically) { |
| 395 |
value1 = log (value1); |
| 396 |
value2 = log (value2); |
| 397 |
} |
| 398 |
valuemid = 0.5 * (value1 + value2); |
| 399 |
/* |
| 400 |
Left from the midpoint. |
| 401 |
*/ |
| 402 |
for (integer inewpoint = 1; inewpoint <= numberOfPointsPerParabola; inewpoint ++) { |
| 403 |
const double newTime = time1 + inewpoint * timeStep; |
| 404 |
const double phase = (newTime - time1) / (tmid - time1); |
| 405 |
double newValue = value1 + (valuemid - value1) * phase * phase; |
| 406 |
if (logarithmically) |
| 407 |
newValue = exp (newValue); |
| 408 |
RealTier_addPoint (thee.get(), newTime, newValue); |
| 409 |
} |
| 410 |
/* |
| 411 |
The midpoint. |
| 412 |
*/ |
| 413 |
RealTier_addPoint (thee.get(), tmid, logarithmically ? exp (valuemid) : valuemid); |
| 414 |
/* |
| 415 |
Right from the midpoint. |
| 416 |
*/ |
| 417 |
for (integer inewpoint = 1; inewpoint <= numberOfPointsPerParabola; inewpoint ++) { |
| 418 |
const double newTime = tmid + inewpoint * timeStep; |
| 419 |
const double phase = (time2 - newTime) / (time2 - tmid); |
| 420 |
double newValue = value2 + (valuemid - value2) * phase * phase; |
| 421 |
if (logarithmically) |
| 422 |
newValue = exp (newValue); |
| 423 |
RealTier_addPoint (thee.get(), newTime, newValue); |
| 424 |
} |
| 425 |
} |
| 426 |
Thing_swap (me, thee.get()); |
| 427 |
} catch (MelderError) { |
| 428 |
Melder_throw (me, U": not interpolated quadratically."); |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
autoTable RealTier_downto_Table (RealTier me, conststring32 indexText, conststring32 timeText, conststring32 valueText) { |
| 433 |
try { |
| 434 |
autoTable thee = Table_createWithoutColumnNames (my points.size, |
| 435 |
(!! indexText) + (!! timeText) + (!! valueText)); |
| 436 |
integer icol = 0; |
| 437 |
if (indexText) Table_renameColumn_e (thee.get(), ++ icol, indexText); |
| 438 |
if (timeText ) Table_renameColumn_e (thee.get(), ++ icol, timeText); |
| 439 |
if (valueText) Table_renameColumn_e (thee.get(), ++ icol, valueText); |
| 440 |
for (integer ipoint = 1; ipoint <= my points.size; ipoint ++) { |
| 441 |
RealPoint point = my points.at [ipoint]; |
| 442 |
icol = 0; |
| 443 |
if (indexText) Table_setNumericValue (thee.get(), ipoint, ++ icol, ipoint); |
| 444 |
if (timeText) Table_setNumericValue (thee.get(), ipoint, ++ icol, point -> number); |
| 445 |
if (valueText) Table_setNumericValue (thee.get(), ipoint, ++ icol, point -> value); |
| 446 |
} |
| 447 |
return thee; |
| 448 |
} catch (MelderError) { |
| 449 |
Melder_throw (me, U": not converted to Table."); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
autoRealTier Vector_to_RealTier (Vector me, integer channel, ClassInfo klas) { |
| 454 |
try { |
| 455 |
autoRealTier thee = RealTier_createWithClass (my xmin, my xmax, klas); |
| 456 |
for (integer i = 1; i <= my nx; i ++) |
| 457 |
RealTier_addPoint (thee.get(), Sampled_indexToX (me, i), my z [channel] [i]); |
| 458 |
return thee; |
| 459 |
} catch (MelderError) { |
| 460 |
Melder_throw (me, U": not converted to ", klas -> className, U"."); |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
autoRealTier Vector_to_RealTier_peaks (Vector me, integer channel, ClassInfo klas) { |
| 465 |
try { |
| 466 |
autoRealTier thee = RealTier_createWithClass (my xmin, my xmax, klas); |
| 467 |
for (integer i = 2; i < my nx; i ++) { |
| 468 |
double left = my z [channel] [i - 1], centre = my z [channel] [i], right = my z [channel] [i + 1]; |
| 469 |
if (left <= centre && right < centre) { |
| 470 |
double x, maximum; |
| 471 |
Vector_getMaximumAndX (me, my x1 + (i - 2.5) * my dx, my x1 + (i + 0.5) * my dx, |
| 472 |
channel, kVector_peakInterpolation :: PARABOLIC, & maximum, & x); |
| 473 |
RealTier_addPoint (thee.get(), x, maximum); |
| 474 |
} |
| 475 |
} |
| 476 |
return thee; |
| 477 |
} catch (MelderError) { |
| 478 |
Melder_throw (me, U": not converted to ", klas -> className, U" (peaks)."); |
| 479 |
} |
| 480 |
} |
| 481 |
|
| 482 |
autoRealTier Vector_to_RealTier_valleys (Vector me, integer channel, ClassInfo klas) { |
| 483 |
try { |
| 484 |
autoRealTier thee = RealTier_createWithClass (my xmin, my xmax, klas); |
| 485 |
for (integer i = 2; i < my nx; i ++) { |
| 486 |
double left = my z [channel] [i - 1], centre = my z [channel] [i], right = my z [channel] [i + 1]; |
| 487 |
if (left >= centre && right > centre) { |
| 488 |
double x, minimum; |
| 489 |
Vector_getMinimumAndX (me, my x1 + (i - 2.5) * my dx, my x1 + (i + 0.5) * my dx, |
| 490 |
channel, kVector_peakInterpolation :: PARABOLIC, & minimum, & x); |
| 491 |
RealTier_addPoint (thee.get(), x, minimum); |
| 492 |
} |
| 493 |
} |
| 494 |
return thee; |
| 495 |
} catch (MelderError) { |
| 496 |
Melder_throw (me, U": not converted to ", klas -> className, U" (valleys)."); |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
autoRealTier PointProcess_upto_RealTier (PointProcess me, double value, ClassInfo klas) { |
| 501 |
try { |
| 502 |
autoRealTier thee = RealTier_createWithClass (my xmin, my xmax, klas); |
| 503 |
for (integer i = 1; i <= my nt; i ++) |
| 504 |
RealTier_addPoint (thee.get(), my t [i], value); |
| 505 |
return thee; |
| 506 |
} catch (MelderError) { |
| 507 |
Melder_throw (me, U": not converted to RealTier."); |
| 508 |
} |
| 509 |
} |
| 510 |
|
| 511 |
void RealTier_formula (RealTier me, conststring32 expression, Interpreter interpreter, RealTier thee) { |
| 512 |
try { |
| 513 |
Formula_compile (interpreter, me, expression, kFormula_EXPRESSION_TYPE_NUMERIC, true); |
| 514 |
Formula_Result result; |
| 515 |
if (! thee) |
| 516 |
thee = me; |
| 517 |
for (integer icol = 1; icol <= my points.size; icol ++) { |
| 518 |
Formula_run (0, icol, & result); |
| 519 |
if (isundef (result. numericResult)) |
| 520 |
Melder_throw (U"Cannot put an undefined value into the tier."); |
| 521 |
thy points.at [icol] -> value = result. numericResult; |
| 522 |
} |
| 523 |
} catch (MelderError) { |
| 524 |
Melder_throw (me, U": formula not completed."); |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
void RealTier_removePointsBelow (RealTier me, double level) { |
| 529 |
for (integer ipoint = my points.size; ipoint > 0; ipoint --) { |
| 530 |
RealPoint point = my points.at [ipoint]; |
| 531 |
if (point -> value < level) |
| 532 |
AnyTier_removePoint (me->asAnyTier(), ipoint); |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
void RealTier_PointProcess_into_RealTier (RealTier me, PointProcess pp, RealTier thee) { |
| 537 |
for (integer i = 1; i <= pp -> nt; i ++) { |
| 538 |
const double time = pp -> t [i]; |
| 539 |
const double value = RealTier_getValueAtTime (me, time); |
| 540 |
RealTier_addPoint (thee, time, value); |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
autoRealTier RealTier_PointProcess_to_RealTier (RealTier me, PointProcess pp) { |
| 545 |
try { |
| 546 |
if (my points.size == 0) |
| 547 |
Melder_throw (U"No points."); |
| 548 |
autoRealTier thee = RealTier_create (pp -> xmin, pp -> xmax); |
| 549 |
RealTier_PointProcess_into_RealTier (me, pp, thee.get()); |
| 550 |
return thee; |
| 551 |
} catch (MelderError) { |
| 552 |
Melder_throw (me, U" & ", pp, U": not converted to RealTier."); |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
autoRealTier AnyRealTier_downto_RealTier (RealTier me) { |
| 557 |
try { |
| 558 |
autoRealTier thee = Thing_new (RealTier); |
| 559 |
my structRealTier :: v1_copy (thee.get()); |
| 560 |
return thee; |
| 561 |
} catch (MelderError) { |
| 562 |
Melder_throw (me, U": not converted to RealTier."); |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
static void RealTier_checkThatNoPointFallsOutsideDefinedTimeDomain (RealTier me) { |
| 567 |
if (my points.size == 0) { |
| 568 |
// nothing to check |
| 569 |
} else if (my points.size == 1) { |
| 570 |
const double onlyTime = my points.at [1] -> number; |
| 571 |
if (isdefined (my xmin)) |
| 572 |
Melder_require (my xmin <= onlyTime, |
| 573 |
U"The only point (at time ", onlyTime, U" seconds) falls outside the time domain, i.e. before ", my xmin, U" seconds."); |
| 574 |
if (isdefined (my xmax)) |
| 575 |
Melder_require (my xmax >= onlyTime, |
| 576 |
U"The only point (at time ", onlyTime, U" seconds) falls outside the time domain, i.e. after ", my xmax, U" seconds."); |
| 577 |
} else { |
| 578 |
const double firstTime = my points.at [1] -> number; |
| 579 |
const double lastTime = my points.at [my points.size] -> number; |
| 580 |
if (isdefined (my xmin)) |
| 581 |
Melder_require (my xmin <= firstTime, |
| 582 |
U"The first point (at time ", firstTime, U" seconds) falls outside the time domain, i.e. before ", my xmin, U" seconds."); |
| 583 |
if (isdefined (my xmax)) |
| 584 |
Melder_require (my xmax >= lastTime, |
| 585 |
U"The last point (at time ", lastTime, U" seconds) falls outside the time domain, i.e. after ", my xmax, U" seconds."); |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
static void RealTier_fitUndefinedTimeDomainToData (RealTier me) { |
| 590 |
if (my points.size == 0) { |
| 591 |
if (isundef (my xmin) && isundef (my xmax)) { |
| 592 |
my xmin = 0.0; |
| 593 |
my xmax = 1.0; |
| 594 |
} else if (isundef (my xmin)) { |
| 595 |
my xmin = my xmax - 1.0; |
| 596 |
} else if (isundef (my xmax)) { |
| 597 |
my xmax = my xmin + 1.0; |
| 598 |
} |
| 599 |
} else if (my points.size == 1) { |
| 600 |
const double onlyTime = my points.at [1] -> number; |
| 601 |
if (isundef (my xmin) && isundef (my xmax)) { |
| 602 |
my xmin = onlyTime - 1.0; |
| 603 |
my xmax = onlyTime + 1.0; |
| 604 |
} else if (isundef (my xmin)) { |
| 605 |
my xmin = onlyTime - 1.0 * ( my xmax == onlyTime ); |
| 606 |
} else if (isundef (my xmax)) { |
| 607 |
my xmax = onlyTime + 1.0 * ( my xmin == onlyTime ); |
| 608 |
} |
| 609 |
} else { |
| 610 |
const double firstTime = my points.at [1] -> number; |
| 611 |
const double lastTime = my points.at [my points.size] -> number; |
| 612 |
if (isundef (my xmin)) |
| 613 |
my xmin = firstTime; |
| 614 |
if (isundef (my xmax)) |
| 615 |
my xmax = lastTime; |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
autoRealTier Table_to_RealTier (Table me, integer timeColumn, integer valueColumn, double tmin, double tmax) { |
| 620 |
try { |
| 621 |
Melder_require (timeColumn >= 1 && timeColumn <= my numberOfColumns, |
| 622 |
U"The column number (for the times) should be between 1 and ", my numberOfColumns); |
| 623 |
Melder_require (valueColumn >= 1 && valueColumn <= my numberOfColumns, |
| 624 |
U"The column number (for the values) should be between 1 and ", my numberOfColumns); |
| 625 |
Melder_require (! (tmax <= tmin), // NaN-safe |
| 626 |
U"The end of the time domain (", tmax, U") should be greater than the start of the time domain (", tmin, U")."); |
| 627 |
autoRealTier thee = RealTier_create (tmin, tmax); |
| 628 |
Table_numericize_a (me, timeColumn); |
| 629 |
Table_numericize_a (me, valueColumn); |
| 630 |
for (integer irow = 1; irow <= my rows.size; irow ++) { |
| 631 |
TableRow row = my rows.at [irow]; |
| 632 |
RealTier_addPoint (thee.get(), row -> cells [timeColumn]. number, row -> cells [valueColumn]. number); |
| 633 |
} |
| 634 |
/* |
| 635 |
At this point, all times are in sorted order and unique, |
| 636 |
because RealTier_addPoint inserts its time in order and complains if the time already exists. |
| 637 |
The data-dependent tests therefore need to be only about the time domain. |
| 638 |
*/ |
| 639 |
RealTier_checkThatNoPointFallsOutsideDefinedTimeDomain (thee.get()); |
| 640 |
RealTier_fitUndefinedTimeDomainToData (thee.get()); |
| 641 |
return thee; |
| 642 |
} catch (MelderError) { |
| 643 |
Melder_throw (me, U": not converted to RealTier."); |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
autoRealTier Matrix_to_RealTier (Matrix me, integer timeColumn, integer valueColumn, double tmin, double tmax) { |
| 648 |
try { |
| 649 |
Melder_require (timeColumn >= 1 && timeColumn <= my nx, |
| 650 |
U"The column number (for the times) should be between 1 and ", my nx); |
| 651 |
Melder_require (valueColumn >= 1 && valueColumn <= my nx, |
| 652 |
U"The column number (for the values) should be between 1 and ", my nx); |
| 653 |
Melder_require (! (tmax <= tmin), // NaN-safe |
| 654 |
U"The end of the time domain (", tmax, U") should be greater than the start of the time domain (", tmin, U")."); |
| 655 |
autoRealTier thee = RealTier_create (tmin, tmax); |
| 656 |
for (integer irow = 1; irow <= my ny; irow ++) |
| 657 |
RealTier_addPoint (thee.get(), my z [irow] [timeColumn], my z [irow] [valueColumn]); |
| 658 |
/* |
| 659 |
At this point, all times are in sorted order and unique, |
| 660 |
because RealTier_addPoint inserts its time in order and complains if the time already exists. |
| 661 |
The data-dependent tests therefore need to be only about the time domain. |
| 662 |
*/ |
| 663 |
RealTier_checkThatNoPointFallsOutsideDefinedTimeDomain (thee.get()); |
| 664 |
RealTier_fitUndefinedTimeDomainToData (thee.get()); |
| 665 |
return thee; |
| 666 |
} catch (MelderError) { |
| 667 |
Melder_throw (me, U": not converted to RealTier."); |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
/* End of file RealTier.cpp */ |