/* Pitch_Intensity.c */
/* Paul Boersma, April 27, 1997 */

#include "Pitch_Intensity.h"

static void Pitch_getExtrema (Pitch me, double *minimum, double *maximum) {
	long i;
	*minimum = 1e30, *maximum = -1e30;
	for (i = 1; i <= my nx; i ++) {
		double frequency = my frame [i]. candidate [1]. frequency;
		if (frequency == 0.0) continue;   /* Voiceless. */
		if (frequency < *minimum) *minimum = frequency;
		if (frequency > *maximum) *maximum = frequency;
	}
	if (*maximum == -1e30) *maximum = 0.0;
	if (*minimum == 1e30) *minimum = 0.0;
}

void Pitch_Intensity_draw (Pitch pitch, Intensity intensity, Graphics g,
	double f1, double f2, double s1, double s2, int garnish)
{
	long i;
	if (f2 <= f1) Pitch_getExtrema (pitch, & f1, & f2);
	if (f1 == 0.0) return;   /* All voiceless. */
	if (f1 == f2) { f1 -= 1.0; f2 += 1.0; }
	if (s2 <= s1) Matrix_getWindowExtrema (intensity, 0, 0, 1, 1, & s1, & s2);
	if (s1 == s2) { s1 -= 1.0; s2 += 1.0; }
	Graphics_setWindow (g, f1, f2, s1, s2);
	Graphics_setInner (g);
	for (i = 1; i <= pitch -> nx; i ++) {
		double t = Sampled_indexToX (pitch, i);
		double index = Sampled_xToIndex (intensity, t);
		double x = pitch -> frame [i]. candidate [1]. frequency;
		double y = NUM_interpolate_sinc (intensity -> z [1], intensity -> nx, index, 50);
		if (x == 0) continue;   /* Voiceless. */
		Graphics_fillCircle_mm (g, x, y, 1.0);
	}
	Graphics_unsetInner (g);
	if (garnish) {
		Graphics_drawInnerBox (g);
		Graphics_textBottom (g, 1, "Fundamental frequency (Hz)");
		Graphics_marksBottom (g, 2, 1, 1, 0);
		Graphics_textLeft (g, 1, "Intensity (dB)");
		Graphics_marksLeft (g, 2, 1, 1, 0);
	}
}

/* End of file Pitch_Intensity.c */
