/* LPC_to_Spectrogram.c */
/* David Weenink, 19991020 */

#include "LPC_to_Spectrogram.h"

Spectrogram LPC_to_Spectrogram (LPC me, double dfMin, double bandwidthReduction,
	double deEmphasisFrequency)
{
	Spectrogram thee = NULL; long i, j, nfft = 2;
	double freqStep, samplingFrequency = 1.0 / my samplingInterval;
	
	if (dfMin <= 0)
	{
		nfft = 512; dfMin = samplingFrequency / nfft;
	}
	while (samplingFrequency / nfft > dfMin || nfft <= my maxnCoefficients) nfft *= 2;
	freqStep = samplingFrequency / nfft;
	
	if ((thee = Spectrogram_create (my xmin, my xmax, my nx, my dx, my x1,
			0, samplingFrequency / 2, nfft / 2 + 1, freqStep, 0)) == NULL) return NULL;
		
	for (i=1; i <= my nx; i++)
	{
		double t = Sampled_indexToX (me, i);
		Spectrum spec = LPC_to_Spectrum (me, t, dfMin, bandwidthReduction, deEmphasisFrequency);
		if (spec == NULL) goto end;
		for (j=1; j <= spec -> nx; j++)
		{
			float re = spec -> z[1][j], im = spec -> z[2][j];
			thy z[j][i] =  re * re + im * im;
		}
		forget (spec);
	}	
end:
	if (Melder_hasError()) forget (thee);
	return thee;
}

/* End of file LPC_to_Spectrogram.c */
