/* Sound_and_Spectrogram.c
 *
 * Copyright (C) 1992-2003 Paul Boersma
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * pb 2002/05/28
 * pb 2002/07/16 GPL
 * pb 2003/07/02 checks on NUMrealft
 * pb 2003/11/30 Sound_to_Spectrogram_windowShapeText
 */

#include "Sound_and_Spectrogram.h"
#include "NUM2.h"

const char * Sound_to_Spectrogram_windowShapeText (int windowShape) {
	return
		windowShape == Sound_to_Spectrogram_WINDOW_SHAPE_SQUARE ? "Square (rectangular)" :
		windowShape == Sound_to_Spectrogram_WINDOW_SHAPE_HAMMING ? "Hamming (raised sine-squared)" :
		windowShape == Sound_to_Spectrogram_WINDOW_SHAPE_BARTLETT ? "Bartlett (triangular)" :
		windowShape == Sound_to_Spectrogram_WINDOW_SHAPE_WELCH ? "Welch (parabolic)" :
		windowShape == Sound_to_Spectrogram_WINDOW_SHAPE_HANNING ? "Hanning (sine-squared)" :
		windowShape == Sound_to_Spectrogram_WINDOW_SHAPE_GAUSSIAN ? "Gaussian" :
		"(unknown)";
}

Spectrogram Sound_to_Spectrogram (Sound me, double effectiveAnalysisWidth, double fmax,
	double minimumTimeStep1, double minimumFreqStep1, int windowType,
	double maximumTimeOversampling, double maximumFreqOversampling)
{
	Spectrogram thee = NULL;
	double nyquist = 0.5 / my dx;
	double physicalAnalysisWidth = windowType == 5 ? 2 * effectiveAnalysisWidth : effectiveAnalysisWidth;
	double effectiveTimeWidth = effectiveAnalysisWidth / sqrt (NUMpi);
	double effectiveFreqWidth = 1 / effectiveTimeWidth;
	double minimumTimeStep2 = effectiveTimeWidth / maximumTimeOversampling;
	double minimumFreqStep2 = effectiveFreqWidth / maximumFreqOversampling;
	double timeStep = minimumTimeStep1 > minimumTimeStep2 ? minimumTimeStep1 : minimumTimeStep2;
	double freqStep = minimumFreqStep1 > minimumFreqStep2 ? minimumFreqStep1 : minimumFreqStep2;

	long nsamp_window, halfnsamp_window, numberOfTimes, numberOfFreqs, nsampFFT = 1, half_nsampFFT;
	long iframe, iband, i, j;
	float *amplitude = my z [1], *frame = NULL, *window = NULL, *oneByBinWidth = NULL;
	long *lofreqi = NULL;
	double duration = my dx * (double) my nx, t1, windowssq = 0.0;

	if (fmax <= 0.0 || fmax > nyquist) fmax = nyquist;
	numberOfFreqs = (long) floor (fmax / freqStep);
	if (numberOfFreqs < 1) return NULL;

	nsamp_window = (long) floor (physicalAnalysisWidth / my dx);
	halfnsamp_window = nsamp_window / 2 - 1;
	nsamp_window = halfnsamp_window * 2;
	if (physicalAnalysisWidth > duration)
		return Melder_errorp ("(Sound_to_Spectrogram:) Your sound is too short:\n"
			"it should be at least as long as %s.",
			windowType == 5 ? "two window lengths" : "one window length");
	numberOfTimes = 1 + (long) floor ((duration - physicalAnalysisWidth) / timeStep);   /* >= 1 */
	t1 = my x1 + 0.5 * ((double) (my nx - 1) * my dx - (double) (numberOfTimes - 1) * timeStep);
		/* Centre of first frame. */

	thee = Spectrogram_create (my xmin, my xmax, numberOfTimes, timeStep, t1,
			0.0, fmax, numberOfFreqs, freqStep, 0.5 * freqStep); cherror

	nsampFFT = 1;
	while (nsampFFT < nsamp_window || nsampFFT < 2 * numberOfFreqs * (nyquist / fmax))
		nsampFFT *= 2;
	half_nsampFFT = nsampFFT / 2;

	frame = NUMfvector (1, nsampFFT); cherror
	window = NUMfvector (1, nsamp_window); cherror
	lofreqi = NUMlvector (1, numberOfFreqs + 1); cherror
	oneByBinWidth = NUMfvector (1, numberOfFreqs); cherror

	for (i = 1; i <= nsamp_window; i ++) {
		double nSamplesPerWindow_f = physicalAnalysisWidth / my dx;
		double phase = (double) i / nSamplesPerWindow_f;   /* 0 .. 1 */
		double value;
		switch (windowType) {
			case 0: /* None (rectangular). */
				value = 1.0;
			break; case 1: /* Hamming (raised sine-squared). */
				value = 0.54 - 0.46 * cos (2.0 * NUMpi * phase);
			break; case 2: /* Bartlett (triangular). */
				value = 1.0 - fabs ((2.0 * phase - 1.0));
			break; case 3: /* Welch (parabolic). */
				value = 1.0 - (2.0 * phase - 1.0) * (2.0 * phase - 1.0);
			break; case 4: /* Hanning (sine-squared). */
				value = 0.5 * (1.0 - cos (2.0 * NUMpi * phase));
			break; case 5: /* Gaussian. */
			{
				double imid = 0.5 * (double) (nsamp_window + 1), edge = exp (-12.0);
				phase = ((double) i - imid) / nSamplesPerWindow_f;   /* -0.5 .. +0.5 */
				value = (exp (-48.0 * phase * phase) - edge) / (1.0 - edge);
				break;
			}
			break; default:
				value = 1.0;
		}
		window [i] = (float) value;
		windowssq += value * value;
	}
	/* Binning frame [1..nBands]. */
	for (iband = 1; iband <= numberOfFreqs + 1; iband ++) {
		double lofreq = (double) (iband - 1) * freqStep;
		lofreqi [iband] = (long) floor (lofreq * my dx * (double) nsampFFT) + 1;
		if (lofreqi [iband] > half_nsampFFT) lofreqi [iband] = half_nsampFFT;
	}
	for (iband = 1; iband <= numberOfFreqs; iband ++)
		oneByBinWidth [iband] = 1.0f / (float) windowssq /
			(float) (lofreqi [iband + 1] - lofreqi [iband] + 1);

	for (iframe = 1; iframe <= numberOfTimes; iframe ++) {
		double t = Sampled_indexToX (thee, iframe);
		float tmp;
		long leftSample = Sampled_xToLowIndex (me, t), rightSample = leftSample + 1;
		long startSample = rightSample - halfnsamp_window;
		long endSample = leftSample + halfnsamp_window;
		Melder_assert (startSample >= 1);
		Melder_assert (endSample <= my nx);
		for (j = 1, i = startSample; j <= nsamp_window; j ++)
			frame [j] = amplitude [i ++] * window [j];
		for (j = nsamp_window + 1; j <= nsampFFT; j ++) frame [j] = 0.0f;

		/* Compute Fast Fourier Transform of the frame. */

		NUMrealft (frame, nsampFFT, 1); cherror   /* Complex spectrum. */

		/* Put power spectrum in frame [1..half_nsampFFT + 1]. */

		frame [1] *= frame [1];   /* DC component. */
		tmp = frame [2];   /* Nyquist frequency. */
		for (i = 2; i <= half_nsampFFT; i ++)
			frame [i] = frame [i + i - 1] * frame [i + i - 1] + frame [i + i] * frame [i + i];
		frame [half_nsampFFT + 1] = tmp * tmp;   /* ? */

		/* Bin into frame [1..nBands]. */
		for (iband = 1; iband <= numberOfFreqs; iband ++) {
			float power = 0.0f;
			for (i = lofreqi [iband]; i <= lofreqi [iband + 1]; i ++) power += frame [i];
			thy z [iband] [iframe] = power * oneByBinWidth [iband];
		}
	}
end:
	NUMfvector_free (frame, 1);
	NUMfvector_free (window, 1);
	NUMlvector_free (lofreqi, 1);
	NUMfvector_free (oneByBinWidth, 1);
	iferror forget (thee);
	return thee;
}

Sound Spectrogram_to_Sound (Spectrogram me, double fsamp) {
	double dt = 1 / fsamp;
	long n = (my xmax - my xmin) / dt, i, j;
	Sound thee = NULL;
	if (n < 0) return NULL;
	thee = Sound_create (my xmin, my xmax, n, dt, 0.5 * dt); cherror
	for (i = 1; i <= n; i ++) {
		double t = Sampled_indexToX (thee, i);
		double rframe = Sampled_xToIndex (me, t), phase, value = 0.0;
		long leftFrame, rightFrame;
		if (rframe < 1 || rframe >= my nx) continue;
		leftFrame = floor (rframe), rightFrame = leftFrame + 1, phase = rframe - leftFrame;
		for (j = 1; j <= my ny; j ++) {
			double f = Matrix_rowToY (me, j);
			double power = my z [j] [leftFrame] * (1 - phase) + my z [j] [rightFrame] * phase;
			value += sqrt (power) * sin (2 * NUMpi * f * t);
		}
		thy z [1] [i] = value;
	}
end:
	iferror forget (thee);
	return thee;
}

/* End of file Sound_and_Spectrogram.c */
