For a simple fft-experiment the speed of the routine is not so important. But for e.g. parallel-code-search a fast FFT-implementation is necessary. I compared three implementations.
FFT1 is a previous described implementation. I just simply coded the formulas for a regular Fourier transformation. It worked like a charm!
the code:
package FFT1;
public class Fourier {
public Fourier() {
}
public Complex[] dft(double[] x) {
int n = x.length;
Complex[] xX = new Complex[n]; // FFT-bins
for (int k = 0; k < n; k++) {
Complex accum = new Complex(0.0, 0.0);
for (int nn = 0; nn < n; nn++) {
Complex wnkN = Complex.expjphi(-2 * Math.PI * nn * k / n);
Complex prod = wnkN.multiply(x[nn]);
accum = accum.add(prod);
}
xX[k] = accum;
}
return xX;
}
public Complex[] idft(Complex[] X) {
int n = X.length;
Complex[] x = new Complex[n];
for (int k = 0; k < n; k++) {
Complex accum = new Complex(0.0, 0.0);
for (int nn = 0; nn < n; nn++) {
Complex wnkN = Complex.expjphi(2 * Math.PI * nn * k / n);
Complex prod = wnkN.multiply(X[nn]);
accum = accum.add(prod);
}
x[k] = accum.multiply((double) (1.0 / n));
}
return x;
}
}
|
And the runtime results:
running straight-forward FFT numSam = 1024 Reading ini-file... status = OK Reading input-file data\GPSdata-DiscreteComponents-fs38_192-if9_55.bin status = OK outputFileName = data\output start 7-feb-2011 16:44:35 stop 7-feb-2011 16:44:35 runtime (msec): 228.0
package FFT1;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import util.FileIO;
import util.ShowPlot2d;
public class Engine {
DateFormat f = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
DateFormat.MEDIUM);
DateFormat ff = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
DateFormat.MEDIUM);
double time1;
int numSam = (int) Math.pow(2, 10);
double[] inBuffer;
public Engine() {
}
public void startTimer(){
Calendar cal = Calendar.getInstance();
time1 = cal.getTimeInMillis();
Date now = cal.getTime();
System.out.println("start " + "\t" + " " + f.format(now));
}
public void stopTimer(){
Calendar cal = Calendar.getInstance();
double time2 = cal.getTimeInMillis();
System.out.println("stop " + "\t" + " " + ff.format(time2));
double dif = time2 - time1;
System.out.println("runtime (msec): " + dif + "\n");
}
public double[] readSamples(int number) {
FileIO inputfile = new FileIO();
inputfile.FileSetup("output");
int kar;
double inputSamples[] = new double[number];
for (int k = 0; k < number; k++) {
kar = inputfile.readByte();
if (kar > 127) {
kar = kar - 256;
}
inputSamples[k] = kar;
}
return inputSamples;
}
public void testFFT1(String title) {
System.out.println(title);
System.out.println("numSam = " + numSam);
inBuffer = readSamples(numSam);
Fourier myFourier = new Fourier();
startTimer();
Complex[] outBuffer = myFourier.dft(inBuffer);
stopTimer();
double[] plotBuffer = new double[numSam];
for (int i = 0; i < outBuffer.length; i++) {
plotBuffer[i] = outBuffer[i].magnitude();
}
ShowPlot2d myPlot = new ShowPlot2d(1);
myPlot.add(plotBuffer, 1, "myPlot");
myPlot.showFigure();
}
}
|
FFT2 is a more efficient implementation that I found on the internet:
package FFT2;
/*************************************************************************
* Compilation: javac FFT.java
* Execution: java FFT N
* Dependencies: Complex.java
*
* Compute the FFT and inverse FFT of a length N complex sequence.
* Bare bones implementation that runs in O(N log N) time. Our goal
* is to optimize the clarity of the code, rather than performance.
*
* Limitations
* -----------
* - assumes N is a power of 2
*
* - not the most memory efficient algorithm (because it uses
* an object type for representing complex numbers and because
* it re-allocates memory for the subarray, instead of doing
* in-place or reusing a single temporary array)
*
*************************************************************************/
public class FFT {
// compute the FFT of x[], assuming its length is a power of 2
public static Komplex[] fft(Komplex[] x) {
int N = x.length;
// base case
if (N == 1) return new Komplex[] { x[0] };
// radix 2 Cooley-Tukey FFT
if (N % 2 != 0) { throw new RuntimeException("N is not a power of 2"); }
// fft of even terms
Komplex[] even = new Komplex[N/2];
for (int k = 0; k < N/2; k++) {
even[k] = x[2*k];
}
Komplex[] q = fft(even);
// fft of odd terms
Komplex[] odd = even; // reuse the array
for (int k = 0; k < N/2; k++) {
odd[k] = x[2*k + 1];
}
Komplex[] r = fft(odd);
// combine
Komplex[] y = new Komplex[N];
for (int k = 0; k < N/2; k++) {
double kth = -2 * k * Math.PI / N;
Komplex wk = new Komplex(Math.cos(kth), Math.sin(kth));
y[k] = q[k].plus(wk.times(r[k]));
y[k + N/2] = q[k].minus(wk.times(r[k]));
}
return y;
}
// compute the inverse FFT of x[], assuming its length is a power of 2
public static Komplex[] ifft(Komplex[] x) {
int N = x.length;
Komplex[] y = new Komplex[N];
// take conjugate
for (int i = 0; i < N; i++) {
y[i] = x[i].conjugate();
}
// compute forward FFT
y = fft(y);
// take conjugate again
for (int i = 0; i < N; i++) {
y[i] = y[i].conjugate();
}
// divide by N
for (int i = 0; i < N; i++) {
y[i] = y[i].times(1.0 / N);
}
return y;
}
// compute the circular convolution of x and y
public static Komplex[] cconvolve(Komplex[] x, Komplex[] y) {
// should probably pad x and y with 0s so that they have same length
// and are powers of 2
if (x.length != y.length) { throw new RuntimeException("Dimensions don't agree"); }
int N = x.length;
// compute FFT of each sequence
Komplex[] a = fft(x);
Komplex[] b = fft(y);
// point-wise multiply
Komplex[] c = new Komplex[N];
for (int i = 0; i < N; i++) {
c[i] = a[i].times(b[i]);
}
// compute inverse FFT
return ifft(c);
}
// compute the linear convolution of x and y
public static Komplex[] convolve(Komplex[] x, Komplex[] y) {
Komplex ZERO = new Komplex(0, 0);
Komplex[] a = new Komplex[2*x.length];
for (int i = 0; i < x.length; i++) a[i] = x[i];
for (int i = x.length; i < 2*x.length; i++) a[i] = ZERO;
Komplex[] b = new Komplex[2*y.length];
for (int i = 0; i < y.length; i++) b[i] = y[i];
for (int i = y.length; i < 2*y.length; i++) b[i] = ZERO;
return cconvolve(a, b);
}
// display an array of Complex numbers to standard output
public static void show(Komplex[] x, String title) {
System.out.println(title);
System.out.println("-------------------");
for (int i = 0; i < x.length; i++) {
System.out.println(x[i]);
}
System.out.println();
}
}
|
I had to change the name of the class Complex into Komplex to avoid problems in other java-code.
Running the program yields:
running faster FFT numSam = 1024 Reading ini-file... status = OK Reading input-file data\GPSdata-DiscreteComponents-fs38_192-if9_55.bin status = OK outputFileName = data\output start 7-feb-2011 16:55:29 stop 7-feb-2011 16:55:29 runtime (msec): 9.0
This is fast! But with inplace-FFT it can even be faster:
package DSP;
import src_old.Complex;
/*************************************************************************
* Compilation: javac InplaceFFT.java
* Execution: java InplaceFFT N
* Dependencies: Complex.java
*
* Compute the FFT of a length N complex sequence in-place.
* Uses a non-recursive version of the Cooley-Tukey FFT.
* Runs in O(N log N) time.
*
* Reference: Algorithm 1.6.1 in Computational Frameworks for the
* Fast Fourier Transform by Charles Van Loan.
*
*
* Limitations
* -----------
* - assumes N is a power of 2
*
*
*************************************************************************/
public class InplaceFFT {
// compute the FFT of x[], assuming its length is a power of 2
public static void fft(Complex[] x) {
// check that length is a power of 2
int N = x.length;
if (Integer.highestOneBit(N) != N) {
throw new RuntimeException("N is not a power of 2");
}
// bit reversal permutation
int shift = 1 + Integer.numberOfLeadingZeros(N);
for (int k = 0; k < N; k++) {
int j = Integer.reverse(k) >>> shift;
if (j > k) {
Complex temp = x[j];
x[j] = x[k];
x[k] = temp;
}
}
// butterfly updates
for (int L = 2; L <= N; L = L+L) {
for (int k = 0; k < L/2; k++) {
double kth = -2 * k * Math.PI / L;
Complex w = new Complex(Math.cos(kth), Math.sin(kth));
for (int j = 0; j < N/L; j++) {
Complex tao = w.times(x[j*L + k + L/2]);
x[j*L + k + L/2] = x[j*L + k].minus(tao);
x[j*L + k] = x[j*L + k].plus(tao);
}
}
}
}
public static void ifft(Complex[] x) {
// inverse FFT using above FFT
for(int i=0;i |
Running this implementation yields:
running inplace FFT numSam = 1024 Reading ini-file... status = OK Reading input-file data\GPSdata-DiscreteComponents-fs38_192-if9_55.bin status = OK outputFileName = data\output start 7-feb-2011 16:58:03 stop 7-feb-2011 16:58:03 runtime (msec): 5.0
Inplace-FFT is twice as fast as a regular implementation. Straightforward implementation without any optimizations is too slow for practical use.