document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2. -------------------------------------------------------------------------------------------------
  3.  Assignment no : 11
  4.  Title : Obtain the Fourier transform of different window function. Plot the magnitude and phase Spectrum
  5. -------------------------------------------------------------------------------------------------
  6. */
  7.  
  8. #include<stdio.h>
  9. #include<conio.h>
  10. #include<math.h>
  11. #include<graphics.h>
  12.  
  13. void main()
  14. {
  15.       int M,n,ch,i;
  16.       float x[30],Xreal[30],Ximg[30],w,mag[30],ph[30],inter;
  17.       int gd=DETECT,gm;
  18.       clrscr();
  19.  
  20.       printf("\\nEnter the value of M:");
  21.       scanf("%d",&M);
  22.  
  23.       printf("\\n\\nWINDOWS:");
  24.       printf("\\n1.Rectangular window");
  25.       printf("\\n2.Hamming window");
  26.       printf("\\n3.Hanning window");
  27.       printf("\\n4.Blackman window");
  28.       printf("\\n5.Triangular window");
  29.       printf("\\n6.Exit");
  30.       printf("\\nEnter your choice:");
  31.       scanf("%d",&ch);
  32.       switch(ch)
  33.       {
  34.        case 1: //rectangular
  35.            for(n=0;n<M;n++)
  36.            {
  37.                      x[n]=1;
  38.            }
  39.           break;
  40.  
  41.       case 2:  //hamming
  42.            for(n=0;n<M;n++)
  43.            {
  44.                 x[n]=0.54+(0.46*cos(2*M_PI*n/(M-1)));
  45.            }
  46.          break;
  47.      case 3:  //hanning
  48.           for(n=0;n<M;n++)
  49.           {
  50.                  x[n]=(1-(cos(2*M_PI*n/(M-1))))*0.5;
  51.           }
  52.         break;
  53.  
  54.     case 4:   //Blackman
  55.           for(n=0;n<M;n++)
  56.           {
  57.              x[n]=0.42+(0.5*cos(2*M_PI*n/(M-1)))+(0.08*cos(4*M_PI*n/(M-1)));
  58.           }
  59.         break;
  60.  
  61.        case 5:   //tringular window
  62.           for(n=0;n<M;n++)
  63.          {
  64.                 x[n]=1-(2*n)/M-1;
  65.          }
  66.        break;
  67.        }
  68.        printf("\\n\\nThe sequence x(n) :");
  69.        for(n=0;n<M;n++)
  70.        {
  71.            printf("\\nx(%d):",n);
  72.            printf("%f",x[n]);
  73.        }
  74.  
  75.        // fourier transform
  76.        printf("\\n\\nFourier transform is:");
  77.        i=0;
  78.        inter=M_PI/30;
  79.        for(w=0;w<M_PI;w=w+inter)
  80.        {     Xreal[i]=Ximg[i]=0;
  81.              for(n=0;n<M;n++)
  82.              {
  83.                 Xreal[i]+=x[n]*cos(w*n);
  84.                 Ximg[i]-=x[n]*sin(w*n);
  85.              }
  86.  
  87.              printf("\\nX(w):");
  88.              if(Ximg[i]<0)
  89.              printf("%f%f",Xreal[i],Ximg[i]);
  90.              else
  91.              printf("%f+%f",Xreal[i],Ximg[i]);
  92.  
  93.              mag[i]=sqrt((Xreal[i]*Xreal[i])+(Ximg[i]*Ximg[i]));
  94.              ph[i]=atan(Ximg[i]/Xreal[i]);
  95.              i++;
  96.        }
  97.      //plots
  98.         initgraph(&gd,&gm,"c:\\tc\\bgi");
  99.      line(100,20,100,200);
  100.      outtextxy(10,20,"Magnitude");
  101.      line(100,250,100,400);
  102.      outtextxy(10,250,"Phase");
  103.      line(100,200,400,200);
  104.      outtextxy(400,200,"W");
  105.      line(100,400,400,400);
  106.      outtextxy(400,400,"W");
  107.      i=0;
  108.      for(w=0;w<=M_PI;w=w+inter)
  109.      {
  110.      putpixel((w*100)+100,(200-(mag[i]*10)),15);
  111.      putpixel((w*100)+100,(400-(ph[i]*10)),15);
  112.      i++;
  113.      }
  114.      getch();
  115.      closegraph();
  116. }
');