Java Program to Compute Discrete Fourier Transform Coefficients

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.

  1. //This is a sample program to calculate a DFT Coefficients using the formula
  2. import java.util.Scanner;
  3.  
  4. public class DFT_Coefficient 
  5. {
  6.     double real,  img;
  7.     public DFT_Coefficient() 
  8.     {
  9.         this.real = 0.0;
  10.         this.img = 0.0;
  11.     }
  12.     public static void main(String args[])
  13.     {
  14.         int N = 10;
  15.         Scanner sc = new Scanner(System.in);
  16.         System.out.println("Calculation DFT Coefficients");
  17.         System.out.println("Enter the coefficient of simple linear funtion:");
  18.         System.out.println("ax + by = c");
  19.         double a = sc.nextDouble();
  20.         double b = sc.nextDouble();
  21.         double c = sc.nextDouble();
  22.  
  23.         double []function = new double[N];
  24.         for(int i=0; i<N; i++)
  25.         {
  26.             function[i] = (((a*(double)i) + (b*(double)i)) - c);
  27. 			//System.out.print( "  "+function[i] + "  ");
  28.         }
  29.  
  30.         System.out.println("Enter the max K value: ");
  31.         int k = sc.nextInt();
  32.  
  33.         double []cos = new double[N];
  34.         double []sin = new double[N];
  35.  
  36.         for(int i=0; i<N; i++)
  37.         {
  38.             cos[i] = Math.cos((2 * i * k * Math.PI) / N);
  39.             sin[i] = Math.sin((2 * i * k * Math.PI) / N);
  40.         }
  41.  
  42.         DFT_Coefficient dft_val = new DFT_Coefficient();
  43.         System.out.println("The coefficients are: ");
  44.  
  45.         for(int i=0; i<N; i++)
  46.         {
  47.             dft_val.real += function[i] * cos[i];
  48.             dft_val.img += function[i] * sin[i];	
  49.         }
  50.         System.out.println("("+dft_val.real + ") - " + "("+dft_val.img + " i)");
  51.         sc.close();
  52.     }
  53. }

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)

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

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

If you find any mistake above, kindly email to [email protected]

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 & discussions at Telegram SanfoundryClasses.