Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- -------------------------------------------------------------------------------------------------
- Assignment No:7
- Title - Compute the Circular Convolution of given two sequences and test it for Linear Convolution.
- -------------------------------------------------------------------------------------------------
- */
- #include<stdio.h>
- #include<iostream.h>
- #include<conio.h>
- #include<math.h>
- # define max 20
- void main()
- {
- int ch,len,m,n,x[max],xn[max],h[max],hn[max],y[max],i,j, k,x2[max],a[max];
- clrscr();
- do
- {
- cout<<"\nEnter your choice :";
- cout<<"\n---------------------------------------------------\n";
- cout<<"\nMENU\n";
- cout<<"\n---------------------------------------------------\n";
- cout<<"\n1. Same result for Linear and Circular Convolution.";
- cout<<"\n2. Different result for Linear and Circular Convolution.";
- cout<<"\n3. Exit";
- cout<<"\n---------------------------------------------------\n";
- cout<<"\nEnter the Choice:";
- cin>>ch;
- switch(ch)
- {
- case 1:
- cout<<"\nLinear Convolution\n";
- cout<<"\n Enter The Number Of Values In X => ";
- cin>>m;
- cout<<"\n Enter The Number Of Values In H => ";
- cin>>n;
- for(i=0;i<max;i++)
- {
- x[i]=h[i]=0;
- }
- cout<<"\n Enter The Input For X[n] => \n";
- for(i=0;i<m;i++)
- cin>>x[i];
- cout<<"\n Enter The Input For H[n] => \n";
- for(i=0;i<m;i++)
- cin>>h[i];
- for(i=m;i<m+n-1;i++)
- x[i]=0;
- for(i=n;i<m+n-1;i++)
- h[i]=0;
- for(i=0;i<m+n-1;i++)
- {
- y[i]=0;
- for(j=0;j<=i;j++)
- y[i]=y[i]+(x[j]*h[i-j]);
- }
- cout<<"\nOUTPUT OF LINEAR CONVOLUTION:";
- for(i=0;i<m+n-1;i++)
- cout<<"\t"<<y[i];
- len=m+n-1;
- for(i=0;i<len;i++)
- {
- xn[i]=hn[i]=0;
- }
- for(i=0;i<m;i++)
- {
- xn[i]=x[i];
- }
- for(i=0;i<n;i++)
- {
- hn[i]=h[i];
- }
- y[0]=0;
- a[0]=hn[0];
- for(j=1;j<len;j++) //folding h(n) to h(-n)//
- a[j]=hn[len-j]; //Circular convolution//
- for(i=0;i<len;i++)
- y[0]+=xn[i]*a[i];
- for(k=1;k<len;k++)
- {
- y[k]=0; //circular shift//
- for(j=1;j<len;j++)
- x2[j]=a[j-1];
- x2[0]=a[len-1];
- for(i=0;i<len;i++)
- {
- a[i]=x2[i];
- y[k]+=xn[i]*x2[i];
- }
- }
- //displaying the result//
- cout<<" \nTHE CIRCULAR CONVOLUTION = ";
- for(i=0;i<len;i++)
- cout<<"\t"<<y[i];
- cout<<"\n"<<"HERE THE OUTPUT OF BOTH SEQUENCES IS SAME";
- break;
- case 2:
- cout<<"\nLinear Convolution\n";
- cout<<"\n Enter The Number Of Values In X => ";
- cin>>m;
- cout<<"\n Enter The Number Of Values In H => ";
- cin>>n;
- cout<<"\n Enter The Input For X[n] => \n";
- for(i=0;i<m;i++)
- cin>>x[i];
- cout<<"\n Enter The Input For H[n] => \n";
- for(i=0;i<m;i++)
- cin>>h[i];
- for(i=m;i<m+n-1;i++)
- x[i]=0;
- for(i=n;i<m+n-1;i++)
- h[i]=0;
- for(i=0;i<m+n-1;i++)
- {
- y[i]=0;
- for(j=0;j<=i;j++)
- y[i]=y[i]+(x[j]*h[i-j]);
- }
- cout<<"\nOUTPUT OF LINEAR CONVOLUTION:";
- for(i=0;i<m+n-1;i++)
- cout<<"\t"<<y[i];
- y[0]=0;
- a[0]=h[0];
- for(j=1;j<n;j++) /*folding h(n) to h(-n)*/
- a[j]=h[n-j]; /*Circular convolution*/
- for(i=0;i<n;i++)
- y[0]+=x[i]*a[i];
- for(k=1;k<n;k++)
- {
- y[k]=0; /*circular shift*/
- for(j=1;j<n;j++)
- x2[j]=a[j-1];
- x2[0]=a[n-1];
- for(i=0;i<n;i++)
- {
- a[i]=x2[i];
- y[k]+=x[i]*x2[i];
- }
- }
- /*displaying the result*/
- cout<<" \nThHE CIRCULAR CONVOLUTION = ";
- for(i=0;i<n;i++)
- cout<<"\t"<<y[i];
- cout<<"\n"<<"HERE THE OUTPUT OF BOTH SEQUENCES IS DIFFERENT";
- break;
- }
- }while(ch!=3);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement