Monday, 13 January 2014

dsp.CIERCULAR CONVOLUTION USING DFT AND IDFT

/*-------------------CIERCULAR CONVOLUTION USING DFT AND IDFT---------------
 This program computes the circular convolution of two causal sequences x(n)
 and h(n) using DFT and IDFT.

 Inputs: 1.Length of the sequence i.e.N
2.Samples of the two sequences to be convolved

 Outputs:  Circular convolution sequence of x(n) and h(n)

 Assumptions: The given sequences are real and causal.
============================================================================*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
 float h[20],x[20],y[20];
 float h_real[20],h_imag[20],x_real[20],x_imag[20];
 float N,M,n,k,m,y_real[20],y_imag[20];

 clrscr();
 printf("\t\tCircular Convolution using DFT and IDFT\n\n");

 printf("\n Enter the value of N=");
 scanf("%f",&N);

 printf("\n Enter the sequence h(n):\n");
 for(n=0;n<N;n++)
 {
  printf("\n h[%1.0f]= ",n);
  scanf("%f",&h[n]);
 }

 printf("\n\n Enter the Sequence x(n):\n");
 for(n=0;n<N;n++)
 {
  printf("\n x[%1.0f]= ",n);
  scanf("%f",&x[n]);
 }

 for(k=0;k<N;k++)
 {
  h_real[k]=h_imag[k]=0.0;
  for(n=0;n<N;n++)
  {
   h_real[k]=h_real[k]+h[n]*cos((2*M_PI*k*n)/N);
   h_imag[k]=h_imag[k]+h[n]*sin((2*M_PI*k*n)/N);
  }
  h_imag[k]=h_imag[k]*(-1.0);
 }

 for(k=0;k<N;k++)
 {
  x_real[k]=x_imag[k]=0.0;
  for(n=0;n<N;n++)
  {
   x_real[k]=x_real[k]+x[n]*cos((2*M_PI*k*n)/N);
   x_imag[k]=x_imag[k]+x[n]*sin((2*M_PI*k*n)/N);
  }
  x_imag[k]=x_imag[k]*(-1.0);
 }

 for(k=0;k<N;k++)
 {
  y_real[k]=h_real[k]*x_real[k]-h_imag[k]*x_imag[k];
  y_imag[k]=h_real[k]*x_imag[k]+h_imag[k]*x_real[k];
 }

 for(n=0;n<N;n++)
 {
  y[n]=0;
  for(k=0;k<N;k++)
  {
   y[n]=y[n]+y_real[k]*cos((2*M_PI*k*n)/N)
   -y_imag[k]*sin((2*M_PI*k*n)/N);
  }
  y[n]=y[n]/N;
 }

 printf("\n The Circular of convolution is......");
 for(n=0;n<N;n++)
  printf("\n\n y[%1.0f]= %f",n,y[n]);
 getch();
}
***op***
          Circular Convolution using DFT and IDFT


 Enter the value of N=4

 Enter the sequence h(n):

 h[0]= 0

 h[1]= 1

 h[2]= 2

 h[3]= 3


 Enter the Sequence x(n):

 x[0]= 2

 x[1]= 1

 x[2]= 1

 x[3]= 2

 The Circular of convolution is......

 y[0]= 7.000000

 y[1]= 9.000000

 y[2]= 11.000000

 y[3]= 9.000000

No comments:

Post a Comment