-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
116 lines (108 loc) · 3.52 KB
/
script.js
File metadata and controls
116 lines (108 loc) · 3.52 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
let harmonicsChart;
function calculateHarmonics() {
const frequencyInput = document.getElementById("frequency");
const frequency = parseFloat(frequencyInput.value);
const tableBody = document.getElementById("harmonicsTableBody");
if (isNaN(frequency) || frequency <= 0) {
tableBody.innerHTML = "<tr><td colspan='6'>Please enter a valid frequency.</td></tr>";
if (harmonicsChart) harmonicsChart.destroy();
return;
}
// Calculate harmonics and properties
const harmonics = [];
for (let i = 1; i <= 10; i++) {
const harmonicFreq = frequency * i;
const wavelength = (300 / harmonicFreq).toFixed(3); // Wavelength in meters
const ml = (wavelength * 0.95).toFixed(3); // Mechanical length with shorting factor k=0.95
harmonics.push({
harmonic: i,
frequency: harmonicFreq.toFixed(3),
wavelength,
ml,
mlHalf: (ml / 2).toFixed(3),
mlQuarter: (ml / 4).toFixed(3),
});
}
// Update table with harmonics data
tableBody.innerHTML = harmonics
.map(
(h) => `
<tr>
<td>${h.harmonic}</td>
<td>${h.frequency} MHz</td>
<td>${h.wavelength} m</td>
<td>${h.ml} m</td>
<td>${h.mlHalf} m</td>
<td>${h.mlQuarter} m</td>
</tr>
`
)
.join("");
// Update chart with harmonics data
updateGraph(harmonics, frequency);
}
function updateGraph(harmonics, baseFrequency) {
const ctx = document.getElementById("harmonicsChart").getContext("2d");
if (harmonicsChart) harmonicsChart.destroy();
harmonicsChart = new Chart(ctx, {
type: "line",
data: {
labels: harmonics.map((h) => `H${h.harmonic}`),
datasets: [
{
label: `Harmonics of ${baseFrequency} MHz`,
data: harmonics.map((h) => parseFloat(h.frequency)),
backgroundColor: "rgba(33, 230, 193, 0.2)",
borderColor: "#21e6c1",
borderWidth: 2,
pointRadius: 5,
pointBackgroundColor: "#21e6c1",
},
],
},
options: {
responsive: true,
plugins: {
legend: {
display: true,
labels: {
color: "#e0e0e0",
},
},
title: {
display: true,
text: `Harmonic Frequencies of ${baseFrequency} MHz`,
color: "#e0e0e0",
font: {
size: 16,
},
},
},
scales: {
x: {
title: {
display: true,
text: "Harmonic Order",
color: "#e0e0e0",
},
ticks: {
color: "#e0e0e0",
},
},
y: {
title: {
display: true,
text: "Frequency (MHz)",
color: "#e0e0e0",
},
ticks: {
color: "#e0e0e0",
},
grid: {
color: "#2e3b55",
},
},
},
},
});
}