-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphhandler.cpp
More file actions
125 lines (104 loc) · 4.34 KB
/
graphhandler.cpp
File metadata and controls
125 lines (104 loc) · 4.34 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "graphhandler.h"
#include <math.h>
#include <QTabBar>
# define PI 3.14159265358979323846
void GraphHandler::fill_points_time_domain(QVector<QPointF> &points, double period, double sampling_frequency, double carrier_frequency, double deviation)
{
points.clear();
int samples_number = std::ceil(period * sampling_frequency);
points.reserve(samples_number + 1);
double dt = period / samples_number;
for (int i = 0; i < samples_number + 1; ++i)
{
double t = dt * i;
double y = cos(-PI / 2.0f + 2.0f * PI * ((carrier_frequency - deviation) * t + deviation * t * ( t / period)));
points.emplace_back(i, y);
}
}
double GraphHandler::calculate_imaginary_part(const QVector<QPointF> &time_domain_points, int k)
{
double S = 0;
for (int n = 0; n < time_domain_points.size(); ++n)
{
S += time_domain_points[n].y() * sin((2.0f * PI * k * n) / time_domain_points.size());
}
return -S;
}
double GraphHandler::calculate_real_part(const QVector<QPointF> &time_domain_points, int k)
{
double S = 0;
for (int n = 0; n < time_domain_points.size(); ++n)
{
S += time_domain_points[n].y() * cos((2.0f * PI * k * n) / time_domain_points.size());
}
return S;
}
void GraphHandler::fill_points_frequency_domain(QVector<QPointF> &points_container, const QVector<QPointF> &time_domain_points, double boundary)
{
// we are interested only with the frequencies that lie between Fmin and Fmax obviously
points_container.clear();
points_container.reserve(boundary);
double R_max = 0;
int i = -boundary / 2;
for (int k = 0; k < boundary; ++k, ++i)
{
double imaginary = calculate_imaginary_part(time_domain_points, k);
double real = calculate_real_part(time_domain_points, k);
double R = sqrtf(real * real + imaginary * imaginary) / time_domain_points.size();
if (R > R_max)
R_max = R;
points_container.emplace_back(i, R);
}
for (auto& p : points_container)
{
p.setY(p.y() / R_max);
}
}
GraphHandler::GraphHandler(QWidget *parent) : QTabWidget{parent}
{
tabBar()->setDocumentMode(true);
tabBar()->setExpanding(true);
// initialisation of lfm graph in time domain
lfm_time_graph = new Graph(this);
lfm_time_graph->set_argument_text("t, sec");
lfm_time_graph->set_value_text("s(t)");
lfm_time_graph->set_graph_size({720, 250});
lfm_time_graph->set_amplitude(250);
lfm_time_graph->set_amplitude_text("1");
// initialisation of lfm graph in frequency domain
lfm_frequency_graph = new Graph(this);
lfm_frequency_graph->set_starting_point({400, lfm_frequency_graph->get_world_size().height() - 30});
lfm_frequency_graph->set_graph_size({600, 500});
lfm_frequency_graph->set_amplitude(500);
lfm_frequency_graph->set_argument_text("f, Hz");
lfm_frequency_graph->set_value_text("s(w)");
// y amplitude marks
int m = 6;
QVector<std::pair<double, QString> > marks_y(m);
for (int i = 0; i < m; ++i)
{
marks_y[i] = {float(i) / float(m), QString::number(float(i) / float(m - 1), 'g', 2)};
}
lfm_frequency_graph->set_special_marks_y(marks_y);
this->addTab(lfm_time_graph, "Time domain");
this->addTab(lfm_frequency_graph, "Frequency domain");
}
void GraphHandler::reset(LFMSettings settings)
{
// time domain
QVector<QPointF> lfm_points;
fill_points_time_domain(lfm_points, 1 / settings.mf, settings.sf, settings.cf, settings.fd);
lfm_time_graph->update_points(lfm_points);
// frequency domain
QVector<QPointF> freq_points;
fill_points_frequency_domain(freq_points, lfm_points, (2.0 * settings.cf) / settings.mf);
// x marks
QVector<std::pair<int, QString> > marks_x(3);
marks_x[0] = {(settings.cf - settings.fd) / settings.mf, QString::number(settings.mf * int(settings.cf / settings.mf) - settings.mf * std::ceil(settings.fd / settings.mf)) + "Hz"};
marks_x[1] = {(settings.cf + settings.fd) / settings.mf, QString::number(settings.mf * std::ceil(settings.cf / settings.mf) + settings.mf * std::ceil(settings.fd / settings.mf)) + "Hz"};
marks_x[2] = {settings.cf / settings.mf, QString::number(settings.cf) + "Hz"};
lfm_frequency_graph->set_special_marks_x(marks_x);
lfm_frequency_graph->update_points(freq_points);
lfm_time_graph->update();
lfm_frequency_graph->update();
}