Monday, 13 January 2014

dsp.Fourier Transform of the sequence and computation of transfer function. This program computes the fourier transform of the sequence x(n) and plots its magnitude and phase transfer function characteristics on the screen.

/* Fourier Transform of the sequence and computation of transfer function.
   This program computes the fourier transform of the sequence x(n) and
   plots its magnitude and phase transfer function characteristics on the
   screen.

   Inputs: 1.Number of samples of x(n)
  2.Values of samples of x(n)
  3.Frequency w at which fourier transform is to be evaluated.

   Outputs: X(w) at given value is displayed.
   Magnitude and phase plots for 0 to pi values of w are
   displayed on screen

   Assumptions: This program is for written real values of sequence x(n)
   Magnitude and phase transfer function plots are computed
   for x(n) for o to pi values of w.
==========================================================================*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>

void main()
{
 float x[100],w,xwreal,xwimag,pi,wstep;
 float static mag[640],phase[640],ymag,yphase;
 int k,N,n,gd,gm;

 clrscr();
 printf("\n Fourier Transform and Computation of transfer function.\n");
 printf("\n Enter the number samples in the Sequence x(n): ");
 scanf("%d",&N);
 printf("\n\n Enter the samples of Sequence x(n):\n");

 for(n=0;n<N;n++)
 {
  printf("\n x(%d)=",n);
  scanf("%f",&x[n]);
 }

 printf("\n Enter the Frequency W at which fourier transform is");
 printf("\n\n to be evaluated w(between 0 to pi)= ");
 scanf("%f",&w);
 xwreal=xwimag=0.0;

 for(n=0;n<N;n++)
 {
  xwreal=xwreal+x[n]*cos(w*n);
  xwimag=xwimag+x[n]*sin(w*n);
 }
 xwimag=xwimag*(-1.0);

 printf("\n The value of fourier transform is:");
 printf("\n\n x(w=%f)= %f+j(%f)",w,xwreal,xwimag);
 printf("\n\n Press any key to see magnitude and phase.....");
 getch();

 w=0.0;
 pi=22.0/7.0;
 wstep=pi/640.0;

 for(k=0;k<640;k++)
 {
  xwreal=xwimag=0.0;
  w=w+wstep;
  for(n=0;n<N;n++)
  {
   xwreal=xwreal+x[n]*cos(w*n);
   xwimag=xwimag+x[n]*sin(w*n);
  }
  xwimag=xwimag*(-1.0);
  mag[k]=sqrt(xwreal*xwreal+xwimag*xwimag);
  phase[k]=atan2(xwimag,xwreal);
 }

 detectgraph(&gd,&gm);
 initgraph(&gd,&gm,"c:\\tc\\bgi");
 setlinestyle(DOTTED_LINE,1,1);
 line(0,250,640,250);
 line(0,350,640,350);

 for(k=0;k<640;k++)
 {
  ymag=250-mag[k]*200;
  putpixel(k,ymag,YELLOW);
  yphase=350-phase[k]*50;
  putpixel(k,yphase,GREEN);
 }

 outtextxy(500,260,"Magnitude plot");
 outtextxy(500,450,"Phase plot");
 getch();
 closegraph();
}

***op***

                           Fourier Transform and Computation of transfer function.


Enter the number samples in the Sequence x (n): 1


 Enter the samples of Sequence x (n):

 x (0)=1

 Enter the Frequency w at which Fourier transform is

 to be evaluated w(between 0 to pi)= pi

 The value of Fourier transform is:

 x (w=-0.000000)= 1.000000+j(-0.000000)

 Press any key to see magnitude and phase.....









                                                                                                 

No comments:

Post a Comment