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.

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.