Generate Waves of Sine , Cosine and Exponential Signals

Posted by fasxxzczc on Wednesday, 21 March 2012

Generate Waves of Sine , Cosine and Exponential Signals

Problem Statement : Write a C program to generate samples of sine, Cosine and exponential signals at specified sampling frequency and signal parameters


1:  //     Generation of samples of Cosine,Sine & Exponential Waves  
2:  #include<dos.h>  
3:  #include<graphics.h>  
4:  #include<math.h>  
5:  #include<iostream.h>  
6:  #include<stdio.h>  
7:  #include<conio.h>  
8:  #include<stdlib.h>  
9:  void draw_seq(int sam,int choice)  //a is number of samples  
10:  {  
11:   int i,j,size,k;  
12:   float F,Fs,a;  
13:   setcolor(15);  
14:   line(getmaxx()/2,0,getmaxx()/2,getmaxy());   //drawing axes  
15:   line(0,getmaxy()/2,getmaxx(),getmaxy()/2);  
16:   setcolor(14);  
17:   setfillstyle(SOLID_FILL,14);  
18:   switch(choice)  
19:   {  
20:   case 1 :  
21:       outtextxy(getmaxx()-103,20,"Sine Wave");  
22:       cout<<"Input Sampling Frequency::";  
23:       cin>>Fs;  
24:       cout<<"Input Analog Frequency::";  
25:       cin>>F;  
26:       for(i=0;i<=sam;i++)  
27:       {  
28:        float x=sin(2*3.14*(F/Fs)*i);  
29:        line(getmaxx()/2+i*10,getmaxy()/2-x*30,getmaxx()/2+i*10,getmaxy()/2);  
30:        pieslice(getmaxx()/2+(i*10),getmaxy()/2-x*30,0,360,2);  
31:       }  
32:       break;  
33:   case 2 :  
34:       outtextxy(getmaxx()-103,20,"Cosine Wave");  
35:       cout<<"Input Sampling Frequency::";  
36:       cin>>Fs;  
37:       cout<<"Input Analog Frequency::";  
38:       cin>>F;  
39:       for(i=0;i<=sam;i++)  
40:       {  
41:        float x=cos(2*3.14*(F/Fs)*i);  
42:        line(getmaxx()/2+i*10,getmaxy()/2-x*30,getmaxx()/2+i*10,getmaxy()/2);  
43:        pieslice(getmaxx()/2+(i*10),getmaxy()/2-x*30,0,360,2);  
44:       }  
45:       break;  
46:   case 3 :  
47:       outtextxy(getmaxx()-123,20,"Exponential Wave");  
48:       cout<<"Input Constant k::";  
49:       cin>>k;  
50:       cout<<"Input Magnitude a::";  
51:       cin>>a;  
52:       for(i=0;i<=sam;i++)  
53:       {  
54:        float x=k*pow(a,i);  
55:        line(getmaxx()/2+i*10,getmaxy()/2-x*40,getmaxx()/2+i*10,getmaxy()/2);  
56:        pieslice(getmaxx()/2+(i*10),getmaxy()/2-x*40,0,360,2);  
57:       }  
58:       break;  
59:   }  
60:   getch();  
61:  }  
62:  void main(void)  
63:  {  
64:   int gd=DETECT,gm;  
65:   int sample,choice;  
66:   while(1)  
67:   {  
68:   initgraph(&gd,&gm,"e:\\tc\\bgi");  
69:   cleardevice();  
70:   cout<<"\n\t\tGeneration of Standard Sequences\n\t1.Sine Wave";  
71:   cout<<"\n\t2.Cosine Wave\n\t3.Exponential\n\t4.Exit";  
72:   setcolor(GREEN);  
73:   outtextxy(10,100,"Enter Ur Choice :");  
74:   gotoxy(32,7);  
75:   cin>>choice;  
76:   if(choice==4)  
77:       exit(0);  
78:   setcolor(GREEN);  
79:   outtextxy(10,120,"Enter Number of SAMPLE :");  
80:   gotoxy(32,8);  
81:   cin>>sample;  
82:   if(sample==0)  
83:   {     setcolor(14);  
84:       outtextxy(getmaxx()/2-150,getmaxy()/2-10,"No Graph Exists for ZERO Samples");  
85:   }  
86:   switch(choice)  
87:   {  
88:    case 1:  
89:       draw_seq(sample,1);  
90:       break;  
91:    case 2:  
92:       draw_seq(sample,2);  
93:       break;  
94:    case 3:  
95:       draw_seq(sample,3);  
96:       break;  
97:   }  
98:   }  
99:  }  

{ 0 comments... read them below or add one }

Post a Comment