1
+ let waves = [ ] ;
2
+
1
3
let graph_xrange , graph_xscale ;
2
4
let graph_y , graph_yrange ;
3
5
4
- let waves = [ ] ;
6
+ let circle_y , circle_radius ;
7
+
8
+ let sampling_freq = 1 ;
9
+ let dtheta = 0 ;
5
10
6
11
let updated ;
7
12
@@ -10,9 +15,64 @@ function update() {
10
15
}
11
16
12
17
function render ( ) {
18
+ context . fillStyle = "#000000" ;
13
19
context . fillRect ( 0 , 0 , canvas_width , canvas_height ) ;
14
20
15
21
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
+ }
16
76
}
17
77
18
78
function drawGraph ( ) {
@@ -72,6 +132,11 @@ function updateParams(variable) {
72
132
if ( variable == "freq" ) {
73
133
freq_display . innerHTML = `Wave frequency: ${ freq_input . value } Hz` ;
74
134
}
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
+ }
75
140
}
76
141
77
142
function addWave ( ) {
@@ -93,7 +158,11 @@ function clearWaves() {
93
158
function initParams ( ) {
94
159
graph_xrange = 1 ;
95
160
graph_xscale = graph_xrange / canvas_width ;
161
+ dtheta = 1 / Math . PI ;
162
+
96
163
graph_y = 0.1 * canvas_height ;
164
+ circle_y = 0.6 * canvas_height ;
165
+ circle_radius = 0.35 * canvas_height ;
97
166
98
167
waves . push ( {
99
168
"type" : "sin" ,
@@ -103,4 +172,5 @@ function initParams() {
103
172
updated = true ;
104
173
105
174
updateParams ( 'freq' ) ;
175
+ updateParams ( 'sampling-freq' ) ;
106
176
}
0 commit comments