-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFourier_Sqr_Waves.hpp
More file actions
65 lines (60 loc) · 1.91 KB
/
Fourier_Sqr_Waves.hpp
File metadata and controls
65 lines (60 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
class fourier
{
private:
int _n, _loop_count, i, k;
float p, omega, _a, sum, fs, _time_period;
public:
void input(double n, double time_period, double a, double loop_count)
{
_n = n;
_time_period = time_period;
_a = a;
_loop_count = loop_count;
// std::cout << "enter the value of n for no.of sin waves to be superimposed:";
// std::cin >> n;
// std::cout << "\nenter the value of timeperiod of square wave:";
// std::cin >> timp;
// std::cout << "\nenter the value of amplitude:";
// std::cin >> a;
// std::cout << "\nenter the no.of times you would like to run the loop:";
// std::cin >> t;
}
void calc()
{
std::ofstream out("fourier.dat");
omega = (2 * (4 * atan(1.0)) / _time_period);
for (k = 1; k <= _loop_count; k++)
{
p = omega * k;
sum = 0.0;
for (i = 1; i <= _n; i += 2)
{
sum = sum + (sin(i * p)) / i;
}
fs = (4 * _a * sum) / (4 * atan(1));
out << k << " " << fs << std::endl;
}
out.close();
// Important if building for both Unix and Windows OS
// Plot the data using gnuplot
#ifdef _WIN32
FILE *gnuplotPipe = _popen("gnuplot -persistent", "w");
#else
FILE *gnuplotPipe = popen("gnuplot -persistent", "w");
#endif
if (!gnuplotPipe)
{
std::cerr << "Error opening pipe to gnuplot" << std::endl;
return;
}
fprintf(gnuplotPipe, "set title 'Fourier Analysis Of Square Waves'\n");
fprintf(gnuplotPipe, "set xlabel 'Time'\n");
fprintf(gnuplotPipe, "set ylabel 'Amplitude'\n");
fprintf(gnuplotPipe, "plot 'fourier.dat' using 1:2 with lines title 'Resultant'\n");
#ifdef _WIN32
_pclose(gnuplotPipe);
#else
pclose(gnuplotPipe);
#endif
}
};