void Sound_setZero (Sound me, double tmin_in, double tmax_in, int roundTimesToNearestZeroCrossing) { Function_unidirectionalAutowindow (me, & tmin_in, & tmax_in); Function_intersectRangeWithDomain (me, & tmin_in, & tmax_in); for (long channel = 1; channel <= my ny; channel ++) { double tmin = tmin_in, tmax = tmax_in; if (roundTimesToNearestZeroCrossing) { if (tmin > my xmin) tmin = Sound_getNearestZeroCrossing (me, tmin_in, channel); if (tmax < my xmax) tmax = Sound_getNearestZeroCrossing (me, tmax_in, channel); } if (tmin == NUMundefined) tmin = my xmin; if (tmax == NUMundefined) tmax = my xmax; long imin, imax; Sampled_getWindowSamples (me, tmin, tmax, & imin, & imax); for (long i = imin; i <= imax; i ++) { my z [channel] [i] = 0.0; } } }
double Sampled_getQuantile (I, double xmin, double xmax, double quantile, long ilevel, int unit) { iam (Sampled); long i, imin, imax, numberOfDefinedSamples = 0; double *values = NUMdvector (1, my nx), result = NUMundefined; iferror return NUMundefined; Function_unidirectionalAutowindow (me, & xmin, & xmax); if (! Function_intersectRangeWithDomain (me, & xmin, & xmax)) return NUMundefined; Sampled_getWindowSamples (me, xmin, xmax, & imin, & imax); for (i = imin; i <= imax; i ++) { double value = our getValueAtSample (me, i, ilevel, unit); if (NUMdefined (value)) { values [++ numberOfDefinedSamples] = value; } } if (numberOfDefinedSamples >= 1) { NUMsort_d (numberOfDefinedSamples, values); result = NUMquantile (numberOfDefinedSamples, values, quantile); } NUMdvector_free (values, 1); return result; }
double Sampled_getQuantile (Sampled me, double xmin, double xmax, double quantile, long ilevel, int unit) { try { autoNUMvector <double> values (1, my nx); Function_unidirectionalAutowindow (me, & xmin, & xmax); if (! Function_intersectRangeWithDomain (me, & xmin, & xmax)) return NUMundefined; long imin, imax, numberOfDefinedSamples = 0; Sampled_getWindowSamples (me, xmin, xmax, & imin, & imax); for (long i = imin; i <= imax; i ++) { double value = my v_getValueAtSample (i, ilevel, unit); if (NUMdefined (value)) { values [++ numberOfDefinedSamples] = value; } } double result = NUMundefined; if (numberOfDefinedSamples >= 1) { NUMsort_d (numberOfDefinedSamples, values.peek()); result = NUMquantile (numberOfDefinedSamples, values.peek(), quantile); } return result; } catch (MelderError) { Melder_throw (me, ": quantile not computed."); } }
void Sampled_getMaximumAndX (Sampled me, double xmin, double xmax, long ilevel, int unit, int interpolate, double *return_maximum, double *return_xOfMaximum) { long imin, imax, i; double maximum = -1e301, xOfMaximum = 0.0; if (xmin == NUMundefined || xmax == NUMundefined) { maximum = xOfMaximum = NUMundefined; goto end; } Function_unidirectionalAutowindow (me, & xmin, & xmax); if (! Function_intersectRangeWithDomain (me, & xmin, & xmax)) { maximum = xOfMaximum = NUMundefined; // requested range and logical domain do not intersect goto end; } if (! Sampled_getWindowSamples (me, xmin, xmax, & imin, & imax)) { /* * No sample centres between tmin and tmax. * Try to return the greater of the values at these two points. */ double fleft = Sampled_getValueAtX (me, xmin, ilevel, unit, interpolate); double fright = Sampled_getValueAtX (me, xmax, ilevel, unit, interpolate); if (NUMdefined (fleft) && fleft > maximum) maximum = fleft, xOfMaximum = xmin; if (NUMdefined (fright) && fright > maximum) maximum = fright, xOfMaximum = xmax; } else { for (i = imin; i <= imax; i ++) { double fmid = my v_getValueAtSample (i, ilevel, unit); if (fmid == NUMundefined) continue; if (interpolate == FALSE) { if (fmid > maximum) maximum = fmid, xOfMaximum = i; } else { /* * Try an interpolation, possibly even taking into account a sample just outside the selection. */ double fleft = i <= 1 ? NUMundefined : my v_getValueAtSample (i - 1, ilevel, unit); double fright = i >= my nx ? NUMundefined : my v_getValueAtSample (i + 1, ilevel, unit); if (fleft == NUMundefined || fright == NUMundefined) { if (fmid > maximum) maximum = fmid, xOfMaximum = i; } else if (fmid > fleft && fmid >= fright) { double y [4], i_real, localMaximum; y [1] = fleft, y [2] = fmid, y [3] = fright; localMaximum = NUMimproveMaximum (y, 3, 2, NUM_PEAK_INTERPOLATE_PARABOLIC, & i_real); if (localMaximum > maximum) maximum = localMaximum, xOfMaximum = i_real + i - 2; } } } xOfMaximum = my x1 + (xOfMaximum - 1) * my dx; /* From index plus phase to time. */ /* Check boundary values. */ if (interpolate) { double fleft = Sampled_getValueAtX (me, xmin, ilevel, unit, TRUE); double fright = Sampled_getValueAtX (me, xmax, ilevel, unit, TRUE); if (NUMdefined (fleft) && fleft > maximum) maximum = fleft, xOfMaximum = xmin; if (NUMdefined (fright) && fright > maximum) maximum = fright, xOfMaximum = xmax; } if (xOfMaximum < xmin) xOfMaximum = xmin; if (xOfMaximum > xmax) xOfMaximum = xmax; } if (maximum == -1e301) maximum = xOfMaximum = NUMundefined; end: if (return_maximum) *return_maximum = maximum; if (return_xOfMaximum) *return_xOfMaximum = xOfMaximum; }
static void Sampled_getSum2AndDefinitionRange (Sampled me, double xmin, double xmax, long ilevel, int unit, double mean, int interpolate, double *return_sum2, double *return_definitionRange) { /* This function computes the area under the linearly interpolated squared difference curve between xmin and xmax. Outside [x1-dx/2, xN+dx/2], the curve is undefined and neither times nor values are counted. In [x1-dx/2,x1] and [xN,xN+dx/2], the curve is linearly extrapolated. */ long imin, imax, isamp; double sum2 = 0.0, definitionRange = 0.0; Function_unidirectionalAutowindow (me, & xmin, & xmax); if (Function_intersectRangeWithDomain (me, & xmin, & xmax)) { if (interpolate) { if (Sampled_getWindowSamples (me, xmin, xmax, & imin, & imax)) { double leftEdge = my x1 - 0.5 * my dx, rightEdge = leftEdge + my nx * my dx; for (isamp = imin; isamp <= imax; isamp ++) { double value = my v_getValueAtSample (isamp, ilevel, unit); // a fast way to integrate a linearly interpolated curve; works everywhere except at the edges if (NUMdefined (value)) { value -= mean; value *= value; definitionRange += 1.0; sum2 += value; } } /* * Corrections within the first and last sampling intervals. */ if (xmin > leftEdge) { // otherwise, constant extrapolation over 0.5 sample is OK double phase = (my x1 + (imin - 1) * my dx - xmin) / my dx; // this fraction of sampling interval is still to be determined double rightValue = Sampled_getValueAtSample (me, imin, ilevel, unit); double leftValue = Sampled_getValueAtSample (me, imin - 1, ilevel, unit); if (NUMdefined (rightValue)) { rightValue -= mean; rightValue *= rightValue; definitionRange -= 0.5; // delete constant extrapolation over 0.5 sample sum2 -= 0.5 * rightValue; if (NUMdefined (leftValue)) { leftValue -= mean; leftValue *= leftValue; definitionRange += phase; // add current fraction sum2 += phase * (rightValue + 0.5 * phase * (leftValue - rightValue)); // interpolate to outside sample } else { if (phase > 0.5) phase = 0.5; definitionRange += phase; // add current fraction, but never more than 0.5 sum2 += phase * rightValue; } } else if (NUMdefined (leftValue) && phase > 0.5) { leftValue -= mean; leftValue *= leftValue; definitionRange += phase - 0.5; sum2 += (phase - 0.5) * leftValue; } } if (xmax < rightEdge) { // otherwise, constant extrapolation is OK double phase = (xmax - (my x1 + (imax - 1) * my dx)) / my dx; // this fraction of sampling interval is still to be determined double leftValue = Sampled_getValueAtSample (me, imax, ilevel, unit); double rightValue = Sampled_getValueAtSample (me, imax + 1, ilevel, unit); if (NUMdefined (leftValue)) { leftValue -= mean; leftValue *= leftValue; definitionRange -= 0.5; // delete constant extrapolation over 0.5 sample sum2 -= 0.5 * leftValue; if (NUMdefined (rightValue)) { rightValue -= mean; rightValue *= rightValue; definitionRange += phase; // add current fraction sum2 += phase * (leftValue + 0.5 * phase * (rightValue - leftValue)); // interpolate to outside sample } else { if (phase > 0.5) phase = 0.5; definitionRange += phase; // add current fraction, but never more than 0.5 sum2 += phase * leftValue; } } else if (NUMdefined (rightValue) && phase > 0.5) { rightValue -= mean; rightValue *= rightValue; definitionRange += phase - 0.5; sum2 += (phase - 0.5) * rightValue; } } } else { // no sample centres between xmin and xmax /* * Try to return the mean of the interpolated values at these two points. * Thus, a small (xmin, xmax) range gives the same value as the (xmin+xmax)/2 point. */ double leftValue = Sampled_getValueAtSample (me, imax, ilevel, unit); double rightValue = Sampled_getValueAtSample (me, imin, ilevel, unit); double phase1 = (xmin - (my x1 + (imax - 1) * my dx)) / my dx; double phase2 = (xmax - (my x1 + (imax - 1) * my dx)) / my dx; if (imin == imax + 1) { // not too far from sample definition region if (NUMdefined (leftValue)) { leftValue -= mean; leftValue *= leftValue; if (NUMdefined (rightValue)) { rightValue -= mean; rightValue *= rightValue; definitionRange += phase2 - phase1; sum2 += (phase2 - phase1) * (leftValue + 0.5 * (phase1 + phase2) * (rightValue - leftValue)); } else if (phase1 < 0.5) { if (phase2 > 0.5) phase2 = 0.5; definitionRange += phase2 - phase1; sum2 += (phase2 - phase1) * leftValue; } } else if (NUMdefined (rightValue) && phase2 > 0.5) { rightValue -= mean; rightValue *= rightValue; if (phase1 < 0.5) phase1 = 0.5; definitionRange += phase2 - phase1; sum2 += (phase2 - phase1) * rightValue; } } } } else { // no interpolation double rimin = Sampled_xToIndex (me, xmin), rimax = Sampled_xToIndex (me, xmax); if (rimax >= 0.5 && rimin < my nx + 0.5) { imin = rimin < 0.5 ? 0 : (long) floor (rimin + 0.5); imax = rimax >= my nx + 0.5 ? my nx + 1 : (long) floor (rimax + 0.5); for (isamp = imin + 1; isamp < imax; isamp ++) { double value = my v_getValueAtSample (isamp, ilevel, unit); if (NUMdefined (value)) { value -= mean; value *= value; definitionRange += 1.0; sum2 += value; } } if (imin == imax) { double value = my v_getValueAtSample (imin, ilevel, unit); if (NUMdefined (value)) { double phase = rimax - rimin; value -= mean; value *= value; definitionRange += phase; sum2 += phase * value; } } else { if (imin >= 1) { double value = my v_getValueAtSample (imin, ilevel, unit); if (NUMdefined (value)) { double phase = imin - rimin + 0.5; value -= mean; value *= value; definitionRange += phase; sum2 += phase * value; } } if (imax <= my nx) { double value = my v_getValueAtSample (imax, ilevel, unit); if (NUMdefined (value)) { double phase = rimax - imax + 0.5; value -= mean; value *= value; definitionRange += phase; sum2 += phase * value; } } } } } } if (return_sum2) *return_sum2 = sum2; if (return_definitionRange) *return_definitionRange = definitionRange; }