Skip to content

Commit f4e2151

Browse files
committed
Implemented Fourier wheel
1 parent 097a2ba commit f4e2151

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

fourier_series/basic.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ let context = canvas.getContext("2d");
1515
let freq_display = document.getElementById("freq-display");
1616
let freq_input = document.getElementById("freq-input");
1717

18+
let sampling_freq_display = document.getElementById("sampling-freq-display");
19+
let sampling_freq_input = document.getElementById("sampling-freq-input");
20+
1821
let wave_type = document.getElementById("wave-type");
1922
let waves_display = document.getElementById("waves-display");
23+
let fourier_display = document.getElementById("fourier-display");
2024

2125
if (mobile) {
2226
canvas_width = 0.9 * screen_width;
@@ -55,6 +59,8 @@ window.onload = function () {
5559

5660
function defaultParams() {
5761
freq_input.value = 1;
62+
sampling_freq_input.value = 1;
63+
wave_type.value = "sin";
5864
}
5965

6066
let click_x, click_y, pressed;

fourier_series/simulation.html

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ <h2>Fourier Series</h2>
5656
</div>
5757
<div class="col s12 l4">
5858
<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')"
59+
<span id="freq-display">Frequency: </span>
60+
<input id="freq-input" type="range" min="0.5" max="10" step="0.1" value="1" oninput="updateParams('freq')"
6161
onchange="updateParams('freq')">
6262
<select id="wave-type" class="browser-default">
6363
<option value="sin">Sine</option>
@@ -68,6 +68,11 @@ <h2>Fourier Series</h2>
6868
<hr>
6969

7070
<p id="waves-display"></p>
71+
<p id="fourier-display"></p>
72+
73+
<span id="sampling-freq-display">Sampling Frequency: </span>
74+
<input id="sampling-freq-input" type="range" min="0.5" max="10" step="0.1" value="1"
75+
oninput="updateParams('sampling-freq')" onchange="updateParams('sampling-freq')">
7176
</center>
7277
</div>
7378
</div>

fourier_series/simulation.js

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
let waves = [];
2+
13
let graph_xrange, graph_xscale;
24
let graph_y, graph_yrange;
35

4-
let waves = [];
6+
let circle_y, circle_radius;
7+
8+
let sampling_freq = 1;
9+
let dtheta = 0;
510

611
let updated;
712

@@ -10,9 +15,64 @@ function update() {
1015
}
1116

1217
function render() {
18+
context.fillStyle = "#000000";
1319
context.fillRect(0, 0, canvas_width, canvas_height);
1420

1521
drawGraph();
22+
drawCircle();
23+
}
24+
25+
function drawCircle() {
26+
context.beginPath();
27+
context.moveTo(canvas_width / 2, circle_y - circle_radius);
28+
context.lineTo(canvas_width / 2, circle_y + circle_radius);
29+
context.stroke();
30+
31+
context.beginPath();
32+
context.moveTo(canvas_width / 2 - circle_radius, circle_y);
33+
context.lineTo(canvas_width / 2 + circle_radius, circle_y);
34+
context.stroke();
35+
36+
if (waves.length > 0) {
37+
let x_values = [];
38+
let y_values = [];
39+
let r, theta;
40+
41+
context.beginPath();
42+
for (let i = 0; i < graph_xrange; i += 0.001) {
43+
r = 0;
44+
for (let wave of waves) {
45+
if (wave["type"] == "sin") {
46+
r += Math.sin(2 * Math.PI * wave["freq"] * i);
47+
}
48+
else if (wave["type"] == "cos") {
49+
r += Math.cos(2 * Math.PI * wave["freq"] * i);
50+
}
51+
}
52+
theta = i * 2 * Math.PI * sampling_freq;
53+
x_values.push(r * Math.cos(theta));
54+
y_values.push(r * Math.sin(theta));
55+
r = 0.9 * circle_radius * r / waves.length;
56+
57+
context.lineTo(canvas_width / 2 + r * Math.cos(theta), circle_y - r * Math.sin(theta));
58+
}
59+
context.stroke();
60+
61+
let com_x = 0, com_y = 0;
62+
for (let i = 0; i < x_values.length; i++) {
63+
com_x += x_values[i];
64+
com_y += y_values[i];
65+
}
66+
com_x /= x_values.length;
67+
com_y /= y_values.length;
68+
let magnitude = Math.sqrt(com_x * com_x + com_y * com_y);
69+
70+
fourier_display.innerHTML = `F(${sampling_freq}) = ${com_x.toFixed(2)} + (${com_y.toFixed(2)})i`;
71+
fourier_display.innerHTML += `<br> Magnitude = ${magnitude.toFixed(2)}`;
72+
}
73+
else {
74+
fourier_display.innerHTML = "F(all) = 0 <br> Magnitude = 0";
75+
}
1676
}
1777

1878
function drawGraph() {
@@ -72,6 +132,11 @@ function updateParams(variable) {
72132
if (variable == "freq") {
73133
freq_display.innerHTML = `Wave frequency: ${freq_input.value} Hz`;
74134
}
135+
else if (variable == "sampling-freq") {
136+
sampling_freq_display.innerHTML = `Sampling frequency: ${sampling_freq_input.value} Hz`;
137+
sampling_freq = parseFloat(sampling_freq_input.value);
138+
updated = true;
139+
}
75140
}
76141

77142
function addWave() {
@@ -93,7 +158,11 @@ function clearWaves() {
93158
function initParams() {
94159
graph_xrange = 1;
95160
graph_xscale = graph_xrange / canvas_width;
161+
dtheta = 1 / Math.PI;
162+
96163
graph_y = 0.1 * canvas_height;
164+
circle_y = 0.6 * canvas_height;
165+
circle_radius = 0.35 * canvas_height;
97166

98167
waves.push({
99168
"type": "sin",
@@ -103,4 +172,5 @@ function initParams() {
103172
updated = true;
104173

105174
updateParams('freq');
175+
updateParams('sampling-freq');
106176
}

0 commit comments

Comments
 (0)