N Point Circular Convolution of sequences

Posted by fasxxzczc on Wednesday, 21 March 2012

 N Point Circular Convolution of  sequences

Problem Statement : Find the N point circular convolution of given two sequences. Test it for linear convolution





1:  #include<graphics.h>  
2:  #include<iostream.h>  
3:  #include<conio.h>  
4:  #include<stdlib.h>  
5:  void main(void)  
6:  {  
7:   int sample,impulse,sam[10],imp[10],out[10][10],outp[10]={0},i;  
8:   int gd=DETECT,gm,maxx,maxy,j;  
9:   char str[5];  
10:   initgraph(&gd,&gm,"c:\\tc\\bgi");  
11:   maxx=getmaxx();   // Returns maximum x or y screen coordinate  
12:   maxy=getmaxy();  
13:   cout<<"\n\tEnter the number of element in sample sequence::";  
14:   cin>>sample;  
15:   cout<<"\n\tEnter the number of element in impulse sequence::";  
16:   cin>>impulse;           //input impulse & sample sequence  
17:   cout<<"\n\tEnter the sample sequence";  
18:   for(i=0;i<sample;i++)  
19:   cin>>sam[i];  
20:   cout<<"\n\tEnter the impusle sequence";  
21:   for(i=0;i<impulse;i++)  
22:   cin>>imp[i];  
23:   for(i=0;i<sample;i++)  
24:   for(j=0;j<impulse;j++)  
25:    out[i][j]=sam[i]*imp[j];  
26:   for(i=0;i<impulse;i++)  
27:   for(j=0;j<sample;j++)  
28:    outp[j+i]=outp[j+i]+out[j][i];  
29:   int samp=i+j-1;  
30:   setcolor(RED);  
31:   line(0,maxy/2,maxx,maxy/2);  
32:   line(maxx/2,0,maxx/2,maxy);  
33:   setcolor(GREEN);  
34:   for(i=0;i<samp;i++)  
35:   {  
36:    line(maxx/2+i*30,maxy/2,maxx/2+i*30,maxy/2-outp[i]*5);  
37:    moveto(maxx/2+i*30,maxy/2-outp[i]*5-10);  
38:    itoa(outp[i],str,10);        //converts integer to string  
39:    outtext(str);  
40:   }  
41:   getch();  
42:  }  

Using Graph

1:  #include<graphics.h>  
2:  #include<iostream.h>  
3:  #include<conio.h>  
4:  #include<stdlib.h>  
5:  void main(void)  
6:  {  
7:   int sample,impulse,sam[10]={0},imp[10]={0},outp[10]={0},i;  
8:   int gd=DETECT,gm,maxx,maxy,j,k,sum;  
9:   char str[5];  
10:   initgraph(&gd,&gm,"e:\\tc\\bgi");  
11:   maxx=getmaxx();   // Returns maximum x or y screen coordinate  
12:   maxy=getmaxy();  
13:   cout<<"\n\tEnter the number of element in sample sequence::";  
14:   cin>>sample;  
15:   cout<<"\n\tEnter the number of element in impulse sequence::";  
16:   cin>>impulse;           //input impulse & sample sequence  
17:   cout<<"\n\tEnter the sample sequence";  
18:   for(i=0;i<sample;i++)  
19:   cin>>sam[i];  
20:   cout<<"\n\tEnter the impusle sequence";  
21:   for(i=0;i<impulse;i++)  
22:   cin>>imp[i];  
23:   for(k=0;k<(sample+impulse-1);k++)  
24:   {  
25:   cleardevice();  
26:   setcolor(MAGENTA);  
27:   outtextxy(maxx/2+40,0,"Sample Sequence x(n)");  
28:   outtextxy(maxx/2+40,165,"Impulse Response h(-n+k)");  
29:   outtextxy(maxx/2+40,330,"x(k)*h(-k+n)");  
30:   setcolor(RED);  
31:   line(maxx/2,0,maxx/2,maxy);  
32:   line(0,maxy/6,maxx,maxy/6);  
33:   line(0,maxy/2,maxx,maxy/2);  
34:   line(0,5*maxy/6,maxx,5*maxy/6);  
35:   setlinestyle(DOTTED_LINE,1,1);  
36:   setcolor(GREEN);  
37:   line(0,maxy/3,maxx,maxy/3);  
38:   line(0,2*maxy/3,maxx,2*maxy/3);  
39:   setlinestyle(SOLID_LINE,1,3);  
40:   for(i=0;i<sample;i++)  
41:   {  
42:    line(maxx/2+i*30,maxy/6,maxx/2+i*30,maxy/6-sam[i]*2);  
43:    if(sam[i]>=0)  
44:        moveto(maxx/2+i*30,maxy/6-sam[i]*2-10);  
45:    else  
46:         moveto(maxx/2+i*30,maxy/6-sam[i]*2+10);  
47:    itoa(sam[i],str,10);        //converts integer to string  
48:    outtext(str);  
49:   }  
50:   for(i=impulse-1;i>=0;i--)  
51:   {  
52:    line(maxx/2-(i-k)*30,maxy/2,maxx/2-(i-k)*30,maxy/2-imp[i]*2);  
53:    if(imp[i]>=0)  
54:        moveto(maxx/2-(i-k)*30,maxy/2-imp[i]*2-10);  
55:    else  
56:         moveto(maxx/2-(i-k)*30,maxy/2-imp[i]*2+10);  
57:    itoa(imp[i],str,10);        //converts integer to string  
58:    outtext(str);  
59:   }  
60:   int temp=k;  
61:   sum=0;  
62:   for(i=k;i>=0;i--)  
63:   {  
64:    sum+=sam[i]*imp[temp-i];  
65:    line(maxx/2+i*30,5*maxy/6,maxx/2+i*30,5*maxy/6-sam[i]*imp[temp-i]*2);  
66:    if(outp[i]>=0)  
67:        moveto(maxx/2+i*30,5*maxy/6-sam[i]*imp[temp-i]*2-10);  
68:    else  
69:         moveto(maxx/2+i*30,5*maxy/6-sam[i]*imp[temp-i]*2+10);  
70:    itoa(sam[i]*imp[temp-i],str,10);        //converts integer to string  
71:    outtext(str);  
72:   }  
73:   outp[k]=sum;  
74:   outtextxy(330,maxy-10,"y(");  
75:   itoa(k,str,10);        //converts integer to string  
76:   outtextxy(330+15,maxy-10,str);  
77:   outtextxy(330+30,maxy-10,")::");  
78:   itoa(outp[k],str,10);        //converts integer to string  
79:   outtextxy(390,maxy-10,str);  
80:   setcolor(YELLOW);  
81:   outtextxy(20,maxy-10,"Press Any Key to Continue::");  
82:   getch();  
83:   }  
84:   cleardevice();  
85:   outtextxy(maxx/2+20,20,"Output y(n)");  
86:   setcolor(RED);  
87:   setlinestyle(SOLID_LINE,1,1);  
88:   line(maxx/2,0,maxx/2,maxy);  
89:   line(0,maxy/2,maxx,maxy/2);  
90:   setcolor(GREEN);  
91:   setlinestyle(SOLID_LINE,1,3);  
92:   for(i=0;i<(sample+impulse-1);i++)  
93:   {  
94:    line(maxx/2+i*30,maxy/2,maxx/2+i*30,maxy/2-outp[i]*2);  
95:    if(sam[i]>=0)  
96:        moveto(maxx/2+i*30,maxy/2-outp[i]*2-10);  
97:    else  
98:         moveto(maxx/2+i*30,maxy/2-outp[i]*2+10);  
99:    itoa(outp[i],str,10);        //converts integer to string  
100:    outtext(str);  
101:   }  
102:   getch();  
103:   closegraph();  
104:  }  


More aboutN Point Circular Convolution of sequences

N point DFT or IDFT of sequence

Posted by fasxxzczc

N point DFT  or IDFT of  sequence


Problem Statement :Find the N point DFT / IDFT of the given sequence x (n) .Plot the magnitude spectrum|X(K)| Vs K. (Analyze the output for different N and the same input sequence x(n). Also observe the periodicity and symmetry property) .


1:  #include<stdio.h>  
2:  #include<conio.h>  
3:  #include<complex.h>  
4:  void main()  
5:  {  
6:     float x[20],pi=22.0/7.0;  
7:     complex X[20];  
8:     int k,n,N;  
9:     clrscr();  
10:     printf("\t\t\tDFT Fourier Transform\n");  
11:     printf("\nEnter the number samples in the sequence x(n), N = ");  
12:     scanf("%d",&N);  
13:     printf("\nEnter the samples of sequence x(n)\n");  
14:     for(n = 0; n < N; n++)  
15:     {  
16:       printf("x(%d) = ",n);  
17:       scanf("%f",&x[n]);  
18:     }  
19:     for(k = 0; k < N; k++)  
20:     {  
21:       X[k]=complex(0.0,0.0);  
22:       for(n = 0; n < N; n++)  
23:       {  
24:        complex temp=complex(0.0,-2*pi*k*n/N);  
25:        X[k] = X[k] + x[n]*exp(temp);  
26:       }  
27:     }  
28:     printf("\nThe %d point DFT of given sequence is...",N);  
29:     printf("\n\tReal X(k)\t\tImaginary X(k)");  
30:     for(k = 0; k < N; k++)  
31:      printf("\nX(%d) = %3.6f\t\t %3.4f",k,real(X[k]),imag(X[k]));  
32:     getch();  
33:  }  
More aboutN point DFT or IDFT of sequence

N-Point FFT Algorithm to find DFT or IDFT

Posted by fasxxzczc

N-Point FFT Algorithm to find DFT or IDFT


Problem Statement : Implement the N-point radix-2 DIT or DIF FFT algorithm to find DFT or IDFT of given sequence x (n). (Analyze the output for different N Program should work for any value of N (generalized)) .



1:  #include<conio.h>  
2:  #include<stdio.h>  
3:  #include<complex.h>  
4:  #include<stdlib.h>  
5:  complex data[64];  
6:  void main()  
7:  {  
8:     int ind(int n,int i);  
9:     void split(int N,int n);  
10:     double x,y;  
11:     int N,n,i,j,k,ch;  
12:     clrscr();  
13:     cout<<"    Radix-2 DIT FFT algorithm FOR DFT & IDFT\n\n";  
14:     cout<<"Enter the number of samples in the sequence x(n), N = ";  
15:     cin>>N;           // number of samples in x(n)  
16:     cout<<"\nEnter the samples of sequence x(n)";  
17:     for(n=0;n<N;n++)  
18:     {  
19:      cout<<"\nReal x("<<n<<") = ";    // enter values of samples of x(n)  
20:      cin>>x;  
21:      cout<<"Imag x("<<n<<") = ";    // enter values of samples of x(n)  
22:      cin>>y;  
23:      data[n]=complex(x,y);  
24:     }  
25:     cout<<"\n\t1.DFT\n\t2.IDFT\n\t3.EXIT\n\n\tEnter Ur Choice::";  
26:     cin>>ch;  
27:     switch(ch)  
28:     {  
29:     case 1:  
30:          n=log(N)/log(2);  
31:          split(N,n);  
32:          i=0;  
33:          while(i!=n)  
34:          {  
35:          int p1=pow(2,i),p2=pow(2,i+1),h=(N/p2);  
36:          for(j=0;j<h;j++)  
37:          {  
38:            for(k=0;k<p1;k++)  
39:            {  
40:             complex temp,temp1,temp2;  
41:             temp=complex(0.0,-(44.0*k)/(7.0*p2));  
42:             temp=exp(temp);  
43:             temp1=data[p2*j+k]+temp*data[p2*j+k+p1];  
44:             temp2=data[p2*j+k]-temp*data[p2*j+k+p1];  
45:             data[p2*j+k]=temp1;  
46:             data[p2*j+k+p1]=temp2;  
47:            }  
48:          }  
49:          i++;  
50:          }  
51:          break;  
52:     case 2:  
53:          n=log(N)/log(2);  
54:          i=n-1;  
55:          while(i>=0)  
56:          {  
57:          int p1=pow(2,i),p2=pow(2,i+1),h=(N/p2);  
58:          for(j=0;j<h;j++)  
59:          {  
60:            for(k=0;k<p1;k++)  
61:            {  
62:             complex temp,temp1,temp2;  
63:             temp=complex(0.0,(44.0*k)/(7.0*p2));  
64:             temp=exp(temp);  
65:             temp1=(data[p2*j+k]+data[p2*j+k+p1]);  
66:             temp2=((data[p2*j+k]-data[p2*j+k+p1])*temp);  
67:             if(i==n-1)  
68:             {  
69:             temp1=temp1/N;  
70:             temp2=temp2/N;  
71:             }  
72:             data[p2*j+k]=temp1;  
73:             data[p2*j+k+p1]=temp2;  
74:            }  
75:          }  
76:          i--;  
77:          }  
78:          split(N,n);  
79:          break;  
80:     case 3:  
81:         exit(0);  
82:     }  
83:     clrscr();  
84:     cout<<"\nReal(z)";  
85:     gotoxy(30,2);  
86:     cout<<"Imag(z)\n";  
87:     for(i=0;i<N;i++)  
88:     {  
89:     printf("%1.2lf",real(data[i]));  
90:     gotoxy(30,3+i);  
91:     printf("%1.2lf\n",imag(data[i]));  
92:     }  
93:     getch();  
94:  }  
95:  int ind(int i,int n)  
96:  {  
97:   int j,temp,num=0;  
98:   for(j=0;j<n;j++)  
99:   {  
100:   temp=i&(int)(pow(2,j));  
101:   if(temp!=0)  
102:    num+=pow(2,n-j-1);  
103:   }  
104:   return num;  
105:  }  
106:  void split(int N,int n)  
107:  {  
108:     int index,i;  
109:     complex temp;  
110:     for(i=0;i<N;i++)  
111:     {  
112:      index=ind(i,n);  
113:      if(index>i)  
114:      {  
115:       temp=data[i];  
116:       data[i]=data[index];  
117:       data[index]=temp;  
118:      }  
119:     }  
120:  }  
More aboutN-Point FFT Algorithm to find DFT or IDFT

Pole Zero Plot from System function H(Z) Expressed as Rational Function

Posted by fasxxzczc

Pole Zero Plot from  System function H(Z) Expressed as Rational Function


Problem Statement :Draw a pole zero plot from a given system function H(Z) expressed as rational function. (Display pole zero table and pole zero plot) .



1:  #include<iostream.h>  
2:  #include<conio.h>  
3:  #include<stdio.h>  
4:  void main(void)  
5:  {  
6:   int pol[5]={0},zer[5]={0},num1,num2,a[6],b[6],i;  
7:   clrscr();  
8:   cout<<"\n\tInput number of poles for x(n)(MAX 5)::";  
9:   cin>>num1;  
10:   cout<<"\n\tInput Values::\n";  
11:   for(i=1;i<=num1;i++)  
12:   {  
13:   cout<<"\n\tp["<<i<<"]::";  
14:   cin>>pol[i];  
15:   }  
16:   cout<<"\n\tInput number of zeros for x(n)(MAX 5)::";  
17:   cin>>num2;  
18:   cout<<"\n\tInput Values::\n";  
19:   for(i=1;i<=num2;i++)  
20:   {  
21:   cout<<"\n\tz["<<i<<"]::";  
22:   cin>>zer[i];  
23:   }  
24:   a[0]=b[0]=1;  
25:   a[1]=-(pol[1]+pol[2]+pol[3]+pol[4]+pol[5]);  
26:   a[2]=pol[1]*(pol[2]+pol[3]+pol[4]+pol[5])+pol[2]*(pol[3]+pol[4]+pol[5])  
27:     +pol[3]*(pol[4]+pol[5])+pol[4]*pol[5];  
28:   a[3]=-(pol[1]*pol[2]*(pol[3]+pol[4]+pol[5])+pol[2]*pol[3]*(pol[4]+pol[5])  
29:     +pol[4]*pol[5]*(pol[1]+pol[2]+pol[3])+pol[1]*pol[3]*(pol[4]+pol[5]));  
30:   a[4]=pol[1]*pol[2]*pol[3]*(pol[4]+pol[5])+pol[3]*pol[4]*pol[5]*(pol[1]+  
31:     pol[2])+pol[1]*pol[2]*pol[4]*pol[5];  
32:   a[5]=-(pol[1]*pol[2]*pol[3]*pol[4]*pol[5]);  
33:   b[1]=-(zer[1]+zer[2]+zer[3]+zer[4]+zer[5]);  
34:   b[2]=zer[1]*(zer[2]+zer[3]+zer[4]+zer[5])+zer[2]*(zer[3]+zer[4]+zer[5])  
35:     +zer[3]*(zer[4]+zer[5])+zer[4]*zer[5];  
36:   b[3]=-(zer[1]*zer[2]*(zer[3]+zer[4]+zer[5])+zer[2]*zer[3]*(zer[4]+zer[5])  
37:     +zer[4]*zer[5]*(zer[1]+zer[2]+zer[3])+zer[1]*zer[3]*(zer[4]+zer[5]));  
38:   b[4]=zer[1]*zer[2]*zer[3]*(zer[4]+zer[5])+zer[3]*zer[4]*zer[5]*(zer[1]+  
39:     zer[2])+zer[1]*zer[2]*zer[4]*zer[5];  
40:   b[5]=-(zer[1]*zer[2]*zer[3]*zer[4]*zer[5]);  
41:   cout<<"\n\nCoefficient for x(n)::";  
42:   for(i=0;i<=num1;i++)  
43:       cout<<b[i]<<",";  
44:   cout<<"\b ";  
45:   cout<<"\n\nCoefficient for y(n)::";  
46:   for(i=0;i<=num2;i++)  
47:       cout<<a[i]<<",";  
48:   cout<<"\b ";  
49:   getch();  
50:  }  
More aboutPole Zero Plot from System function H(Z) Expressed as Rational Function