Java Program to Compute DFT Coefficients Directly

This is the java implementation of calculating coefficients of the given function performing the Discrete-Fourier Transform. Formula for calculating the coefficient is X(k) = Sum(x(n)*cos(2*PI*k*n/N) – iSum(x(n)*sin(2*PI*k*n/N)) over 0 to N-1

Here is the source code of the Java Program to Compute DFT Coefficients Directly. 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 calculate a DFT Coefficients using the formula
import java.util.Scanner;
 
public class DFT_Coefficient 
{
    double real,  img;
    public DFT_Coefficient() 
    {
        this.real = 0.0;
        this.img = 0.0;
    }
    public static void main(String args[])
    {
        int N = 10;
        Scanner sc = new Scanner(System.in);
        System.out.println("Calculation DFT Coefficients");
        System.out.println("Enter the coefficient of simple linear funtion:");
        System.out.println("ax + by = c");
        double a = sc.nextDouble();
        double b = sc.nextDouble();
        double c = sc.nextDouble();
 
        double []function = new double[N];
        for(int i=0; i<N; i++)
        {
            function[i] = (((a*(double)i) + (b*(double)i)) - c);
			//System.out.print( "  "+function[i] + "  ");
        }
 
        System.out.println("Enter the max K value: ");
        int k = sc.nextInt();
 
        double []cos = new double[N];
        double []sin = new double[N];
 
        for(int i=0; i<N; i++)
        {
            cos[i] = Math.cos((2 * i * k * Math.PI) / N);
            sin[i] = Math.sin((2 * i * k * Math.PI) / N);
        }
 
        DFT_Coefficient dft_val = new DFT_Coefficient();
        System.out.println("The coefficients are: ");
 
        for(int i=0; i<N; i++)
        {
            dft_val.real += function[i] * cos[i];
            dft_val.img += function[i] * sin[i];	
        }
        System.out.println("("+dft_val.real + ") - " + "("+dft_val.img + " i)");
        sc.close();
    }
}

Output:

$ javac DFT_Coefficient.java
$ java DFT_Coefficient
 
Calculation DFT Coefficients
Enter the coefficient of simple linear funtion:
ax + by = c
1 2 3
Enter the max K value: 
2
The coefficients are: 
(-15.00000000000001) - (-20.6457288070676 i)

Related posts:

Java Program to Implement String Matching Using Vectors
Guava CharMatcher
Java Program to Implement Borwein Algorithm
Using a Mutex Object in Java
LinkedList trong java
Disable Spring Data Auto Configuration
Use Liquibase to Safely Evolve Your Database Schema
Java Program to Perform Searching in a 2-Dimension K-D Tree
JUnit5 @RunWith
Quản lý bộ nhớ trong Java với Heap Space vs Stack
Java Program to Solve a Matching Problem for a Given Specific Case
Rate Limiting in Spring Cloud Netflix Zuul
Concurrent Test Execution in Spring 5
Lớp lồng nhau trong java (Java inner class)
Spring Boot - Service Components
Hướng dẫn sử dụng Java Annotation
Java – Delete a File
Java Program to Implement Bellman-Ford Algorithm
Java Program to Implement the Monoalphabetic Cypher
Java Program to Find the Edge Connectivity of a Graph
Sử dụng CountDownLatch trong Java
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Java Program to Check Whether Topological Sorting can be Performed in a Graph
Spring 5 and Servlet 4 – The PushBuilder
Java Program to Check Whether a Given Point is in a Given Polygon
Các chương trình minh họa sử dụng Cấu trúc điều khiển trong Java
Java Program to Implement Solovay Strassen Primality Test Algorithm
4 tính chất của lập trình hướng đối tượng trong Java
Java Program to Implement Floyd-Warshall Algorithm
Java Program to Implement Find all Forward Edges in a Graph
Java Program to Perform Search in a BST
Join and Split Arrays and Collections in Java