document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2. -------------------------------------------------------------------------------------------------
  3.  Assignment No: 04
  4.  Title - Find the N point DFT/IDFT of the given sequence x(n). Plot the magnitude spectrum X(K) Vs K. Analyze the output for different N and the same input sequence x(n). Also observe the periodicity and symmetry property.
  5. -------------------------------------------------------------------------------------------------
  6. */
  7.  
  8. #include<conio.h>
  9. #include<iostream.h>
  10. #include<math.h>
  11. #include<process.h>
  12.  
  13. void main()
  14. {
  15. int N,n,i,j,Real[20],Imag[20],real,imag,xnr[20],xnl[20],ch,real2,imag2;
  16. int xkr[20],xkl[20],xn1[20],xn[20],k=0,y,Real1[20],imag1[20];
  17. clrscr();
  18. cout<<"\\nEnter the number of element:";
  19. cin>>n;
  20. N=n;
  21. do{
  22. cout<<"\\n1.DFT";
  23. cout<<"\\n2.IDFT";
  24. cout<<"\\n3.EXIT";
  25. cout<<"\\nEnter your choice:";
  26. cin>>ch;
  27. switch(ch)
  28. {
  29. case 1:
  30. n=N;
  31. for(i=0;i<n;i++)
  32. {
  33.         cout<<"\\n";
  34.         for(j=0;j<n;j++)
  35.         {
  36.             Real[k]=cos(2*M_PI*i*j/N);
  37.             Imag[k]=-1*sin(2*M_PI*i*j/N);
  38.             cout<<"\\t";
  39.             cout<<Real[k]<<"i("<<Imag[k]<<"j)";
  40.             k++;
  41.         }
  42. }
  43. for(i=0;i<N;i++)
  44. {
  45.     cout<<"\\nEnter x1("<<i<<")";
  46.     cin>>xn1[i];
  47. }
  48. cout<<"\\nDFT of first sequence:";
  49. k=0;
  50. for(i=0;i<N;i++)
  51. {
  52. real2=imag2=0.0;
  53. for(j=0;j<N;j++)
  54. {
  55.         real2=real2+(xn1[j]*Real[k]);
  56.         imag2=imag2+(xn1[j]*Imag[k]);
  57.         k++;
  58. }
  59. xkr[i]=real2;
  60. xkl[i]=imag2;
  61. }
  62. cout<<"\\n\\nRESULTANT X(K):{";
  63. for(i=0;i<N;i++)
  64. {
  65.     cout<<xkr[i]<<"+"<<xkl[i]<<"j,";
  66. }
  67. cout<<"}";
  68. break;
  69.  
  70. case 2:
  71. cout<<"\\n IDFT matrix:";
  72. for(i=0;i<n;i++)
  73. {
  74. cout<<"\\n";
  75. for(j=0;j<n;j++)
  76. {
  77.         Real1[k]=cos(2*M_PI*i*j/N);
  78.         imag1[k]=1*sin(2*M_PI*i*j/N);
  79.         cout<<"\\t";
  80.         cout<<Real1[k]<<"+("<<imag1[k]<<"j)";
  81.         k++;
  82.     }
  83. }
  84. for(i=0;i<N;i++)
  85. {
  86. cout<<"\\nEnter real part:";
  87. cin>>xkr[i];
  88. cout<<"\\nEnter imag part:";
  89. cin>>xkl[i];
  90. }
  91. cout<<"\\nIDFT of first sequence:";
  92. k=0;
  93. for(i=0;i<N;i++)
  94. {
  95.     real=imag=0.0;
  96.     for(j=0;j<N;j++)
  97.     {
  98.     real=real+((xkr[j]*Real1[k])+xkl[j]*imag1[k]*-1);
  99.     imag=imag+(xkr[j]*imag1[k])+xkl[j]*Real1[k];
  100.     k++;
  101.     }
  102.     xnr[i]=real/N;
  103.     xnl[j]=imag/N;
  104.     }
  105.     cout<<"\\nREASULTANT X(n):{" ;
  106.     for(i=0;i<N;i++)
  107.     {
  108.     cout<<xnr[i]<<"+"<<xnl[j]<<"j,";
  109.     }
  110.     cout<<"}";
  111.     break;
  112.     case 3: exit(0);
  113.     }
  114.     }while(ch<3);
  115.     getch();
  116. }
');