/* Sound_to_Harmonicity.c */
/* Paul Boersma, April 20, 1996 */

#include <math.h>
#include "Sound_to_Pitch.h"
#include "Sound_to_Harmonicity.h"

Harmonicity Sound_to_Harmonicity_ac (Sound me, double dt, double minimumPitch,
	double silenceThreshold, double periodsPerWindow)
{
	Harmonicity thee;
	long i;
	Pitch pitch = Sound_to_Pitch_any (me, dt, minimumPitch, periodsPerWindow, 15, 1,
		silenceThreshold, 0, 0, 0, 0, 1e30);
	if (! pitch) return NULL;
	thee = Harmonicity_create (my xmin, my xmax, pitch -> nx,
		pitch -> dx, pitch -> x1);
	if (thee)
		for (i = 1; i <= thy nx; i ++)
			if (pitch -> frame [i]. candidate [1]. frequency == 0)
				thy z [1] [i] = -200;
			else {
				double r = pitch -> frame [i]. candidate [1]. strength;
				thy z [1] [i] = r <= 1e-15 ? -150 : r > 1 - 1e-15 ? 150 : 10 * log10 (r / (1 - r));
			}
	forget (pitch);
	return thee;
}

Harmonicity Sound_to_Harmonicity_cc (Sound me, double dt, double minimumPitch,
	double silenceThreshold, double periodsPerWindow)
{
	Harmonicity thee;
	long i;
	Pitch pitch = Sound_to_Pitch_any (me, dt, minimumPitch, periodsPerWindow, 15, 3,
		silenceThreshold, 0, 0, 0, 0, 1e30);
	if (! pitch) return NULL;
	thee = Harmonicity_create (my xmin, my xmax, pitch -> nx,
		pitch -> dx, pitch -> x1);
	if (thee)
		for (i = 1; i <= thy nx; i ++)
			if (pitch -> frame [i]. candidate [1]. frequency == 0)
				thy z [1] [i] = -200;
			else {
				double r = pitch -> frame [i]. candidate [1]. strength;
				thy z [1] [i] = r <= 1e-15 ? -150 : r > 1 - 1e-15 ? 150 : 10 * log10 (r / (1 - r));
			}
	forget (pitch);
	return thee;
}

/* End of file Sound_to_Harmonicity.c */
