/** Demo programms to shown the use of the FFTWComplex class * to take low level FFT one-dimensional tranforms * of split data array. * * The split scheme is designed to simply interface to pre-written * code that hold real/imag data in seperate array. This format * is NOT supported by the DataArray classes, so you have to do * more yourself. * @author Will Hossack, 2008 * * * This file is part of jfftw. * * Jfftw 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. * * Jfftw 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 Jfftw. If not, see . * * */ package jfftw.demo; import jfftw.*; public class FFTWComplexDemoSplit { public static void main(String args[]) { int n = 2048; // Length of 1D-FFT // Form in/out buffers for split complex // array. double[][] inBuffer = new double[2][n]; // Direct allocation double[] realOutBuffer = new double[n]; // Real Part double[] imagOutBuffer = new double[n]; // Imaginary part // Take real/imaginary outBuffer array and glue together // as a single split array. // Note this does NOT reallocated the arrays. double[][] outBuffer = ArrayUtil.split(realOutBuffer,imagOutBuffer); // Fill the real part of the inBuffer with randoms // and imaginary with constant using // the ArrayUtil method to access the ith complex // element with two doubles for(int i = 0; i < n; i++) { ArrayUtil.setComplex(inBuffer,i,Math.random() + 1.0, 1.0); } // Form a Complex FFT object to do the work FFTWComplex fft = new FFTWComplex(); // Take out-of place forward transform. // inBuffer will not be changed, and // outBuffer will hold the FFT. outBuffer = fft.oneDimensional(inBuffer,outBuffer,FFTW.FORWARD); // Take in-place backward transform of outBuffer // This will overwrite outBuffer with its // inverse FFT outBuffer = fft.oneDimensional(outBuffer,FFTW.BACKWARD,true); // There is no normalisation, so // after Forward FFT + Inverse FFT, each element of // outBuffer will be a factor of n larger than expected. // Use ArrayUtil method to normalise. ArrayUtil.mult(outBuffer,1.0/n); // inBuffer and outBuffer should now the // (almost) identical. Form the real and imaginary // modulus differences, this time using direct array // access. // For the ith Complex element, we have // Real part in array element [0][i] // Imaginary part in array element [1][i] double realDiff = 0.0; double imagDiff = 0.0; for(int i = 0; i < n; i++) { realDiff += Math.abs(inBuffer[0][i] - outBuffer[0][i]); imagDiff += Math.abs(inBuffer[1][i] - outBuffer[1][i]); } // Print out the differences, these will be small // but NOT zero due to numerical rounding errors. System.out.println("Real Difference is : " + realDiff + "\nImaginary Difference : " + imagDiff); } }