-
Notifications
You must be signed in to change notification settings - Fork 0
/
AMFM.pde
267 lines (218 loc) · 8.66 KB
/
AMFM.pde
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
Amplitude and Frequency Modulation
----------------------------------
This sketch shows the difference between amplitude and frqeuency modulation. It has a random
continuous/analog message signal that is modulated onto a carrier signal.
*/
// Global Constants
final int BG_COLOR = #1D1D1D;
final int FRAMEWORK_COLOR = #FFFAFB;
final int MESSAGE_COLOR = #8B80F9;
final int AM_COLOR = #339989;
final int FM_COLOR = #80475E;
final int CARRIER_COLOR = #FFFAFB;
final int TRIANGLE_SIZE = 10;
final int MIXER_SIZE = 1080/15;
final int WIDTH = 1080/3;
final int AMPLITUDE = 0;
final String TITLE = "Amplitude (AM) & Frequency (FM) Modulation";
final int TITLE_Y = 50;
final float FM_Y = 1080*3/4;
final float AM_Y = 1080*1/4;
final int DASHED_LINE = 15;
final float F_MESSAGE_MAX = 2;
final float F_MESSAGE_MIN = 0.1;
final float K_f = 0.01;
final float F_CARRIER = 10;
final float DELTA_THETA = 0.005;
final int GAP = 50;
// Global Variables
float xspacing = 0.5;
int signal_width;
int maxwaves = 5;
float[] message_amplitude = new float[maxwaves];
float[] message_frequencies = new float[maxwaves];
float theta = 0.0; // Start angle at 0
float carrier_amplitude = 80.0; // Height of wave
float period = 1000.0; // How many pixels before the wave repeats
float dx; // Value for incrementing X, a function of period and xspacing
float[] carrier_y; // Using an array to store height values for the wave
float[] message_y;
float[] am_y, fm_y;
int line_x_start, line_x_finish, line_x_temp_finish;
int line_y_start, line_y_finish, line_y_temp_finish;
int signals_opacity = 0;
int title_opacity = 0;
int output_line_x_start, output_line_x_finish, output_line_x_temp_finish;
int framework_opacity = 0;
String state;
void setup() {
size(1080, 1080);
pixelDensity(displayDensity());
textAlign(CENTER, CENTER);
signal_width = width/4;
dx = (TWO_PI / period) * xspacing;
int array_size = int(signal_width/xspacing);
carrier_y = new float[array_size];
message_y = new float[array_size];
am_y = new float[array_size];
fm_y = new float[array_size];
initMessageAmplitudes();
initLines();
state = "initialize";
} // End of setup()
void draw() {
background(BG_COLOR);
switch(state) {
case "initialize":
drawFramework(framework_opacity);
framework_opacity = framework_opacity >= 255 ? 255: framework_opacity + 5;
if (framework_opacity == 255) {
state = "draw";
}
break;
case "draw":
signals_opacity = signals_opacity >= 255 ? 255: signals_opacity + 5;
drawFramework(255);
calcSignals();
renderSignals(signals_opacity);
break;
}
} // End of draw()
// ---------- drawFramework() ---------
void drawFramework(int m_opacity) {
fill(FRAMEWORK_COLOR, 255);
stroke(FRAMEWORK_COLOR, 255);
strokeWeight(3);
textSize(40);
text(TITLE, width/2, TITLE_Y);
line(width/2 - textWidth(TITLE)/2, TITLE_Y + textAscent(), width/2 + textWidth(TITLE)/2, TITLE_Y + textAscent());
stroke(FRAMEWORK_COLOR, m_opacity);
fill(FRAMEWORK_COLOR, m_opacity);
textSize(20);
text("Message Signal", GAP + signal_width/2, height/2 + carrier_amplitude);
text("Carrier Signal", width/2, height/2 + carrier_amplitude*1.2);
text("AM Signal", width - GAP - signal_width/2, AM_Y + carrier_amplitude*1.5);
text("FM Signal", width - GAP - signal_width/2, FM_Y + carrier_amplitude*1.2);
text("Amplitude Modulator", width/2, AM_Y - MIXER_SIZE);
text("Frequency Modulator", width/2, FM_Y + MIXER_SIZE);
fill(BG_COLOR, 255);
strokeWeight(3);
float mixer_offset = MIXER_SIZE/2 * cos(PI/4);
// AM Mixer
circle(width/2, AM_Y, MIXER_SIZE);
line(width/2 - mixer_offset, AM_Y - mixer_offset, width/2 + mixer_offset, AM_Y + mixer_offset);
line(width/2 - mixer_offset, AM_Y + mixer_offset, width/2 + mixer_offset, AM_Y - mixer_offset);
// FM Mixer
circle(width/2, FM_Y, MIXER_SIZE);
line(width/2 - mixer_offset, FM_Y - mixer_offset, width/2 + mixer_offset, FM_Y + mixer_offset);
line(width/2 - mixer_offset, FM_Y + mixer_offset, width/2 + mixer_offset, FM_Y - mixer_offset);
stroke(FRAMEWORK_COLOR, m_opacity);
line(line_x_start, AM_Y, line_x_finish, AM_Y);
line(line_x_start, FM_Y, line_x_finish, FM_Y);
line(line_x_start, line_y_start, line_x_start, line_y_finish);
line(line_x_start, height - line_y_start, line_x_start, width - line_y_finish);
// Vertical lines from carrier
line(width/2, height/2 - carrier_amplitude*1.2, width/2, AM_Y + MIXER_SIZE/2);
line(width/2, height/2 + carrier_amplitude*1.5, width/2, FM_Y - MIXER_SIZE/2);
// Output lines
line(width/2 + MIXER_SIZE/2, AM_Y, width - GAP - signal_width*1.1, AM_Y);
line(width/2 + MIXER_SIZE/2, FM_Y, width - GAP - signal_width*1.1, FM_Y);
fill(FRAMEWORK_COLOR, m_opacity);
float x_1 = width/2 - TRIANGLE_SIZE - MIXER_SIZE/2;
float y_1 = AM_Y - TRIANGLE_SIZE/2;
float x_2 = width/2 - TRIANGLE_SIZE - MIXER_SIZE/2;
float y_2 = AM_Y + TRIANGLE_SIZE/2;
float x_3 = width/2 - MIXER_SIZE/2-2;
float y_3 = AM_Y;
triangle(x_1, y_1, x_2, y_2, x_3, y_3);
triangle(x_1, height - y_1, x_2, height - y_2, x_3, height - y_3);
x_1 = width/2 - TRIANGLE_SIZE/2;
y_1 = AM_Y + MIXER_SIZE/2 + TRIANGLE_SIZE;
x_2 = width/2 + TRIANGLE_SIZE/2;
y_2 = AM_Y + MIXER_SIZE/2 + TRIANGLE_SIZE;
x_3 = width/2;
y_3 = AM_Y + MIXER_SIZE/2 + 3;
triangle(x_1, y_1, x_2, y_2, x_3, y_3);
triangle(x_1, height - y_1, x_2, height - y_2, x_3, height - y_3);
x_1 = width - GAP - signal_width*1.1 - TRIANGLE_SIZE;
y_1 = AM_Y + TRIANGLE_SIZE/2;
x_2 = width - GAP - signal_width*1.1 - TRIANGLE_SIZE ;
y_2 = AM_Y - TRIANGLE_SIZE/2;
x_3 = width - GAP - signal_width*1.1;
y_3 = AM_Y;
triangle(x_1, y_1, x_2, y_2, x_3, y_3);
triangle(x_1, height - y_1, x_2, height - y_2, x_3, height - y_3);
} // End of drawFramework()
void initLines() {
line_x_start = GAP + signal_width/2;
line_x_finish = width/2 - MIXER_SIZE/2;
line_y_start = height/2 - int(carrier_amplitude) - 25;
line_y_finish = int(AM_Y);
output_line_x_start = width/2 + MIXER_SIZE/2;
output_line_x_finish = width - width/12 - WIDTH;
output_line_x_temp_finish = output_line_x_start;
}
void initMessageAmplitudes() {
for (int i = 0; i < maxwaves; i++) {
message_amplitude[i] = random(5,17);
message_frequencies[i] = random(F_MESSAGE_MIN, F_MESSAGE_MAX);
}
}
void calcSignals() {
theta += DELTA_THETA;
float x = theta;
// Set all height values to zero
for (int i = 0; i < message_y.length; i++) {
message_y[i] = 0;
}
// Accumulate wave height values
for (int j = 0; j < maxwaves; j++) {
x = theta;
for (int i = 0; i < message_y.length; i++) {
// Every other wave is cosine instead of sine
if (j % 2 == 0) message_y[i] += sin(2*PI*message_frequencies[j]*x)*message_amplitude[j];
else message_y[i] += cos(2*PI*message_frequencies[j]*x)*message_amplitude[j];
x+=dx;
}
}
x = theta;
float sum = 0.0;
for (int i = 0; i < am_y.length; i++) {
// CARRIER
carrier_y[i] = sin(2*PI*F_CARRIER*x)*carrier_amplitude;
// Calc AM
am_y[i] = (1-message_y[i]/carrier_amplitude)*carrier_amplitude*sin(2*PI*F_CARRIER*x);
// Calc FM
sum += message_y[i]/2 - 15;
float phi = 2*PI*F_CARRIER*x - K_f*(sum+15);
fm_y[i] = carrier_amplitude*cos(phi);
x+=dx;
}
}
void renderSignals(int s_opacity) {
strokeWeight(2);
// A simple way to draw the wave with an ellipse at each location
for (int x = 1; x < carrier_y.length; x++) {
// Carrier
stroke(CARRIER_COLOR, s_opacity);
line((x-1)*xspacing+width/2 - signal_width/2, height/2 + carrier_y[x-1], x*xspacing+width/2 - signal_width/2, height/2+carrier_y[x]);
// Message
stroke(MESSAGE_COLOR, s_opacity);
line((x-1)*xspacing + GAP, height/2 + message_y[x-1], x*xspacing + GAP, height/2 + message_y[x]);
// AM
stroke(AM_COLOR, s_opacity);
line((x-1)*xspacing+width - GAP -signal_width, AM_Y + am_y[x-1], x*xspacing+width - GAP -signal_width, AM_Y + am_y[x]);
if ((x / DASHED_LINE) % 2 == 0){
stroke(MESSAGE_COLOR, s_opacity);
line((x-1)*xspacing+width - GAP - signal_width, AM_Y + message_y[x-1] - carrier_amplitude, x*xspacing+width - GAP -signal_width, AM_Y + message_y[x]-carrier_amplitude);
}
// FM
stroke(FM_COLOR, s_opacity);
line((x-1)*xspacing+width - GAP -signal_width, FM_Y + fm_y[x-1], x*xspacing+width - GAP -signal_width, FM_Y+fm_y[x]);
if ((x / DASHED_LINE) % 2 == 0){
stroke(MESSAGE_COLOR, s_opacity);
line((x-1)*xspacing+width - GAP - signal_width, FM_Y + message_y[x-1] - carrier_amplitude*1.75, x*xspacing+width - GAP -signal_width, FM_Y + message_y[x]-carrier_amplitude*1.75);
}
}
}