# C++ Program to Compute Discrete Fourier Transform using Naive Approach

«
»
This is a C++ Program to perform Discrete Fourier Transform using Naive approach. 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 Discrete Fourier Transform Using Naive Approach. 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 << "Discrete Fourier Transform using naive method\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[k];`
48. `    cout << "The coefficients are: ";`
49. `    for (int j = 0; j < k; j++)`
50. `    {`
51. `        for (int i = 0; i < N; i++)`
52. `        {`
53. `            dft_val[j].real += function[i] * cosine[i];`
54. `            dft_val[j].img += function[i] * sine[i];`
55. `        }`
56. `        cout << "(" << dft_val[j].real << ") - " << "(" << dft_val[j].img << " i)\n";`
57. `    }`
58. `}`

Output:

```\$ g++ DFTNaive.cpp
\$ a.out

Discrete Fourier Transform using naive method
Enter the coefficient of simple linear function:
ax + by = c
1 2 3
Enter the max K value: 20
The coefficients are:
(105) - (-1.03386e-005 i)
(105) - (-1.03386e-005 i)
(105) - (-1.03386e-005 i)
(105) - (-1.03386e-005 i)
(105) - (-1.03386e-005 i)
(105) - (-1.03386e-005 i)
(105) - (-1.03386e-005 i)
(105) - (-1.03386e-005 i)
(105) - (-1.03386e-005 i)
(105) - (-1.03386e-005 i)
(105) - (-1.03386e-005 i)
(105) - (-1.03386e-005 i)
(105) - (-1.03386e-005 i)
(105) - (-1.03386e-005 i)
(105) - (-1.03386e-005 i)
(105) - (-1.03386e-005 i)
(105) - (-1.03386e-005 i)
(105) - (-1.03386e-005 i)
(105) - (-1.03386e-005 i)
(105) - (-1.03386e-005 i)```

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

Note: Join free Sanfoundry classes at Telegram or Youtube 