Java Perform to 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.

//This is a sample program to perform 2D FFT inplace 
import java.util.Scanner;
 
public class TwoD_FFT 
{
    static void twoDfft(double[][] inputData, double[][] realOut,
            double[][] imagOut, double[][] amplitudeOut) 
    {
        int height = inputData.length;
        int width = inputData[0].length;
 
        // Two outer loops iterate on output data.
        for (int yWave = 0; yWave < height; yWave++) 
        {
            for (int xWave = 0; xWave < width; xWave++) 
            {
                // Two inner loops iterate on input data.
                for (int ySpace = 0; ySpace < height; ySpace++) 
                {
                    for (int xSpace = 0; xSpace < width; xSpace++) 
                    {
                        // Compute real, imag, and ampltude.
                        realOut[yWave][xWave] += (inputData[ySpace][xSpace] * Math
                                .cos(2
                                        * Math.PI
                                        * ((1.0 * xWave * xSpace / width) + (1.0
                                                * yWave * ySpace / height))))
                                / Math.sqrt(width * height);
                        imagOut[yWave][xWave] -= (inputData[ySpace][xSpace] * Math
                                .sin(2
                                        * Math.PI
                                        * ((1.0 * xWave * xSpace / width) + (1.0
                                                * yWave * ySpace / height))))
                                / Math.sqrt(width * height);
                        amplitudeOut[yWave][xWave] = Math
                                .sqrt(realOut[yWave][xWave]
                                        * realOut[yWave][xWave]
                                        + imagOut[yWave][xWave]
                                        * imagOut[yWave][xWave]);
                    }
                    System.out.println(realOut[yWave][xWave] + " + "
                            + imagOut[yWave][xWave] + " i");
                }
            }
        }
    }
 
    public static void main(String args[]) 
    {
        System.out.println("Enter the size: ");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        double[][] input = new double[n][n];
        double[][] real = new double[n][n];
        double[][] img = new double[n][n];
        double[][] amplitutude = new double[n][n];
        System.out.println("Enter the 2D elements ");
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                input[i][j] = sc.nextDouble();
 
        twoDfft(input, real, img, amplitutude);
 
        sc.close();
    }
}

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

Related posts:

HttpAsyncClient Tutorial
Java Program to Implement Counting Sort
Getting the Size of an Iterable in Java
Spring Boot - Hystrix
Java Program to Represent Linear Equations in Matrix Form
Java Program to Implement Maximum Length Chain of Pairs
Guide to Mustache with Spring Boot
Assert an Exception is Thrown in JUnit 4 and 5
Hướng dẫn Java Design Pattern – Service Locator
Java Program to Represent Graph Using Adjacency List
Consumer trong Java 8
Java Program to Implement the Binary Counting Method to Generate Subsets of a Set
Cài đặt và sử dụng Swagger UI
Java Program to Construct K-D Tree for 2 Dimensional Data
Java Program to Implement Gift Wrapping Algorithm in Two Dimensions
Auditing with JPA, Hibernate, and Spring Data JPA
Iterable to Stream in Java
Spring Security – Reset Your Password
Java Deep Learning Essentials - Yusuke Sugomori
Java Program to subtract two large numbers using Linked Lists
Introduction to Spring Data REST
Encode/Decode to/from Base64
Java Program to Implement Slicker Algorithm that avoids Triangulation to Find Area of a Polygon
Java Program to Check Whether a Given Point is in a Given Polygon
Quick Guide to Spring Controllers
Java Program to Find Whether a Path Exists Between 2 Given Nodes
Logout in an OAuth Secured Application
Interface trong Java 8 – Default method và Static method
Java Program to Find the Number of Ways to Write a Number as the Sum of Numbers Smaller than Itself
Java Program to Compute DFT Coefficients Directly
Java Program to Create a Balanced Binary Tree of the Incoming Data
Quick Guide to java.lang.System