C++ Program to Compute Discrete Fourier Transform Coefficients

This is a C++ Program to compute the coefficients of the DFT (Discrete Fourier Transform) directly. In mathematics, the discrete Fourier transform (DFT) converts a finite list of equally spaced samples of a function into the list of coefficients of a finite combination of complex sinusoids, ordered by their frequencies, that has those same sample values. It can be said to convert the sampled function from its original domain (often time or position along a line) to the frequency domain.

Here is source code of the C++ Program to Compute DFT Coefficients Directly. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<iostream>
  2. #include<math.h>
  3.  
  4. using namespace std;
  5.  
  6. #define PI 3.14159265
  7.  
  8. class DFT_Coefficient
  9. {
  10.     public:
  11.         double real, img;
  12.         DFT_Coefficient()
  13.         {
  14.             real = 0.0;
  15.             img = 0.0;
  16.         }
  17. };
  18. int main(int argc, char **argv)
  19. {
  20.     int N = 10;
  21.     cout << "Calculation DFT Coefficients\n";
  22.     cout << "Enter the coefficient of simple linear function:\n";
  23.     cout << "ax + by = c\n";
  24.     double a, b, c;
  25.     cin >> a >> b >> c;
  26.  
  27.     double function[N];
  28.     for (int i = 0; i < N; i++)
  29.     {
  30.         function[i] = (((a * (double) i) + (b * (double) i)) - c);
  31.         //System.out.print( "  "+function[i] + "  ");
  32.     }
  33.  
  34.     cout << "Enter the max K value: ";
  35.     int k;
  36.     cin >> k;
  37.  
  38.     double cosine[N];
  39.     double sine[N];
  40.  
  41.     for (int i = 0; i < N; i++)
  42.     {
  43.         cosine[i] = cos((2 * i * k * PI) / N);
  44.         sine[i] = sin((2 * i * k * PI) / N);
  45.     }
  46.  
  47.     DFT_Coefficient dft_val;
  48.     cout << "The coefficients are: ";
  49.  
  50.     for (int i = 0; i < N; i++)
  51.     {
  52.         dft_val.real += function[i] * cosine[i];
  53.         dft_val.img += function[i] * sine[i];
  54.     }
  55.     cout << "(" << dft_val.real << ") - " << "(" << dft_val.img << " i)";
  56.  
  57. }

Output:

$ g++ DFTCoefficient.cpp
$ a.out
 
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) - (-20.6457 i)
 
------------------
(program exited with code: 0)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement

Here’s the list of Best Books in C++ 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 & discussions at Telegram SanfoundryClasses.