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: }