/* Sound.c */
/* Paul Boersma, July 3, 2001 */

#include "Sound.h"

#include "enum_c.h"
#include "Sound_enums.h"

Sound Sound_clipboard;

double Sound_getEnergyInAir (Sound me) {
	long i, n = my nx;
	double sum2 = 0.0; for (i = 1; i <= n; i ++) sum2 += my z [1] [i] * my z [1] [i];
	return sum2 * my dx / 400;
}

double Sound_getIntensity_dB (Sound me) {
	long i, n = my nx;
	double sum2 = 0.0; for (i = 1; i <= n; i ++) sum2 += my z [1] [i] * my z [1] [i];
	return 10 * log10 (sum2 / n / 4.0e-10);
}

double Sound_getPowerInAir (Sound me) {
	long i, n = my nx;
	double sum2 = 0.0; for (i = 1; i <= n; i ++) sum2 += my z [1] [i] * my z [1] [i];
	return sum2 / n / 400;
}

static void info (I) {
	iam (Sound);
	const double rho_c = 400;   /* rho = 1.14 kg m-3; c = 353 m s-1; [rho c] = kg m-2 s-1 */
	long i, numberOfSamples = my nx;
	float *amplitude = my z [1];
	double minimum = amplitude [1], maximum = minimum;
	double sum = 0.0, sumOfSquares = 0.0, mean, rms, penergy, energy, power;
	for (i = 1; i <= numberOfSamples; i ++) {
		double value = amplitude [i];
		sum += value;
		sumOfSquares += value * value;
		if (value < minimum) minimum = value;
		if (value > maximum) maximum = value;
	}
	mean = sum / my nx;   /* Pa = N m-2 = kg m-1 s-2 */
	rms = sqrt (sumOfSquares / my nx);   /* Pa */
	penergy = sumOfSquares * my dx;   /* Pa2 s = kg2 m-2 s-3 */
	energy = penergy / rho_c;   /* kg s-2 = Joule m-2 */
	power = energy / (my dx * my nx);   /* kg s-3 = Watt/m2 */
	Melder_info (
		"Starting time %.8g seconds.\n"
		"End time %.8g seconds (total duration %.8g seconds).\n"
		"Number of samples: %ld\n"
		"Sampling period %.8g seconds (sampling frequency: %.8g Hertz)\n"
		"First sample at %.8g seconds\n"
		"Amplitude:\n   minimum %.8g Pa\n   maximum %.8g Pa\n"
		"   mean: %.8g Pa\n"
		"   root-mean-square: %.8g Pa\n"
		"Total energy: %.8g Pa2 sec (= in air: %.8g Joule/m2)",
		my xmin, my xmax, my xmax - my xmin, my nx, my dx, 1 / my dx, my x1,
		minimum, maximum, mean, rms, penergy, energy);
	if (power) Melder_info ("Mean power (intensity) in air: %.8g Watt/m2 = %.4g dB", power, 10 * log10 (power / 1e-12));
	else Melder_info ("Mean power (intensity) in air: %.8g Watt/m2", power);
	if (my nx > 1) {
		double sd = 0.0;
		for (i = 1; i <= numberOfSamples; i ++) {
			double value = amplitude [i] - mean;
			sd += value * value;
		}
		sd = sqrt (sd / (my nx - 1));
		Melder_info ("Standard deviation: %.8g Pa\n", sd);
	}
}

class_methods (Sound, Vector)
	class_method (info)
class_methods_end

Sound Sound_create (double xmin, double xmax, long nx, double dx, double x1) {
	Sound me = new (Sound);
	if (! me || ! Matrix_init (me, xmin, xmax, nx, dx, x1, 1, 1, 1, 1, 1))
		forget (me);
	return me;
}

Sound Sound_createSimple (double duration, double samplingFrequency)
{
	return Sound_create (0.0, duration, floor (duration * samplingFrequency + 0.5),
		1 / samplingFrequency, 0.5 / samplingFrequency);
}

Matrix Sound_to_Matrix (Sound me) {
	Matrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1,
					my ymin, my ymax, my ny, my dy, my y1);
	NUMfvector_copyElements (my z [1], thy z [1], 1, my nx);
	return thee;
}

Sound Matrix_to_Sound (Matrix me, long row) {
	Sound thee = Sound_create (my xmin, my xmax, my nx, my dx, my x1);
	if (! thee) return NULL;
	if (row < 0) row = my ny + 1 - row;
	if (row < 1) row = 1;
	if (row > my ny) row = my ny;
	NUMfvector_copyElements (my z [row], thy z [1], 1, my nx);
	return thee;
}

Sound Sound_upsample (Sound me) {
	long nfft = 1, i, imin;
	float *data, factor;
	Sound thee;
	while (nfft < my nx + 2000) nfft *= 2;
	thee = Sound_create
		(my xmin, my xmax, my nx * 2, my dx / 2, my x1);
	if (! thee) return NULL;
	data = NUMfvector (1, 2 * nfft);
	if (! data) return NULL;
	NUMfvector_copyElements (my z [1], data + 1000, 1, my nx);
	NUMrealft (data, nfft, 1);
	imin = nfft * 0.95;
	for (i = imin + 1; i <= nfft; i ++)
		data [i] *= ((double) (nfft - i)) / (nfft - imin);
	data [2] = 0.0;
	NUMrealft (data, 2 * nfft, -1);
	factor = 2.0 / nfft;
	for (i = 1; i <= thy nx; i ++)
		thy z [1] [i] = data [i + 2000] * factor;
	NUMfvector_free (data, 1);
	return thee;
}

Sound Sound_resample (Sound me, double samplingFrequency, long precision) {
	float *from, *to;
	double upfactor = samplingFrequency * my dx;
	long numberOfSamples = floor ((my xmax - my xmin) * samplingFrequency + 0.5), i;
	Sound thee;
	if (fabs (upfactor - 2) < 1e-6) return Sound_upsample (me);
	if (fabs (upfactor - 1) < 1e-6) return Data_copy (me);
	if (numberOfSamples < 1)
		return Melder_errorp ("Cannot resample to 0 samples.");
	thee = Sound_create (my xmin, my xmax, numberOfSamples, 1.0 / samplingFrequency,
		0.5 * (my xmin + my xmax - (numberOfSamples - 1) / samplingFrequency));
	if (thee == NULL)
		return Melder_errorp ("Sound not resampled.");
	if (upfactor < 1.0) {   /* Need anti-aliasing filter? */
		long nfft = 1, i, antiTurnAround = 1000;
		float *data, factor;
		Sound filtered;
		while (nfft < my nx + antiTurnAround * 2) nfft *= 2;
		if (! (data = NUMfvector (1, nfft)) ||
			! (filtered = Sound_create (my xmin, my xmax, my nx, my dx, my x1)))
		{
			NUMfvector_free (data, 1);
			forget (thee);
			return Melder_errorp ("Sound not resampled.");
		}
		NUMfvector_copyElements (my z [1], data + antiTurnAround, 1, my nx);
		NUMrealft (data, nfft, 1);   /* Go to the frequency domain. */
		for (i = floor (upfactor * nfft); i <= nfft; i ++)
			data [i] = 0;   /* Filter away high frequencies. */
		data [2] = 0.0;
		NUMrealft (data, nfft, -1);   /* Return to the time domain. */
		factor = 2.0 / nfft;
		to = filtered -> z [1];
		for (i = 1; i <= my nx; i ++)
			to [i] = data [i + antiTurnAround] * factor;
		NUMfvector_free (data, 1);
		me = filtered;   /* Reference copy. Remove at end. */
	}
	from = my z [1];
	to = thy z [1];
	if (precision <= 1) {
		for (i = 1; i <= numberOfSamples; i ++) {
			double x = thy x1 + (i - 1) * thy dx;   /* Sampled_indexToX (thee, i); */
			double index = (x - my x1) / my dx + 1;   /* Sampled_xToIndex (me, x); */
			long leftSample = floor (index);
			double fraction = index - leftSample;
			to [i] = leftSample < 1 || leftSample >= my nx ? 0.0 :
				(1 - fraction) * from [leftSample] + fraction * from [leftSample + 1];
		}
	} else {
		for (i = 1; i <= numberOfSamples; i ++) {
			double x = thy x1 + (i - 1) * thy dx;   /* Sampled_indexToX (thee, i); */
			double index = (x - my x1) / my dx + 1;   /* Sampled_xToIndex (me, x); */
			to [i] = NUM_interpolate_sinc (my z [1], my nx, index, precision);
		}
	}
	if (upfactor < 1.0)
		forget (me);   /* Forget filtered signal. */
	return thee;
}

Sound Sounds_append (Sound me, Sound thee) {
	Sound him;
	long nx = my nx + thy nx;
	if (my dx != thy dx)
		return Melder_errorp ("Sounds_append: sampling frequencies do not match.");
	him = Sound_create (0, nx * my dx, nx, my dx, 0.5 * my dx);
	if (! him) return NULL;
	NUMfvector_copyElements (my z [1], his z [1], 1, my nx);
	NUMfvector_copyElements (thy z [1], his z [1] + my nx, 1, thy nx);
	return him;
}

Sound Sounds_convolve (Sound me, Sound thee) {
	Sound him = NULL;
	long n1 = my nx, n2 = thy nx;
	long n3 = n1 + n2 - 1, nfft = 1, i;
	float *data1 = NULL, *data2 = NULL, *a, scale;
	if (my dx != thy dx)
		return Melder_errorp ("Sounds_convolve: sampling frequencies do not match.");
	while (nfft < n3) nfft *= 2;
	data1 = NUMfvector (1, nfft); cherror
	data2 = NUMfvector (1, nfft); cherror
	him = Sound_create (my xmin + thy xmin, my xmax + thy xmax, n3, my dx,
		my x1 + thy x1); cherror
	a = my z [1];
	for (i = n1; i > 0; i --) data1 [i] = a [i];
	a = thy z [1];
	for (i = n2; i > 0; i --) data2 [i] = a [i];
	NUMrealft (data1, nfft, 1);
	NUMrealft (data2, nfft, 1);
	data2 [1] *= data1 [1];
	data2 [2] *= data1 [2];
	for (i = 3; i <= nfft; i += 2) {
		float temp = data1 [i] * data2 [i] - data1 [i + 1] * data2 [i + 1];
		data2 [i + 1] = data1 [i] * data2 [i + 1] + data1 [i + 1] * data2 [i];
		data2 [i] = temp;
	}
	NUMrealft (data2, nfft, -1);
	scale = 2.0 / nfft;
	a = him -> z [1];
	for (i = 1; i <= n3; i ++)
		a [i] = data2 [i] * scale;
end:
	NUMfvector_free (data1, 1);
	NUMfvector_free (data2, 1);
	iferror forget (him);
	return him;
}

void Sound_draw (Sound me, Graphics g,
	double tmin, double tmax, double minimum, double maximum, int garnish)
{
	long itmin, itmax;
	if (tmax <= tmin) { tmin = my xmin; tmax = my xmax; }
	Matrix_getWindowSamplesX (me, tmin, tmax, & itmin, & itmax);
	if (maximum <= minimum)
		Matrix_getWindowExtrema (me, itmin, itmax, 1, 1, & minimum, & maximum);
	if (maximum <= minimum) { minimum -= 0.5; maximum += 0.5; }
	Graphics_setInner (g);
	Graphics_setWindow (g, tmin, tmax, minimum, maximum);
	Graphics_function (g, my z [1], itmin, itmax,
		Matrix_columnToX (me, itmin), Matrix_columnToX (me, itmax));
	Graphics_unsetInner (g);
	if (garnish) {
		Graphics_drawInnerBox (g);
		Graphics_textBottom (g, 1, "Time (s)");
		Graphics_marksBottom (g, 2, 1, 1, 0);
		Graphics_marksLeft (g, 2, 1, 1, 0);
		if (minimum < 0.0 && maximum > 0.0) Graphics_markLeft (g, 0.0, 1, 1, 1, NULL);
	}
}

void Sound_drawClipped (Sound me, Graphics g,
	double tmin, double tmax, double minimum, double maximum, int garnish)
{
	Sound clipped = Data_copy (me);
	long i, itmin, itmax;
	float *amp;
	if (tmax <= tmin) { tmin = my xmin; tmax = my xmax; }
	Matrix_getWindowSamplesX (me, tmin, tmax, & itmin, & itmax);
	if (maximum <= minimum)
		Matrix_getWindowExtrema (me, itmin, itmax, 1, 1, & minimum, & maximum);
	if (maximum <= minimum) { minimum -= 0.5; maximum += 0.5; }
	if (! clipped) { Melder_clearError (); return; }
	amp = clipped -> z [1];
	for (i = 1; i <= my nx; i ++) {
		if (amp [i] < minimum) amp [i] = minimum;
		if (amp [i] > maximum) amp [i] = maximum;
	}
	Sound_draw (clipped, g, tmin, tmax, minimum, maximum, garnish);
	forget (clipped);
}

void Sound_drawSamples (Sound me, Graphics g, double tmin, double tmax, double minimum, double maximum, int garnish)
{
	long itmin, itmax, i;
	if (tmax <= tmin) { tmin = my xmin; tmax = my xmax; }
	Matrix_getWindowSamplesX (me, tmin, tmax, & itmin, & itmax);
	if (maximum <= minimum)
		Matrix_getWindowExtrema (me, itmin, itmax, 1, 1, & minimum, & maximum);
	if (maximum <= minimum) { minimum -= 0.5; maximum += 0.5; }

	Graphics_setInner (g);
	Graphics_setWindow (g, tmin, tmax, minimum, maximum);
	for (i = itmin; i <= itmax; i ++) {
		double t = Sampled_indexToX (me, i);
		Graphics_line (g, t, 0, t, my z [1] [i]);
		Graphics_fillCircle (g, t, my z [1] [i], my dx / 20);
	}
	Graphics_unsetInner (g);
	if (garnish) {
		Graphics_drawInnerBox (g);
		Graphics_textBottom (g, 1, "Time (s)");
		Graphics_marksBottom (g, 2, 1, 1, 0);
		Graphics_marksLeft (g, 2, 1, 1, 0);
		if (minimum < 0 && maximum > 0) Graphics_markLeft (g, 0.0, 1, 1, 1, NULL);
	}
}

static double interpolate (Sound me, long i1)
/* Precondition: my z [1] [i1] != my z [1] [i1 + 1]; */
{
	long i2 = i1 + 1;
	double x1 = Sampled_indexToX (me, i1), x2 = Sampled_indexToX (me, i2);
	double y1 = my z [1] [i1], y2 = my z [1] [i2];
	return x1 + (x2 - x1) * y1 / (y1 - y2);   /* Linear. */
}
double Sound_getNearestZeroCrossing (Sound me, double position) {
	float *amplitude = my z [1];
	long leftSample = Sampled_xToLowIndex (me, position);
	long rightSample = leftSample + 1, ileft, iright;
	double leftZero, rightZero;
	/* Are we already at a zero crossing? */
	if (leftSample >= 1 && rightSample <= my nx &&
		(amplitude [leftSample] >= 0.0) !=
		(amplitude [rightSample] >= 0.0))
	{
		return interpolate (me, leftSample);
	}
	/* Search to the left. */
	if (leftSample > my nx) return NUMundefined;
	for (ileft = leftSample - 1; ileft >= 1; ileft --)
		if ((amplitude [ileft] >= 0.0) != (amplitude [ileft + 1] >= 0.0))
		{
			leftZero = interpolate (me, ileft);
			break;
		}
	/* Search to the right. */
	if (rightSample < 1) return NUMundefined;
	for (iright = rightSample + 1; iright <= my nx; iright ++)
		if ((amplitude [iright] >= 0.0) != (amplitude [iright - 1] >= 0.0))
		{
			rightZero = interpolate (me, iright - 1);
			break;
		}
	if (ileft < 1 && iright > my nx) return NUMundefined;
	return ileft < 1 ? rightZero : iright > my nx ? leftZero :
		position - leftZero < rightZero - position ? leftZero : rightZero;
}

Sound Sound_createFromToneComplex (double startingTime, double endTime, double sampleRate,
	int phase, double frequencyStep, double firstFrequency, double ceiling, long numberOfComponents)
{
	Sound me = NULL;
	long maximumNumberOfComponents, icomp, isamp;
	float *amplitude;
	double omegaStep = 2 * NUMpi * frequencyStep, firstOmega, factor, nyquistFrequency = 0.5 * sampleRate;
	if (frequencyStep == 0.0)
		return Melder_errorp ("(Sound_createFromToneComplex:) Frequency step must not be zero.");
	/*
	 * Translate default firstFrequency.
	 */
	if (firstFrequency <= 0.0) firstFrequency = frequencyStep;
	firstOmega = 2 * NUMpi * firstFrequency;
	/*
	 * Translate default ceiling.
	 */
	if (ceiling <= 0.0 || ceiling > nyquistFrequency) ceiling = nyquistFrequency;
	/*
	 * Translate number of components.
	 */
	maximumNumberOfComponents = floor ((ceiling - firstFrequency) / frequencyStep) + 1;
	if (numberOfComponents <= 0 || numberOfComponents > maximumNumberOfComponents)
		numberOfComponents = maximumNumberOfComponents;
	if (numberOfComponents < 1)
		return Melder_errorp ("(Sound_createFromToneComplex:) Zero sine waves.");
	/*
	 * Generate the Sound.
	 */
	factor = 0.99 / numberOfComponents;
	me = Sound_create (startingTime, endTime, floor ((endTime - startingTime) * sampleRate + 0.5),
		1 / sampleRate, startingTime + 0.5 / sampleRate);
	if (! me) return NULL;
	amplitude = my z [1];
	for (isamp = 1; isamp <= my nx; isamp ++) {
		double value = 0.0, t = Sampled_indexToX (me, isamp);
		double omegaStepT = omegaStep * t, firstOmegaT = firstOmega * t;
		if (phase == Sound_TONE_COMPLEX_SINE)
			for (icomp = 1; icomp <= numberOfComponents; icomp ++)
				value += sin (firstOmegaT + (icomp - 1) * omegaStepT);
		else
			for (icomp = 1; icomp <= numberOfComponents; icomp ++)
				value += cos (firstOmegaT + (icomp - 1) * omegaStepT);
		amplitude [isamp] = value * factor;
	}
	return me;
}

void Sound_multiplyByWindow (Sound me, int windowType) {
	long i, n = my nx;
	float *amp = my z [1];
	double imid, edge, onebyedge1, factor;
	switch (windowType) {
		case enumi (Sound_WINDOW, Rectangular):
			;
		break;
		case enumi (Sound_WINDOW, Triangular):   /* "Bartlett" */
			for (i = 1; i <= n; i ++) { double phase = (double) i / n;   /* 0..1 */
				amp [i] *= 1.0 - fabs ((2.0 * phase - 1.0)); }
		break;
		case enumi (Sound_WINDOW, Parabolic):   /* "Welch" */
			for (i = 1; i <= n; i ++) { double phase = (double) i / n;
				amp [i] *= 1.0 - (2.0 * phase - 1.0) * (2.0 * phase - 1.0); }
		break;
		case enumi (Sound_WINDOW, Hanning):
			for (i = 1; i <= n; i ++) { double phase = (double) i / n;
				amp [i] *= 0.5 * (1.0 - cos (2.0 * NUMpi * phase)); }
		break;
		case enumi (Sound_WINDOW, Hamming):
			for (i = 1; i <= n; i ++) { double phase = (double) i / n;
				amp [i] *= 0.54 - 0.46 * cos (2.0 * NUMpi * phase); }
		break;
		case enumi (Sound_WINDOW, Gaussian1):
			imid = 0.5 * (n + 1), edge = exp (-3.0), onebyedge1 = 1 / (1.0 - edge);   /* -0.5..+0.5 */
			for (i = 1; i <= n; i ++) { double phase = ((double) i - imid) / n;
				amp [i] *= (exp (-12.0 * phase * phase) - edge) * onebyedge1; }
		break;
		case enumi (Sound_WINDOW, Gaussian2):
			imid = 0.5 * (double) (n + 1), edge = exp (-12.0), onebyedge1 = 1 / (1.0 - edge);
			for (i = 1; i <= n; i ++) { double phase = ((double) i - imid) / n;
				amp [i] *= (exp (-48.0 * phase * phase) - edge) * onebyedge1; }
		break;
		case enumi (Sound_WINDOW, Gaussian3):
			imid = 0.5 * (double) (n + 1), edge = exp (-27.0), onebyedge1 = 1 / (1.0 - edge);
			for (i = 1; i <= n; i ++) { double phase = ((double) i - imid) / n;
				amp [i] *= (exp (-108.0 * phase * phase) - edge) * onebyedge1; }
		break;
		case enumi (Sound_WINDOW, Gaussian4):
			imid = 0.5 * (double) (n + 1), edge = exp (-48.0), onebyedge1 = 1 / (1.0 - edge);
			for (i = 1; i <= n; i ++) { double phase = ((double) i - imid) / n;
				amp [i] *= (exp (-192.0 * phase * phase) - edge) * onebyedge1; }
		break;
		case enumi (Sound_WINDOW, Gaussian5):
			imid = 0.5 * (double) (n + 1), edge = exp (-75.0), onebyedge1 = 1 / (1.0 - edge);
			for (i = 1; i <= n; i ++) { double phase = ((double) i - imid) / n;
				amp [i] *= (exp (-300.0 * phase * phase) - edge) * onebyedge1; }
		break;
		case enumi (Sound_WINDOW, Kaiser1):
			imid = 0.5 * (double) (n + 1);
			factor = 1 / NUMbesselI (0, (2 * NUMpi));
			for (i = 1; i <= n; i ++) { double phase = 2 * ((double) i - imid) / n;   /* -1..+1 */
				double root = 1 - phase * phase;
				amp [i] *= root <= 0.0 ? 0.0 : factor * NUMbesselI (0, (2 * NUMpi) * sqrt (root)); }
		break;
		case enumi (Sound_WINDOW, Kaiser2):
			imid = 0.5 * (double) (n + 1);
			factor = 1 / NUMbesselI (0, (2 * NUMpi * NUMpi + 0.5));
			for (i = 1; i <= n; i ++) { double phase = 2 * ((double) i - imid) / n;   /* -1..+1 */
				double root = 1 - phase * phase;
				amp [i] *= root <= 0.0 ? 0.0 : factor * NUMbesselI (0, (2 * NUMpi * NUMpi + 0.5) * sqrt (root)); }
		break;
		default:
		break;
	}
}

void Sound_overrideSampleRate (Sound me, double rate) {
	my dx = 1 / rate;
	my x1 = my xmin + 0.5 * my dx;
	my xmax = my xmin + my nx * my dx;
}

Sound Sound_extractPart (Sound me, double t1, double t2, int windowType, double relativeWidth, int preserveTimes) {
	Sound thee = NULL;
	long ix1, ix2;
	/*
	 * We do not clip to the Sound's time domain.
	 * Any samples outside it are taken to be zero.
	 */

	/*
	 * Autowindow.
	 */
	if (t1 == t2) { t1 = my xmin; t2 = my xmax; };
	/*
	 * Allow window tails outside specified domain.
	 */
	if (relativeWidth != 1.0) {
		double margin = 0.5 * (relativeWidth - 1) * (t2 - t1);
		t1 -= margin;
		t2 += margin;
	}
	/*
	 * Determine index range. We use all the real or virtual samples that fit within [t1..t2].
	 */
	ix1 = 1 + (long) ceil ((t1 - my x1) / my dx);
	ix2 = 1 + (long) floor ((t2 - my x1) / my dx);
	if (ix2 < ix1) { Melder_error ("Extracted Sound would contain no samples."); goto end; }
	/*
	 * Create sound, optionally shifted to [0..t2-t1].
	 */
	thee = Sound_create (t1, t2, ix2 - ix1 + 1, my dx, my x1 + (ix1 - 1) * my dx); cherror
	if (! preserveTimes) { thy xmin = 0.0; thy xmax -= t1; thy x1 -= t1; }
	/*
	 * Copy only *real* samples into the new sound.
	 * The *virtual* samples will remain at zero.
	 */
	NUMfvector_copyElements (my z [1], thy z [1] + 1 - ix1,
			( ix1 < 1 ? 1 : ix1 ), ( ix2 > my nx ? my nx : ix2 ));
	/*
	 * Multiply by a window that extends throughout the target domain.
	 */
	Sound_multiplyByWindow (thee, windowType);
end:
	iferror { forget (thee); return Melder_errorp ("(Sound_extractPart:) Not performed."); }
	return thee;
}

int Sound_filterWithFormants (Sound me, double tmin, double tmax,
	int numberOfFormants, float formant [], float bandwidth [])
{
	float *amplitude;
	int iformant;
	long itmin, itmax, n;

	if (tmax <= tmin) { tmin = my xmin; tmax = my xmax; }   /* Autowindowing. */
	if ((n = Sampled_getWindowSamples (me, tmin, tmax, & itmin, & itmax)) <= 2)
		return Melder_error ("Sound too short.");
	amplitude = my z [1] + itmin - 1;   /* Base 1. */
	NUMdeemphasize_f (amplitude, n, my dx, 50.0);
	for (iformant = 1; iformant <= numberOfFormants; iformant ++)
		NUMfilterSecondOrderSection_fb (amplitude, n, my dx, formant [iformant], bandwidth [iformant]);
	NUMautoscale (amplitude, n, 0.9);
	return 1;
}

int Sound_filter_onePole (Sound me, double frequency, double bandwidth) {
	NUMfilterSecondOrderSection_fb (my z [1], my nx, my dx, frequency, bandwidth);
	NUMautoscale (my z [1], my nx, 0.9);
	return 1;
}

void Sound_reverse (Sound me, double tmin, double tmax) {
	long itmin, itmax, n, i;
	float *amp = my z [1];
	if (tmax <= tmin) { tmin = my xmin; tmax = my xmax; }   /* Autowindowing. */
	n = Sampled_getWindowSamples (me, tmin, tmax, & itmin, & itmax) / 2;
	for (i = 0; i < n; i ++) {
		float dummy = amp [itmin + i];
		amp [itmin + i] = amp [itmax - i];
		amp [itmax - i] = dummy;
	}
}

Sound Sounds_crossCorrelate (Sound me, Sound thee, double tmin, double tmax, int normalize) {
	Sound him = NULL;
	double dt, dphase, t1;
	long i1, i2, nt, i;
	if (my dx != thy dx)
		return Melder_errorp ("(Sounds_crossCorrelation:) Sample rates do not match.");
	dt = my dx;
	dphase = (thy x1 - my x1) / dt;
	dphase -= floor (dphase);   /* A number between 0 and 1. */
	i1 = ceil (tmin / dt - dphase);   /* Index of first sample if sample at dphase has index 0. */
	i2 = floor (tmax / dt - dphase);   /* Index of last sample if sample at dphase has index 0. */
	nt = i2 - i1 + 1;
	if (nt < 1)
		return Melder_errorp ("(Sounds_crossCorrelation:) Window too small.");
	t1 = (dphase + i1) * dt;
	him = Sound_create (tmin, tmax, nt, dt, t1);
	if (him == NULL) return NULL;
	for (i = 1; i <= nt; i ++) {
		long di = i - 1 + i1, ime;
		for (ime = 1; ime <= my nx; ime ++) {
			if (ime + di < 1) continue;
			if (ime + di > thy nx) break;
			his z [1] [i] += my z [1] [ime] * thy z [1] [ime + di];
		}
	}
	if (normalize) {
		double mypower = 0.0, thypower = 0.0;
		for (i = 1; i <= my nx; i ++) {
			double value = my z [1] [i];
			mypower += value * value;
		}
		for (i = 1; i <= thy nx; i ++) {
			double value = thy z [1] [i];
			thypower += value * value;
		}
		if (mypower != 0.0 && thypower != 0.0) {
			double factor = 1.0 / (sqrt (mypower) * sqrt (thypower));
			for (i = 1; i <= nt; i ++)
				his z [1] [i] *= factor;
		}
	} else {
		for (i = 1; i <= nt; i ++)
			his z [1] [i] *= dt;
	}
	return him;
}

/* End of file Sound.c */
