/*
-------------------------------------------------------------------------------------------------
Assignment no : 11
Title : Obtain the Fourier transform of different window function. Plot the magnitude and phase Spectrum
-------------------------------------------------------------------------------------------------
*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
int M,n,ch,i;
float x[30],Xreal[30],Ximg[30],w,mag[30],ph[30],inter;
int gd=DETECT,gm;
clrscr();
printf("\\nEnter the value of M:");
scanf("%d",&M);
printf("\\n\\nWINDOWS:");
printf("\\n1.Rectangular window");
printf("\\n2.Hamming window");
printf("\\n3.Hanning window");
printf("\\n4.Blackman window");
printf("\\n5.Triangular window");
printf("\\n6.Exit");
printf("\\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1: //rectangular
for(n=0;n<M;n++)
{
x[n]=1;
}
break;
case 2: //hamming
for(n=0;n<M;n++)
{
x[n]=0.54+(0.46*cos(2*M_PI*n/(M-1)));
}
break;
case 3: //hanning
for(n=0;n<M;n++)
{
x[n]=(1-(cos(2*M_PI*n/(M-1))))*0.5;
}
break;
case 4: //Blackman
for(n=0;n<M;n++)
{
x[n]=0.42+(0.5*cos(2*M_PI*n/(M-1)))+(0.08*cos(4*M_PI*n/(M-1)));
}
break;
case 5: //tringular window
for(n=0;n<M;n++)
{
x[n]=1-(2*n)/M-1;
}
break;
}
printf("\\n\\nThe sequence x(n) :");
for(n=0;n<M;n++)
{
printf("\\nx(%d):",n);
printf("%f",x[n]);
}
// fourier transform
printf("\\n\\nFourier transform is:");
i=0;
inter=M_PI/30;
for(w=0;w<M_PI;w=w+inter)
{ Xreal[i]=Ximg[i]=0;
for(n=0;n<M;n++)
{
Xreal[i]+=x[n]*cos(w*n);
Ximg[i]-=x[n]*sin(w*n);
}
printf("\\nX(w):");
if(Ximg[i]<0)
printf("%f%f",Xreal[i],Ximg[i]);
else
printf("%f+%f",Xreal[i],Ximg[i]);
mag[i]=sqrt((Xreal[i]*Xreal[i])+(Ximg[i]*Ximg[i]));
ph[i]=atan(Ximg[i]/Xreal[i]);
i++;
}
//plots
initgraph(&gd,&gm,"c:\\tc\\bgi");
line(100,20,100,200);
outtextxy(10,20,"Magnitude");
line(100,250,100,400);
outtextxy(10,250,"Phase");
line(100,200,400,200);
outtextxy(400,200,"W");
line(100,400,400,400);
outtextxy(400,400,"W");
i=0;
for(w=0;w<=M_PI;w=w+inter)
{
putpixel((w*100)+100,(200-(mag[i]*10)),15);
putpixel((w*100)+100,(400-(ph[i]*10)),15);
i++;
}
getch();
closegraph();
}