Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #include "graphics.h"
- #include <charconv>
- using namespace std;
- const int W=1200,H=700;
- const double PI = acos(-1);
- float a0 = 0, degree, fp;
- int p, t;
- vector<float>v;
- vector<pair<float,float>>ab;
- float rnd(float var)
- {
- float value = (int)(var * 100 + .5);
- return (float)value / 100;
- }
- void drawline(int x,int y,int x2,int y2,int color,int textx,int texty,char* name)
- {
- moveto(x,y);
- setcolor(color);
- lineto(x2,y2);
- outtextxy(textx,texty,name);
- return;
- }
- void scalex(int step)
- {
- for(int i=step+W/2;i<W-30;i+=step)
- {
- char num_char[5 + sizeof(char)];
- to_chars(num_char, num_char + 5, i-W/2);
- outtextxy(i-sizeof(num_char),H/2+5,num_char);
- }
- for(int i=W/2-step;i>0;i-=step)
- {
- char num_char[5 + sizeof(char)];
- to_chars(num_char, num_char + 5, i-W/2);
- outtextxy(i-sizeof(num_char),H/2+5,num_char);
- }
- return;
- }
- void scaley(int step)
- {
- for(int i=-step+H/2;i>0;i-=step)
- {
- char num_char[20 + sizeof(char)]="";
- to_chars(num_char, num_char + 3,-1*(i-H/2)/2);
- outtextxy(W/2-30,i,num_char);
- }
- for(int i=H/2+step;i<H;i+=step)
- {
- char num_char[5 + sizeof(char)]="";
- to_chars(num_char, num_char + 5, -1*(i-H/2)/2);
- outtextxy(W/2-30,i,num_char);
- }
- return;
- }
- void getVolts()
- {
- cout << "\nEnter The Number Of Samples : ";
- cin >> p;
- degree = 360 / p;
- cout << "\nEnter The Degree of The First Theta : ";
- cin >> fp;
- cout << "\nYou will Enter " << p << " values one for each " << degree << " Degree\n";
- for(int i = 1;i <= p; i++)
- {
- cout << "\n\t>> Enter value number " << i << " at " << fp + (i - 1) * degree << " Degree : ";
- float volt;
- cin>>volt;
- v.push_back(volt);
- }
- return;
- }
- void Draw_XY_Axis()
- {
- initwindow(W, H);
- moveto(W / 2, H / 2);
- // X axis
- drawline(0, H / 2, W, H / 2, WHITE, W - 40, H / 2 + 10, "theta");
- // Y axis
- drawline(W / 2, 0, W / 2, H, WHITE, W / 2 + 10, 0, "V");
- outtextxy(W - 50, H / 2 + 30, "(degree)");
- outtextxy(W / 2 + 10, 15, "(volt)");
- scalex(60);
- scaley(30);
- return;
- }
- void getFourierCofficients()
- {
- for(auto volt : v)a0 += volt;
- for(int i = 1; i < 4; i++)
- {
- float an = 0,bn = 0;
- for(int j = 1; j <= p; j++)
- {
- an += v[j - 1] * cos((fp + (i - 1) * degree) * j * PI / 180);
- bn += v[j - 1] * sin((fp + (i - 1) * degree) * j * PI / 180);
- }
- an = an * 2 / p;
- bn = bn * 2 / p;
- ab.push_back({an,bn});
- }
- a0 /= p;
- return;
- }
- void printFourierCofficientsOnConsole()
- {
- cout << '\n';
- for (int i = 0; i <= 20; i++)cout << '-';
- cout << '\n';
- cout << "Fourier Cofficients : \n";
- cout << "\t>>a0 = " << a0 << '\n';
- cout << "\t>>a1 - " << ab[0].first << '\n';
- cout << "\t>>a2 = " << ab[1].first << '\n';
- cout << "\t>>a3 = " << ab[2].first << '\n';
- cout << "\t>>b1 = " << ab[0].second << '\n';
- cout << "\t>>b2 = " << ab[1].second << '\n';
- cout << "\t>>b3 = " << ab[2].second << '\n';
- for (int i = 0; i <= 20; i++)cout << '-';
- cout << '\n';
- return;
- }
- void DrawTheSignal()
- {
- moveto(W / 2, -v[11] + H / 2);
- for(float i = 0.1; i <= 360;i += 0.1)
- {
- float y = a0;
- for(int m = 0; m < ab.size(); m++)
- {
- float var = ab[m].first * cos((m + 1) * i * PI / 180);
- y += rnd(var);
- var = ab[m].second * sin((m + 1) * i * PI / 180);
- y += rnd(var);
- }
- lineto(i + W / 2, (-y * 2 + H / 2));
- }
- return;
- }
- void printFx()
- {
- string str = "v = " + to_string(a0) + " + " + to_string(ab[0].first) + "cos(th) + " + to_string(ab[1].first) + "cos(2*th)+ " + to_string(ab[2].first) + "cos(3*th) .....";
- char *c = new char[110];
- copy(str.begin(), str.end(), c);
- c[str.size()] = '\0';
- outtextxy(10, 20, c);
- str = " + " + to_string(ab[0].second) + "sin(th) + " + to_string(ab[1].second) + "sin(2*th) + " + to_string(ab[2].second) + "sin(3*th) .....";
- copy(str.begin(), str.end(), c);
- c[str.size()] = '\0';
- outtextxy(10, 40, c);
- return;
- }
- void goToDraw()
- {
- getVolts();
- getFourierCofficients();
- printFourierCofficientsOnConsole();
- Draw_XY_Axis();
- DrawTheSignal();
- printFx();
- cout << "\n\n\n\t\t\t\tEnter 0 to go to the Next Signal\n";
- while (cin.get() != '0');
- return;
- }
- void reset()
- {
- system("cls");
- a0 = 0;
- v.clear();
- ab.clear();
- }
- int main()
- {
- cout << "\t\t\tEnter The Number of Signals You want to draw : ";
- cin >> t;
- for (int i = 1; i <= t; i++)
- {
- reset();
- cout << "\t\t\t\t\t\t Signla Number " << i << '\n';
- goToDraw();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment