-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDFT.js
More file actions
executable file
·49 lines (47 loc) · 1.17 KB
/
DFT.js
File metadata and controls
executable file
·49 lines (47 loc) · 1.17 KB
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
export const dft = (data) => {
const len = data.length;
//const re = new Array(len);
//const im = new Array(len);
const re = new Float32Array(len);
const im = new Float32Array(len);
for (let i = 0; i < len; i++) {
let re1 = 0;
let im1 = 0;
const dth = 2 * Math.PI * i / len;
let th = 0;
for (let j = 0; j < len; j++) {
const d = data[j];
re1 += d * Math.cos(th);
im1 -= d * Math.sin(th);
th += dth;
}
re[i] = re1;
im[i] = im1;
}
return [re, im];
};
export const idft = (re, im) => {
const len = re.length;
const re2 = new Array(len);
const im2 = new Array(len);
for (let i = 0; i < len; i++) {
let re1 = 0.0;
//let im1 = 0.0;
for (let j = 0; j < len; j++) {
const th = 2 * Math.PI * i * j / len;
re1 += re[j] * Math.cos(th) - im[j] * Math.sin(th);
//im1 += re[j] * Math.sin(th) + im[j] * Math.cos(th);
}
re2[i] = re1 / len;
//im2[i] = im1 / len;
}
//return [re2, im2];
return re2;
};
export const makeLen = (re, im) => {
const res = new Array(re.length);
for (let i = 0; i < re.length; i++) {
res[i] = Math.sqrt(re[i] * re[i] + im[i] * im[i]);
}
return res;
};