/*******************************************************************************
 * Copyright (c) 2010 Attila Kovacs <attila_kovacs[AT]post.harvard.edu>.
 * All rights reserved. 
 * 
 * This file is part of crush.
 * 
 *     crush is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     crush is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with crush.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contributors:
 *     Attila Kovacs <attila_kovacs[AT]post.harvard.edu> - initial API and implementation
 ******************************************************************************/
package test;

import java.util.*;

import util.*;



public class FFTTest {

	public static void main(String[] args) {
		
		double[] data = new double[16];
		
		data[0] = 1.0;
		Complex[] spectrum = ArrayUtil.FFT(data);
		System.out.println("delta[0]:");
		print(spectrum);
		
		
		Arrays.fill(data, 1.0);
		spectrum = ArrayUtil.FFT(data);
		System.out.println("cons(1.0):");
		print(spectrum);
		
		
		for(int i=0; i<data.length; i++) data[i] = Math.cos(2.0 * Math.PI * i / data.length);
		spectrum = ArrayUtil.FFT(data);
		System.out.println("cos1:");
		print(spectrum);
		
		for(int i=0; i<data.length; i++) data[i] = Math.sin(4.0 * Math.PI * i / data.length);
		spectrum = ArrayUtil.FFT(data);
		System.out.println("sin2:");
		print(spectrum);		
		
		Random random = new Random();
		data = new double[1024*1024];
		for(int i=0; i<data.length; i++) data[i] = random.nextGaussian();
		double[] power = ArrayUtil.averagePower(data, ArrayUtil.getHammingWindow(16));
		System.out.println("pow:");
		print(power);		
		
		int n = Integer.parseInt(args[0]);
		data = new double[n*1024*1024];
		for(int i=0; i<data.length; i++) data[i] = Math.random();
		long time = -System.currentTimeMillis();
		ArrayUtil.inplaceFFT(data, true);
		time += System.currentTimeMillis();
		int ops = data.length * (int) (Math.log(data.length) / Math.log(2.0));
		
		System.err.println("real transform of " + n + "M points: " + time + "ms --> " + Util.e3.format(1e-3*ops/time) + " Mcycles/sec");
		
		
		Complex[] cdata = new Complex[data.length >> 1];
		for(int i=0; i<cdata.length; i++) cdata[i] = new Complex(Math.random(), Math.random());
		time = -System.currentTimeMillis();
		ArrayUtil.FFT(cdata, true);
		time += System.currentTimeMillis();
		System.err.println("complex transform of " + n + "M points: " + time + "ms --> " + Util.e3.format(1e-3*ops/time) + " Mcycles/sec");
		
		
		
	}

	public static void print(Complex[] data) {
		for(int i=0; i<data.length; i++) 
			System.out.println("  " + i + ":\t" + data[i].toString(Util.f6));
		System.out.println();
	}
	
	public static void print(double[] data) {
		for(int i=0; i<data.length; i++) 
			System.out.println("  " + i + ":\t" + Util.e6.format(data[i]));
		System.out.println();
	}
	
}
