/*--------------------------------------------------------------------------
 * AcfFft.ox - code to compute the ACF yusing the FFT
 *
 *       code (C) Jurgen Doornik 1994-1995
 *
 * This code uses the fast Fourier transform to compute
 * the autocorrelation function of a data vector.
 *
 * Syntax:
 *
 * AcfFft(const mY, const iLag)
 *		mY[cT][cN]    in:  cT x cN data vector
 *      iLag          in:  the highest lag length
 *	Returns a (iLag+1) x cN matrix with the autocorrelation
 *      function of the columns of mY
 *--------------------------------------------------------------------------*/

#include <oxstd.h>

AcfFft(const mY, const iLag)
{
	decl i, x, f1, ct = rows(mY), cn = columns(mY), acf = <>;

	x = standardize(mY)';             /* standardize and transpose the data */
	for (i = 0; i < cn; ++i)
	{
		f1 = fft(x[i][] ~ zeros(1, iLag)); /* do the fft, padded with zeros */
		f1 = cmul(f1, f1[0][] | -f1[1][]); /* multiply by its complex conj. */
		acf ~= fft(f1, 2)[][:iLag]' ./ ct;/* do reverse real fft, scale by T*/
	}
	return acf;
}

