-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfourier.js
173 lines (124 loc) · 3.13 KB
/
fourier.js
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
let time = 0;
let wave = [];
const MAX_POINTS = 1200;
let samples = [];
const MIN_X = 0;
const MAX_X = 3 * Math.PI;
const STEP_X = 0.01;
const MIN_FREC = -5;
const MAX_FREC = 5;
const STEP_F = 0.01;
const FUNCTION = 0;
const FOURIER = 1;
const END = 2;
const cx = 200;
const cy = 400;
const rad = 150;
const diam = rad * 2;
const PI2 = Math.PI * 2;
let state = FOURIER;
let frecIndex = 0;
let RESULT = {};
function myFunction(x) {
return sin(x);
}
function setup() {
createCanvas(1500, 800);
for (var i = MIN_X; i <= MAX_X; i += STEP_X) {
samples.push( new Complex(i, myFunction(i)) );
}
RESULT = fourier(myFunction, MIN_X, MAX_X, STEP_X, MIN_FREC, MAX_FREC, STEP_F);
console.log(RESULT);
}
function fourier(fn, x1, x2, stepX, fra, frb, stepF) {
//console.log(fn, x1, x2, fra, frb, step);
var __aux;
var sm = [], ms = [], fr = [];
var cp, mass, cant;
var frec, ini;
for (frec = fra + 10 * stepF; frec <= frb; frec += stepF) {
__aux = [];
mass = new Complex({ re: 0, im: 0 });
cant = 0;
for (ini = x1; ini <= x2; ini += stepX) {
cp = new Complex({
arg : -2 * Math.PI * frec * ini,
abs : fn(ini) * rad
});
mass = mass.add(cp);
// if ( isNaN(mass.re) || isNaN(mass.im) ) {
// console.log('NAN: ', cp, mass, frec, ini, rad);
// return;
// }
__aux.push(cp);
cant += 1;
}
// console.log('MASS: ', mass, ' CANT: ', cant);
// console.log('CP: ', cp);
mass = mass.div(cant);
sm.push(__aux);
ms.push(mass);
fr.push(frec);
//break;
}
return [ sm, ms, fr ];
}
function draw() {
background(0);
stroke(255, 80);
strokeWeight(3);
noFill();
translate(cx, cy);
ellipse(0, 0, diam);
if ( state === END ) {
return;
}
if ( time >= samples.length ) {
// time -= samples.length;
time = 0;
state = FOURIER;
} else if ( frecIndex >= RESULT[2].length ) {
frecIndex = 0;
// state = FUNCTION;
state = END;
}
if ( state === FUNCTION ) {
beginShape();
noFill();
stroke(color(200, 45, 45), 150);
for (let i = 0, maxi = floor(time); i < maxi; i += 1) {
vertex(samples[i].re * 20, samples[i].im * 20);
}
endShape();
time += 10;
} else if ( state === FOURIER ) {
beginShape();
stroke(color(45, 45, 200), 150);
noFill();
for (let i = 0, maxi = RESULT[0][frecIndex].length; i < maxi; i += 1) {
vertex(RESULT[0][frecIndex][i].re, RESULT[0][frecIndex][i].im);
}
endShape();
beginShape();
stroke(color(200, 45, 45), 150);
fill(color(200, 45, 45), 150);
ellipse(RESULT[1][frecIndex].re, RESULT[1][frecIndex].im, 10);
endShape();
beginShape();
stroke(color(45, 200, 45), 150);
translate(rad * 2, 0);
noFill();
let scale = 100;
let offset = 200;
for (let i = 0, maxi = frecIndex; i <= maxi; i += 1) {
vertex(offset + scale * RESULT[2][i], -RESULT[1][i].re);
}
// translate(-rad, 0);
// translate(-cx, -cy);
endShape();
frecIndex += 1;
// state = FUNCTION;
// state = END;
}
translate(-100, -200);
}