-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathscandata.cpp
More file actions
183 lines (147 loc) · 5.41 KB
/
Copy pathscandata.cpp
File metadata and controls
183 lines (147 loc) · 5.41 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
(C) Copyright 2015 Jeremy Burton
This file is part of Sark-100-antenna-analyzer.
Sark-100-antenna-analyzer is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
Sark-100-antenna-analyzerr is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty
of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "config.h"
#include "eventreceiver.h"
#include "scandata.h"
Sample::Sample()
{
freq = 0;
swr = 1.0;
Z = R = 50.0;
X = 0.0;
}
void Sample::fromRaw(double vf,double vr,double vz,double va)
{
swr = (vf + vr) / (vf - vr);
Z = 50.0 * vz/va;
R = ((2500.0 + Z*Z) * swr)/(50.0 * (swr*swr + 1));
X = Z>R ? sqrt(Z*Z - R*R) : -sqrt(-Z*Z + R*R);
}
ScanData::ScanData()
{
//points = NULL;
//SetPointCount(101);
}
void ScanData::SetPointCount(int n)
{
//point_count=n;
//points = (Sample *)realloc(points,sizeof(Sample)*point_count);
//for (int i=0;i<point_count;i++)
// points[i].Sample();
points.resize(n+1);
//printf("size=%d\n",points.size());
}
int ScanData::GetPointCount()
{
return points.size()-1;
}
void ScanData::UpdateStats()
{
swr_min_idx = Z_min_idx = X_min_idx = R_min_idx = Z_max_idx = X_max_idx = R_max_idx = swr_max_idx = 0;
freq_start = points.front().freq;
freq_end = points.back().freq;
for (unsigned int i=0;i<points.size();i++)
{
//double
//n = points[i].Z*points[i].Z - points[i].R*points[i].R;
//points[i].X2 = copysign(sqrt(abs(n)),n);
//printf("i=%d, swr=%lf, f=%lf\n",i,points[i].swr,points[i].freq);
if (points[i].swr < points[swr_min_idx].swr) { swr_min_idx=i; }
if (points[i].swr > points[swr_max_idx].swr) { swr_max_idx=i; }
if (points[i].Z < points[Z_min_idx].Z) { Z_min_idx=i; }
if (points[i].Z > points[Z_max_idx].Z) { Z_max_idx=i; }
if (points[i].X < points[X_min_idx].X) { X_min_idx=i; }
if (points[i].X > points[X_max_idx].X) { X_max_idx=i; }
if (points[i].R < points[R_min_idx].R) { R_min_idx=i; }
if (points[i].R > points[R_max_idx].R) { R_max_idx=i; }
}
for (int i=swr_bw_lo_idx=swr_min_idx;i>=0 && i<(int)points.size() && points[i].swr<=Config::swr_bw_max;i--)
swr_bw_lo_idx=i;
for (int i=swr_bw_hi_idx=swr_min_idx;i<(int)points.size() && points[i].swr<=Config::swr_bw_max;i++)
swr_bw_hi_idx=i;
//fflush(stdout);
}
#ifdef ENABLE_TEST_DATA
void ScanData::dummy_data(EventReceiver *erx)
{
//freq_start = 26205000.0;
//freq_end = 28205000.0;
double freq_inc = (freq_end-freq_start)/GetPointCount();
for (unsigned int i=0;i<points.size();i++)
{
points[i].freq = freq_start + i*freq_inc;
points[i].swr = 5.1 + sin(double(i)/(points.size()-1)*2*3.14159)*4.0;
points[i].Z = 100 + cos(double(i)/(points.size()-1)*2*3.14159)*50;
//printf("%d: %lf => %lf\n",i,points[i].freq,points[i].swr);
erx->RaiseEvent(EventReceiver::progress_event, i*100/(points.size()-1));
}
//fflush(stdout);
UpdateStats();
}
#endif
void ScanData::toDom(QDomDocument &doc,QDomElement &parent)
{
QDomElement element = doc.createElement("scandata");
unsigned int i;
QDomElement point;
element.setAttribute(QString("fstart"),freq_start);
element.setAttribute(QString("fend"),freq_end);
//version.toDom(doc,ca);
//toDom_Text(doc,element,"fstart",freq_start);
//toDom_Text(doc,element,"fend",freq_end);
for (i=0;i<points.size();i++)
{
point = doc.createElement("point");
point.setAttribute(QString("freq"), points[i].freq);
toDom_Text(doc,point,"SWR",points[i].swr);
toDom_Text(doc,point,"Z",points[i].Z);
toDom_Text(doc,point,"X",points[i].X);
toDom_Text(doc,point,"R",points[i].R);
element.appendChild(point);
}
parent.appendChild(element);
}
bool ScanData::fromDom(QDomElement &e0)
{
Sample point;
//printf("ScanData::fromDom: e0=%s\n",e0.tagName().toAscii().data());
freq_start = e0.attribute("fstart", "0").toDouble();
freq_end = e0.attribute("fend", "0").toDouble();
points.clear();
for (QDomNode n1 = e0.firstChild(); !n1.isNull(); n1 = n1.nextSibling())
{
if (!n1.isElement()) continue; // Skip any non-element nodes
QDomElement e1 = n1.toElement();
if (e1.tagName() == "point")
{
point.freq = e1.attribute("freq", "0").toDouble();
for (QDomNode n2 = e1.firstChild(); !n2.isNull(); n2 = n2.nextSibling())
{
if (!n2.isElement()) continue; // Skip any non-element nodes
QDomElement e2 = n2.toElement();
if (e2.tagName() == "SWR") { point.swr = e2.text().toDouble(); }
else if (e2.tagName() == "Z") { point.Z = e2.text().toDouble(); }
else if (e2.tagName() == "X") { point.X = e2.text().toDouble(); }
else if (e2.tagName() == "R") { point.R = e2.text().toDouble(); }
}
points.push_back(point);
}
}
UpdateStats();
return true;
}