# C Program to Compute Discrete Fourier Transform using Naive Approach

«
»
This is a C Program to perform Discrete Fourier Transform using Naive approach. 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<stdio.h>`
2. `#include<math.h>`
3. `#define PI 3.14159265`
4. `int k = 20;`
5. ` `
6. `struct DFT_Coefficient {`
7. `    double real, img;`
8. `};`
9. ` `
10. `int main(int argc, char **argv) {`
11. `    int N = 10;`
12. `    float a, b, c;`
13. `    int i, j;`
14. `    struct DFT_Coefficient dft_val[k];`
15. `    double cosine[N];`
16. `    double sine[N];`
17. ` `
18. `    printf("Discrete Fourier Transform using naive method\n");`
19. `    printf("Enter the coefficient of simple linear function:\n");`
20. `    printf("ax + by = c\n");`
21. `    scanf("%f", &a);`
22. `    scanf("%f", &b);`
23. `    scanf("%f", &c);`
24. `    double function[N];`
25. `    for (i = 0; i < N; i++) {`
26. `        function[i] = (((a * (double) i) + (b * (double) i)) - c);`
27. `        //System.out.print( "  "+function[i] + "  ");`
28. `    }`
29. `    for (i = 0; i < N; i++) {`
30. `        cosine[i] = cos((2 * i * k * PI) / N);`
31. `        sine[i] = sin((2 * i * k * PI) / N);`
32. `    }`
33. ` `
34. `    printf("The coefficients are: ");`
35. `    for (j = 0; j < k; j++) {`
36. `        for (i = 0; i < N; i++) {`
37. `            dft_val[j].real += function[i] * cosine[i];`
38. `            dft_val[j].img += function[i] * sine[i];`
39. `        }`
40. `        printf("( %e ) - ( %e i)\n", dft_val[j].real, dft_val[j].img);`
41. `    }`
42. `    return 0;`
43. `}`

Output:

```\$ gcc DFTNaive.c
\$ ./a.out

Discrete Fourier Transform using naive method
Enter the coefficient of simple linear function:
ax + by = c
1 2 3
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 