forked from vtwireless/HLSI
-
Notifications
You must be signed in to change notification settings - Fork 1
/
07_capacity.html
230 lines (190 loc) · 8.19 KB
/
07_capacity.html
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
<!DOCTYPE html>
<html lang="en-US">
<head>
<title>HLSI Ex 7</title>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width"/>
<link rel="stylesheet" type="text/css" media="all" href="base.css">
</head>
<body>
<h2>Exercise 7 - Information Capacity</h2>
<p><a href="index.html">[exercises]</a></p>
<p>
<label for="bandwidth">Bandwidth</label>
<input class="width-3" type="range" min="0.1" max="0.95" value="0.1" id="bandwidth" step="0.001"
oninput="update_plot(null,parseFloat(value),null,null)">
<output for="bandwidth" id="bw">0.1</output>
</p>
<p>
<label for="gain">Signal Gain</label>
<input class="width-3" type="range" min="-40" max="0" value="0" id="gain" step="0.01"
oninput="update_plot(null,null,parseFloat(value),null)">
<output for="gain" id="gn"></output>
</p>
<p>
<label for="signal_to_noise_ratio">Signal-to-Noise Ratio: </label><output for="signal_to_noise_ratio" id="snr" class="text-bold"></output>,
<label for="information_capacity">Information Capacity: </label><output for="information_capacity" id="cap" class="text-bold"></output>,
<label for="spectral_efficiency">Spectral Efficiency: </label><output for="spectral_efficiency" id="eta" class="text-bold"></output>
</p>
<!--<ol>
<li>What combination of parameters (bandwidth, gain) maximize the SNR?</li>
<li>What combination of parameters maximize information capacity? Do these correspond to the maximum SNR?</li>
</ol>-->
</body>
<!-- Load in the javascript libraries -->
<script src="d3.v5.min.js"></script>
<script src="fft.js"></script>
<script src="support.js"></script>
<script>
// 2. Use the margin convention practice
var margin = {top: 10, right: 50, bottom: 50, left: 50}
, width = 720 - margin.left - margin.right // Use the window's width
, height = 320 - margin.top - margin.bottom; // Use the window's height
// options
var fs = 40e6, f0 = 1800e6; // sample rate, center frequency
var bw = 0.2, fc = 0.0, gn = -20, n0 = -30, snr = 0, cap = 10;
var nfft = 2048, generator = new siggen(nfft);
generator.m = 40; // set filter semi-length
generator.beta = 1.5; // set filter window exponent (smaller shows more side-lobes)
// determine scale/units
var [scale_freq,units_freq] = scale_units(f0+fs/2,0.1);
var fScale = d3.scaleLinear().domain([(f0-0.5*fs)*scale_freq, (f0+0.5*fs)*scale_freq]).range([0, width]);
var pScale = d3.scaleLinear().domain([-35, 15]).range([height, 0]);
// d3's line generator
var linef = d3.line()
.x(function(d, i) { return fScale((f0+(i/nfft-0.5)*fs)*scale_freq); }) // map frequency
.y(function(d) { return pScale(d.y); }); // map PSD
// 8. An array of objects of length N. Each object has key -> value pair, the key being "y" and the value is a random number
var dataf = d3.range(0,nfft-1).map(function(f) { return {"y": 0 } })
// create SVG objects
var svgf = svg_create(margin, width, height, fScale, pScale);
// add labels
svg_add_labels(svgf, margin, width, height, "Frequency ("+units_freq+"Hz)", "Power Spectral Density (dB)");
// clip paths
svgf.append("clipPath").attr("id","clipf").append("rect").attr("width",width).attr("height",height);
// 9. Append the path, bind the data, and call the line generator
var pathf = svgf.append("path")
.attr("clip-path","url(#clipf)")
.datum(dataf)
.attr("class", "stroke-med no-fill stroke-yellow")
.attr("d", linef);
// generate power spectral density
function generate_psd(f,w,g,n) {
// update generator
generator.clear();
// add signal with gain compensating for signal bandwidth
generator.add_signal(f,w,g + 10*Math.log10(w));
//generator.add_noise(n); // add random noise samples
generator.generate(n); // generate with specific noise floor
//console.log(psd);
return d3.range(0,nfft-1).map(function(i) { return {"y": generator.psd[i] } })
}
// channel capacity (b/s/Hz) given SNR in dB
function efficiency(snr) { return Math.log2(1.0 + Math.pow(10,snr/10)); }
// capacity curve
// (-10, 4 Mb/s, 0.1375 b/s/Hz), (40 dB, 382 Mb/s, 13.288 b/s/Hz)
var datac = d3.range(-10,40+0.01).map(function(d,i) { return {"x":d, "y":efficiency(d)} })
var svgc = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.attr("id", "svg-capacity")
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// create scale
var sscale = d3.scaleLinear().domain([-10,40]).range([0, width]); // snr (Eb/N0?)
var cscale = d3.scaleLog ().domain([0.1, 20]).range([height, 0]); // capacity (b/s/Hz)
svgc.append("defs").append("clipPath")
.attr("id", "clip")
.append("rect")
.attr("width", width)
.attr("height", height);
svgc.append("rect")
.attr("width", "86.2%")
.attr("height", "81%")
.attr("fill", "black");
svgc.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(sscale));
var yTickValues = [0.1,0.2,0.5,1,2,5,10,20]
svgc.append("g")
.attr("class", "y axis")
.call(d3.axisLeft(cscale)
.tickValues(yTickValues)
.tickFormat(function(d, i) {
let [s,u] = scale_units(d);
s = 1e-6; u = 'M';
s = 1; u = '';
return d*s + u;
}))
// grid lines
svgc.append("g").attr("class","grid").call(d3.axisBottom(sscale).tickFormat("").tickSize(height));
svgc.append("g").attr("class","grid").call(d3.axisLeft (cscale).tickFormat("").tickSize(-width).tickValues(yTickValues));
// create x-axis axis label
svgc.append("text")
.attr("transform","translate("+(width/2)+","+(height + 0.75*margin.bottom)+")")
.attr("dy","-0.3em")
.style("text-anchor","middle")
.attr("fill", "white")
.text("SNR (dB)")
// create y-axis label
svgc.append("text")
.attr("transform","rotate(-90)")
.attr("y", 0 - margin.left)
.attr("x", 0 - (height/2))
.attr("dy", "1em")
.style("text-anchor","middle")
.attr("fill", "white")
.text("Spectral Efficiency (bits/second/Hz)")
// clip paths
svgc.append("clipPath").attr("id","clipf").append("rect").attr("width",width).attr("height",height);
// line generator for capacity curve
var linec = d3.line()
.x(function(d, i) { return sscale(d.x); }) // map SNR
.y(function(d) { return cscale(d.y); }); // map capacity
// 9. Append the path, bind the data, and call the line generator
var pathc = svgc.append("path")
.attr("clip-path","url(#clipf)")
.datum(datac)
.attr("class", "stroke-light no-fill stroke-green-o")
.attr("d", linec);
// add operating ponit
svgc.append('g')
.selectAll("dot")
.data( [{"x":0, "y":efficiency(0)}] )
.enter()
.append("circle")
.attr("cx", function (d) { return sscale(d.x); } )
.attr("cy", function (d) { return cscale(d.y); } )
.attr("r", 5)
.attr("class", "stroke-light no-fill stroke-green-o")
.style("fill", "#66ff00");
function update_plot(freq,band,gain,noise) {
if (band!=null) { bw = band; }
if (gain!=null) { gn = gain; }
document.querySelector('#bw').value = d3.format(".2f")(bw*fs*scale_freq) + " " + units_freq + "Hz";
document.querySelector('#bandwidth').value = bw;
document.querySelector('#gn').value = d3.format(".2f")(gn) + " dB";
document.querySelector('#gain').value = gn;
// compute and display SNR
let SNRdB = gn - n0 - 10*Math.log10(bw);
document.querySelector('#snr').value = d3.format(".2f")(SNRdB) + " dB";
// compute and display spectral efficiency, capacity
let R = efficiency(SNRdB); // spectral efficiency (b/s/Hz)
let C = fs * bw * R; // capacity (b/s)
var [scale_cap,units_cap] = scale_units(C,0.1);
document.querySelector('#cap').value = d3.format(".1f")(C*scale_cap) + " " + units_cap + "b/s";
document.querySelector('#eta').value = d3.format(".3f")(R) + " " + "b/s/Hz";
// generate power spectral density, compensating for bandwidth
dataf = generate_psd(fc,bw,gn,n0);
pathf.datum(dataf).attr("d", linef);
// update capacity value
d3.selectAll("#svg-capacity circle")
.data( [{"x":SNRdB, "y":R}] )
.attr("cx", function(d) { return sscale(d.x); })
.attr("cy", function(d) { return cscale(d.y); });
}
// set initial value
update_plot();
</script>
</html>