forked from olgierd/coaxcalculator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html.j2
165 lines (127 loc) · 4.73 KB
/
index.html.j2
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
<html>
<body>
<style type="text/css">
pre { margin: 1 0 0 0; display: block; }
pre.selected, pre:hover { background-color: #dddddd; }
.details { margin: 5 0 5 0; font-weight: bold; }
a:link { text-decoration: none; }
</style>
<script type="application/json" id="cable_data">
{{ cable_json }}
</script>
<input type="number" id="freq" min="1" max="3000" step="1" value="145" onchange="javascript:update();"> MHz<br>
<input type="number" id="length" min="1" max="3000" step="1" value="10" onchange="javascript:update();"> m<br>
<input type="number" id="pwr" min="1" max="1500" step="1" value="100" onchange="javascript:update();"> W (Power in)<br>
<p id="output"></p>
<pre>Read more: <a href="https://github.com/olgierd/coaxcalculator/blob/main/README.md">EN</a> | <a href="https://github.com/olgierd/coaxcalculator/blob/main/README_pl.md">PL</a>.
<script>
var cables = JSON.parse(document.getElementById('cable_data').innerHTML);
var selected = [];
function get_att_for_freq(att_data, frequency) {
att_array = Object.entries(att_data).sort(function(a,b) { return a[0] - b[0];}); // convert dictionary to an array of pairs and sort by frequency
// frequency higher than last data point specified
if(frequency > att_array[att_array.length - 1][0]) {
return 10000;
}
// spot-on hit - no interpolation needed
if(frequency in att_data) {
return att_data[frequency];
}
// find two neighboring values
for(var i = 0; i < att_array.length;i++) {
if(att_array[i][0] > frequency) {
if(i>=1) {
lower = att_array[i-1];
}
else {
lower = [0, 0]; // use 0 dB @ 0 MHz, if frequency is below the first data point
}
upper = att_array[i];
break;
}
}
// simple linear interpolation between two points
return (frequency-lower[0])/(upper[0]-lower[0])*(upper[1]-lower[1])+lower[1];
}
function sortByAtt(rows) {
return rows.sort(function(a,b) {
return a.att100m - b.att100m
});
}
function generateOutputStrings(cable_data, params) {
output = {
att: "-",
pwr_out: "-",
loss_percentage: "-"
}
if(cable_data.att100m < 10000) {
// calculate output power & percentage loss
output.att = cable_data.att100m * params.length/100;
output.pwr_out = 10**(Math.log10(params.pwr_in)-output.att/10);
output.loss_percentage = 100-10**(Math.log10(100)-output.att/10);
// round and change to strings
output.att = (Math.round(output.att*100)/100).toFixed(2);
output.pwr_out = (Math.round(output.pwr_out*100)/100).toFixed(2);
output.loss_percentage = (Math.round(output.loss_percentage*10)/10).toFixed(1);
}
return output;
}
function select(key) {
if(!selected.includes(key)) {
selected.push(key);
}
else {
selected.splice(selected.indexOf(key), 1);
}
update();
}
function update() {
// read input parameters
params = {
"freq": parseInt(document.getElementById('freq').value),
"length": parseInt(document.getElementById('length').value),
"pwr_in": parseInt(document.getElementById('pwr').value)
};
var rows = [];
// calculate attenuation for a given freq for every cable (/100 m)
for (var key in cables) {
cur = {
key: key,
name: cables[key].name,
att100m: get_att_for_freq(cables[key]["attenuation"], params.freq),
link: cables[key].data_source
}
// add extra params (dB/power loss per specified cable length)
rows.push(Object.assign({}, cur, generateOutputStrings(cur, params)));
}
// sort by attenuation
sorted_rows = sortByAtt(rows)
output_p = document.getElementById("output");
output_p.innerHTML = ''
// generate header
out_row = document.createElement('pre');
output = "Calculating for " + params.freq + " MHz & " + params.length + " meter(s) & " + params.pwr_in + " Watts input: <br><br>";
output += "<b>Cable" + ' '.repeat(31) + "Loss Power out</b></br>"
out_row.innerHTML = output;
output_p.appendChild(out_row);
sorted_rows.forEach(o => {
out_row = document.createElement('pre');
out_row.id = o.key;
// select clicked rows
out_row.onclick = function(event) { select(o.key) };
if(selected.includes(o.key)) {
out_row.className = "selected";
}
output = "";
output += o.name.padEnd(25)
output += o.att.padStart(10) + " dB"
output += o.loss_percentage.padStart(6) + "%" + o.pwr_out.padStart(11) + " W";
output += " (<a href='" + o.link + "' target='_blank' rel='noopener noreferrer'>pdf</a>)"
out_row.innerHTML = output;
output_p.appendChild(out_row);
});
}
update();
</script>
</body>
</html>