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:

Working with Tree Model Nodes in Jackson
Introduction to Spring Cloud OpenFeign
Java Program to Solve a Matching Problem for a Given Specific Case
Java Program to Implement Borwein Algorithm
Java Program to Check for balanced parenthesis by using Stacks
Java Program to Find Number of Spanning Trees in a Complete Bipartite Graph
Java Timer
Registration – Activate a New Account by Email
Spring Data JPA Delete and Relationships
Collection trong java
Guide to Dynamic Tests in Junit 5
Updating your Password
JWT – Token-based Authentication trong Jersey 2.x
Java Program to Implement Traveling Salesman Problem using Nearest neighbour Algorithm
A Guide to Java HashMap
Java Program to Implement Quick Sort Using Randomization
Java Program to Encode a Message Using Playfair Cipher
Hướng dẫn Java Design Pattern – Chain of Responsibility
Java Program to Compute the Volume of a Tetrahedron Using Determinants
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Custom Thread Pools In Java 8 Parallel Streams
Java Program for Topological Sorting in Graphs
The Thread.join() Method in Java
How to Read HTTP Headers in Spring REST Controllers
JUnit5 Programmatic Extension Registration with @RegisterExtension
Java Program to Test Using DFS Whether a Directed Graph is Weakly Connected or Not
Documenting a Spring REST API Using OpenAPI 3.0
Java Program to Implement Direct Addressing Tables
Merging Streams in Java
Multipart Upload with HttpClient 4
Giới thiệu thư viện Apache Commons Chain
Java Program to Implement Multi-Threaded Version of Binary Search Tree