public Channel[] fft() {
if(!(width == height))
throw new Exception("square images only");
int size = width;
if(!(Utils.isPowerOf2(size)))
throw new Exception("size must be power of 2");
// convert channel to complex number array
float[] a = new float[size*size*2 + 1];
int n = 1;
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
a[n] = getPixel(x, y);
n += 2;
}
}
// perform fast fourier transform
fastFourierTransform(a, size, 1);
// convert complex number array to channels
n = 1;
Channel magnitude = new Channel(size, size);
Channel phase = new Channel(size, size);
float real, imag;
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
real = a[n++];
imag = a[n++];
magnitude.putPixel(x, y, (float)Math.Sqrt(real*real + imag*imag));
if (imag == 0 && real >= 0) {
phase.putPixel(x, y, (float)Math.PI/2f);
} else if (imag == 0 && real < 0) {
phase.putPixel(x, y, (float)Math.PI/-2f);
} else {
phase.putPixel(x, y, (float)Math.Atan(real/imag));
}
}
}
// return magnitude and phase channels
return new Channel[]{magnitude.offset(size>>1, size>>1), phase};
}