forked from matanleilien/teaching_aids
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
223 lines (193 loc) · 12.9 KB
/
Copy pathindex.html
File metadata and controls
223 lines (193 loc) · 12.9 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Core IQ Modulation Simulator</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdn.plot.ly/plotly-2.32.0.min.js"></script>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<!-- KaTeX for LaTeX rendering -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.css" integrity="sha384-n8MVd4RsNIU0KOVEMeaCssLdsGcjp53xU6S2HTbaCAbT6GYvB8bYmTcG4rjCycP_" crossorigin="anonymous">
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/katex.min.js" integrity="sha384-XjKyOOlVwcjNcsNQDbBFxd10eUwenxVLT4egoET/SqVRCgtWv4SRoTic3VFLVn/g" crossorigin="anonymous"></script>
<script defer src="https://cdn.jsdelivr.net/npm/katex@0.16.9/dist/contrib/auto-render.min.js" integrity="sha384-+VBxd3r6XgURycqtZ117nYw44OOcIax56Z4dCRWbxyPt0Koah1uHoK0o4+/RRE05" crossorigin="anonymous" onload="renderMathInElement(document.body);"></script>
<style>
body {
font-family: 'Inter', sans-serif;
}
.plotly-graph-div {
border-radius: 0.5rem;
overflow: hidden;
}
</style>
</head>
<body class="bg-gray-900 text-gray-200">
<div class="container mx-auto p-4 md:p-8">
<header class="text-center mb-8">
<h1 class="text-3xl md:text-4xl font-bold text-white">Core I/Q Modulation Simulator</h1>
<p class="text-lg text-gray-400 mt-2">Explore the fundamentals of In-Phase and Quadrature signaling.</p>
</header>
<main class="grid grid-cols-1 lg:grid-cols-12 gap-8">
<!-- Controls Panel -->
<div class="lg:col-span-4 bg-gray-800 p-6 rounded-xl shadow-lg">
<h2 class="text-2xl font-semibold mb-6 border-b border-gray-700 pb-3">Controls</h2>
<div class="space-y-6">
<div>
<label for="i-level" class="block text-sm font-medium text-gray-300">In-Phase (I) Level: <span id="i-level-val" class="font-mono bg-gray-700 px-2 py-1 rounded">1.00</span></label>
<input id="i-level" type="range" min="-1" max="1" value="1" step="0.01" class="w-full h-2 bg-gray-600 rounded-lg appearance-none cursor-pointer mt-2">
</div>
<div>
<label for="q-level" class="block text-sm font-medium text-gray-300">Quadrature (Q) Level: <span id="q-level-val" class="font-mono bg-gray-700 px-2 py-1 rounded">0.00</span></label>
<input id="q-level" type="range" min="-1" max="1" value="0" step="0.01" class="w-full h-2 bg-gray-600 rounded-lg appearance-none cursor-pointer mt-2">
</div>
<div class="pt-4 mt-4 border-t border-gray-700">
<label for="amp-level" class="block text-sm font-medium text-gray-300">Amplitude (R): <span id="amp-level-val" class="font-mono bg-gray-700 px-2 py-1 rounded">1.00</span></label>
<input id="amp-level" type="range" min="0" max="1.415" value="1.0" step="0.01" class="w-full h-2 bg-gray-600 rounded-lg appearance-none cursor-pointer mt-2">
</div>
<div>
<label for="phase-level" class="block text-sm font-medium text-gray-300">Phase (φ): <span id="phase-level-val" class="font-mono bg-gray-700 px-2 py-1 rounded">0.0°</span></label>
<input id="phase-level" type="range" min="0" max="360" value="0" step="0.1" class="w-full h-2 bg-gray-600 rounded-lg appearance-none cursor-pointer mt-2">
</div>
<div class="pt-4 mt-4 border-t border-gray-700">
<label for="carrier-freq" class="block text-sm font-medium text-gray-300">Carrier Frequency (f<sub>c</sub>): <span id="carrier-freq-val" class="font-mono bg-gray-700 px-2 py-1 rounded">50 Hz</span></label>
<input id="carrier-freq" type="range" min="10" max="200" value="50" step="5" class="w-full h-2 bg-gray-600 rounded-lg appearance-none cursor-pointer mt-2">
</div>
</div>
</div>
<!-- Visualizations -->
<div class="lg:col-span-8 grid grid-cols-1 md:grid-cols-2 gap-8">
<div class="bg-gray-800 p-3 rounded-xl shadow-lg flex flex-col" style="aspect-ratio: 1 / 1;">
<h3 class="text-lg font-semibold text-center flex-shrink-0 py-2">I/Q Constellation</h3>
<div id="constellation-plot" class="flex-grow min-h-0"></div>
</div>
<div class="flex flex-col gap-6 justify-between">
<div class="bg-gray-800 p-3 rounded-xl shadow-lg flex flex-col h-full">
<h3 class="text-lg font-semibold text-center flex-shrink-0 py-2">I Component: $I \cdot \cos(\omega_c t)$</h3>
<div id="i-comp-plot" class="flex-grow min-h-0"></div>
</div>
<div class="bg-gray-800 p-3 rounded-xl shadow-lg flex flex-col h-full">
<h3 class="text-lg font-semibold text-center flex-shrink-0 py-2">Q Component: $-Q \cdot \sin(\omega_c t)$</h3>
<div id="q-comp-plot" class="flex-grow min-h-0"></div>
</div>
</div>
<div class="md:col-span-2 bg-gray-800 p-3 rounded-xl shadow-lg flex flex-col" style="height: 25vh;">
<h3 class="text-lg font-semibold text-center flex-shrink-0 py-2">Resultant RF Signal: $s(t)$</h3>
<div id="rf-plot" class="flex-grow min-h-0"></div>
</div>
</div>
</main>
</div>
<script>
document.addEventListener("DOMContentLoaded", function() {
// DOM Elements
const iLevelSlider = document.getElementById('i-level');
const qLevelSlider = document.getElementById('q-level');
const ampLevelSlider = document.getElementById('amp-level');
const phaseLevelSlider = document.getElementById('phase-level');
const carrierFreqSlider = document.getElementById('carrier-freq');
// Value Displays
const iLevelVal = document.getElementById('i-level-val');
const qLevelVal = document.getElementById('q-level-val');
const ampLevelVal = document.getElementById('amp-level-val');
const phaseLevelVal = document.getElementById('phase-level-val');
const carrierFreqVal = document.getElementById('carrier-freq-val');
// Plotting Divs
const constellationDiv = document.getElementById('constellation-plot');
const iCompDiv = document.getElementById('i-comp-plot');
const qCompDiv = document.getElementById('q-comp-plot');
const rfDiv = document.getElementById('rf-plot');
// Core Simulation Parameters
const sampleRate = 10000;
const signalDuration = 0.1;
const nSamples = sampleRate * signalDuration;
const time = Array.from({ length: nSamples }, (_, i) => i / sampleRate);
let lastTouchedSlider = 'i';
function handleSliderInput(masterSlider) {
lastTouchedSlider = masterSlider;
let I, Q, R, phi_deg;
if (masterSlider === 'i' || masterSlider === 'q') {
// --- Rectangular to Polar Calculation ---
I = parseFloat(iLevelSlider.value);
Q = parseFloat(qLevelSlider.value);
R = Math.sqrt(I*I + Q*Q);
phi_deg = Math.atan2(Q, I) * (180 / Math.PI);
if (phi_deg < 0) phi_deg += 360;
// Update polar sliders
ampLevelSlider.value = R;
phaseLevelSlider.value = phi_deg;
} else { // masterSlider is 'amp' or 'phase'
// --- Polar to Rectangular Calculation ---
R = parseFloat(ampLevelSlider.value);
phi_deg = parseFloat(phaseLevelSlider.value);
const phi_rad = phi_deg * (Math.PI / 180);
I = R * Math.cos(phi_rad);
Q = R * Math.sin(phi_rad);
// Clamp I and Q to their hard limits
I = Math.max(-1, Math.min(1, I));
Q = Math.max(-1, Math.min(1, Q));
// Update rectangular sliders
iLevelSlider.value = I;
qLevelSlider.value = Q;
}
updateVisuals(I, Q, R, phi_deg);
}
function updateVisuals(I, Q, R, phi_deg) {
const fc = parseFloat(carrierFreqSlider.value);
// Update text displays
iLevelVal.textContent = I.toFixed(2);
qLevelVal.textContent = Q.toFixed(2);
ampLevelVal.textContent = R.toFixed(2);
phaseLevelVal.textContent = `${phi_deg.toFixed(1)}°`;
carrierFreqVal.textContent = `${fc} Hz`;
// Generate waveform components
const i_component = new Float32Array(nSamples);
const q_component = new Float32Array(nSamples);
const rf_signal = new Float32Array(nSamples);
for(let i=0; i<nSamples; i++) {
const angle = 2 * Math.PI * fc * time[i];
i_component[i] = I * Math.cos(angle);
q_component[i] = -Q * Math.sin(angle);
rf_signal[i] = i_component[i] + q_component[i];
}
updatePlots(I, Q, R, time, i_component, q_component, rf_signal);
renderMathInElement(document.body);
}
function updatePlots(I, Q, R, t, i_comp, q_comp, rf_sig) {
const max_amp = 1.5;
const waveLayout = {
xaxis: { title: { text: 'Time (s)', font: {size: 10} }, color: '#9CA3AF', gridcolor: '#4B5563', tickfont: {size: 8}, zeroline: false },
yaxis: { title: { text: 'Amplitude', font: {size: 10} }, range: [-max_amp, max_amp], color: '#9CA3AF', gridcolor: '#4B5563', tickfont: {size: 8}, zeroline: true },
paper_bgcolor: 'rgba(0,0,0,0)', plot_bgcolor: 'rgba(0,0,0,0)',
margin: { l: 40, r: 10, b: 30, t: 10, pad: 4 }
};
Plotly.newPlot(iCompDiv, [{ x: t, y: Array.from(i_comp), type: 'scatter', mode: 'lines', line: { color: '#6EE7B7' } }], waveLayout, {responsive: true, displayModeBar: false});
Plotly.newPlot(qCompDiv, [{ x: t, y: Array.from(q_comp), type: 'scatter', mode: 'lines', line: { color: '#F472B6' } }], waveLayout, {responsive: true, displayModeBar: false});
Plotly.newPlot(rfDiv, [{ x: t, y: Array.from(rf_sig), type: 'scatter', mode: 'lines', line: { color: '#818CF8' } }], waveLayout, {responsive: true, displayModeBar: false});
const constellationLayout = {
xaxis: { title: {text: 'I (In-Phase)', font: {size: 12}}, range: [-max_amp, max_amp], color: '#9CA3AF', gridcolor: '#4B5563', zeroline: true },
yaxis: { title: {text: 'Q (Quadrature)', font: {size: 12}}, range: [-max_amp, max_amp], color: '#9CA3AF', gridcolor: '#4B5563', zeroline: true, scaleanchor: "x", scaleratio: 1 },
paper_bgcolor: 'rgba(0,0,0,0)', plot_bgcolor: 'rgba(0,0,0,0)',
margin: { l: 50, r: 20, b: 40, t: 20 },
shapes: [{
type: 'line', x0: 0, y0: 0, x1: I, y1: Q,
line: { color: '#FBBF24', width: 3 }
}]
};
Plotly.newPlot(constellationDiv, [{
x: [I], y: [Q], mode: 'markers', type: 'scatter',
marker: { color: '#FBBF24', size: 12, line: { color: 'white', width: 2 } }
}], constellationLayout, {responsive: true, displayModeBar: false});
}
// --- Event Listeners ---
iLevelSlider.addEventListener('input', () => handleSliderInput('i'));
qLevelSlider.addEventListener('input', () => handleSliderInput('q'));
ampLevelSlider.addEventListener('input', () => handleSliderInput('amp'));
phaseLevelSlider.addEventListener('input', () => handleSliderInput('phase'));
carrierFreqSlider.addEventListener('input', () => handleSliderInput(lastTouchedSlider));
// --- Initial Load ---
window.onload = () => { handleSliderInput('i'); };
window.onresize = () => { handleSliderInput(lastTouchedSlider); };
});
</script>
</body>
</html>