-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathf2wl.cpp
More file actions
276 lines (251 loc) · 7.16 KB
/
f2wl.cpp
File metadata and controls
276 lines (251 loc) · 7.16 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* frequency <-> wavelengths
* Project Crew™ 3/18/2026
*/
#define PROGNAME "f2wl"
#include <iostream>
#include <iomanip>
#include <cmath>
#include "lib/cxxopts/include/cxxopts.hpp"
using namespace std;
static inline constexpr double c = 299792458;
static inline constexpr double c_Mm = c * 1e-6;
/* fill string with some chars */
string
fill(char c, int n)
{
string s;
s.resize(n, c);
return s;
}
/* number of trailing zeros (bits) */
int
ntz(unsigned short int x)
{
if (x == 0)
return(32);
int n = 1;
if ((x & 0xF) == 0)
{
n = n + 4;
x = x >> 4;
}
if ((x & 0x3) == 0)
{
n = n + 2;
x = x >> 2;
}
return n - (x & 1);
}
/* output functinos are here */
class out
{
private:
ostringstream os;
int prec = 4, w = 11;
void
format()
{
os.seekp(ios_base::beg); // reset
os << setw(w);
}
string
fwf(float f) // fixed width float
{
format();
os << setprecision(prec) << f;
return os.str();
}
string // inch format
infs(float dft)
{
float in;
short int fractz, nu, de;
int ft, frac;
ft = floorf(dft);
in = (dft - ft) * 12;
frac = roundf((in - floorf(in)) * 32);
if(frac == 0)
{
fractz = nu = de = 0;
}else
{
fractz = ntz(frac);
nu = frac >> fractz;
de = 32 >> fractz;
}
format();
os << setprecision(3)
<< setw(10) << dft << "ft || "
<< setw(6) << ft << "ft + "
<< setw(2) << (int)in << " "
<< setw(2) << nu << " / "
<< setw(2) << de << "in";
return os.str();
}
public:
out() // initial
{
os << fixed;
}
void
m(float result) // metric output
{
char p = 0;
if(result < 1)
{
prec = 2;
p = 'c';
w = 9;
result *= 1e2;
}
cout << endl
<< fwf(result) << p << "m @ 1\u03bb(MHz) ";
if(p == 0)
cout << ' ';
cout << " \u03bb/2 = " << fwf(result / 2) << p << "m "
" \u03bb/4 = " << fwf(result / 4) << p << "m" << endl
<< fill(' ', 14 + w)
<< "5/8\u03bb = " << fwf(result * 0.625) << p << "m "
"60%\u03bb = " << fwf(result * 0.600) << p << "m" << endl
<< fill(' ', 16 + w)
<< "2\u03bb = " << fwf(result * 2) << p << "m "
"4\u03bb = " << fwf(result * 4) << p << "m" << endl
<< endl;
}
void
in(float result) // imperial units
{
float dft;
dft = result;
cout << endl
<< " 1\u03bb = " << infs(dft) << endl
<< " \u03bb/2 = " << infs(dft / 2) << endl
<< " \u03bb/4 = " << infs(dft / 4) << endl
<< " 2\u03bb = " << infs(dft * 2) << endl
<< " 4\u03bb = " << infs(dft * 4) << endl
<< " 5/8\u03bb = " << infs(dft * 0.625) << endl
<< " 60%\u03bb = " << infs(dft * 0.600) << endl
<< endl;
}
};
/* how to use me */
void
usage(string help)
{
cerr << help << endl
<< " I can convert the frequency to a wavelength in free space." << endl
<< " A wavelength in meters is convert back to frequency." << endl
<< endl;
exit(EXIT_FAILURE);
}
/* boiler plate */
void
boil(float* freq)
{
cout << fixed << setprecision(0)
<< endl
<< fill(' ', 10)
<< "For input frequency of " << *freq * 1e6 << " Hz";
}
int
main(int argc, char* argv[])
{
float freq, freq_adj, result, vf;
out out;
cxxopts::ParseResult optres;
cxxopts::Options options(PROGNAME,
"\n " PROGNAME" is frequency <-> wavelengths conversion\n");
string help;
options
.positional_help("frequency")
.allow_unrecognised_options()
.add_options()
("h,help", "Print this usage we see here",
cxxopts::value<bool>()->implicit_value("true",
cxxopts::ImplicitArgPolicy::Disabled))
("i,inch", "Output the imperial format (feet & inches)\n"
" (Default is metric)",
cxxopts::value<bool>()->implicit_value("true",
cxxopts::ImplicitArgPolicy::Disabled))
/* ("m,metric", "Output metric format",
cxxopts::value<bool>()->implicit_value("true",
cxxopts::ImplicitArgPolicy::Disabled))
*/ ("v,velocity-factor", "Scale result by velocity-factor %\n"
"For the antennas, use 95.0%. "
"Same as 468 / f.\n",
cxxopts::value<float>()->default_value("100"), "X.XX")
("freq", "frequency in MHz", cxxopts::value<float>())
;
options.parse_positional({"freq"});
help = options.help();
try
{
optres = options.parse(argc, argv);
}catch(cxxopts::exceptions::specified_disabled_args& e)
{
cerr << PROGNAME": " << e.what() << endl;
usage(help);
}catch(cxxopts::exceptions::incorrect_argument_type& e)
{
cerr << PROGNAME": error: I need real numbers." << endl;
usage(help);
}catch(cxxopts::exceptions::missing_argument& e)
{
cerr << PROGNAME": error: " << e.what() << endl;
usage(help);
}
if(optres.count("help"))
{
usage(help);
}
if(optres.count("freq"))
{
freq = optres["freq"].as<float>();
if(freq < 9e-3)
{
cerr << PROGNAME": error: I need real numbers." << endl;
usage(help);
}
}else
{
cerr << PROGNAME": error: I need a frequency in MHz." << endl;
usage(help);
}
boil(&freq); // boiler plate
vf = optres["velocity-factor"].as<float>();
if(vf > 100)
{
cerr << PROGNAME": error: velocity factor > 100%." << endl;
usage(help);
}else if(vf < 1)
{
cerr << PROGNAME": error: velocity factor very small or negative." << endl;
usage(help);
}else if(vf < 100)
{
cout << " @ " << setprecision(2) << vf << "% velocity:" << endl;
}else
{
cout << ':' << endl;
}
freq_adj = freq;
freq_adj *= vf < 100 ? 100 / vf : 1;
result = c_Mm / freq_adj;
if(result > 0.26)
{
if(optres.count("inch"))
{
result /= 0.3048;
out.in(result);
}else // metric
{
out.m(result);
}
}else
{
cout << endl
<< " Short wavelengths detected. "
"Defaulting to metric conversions." << endl;
out.m(result);
}
}