Skip to content

Commit 097a2ba

Browse files
committed
Implemented wave addition
1 parent e76979b commit 097a2ba

File tree

3 files changed

+116
-4
lines changed

3 files changed

+116
-4
lines changed

fourier_series/basic.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ if (/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(naviga
1212
let canvas = document.getElementById("canvas");
1313
let context = canvas.getContext("2d");
1414

15+
let freq_display = document.getElementById("freq-display");
16+
let freq_input = document.getElementById("freq-input");
17+
18+
let wave_type = document.getElementById("wave-type");
19+
let waves_display = document.getElementById("waves-display");
20+
1521
if (mobile) {
1622
canvas_width = 0.9 * screen_width;
1723
}
@@ -34,7 +40,10 @@ function step() {
3440
if (!paused) {
3541
update();
3642
}
37-
render();
43+
if (updated) {
44+
render();
45+
updated = false;
46+
}
3847
animate(step);
3948
}
4049

@@ -45,7 +54,7 @@ window.onload = function () {
4554
}
4655

4756
function defaultParams() {
48-
57+
freq_input.value = 1;
4958
}
5059

5160
let click_x, click_y, pressed;

fourier_series/simulation.html

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,20 @@ <h2>Fourier Series</h2>
5555
<canvas id="canvas"></canvas>
5656
</div>
5757
<div class="col s12 l4">
58-
58+
<center>
59+
<span id="freq-display">Frequency: 1</span>
60+
<input id="freq-input" type="range" min="0.5" max="10" step="0.5" value="1" oninput="updateParams('freq')"
61+
onchange="updateParams('freq')">
62+
<select id="wave-type" class="browser-default">
63+
<option value="sin">Sine</option>
64+
<option value="cos">Cosine</option>
65+
</select> <br>
66+
<button class="btn purple darken-4" onclick="addWave()">Add Wave</button>
67+
<button class="btn purple darken-4" onclick="clearWaves()">Clear Waves</button>
68+
<hr>
69+
70+
<p id="waves-display"></p>
71+
</center>
5972
</div>
6073
</div>
6174
</div>

fourier_series/simulation.js

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,106 @@
1+
let graph_xrange, graph_xscale;
2+
let graph_y, graph_yrange;
3+
4+
let waves = [];
5+
6+
let updated;
7+
18
function update() {
29

310
}
411

512
function render() {
6-
context.fillStyle = "#000000";
713
context.fillRect(0, 0, canvas_width, canvas_height);
14+
15+
drawGraph();
16+
}
17+
18+
function drawGraph() {
19+
context.strokeStyle = "#ffffff";
20+
context.beginPath();
21+
context.moveTo(0, graph_y);
22+
context.lineTo(canvas_width, graph_y);
23+
context.stroke();
24+
25+
let output = [];
26+
for (let i = 0; i < canvas_width; i++) {
27+
output.push(0);
28+
}
29+
30+
if (waves.length > 0) {
31+
for (let wave of waves) {
32+
if (wave["type"] == "sin") {
33+
for (let i = 0; i < canvas_width; i++) {
34+
output[i] += Math.sin(2 * Math.PI * wave["freq"] * i * graph_xscale);
35+
}
36+
37+
}
38+
else if (wave["type"] == "cos") {
39+
for (let i = 0; i < canvas_width; i++) {
40+
output[i] += Math.cos(2 * Math.PI * wave["freq"] * i * graph_xscale);
41+
}
42+
}
43+
}
44+
45+
let max_abs = 0;
46+
for (let i = 0; i < canvas_width; i++) {
47+
if (Math.abs(output[i]) > max_abs) {
48+
max_abs = Math.abs(output[i]);
49+
}
50+
}
51+
52+
context.beginPath();
53+
for (let i = 0; i < canvas_width; i++) {
54+
context.lineTo(i, graph_y * (1 - output[i] / max_abs));
55+
}
56+
context.stroke();
57+
58+
waves_display.innerHTML = "f(x) = ";
59+
for (let i = 0; i < waves.length; i++) {
60+
waves_display.innerHTML += `${waves[i]["type"]}(2&pi;[${waves[i]["freq"]}]x)`;
61+
if (i < waves.length - 1) {
62+
waves_display.innerHTML += " + ";
63+
}
64+
}
65+
}
66+
else {
67+
waves_display.innerHTML = "f(x) = 0";
68+
}
869
}
970

1071
function updateParams(variable) {
72+
if (variable == "freq") {
73+
freq_display.innerHTML = `Wave frequency: ${freq_input.value} Hz`;
74+
}
75+
}
76+
77+
function addWave() {
78+
let frequency = parseFloat(freq_input.value);
79+
let type = document.getElementById("wave-type").value;
1180

81+
waves.push({
82+
"freq": frequency,
83+
"type": type
84+
});
85+
updated = true;
86+
}
87+
88+
function clearWaves() {
89+
waves = [];
90+
updated = true;
1291
}
1392

1493
function initParams() {
94+
graph_xrange = 1;
95+
graph_xscale = graph_xrange / canvas_width;
96+
graph_y = 0.1 * canvas_height;
97+
98+
waves.push({
99+
"type": "sin",
100+
"freq": 1
101+
});
102+
103+
updated = true;
15104

105+
updateParams('freq');
16106
}

0 commit comments

Comments
 (0)