C Program to Compute Discrete Fourier Transform Coefficients

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

Output:

$ gcc DFTCoefficient.c
$ ./a.out
 
Calculation DFT Coefficients
Enter the coefficient of simple linear function:
ax + by = c
1 2 3
Enter the max K value: 2
The coefficients are: ( -1.500000e+001 )-( -2.064573e+001 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.