/* Spectrogram.c */
/* Paul Boersma, September 25, 2000 */

#include <time.h>
#include "Spectrogram.h"

static void info (I) {
	iam (Spectrogram);
	Melder_information (
		"Number of time slices: %ld\nNumber of frequencies: %ld\n"
		"Time step: %.8g\nHighest frequency: %.8g",
		my nx, my ny, my dx, my ymax); 
}

class_methods (Spectrogram, Matrix)
	class_method (info)
class_methods_end

Any Spectrogram_create (double tmin, double tmax, long nt, double dt, double t1,
					double fmin, double fmax, long nf, double df, double f1)
{
	Spectrogram me = new (Spectrogram);
	if (! me || ! Matrix_init (me, tmin, tmax, nt, dt, t1, fmin, fmax, nf, df, f1))
		forget (me);
	return me;    
}

void Spectrogram_paintInside (I, Graphics g, double tmin, double tmax, double fmin, double fmax,
	double dynamic, double preemphasis, double dynamicCompression)
{
	iam (Spectrogram);
	long itmin, itmax, ifmin, ifmax, ifreq, itime;
	double maximum;
	float *preemphasisFactor, *dynamicFactor;
	if (tmax <= tmin) { tmin = my xmin; tmax = my xmax; }
	if (fmax <= fmin) { fmin = my ymin; fmax = my ymax; }
	if (! Matrix_getWindowSamplesX (me, tmin - 0.49999 * my dx, tmax + 0.49999 * my dx, & itmin, & itmax) ||
		 ! Matrix_getWindowSamplesY (me, fmin - 0.49999 * my dy, fmax + 0.49999 * my dy, & ifmin, & ifmax))
		return;
	Graphics_setWindow (g, tmin, tmax, fmin, fmax);
	if (! (preemphasisFactor = NUMfvector (ifmin, ifmax)) ||
		 ! (dynamicFactor = NUMfvector (itmin, itmax)))
		return;
	/* Pre-emphasis in place; also compute maximum after pre-emphasis. */
	for (ifreq = ifmin; ifreq <= ifmax; ifreq ++)
	{
		preemphasisFactor [ifreq] = preemphasis * log ((double) ifreq) / NUMln2;
		for (itime = itmin; itime <= itmax; itime ++)
		{
			double value = my z [ifreq] [itime];   /* Power. */
			value = (10.0/NUMln10) * log (value + 1e-30) + preemphasisFactor [ifreq];   /* dB */
			if (value > dynamicFactor [itime]) dynamicFactor [itime] = value;   /* Local maximum. */
			my z [ifreq] [itime] = value;
		}
	}
	/* Compute global maximum. */
	maximum = 0.0;
	for (itime = itmin; itime <= itmax; itime ++)
		if (dynamicFactor [itime] > maximum) maximum = dynamicFactor [itime]; 
	/* Dynamic compression in place. */
	for (itime = itmin; itime <= itmax; itime ++)
	{
		dynamicFactor [itime] = dynamicCompression * (maximum - dynamicFactor [itime]);
		for (ifreq = ifmin; ifreq <= ifmax; ifreq ++)
			my z [ifreq] [itime] += dynamicFactor [itime];
	}
	Graphics_image (g, my z,
		itmin, itmax,
		Matrix_columnToX (me, itmin - 0.5),
		Matrix_columnToX (me, itmax + 0.5),
		ifmin, ifmax,
		Matrix_rowToY (me, ifmin - 0.5),
		Matrix_rowToY (me, ifmax + 0.5),
		maximum - dynamic, maximum);
	for (ifreq = ifmin; ifreq <= ifmax; ifreq ++)
		for (itime = itmin; itime <= itmax; itime ++)
		{
			double value = exp ((my z [ifreq] [itime] - dynamicFactor [itime]
				- preemphasisFactor [ifreq]) * (NUMln10 / 10.0)) - 1e-30;
			my z [ifreq] [itime] = value > 0.0 ? value : 0.0;
		}
	NUMfvector_free (preemphasisFactor, ifmin);
	NUMfvector_free (dynamicFactor, itmin);
}

void Spectrogram_paint (I, Graphics g,
	double tmin, double tmax, double fmin, double fmax,
	double dynamic, double preemphasis, double dynamicCompression,
	int garnish)
{
	iam (Spectrogram);
	Graphics_setInner (g);
	Spectrogram_paintInside (me, g, tmin, tmax, fmin, fmax,
		dynamic, preemphasis, dynamicCompression);
	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);
		Graphics_textLeft (g, 1, "Frequency (Hz)");
	}
}

Spectrogram Matrix_to_Spectrogram (I) {
	iam (Matrix);
	Spectrogram thee = Spectrogram_create (my xmin, my xmax, my nx, my dx, my x1,
			my ymin, my ymax, my ny, my dy, my y1);
	if (! thee) return NULL;
	NUMfmatrix_copyElements (my z, thy z, 1, my ny, 1, my nx);
	return thee;
}

Matrix Spectrogram_to_Matrix (I) {
	iam (Spectrogram);
	Matrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1,
			my ymin, my ymax, my ny, my dy, my y1);
	if (! thee) return NULL;
	NUMfmatrix_copyElements (my z, thy z, 1, my ny, 1, my nx);
	return thee;
}

/* End of Spectrogram.c */ 
