Monday, 13 January 2014

dsp.C Program for magnitude and phase transfer function plot of a given system(difference equation) This program accepts the coefficients of the difference equation and generates the magnitude and phase transfer function plots.

/* C Program for magnitude and phase transfer function plot of a given
   system(difference equation)

   This program accepts the coefficients of the difference equation and
   generates the magnitude and phase transfer function plots.

   Inputs: 1) Number of coefficients of x(n).
  2) Values of coefficients of x(n),i.e. b0,b1,b2,...etc.
  3) Number of coefficients of y(n).
  4) Values of coefficients of y(n),i.e. a1,a2,a3,...etc.

   Outputs: Magnitude and phase transfer function plot H(e^jw) for w=0 to pi.

   Assumptions: This program is written for up to ten coefficients.*/
//============================================================================
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<graphics.h>
void main()
{
 float Realnum,Imagnum,Realden,Imagden;
 float mag[640],phase[640],pi,w,wstep;
 float static a[10],b[10],yMag,yPhase;
 int M,N,i,k,gd,gm;

 clrscr();
 printf("\n This program displays the magnitude and phase transfer function");
 printf("\n plots from given coefficients of difference equation");
 printf("\n\n Enter the number of coefficients of x(n),M=");
 scanf("%d",&M);
 for(i=0;i<M;i++)
 {
  printf("b%d=",i);
  scanf("%f",&b[i]);
 }
 printf("\n\n Enter the number of coefficients of y(n),N=");
 scanf("%d",&N);
 for(i=0;i<N;i++)
 {
  printf("a%d=",i);
  scanf("%f",&a[i]);
 }
 pi=3.1415927;
 wstep=pi/640.0;
 for(k=0;k<640;k++)
 {
  w=w+wstep;
  Realnum=b[0];
  Imagnum=0;
  for(i=0;i<M;i++)
  {
   Realnum=Realnum+b[i]*cos(i*w);
   Imagnum=Imagnum+b[i]*sin(i*w);
  }
  Imagnum=Imagnum*(-1.0);
  Realden=a[0];
  Imagden=0;
  for(i=1;i<=N;i++)
  {
   Realden=Realden+a[i]*cos(i*w);
   Imagden=Imagden+a[i]*sin(i*w);
  }
  Imagden=Imagden*(-1.0);
  mag[k]=sqrt(Realnum*Realnum+Imagnum*Imagnum)/
sqrt(Realden*Realden+Imagden*Imagden);
  phase[k]=atan2(Imagnum,Realnum)-atan2(Imagden,Realden);
 }
 detectgraph(&gd,&gm);
 initgraph(&gd,&gm,"e:\\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]*25;
  putpixel(k,yMag,WHITE);
  yPhase=350-phase[k]*50;
  putpixel(k,yPhase,WHITE);
 }
 outtextxy(500,200,"Magnitude plot");
 outtextxy(500,450,"Phase plot");
 getch();
 closegraph();
}

***op*********



This Program display the magnitude and phase transform of function to
Plot from given coefficients of difference equation

Enter the no. of coefficients of x(n), M=3

b(0)=1

b(1)=-0.25

b(2)=0.5

Enter the no. of coefficients of  y(n), N=1

 A1=1



No comments:

Post a Comment