Monday, 13 January 2014

dsp.Linear Convolution of two sequences


/* Linear Convolution of two sequences
  This program computes the linear Convolution of two sequences x(n) and h(n)

  INPUTS:1) Number of samples in x(n) (casual sequence)
2) Samples of x(n) in the form x[0],x[1],x[2],...x[n-1]
3) Number of samples in h(n) (casual sequence)
4) Samples of h(n) in the form h[0],h[1],h[2],...h[n-1]

  OUTPUT: Convolution sequence of x(n) and h(n)   */

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

void main()
{
 int n,k,N,M;
 float h[20],x[20],y[20],Total;
 char ch;
 clrscr();

 do
 {
  printf("\n\t\t Linear Convolution ");
  printf("\n\n Enter the number of samples in h(n)=");
  scanf("%d",&N);
  printf("\n Enter the sequence h(n)=");

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

  printf("\n\n Enter the number of samples in x(n)=");
  scanf("%d",&M);
  printf("\n Enter the sequence x(n)=");

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

  printf("\n The result of Convolution (y(n)=x(n)*h(n))is:");
  for(n=0;n<(N+M-1);n++)
  {
   Total=0.0;
   for(k=0;k<M;k++)
   {
    if(n<k || (n-k)>=N)
    continue;
    Total+=x[k]*h[n-k];
   }
   y[n]=Total;

   printf("\n y[%d]=%f",n,y[n]);
  }
  printf("\n DO YOU WANT TO CONTINUE?:(Y/N) ");
  ch=getche();
 }while(ch=='y'||ch=='Y') ;
 getch();
}


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

   Linear Convolution

 Enter the number of samples in h(n)=3

 Enter the sequence h(n)=

 h[0]=1


 h[1]=2


 h[2]=3


 Enter the number of samples in x(n)=3

 Enter the sequence x(n)=

 x[0]=3


 x[1]=2


 x[2]=1

 The result of Convolution (y(n)=x(n)*h(n))is:
 y[0]=3.000000
 y[1]=8.000000
 y[2]=14.000000
 y[3]=8.000000
 y[4]=3.000000
 DO YOU WANT TO CONTINUE?:(Y/N)N















































No comments:

Post a Comment