Java Program to Perform a 2D FFT Inplace Given a Complex 2D Array

«
»
This is the java implementation of performing Discrete Fourier Transform using Fast Fourier Transform algorithm. This class finds the DFT of N (power of 2) complex elements, generated randomly, using FFT. The input to the class is a two dimensional array of sequence.

Here is the source code of the Java Perform to a 2D FFT Inplace Given a Complex 2D Array. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a sample program to perform 2D FFT inplace 
  2. import java.util.Scanner;
  3.  
  4. public class TwoD_FFT 
  5. {
  6.     static void twoDfft(double[][] inputData, double[][] realOut,
  7.             double[][] imagOut, double[][] amplitudeOut) 
  8.     {
  9.         int height = inputData.length;
  10.         int width = inputData[0].length;
  11.  
  12.         // Two outer loops iterate on output data.
  13.         for (int yWave = 0; yWave < height; yWave++) 
  14.         {
  15.             for (int xWave = 0; xWave < width; xWave++) 
  16.             {
  17.                 // Two inner loops iterate on input data.
  18.                 for (int ySpace = 0; ySpace < height; ySpace++) 
  19.                 {
  20.                     for (int xSpace = 0; xSpace < width; xSpace++) 
  21.                     {
  22.                         // Compute real, imag, and ampltude.
  23.                         realOut[yWave][xWave] += (inputData[ySpace][xSpace] * Math
  24.                                 .cos(2
  25.                                         * Math.PI
  26.                                         * ((1.0 * xWave * xSpace / width) + (1.0
  27.                                                 * yWave * ySpace / height))))
  28.                                 / Math.sqrt(width * height);
  29.                         imagOut[yWave][xWave] -= (inputData[ySpace][xSpace] * Math
  30.                                 .sin(2
  31.                                         * Math.PI
  32.                                         * ((1.0 * xWave * xSpace / width) + (1.0
  33.                                                 * yWave * ySpace / height))))
  34.                                 / Math.sqrt(width * height);
  35.                         amplitudeOut[yWave][xWave] = Math
  36.                                 .sqrt(realOut[yWave][xWave]
  37.                                         * realOut[yWave][xWave]
  38.                                         + imagOut[yWave][xWave]
  39.                                         * imagOut[yWave][xWave]);
  40.                     }
  41.                     System.out.println(realOut[yWave][xWave] + " + "
  42.                             + imagOut[yWave][xWave] + " i");
  43.                 }
  44.             }
  45.         }
  46.     }
  47.  
  48.     public static void main(String args[]) 
  49.     {
  50.         System.out.println("Enter the size: ");
  51.         Scanner sc = new Scanner(System.in);
  52.         int n = sc.nextInt();
  53.         double[][] input = new double[n][n];
  54.         double[][] real = new double[n][n];
  55.         double[][] img = new double[n][n];
  56.         double[][] amplitutude = new double[n][n];
  57.         System.out.println("Enter the 2D elements ");
  58.         for (int i = 0; i < n; i++)
  59.             for (int j = 0; j < n; j++)
  60.                 input[i][j] = sc.nextDouble();
  61.  
  62.         twoDfft(input, real, img, amplitutude);
  63.  
  64.         sc.close();
  65.     }
  66. }

Output:

$ javac TwoD_FFT.java
$ java TwoD_FFT
 
Enter the size: 
2
Enter the 2D elements 
2 3
4 2
 
2.5 + 0.0 i
5.5 + 0.0 i
-0.5 + -1.8369701987210297E-16 i
0.5 + -3.0616169978683826E-16 i
2.5 + 0.0 i
-0.5 + -3.6739403974420594E-16 i
-0.5 + -1.8369701987210297E-16 i
-1.5 + -1.8369701987210297E-16 i

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.