-
Notifications
You must be signed in to change notification settings - Fork 0
/
fft-asm-noasm.ts
54 lines (54 loc) · 1.35 KB
/
fft-asm-noasm.ts
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
//
//FFT Module without "use asm" for comparison
//
export default function FftModuleNoAsm(stdlib, foreign, heap) {
//"use asm";
var FLOAT64 = new stdlib.Float64Array(heap);
var sqrt=stdlib.Math.sqrt;
var sz=0;
function setup(n) {
n=n|0;
sz=n;
}
function fft(normalize) {
normalize=normalize|0;
var m=0, mh=0, i=0, j=0, k=0;
var wr=0.0, wi=0.0, xr=0.0, xi=0.0, w=0.0, r=0.0;
var tcnt=0;
for(mh=1;(m=mh<<1)<=(sz|0);mh=m) {
for(i=0;(i|0)<(mh|0);i=(i+1)|0) {
wr=+FLOAT64[(sz*16+tcnt)>>3];
wi=+FLOAT64[(sz*16+tcnt+8)>>3];
tcnt=(tcnt+16)|0;
for(j=i;(j|0)<(sz|0);j=(j+m)|0) {
k=(j+mh)|0;
xr=wr*FLOAT64[k<<3>>3]-wi*FLOAT64[(sz+k)<<3>>3];
xi=wr*FLOAT64[(sz+k)<<3>>3]+wi*FLOAT64[k<<3>>3];
FLOAT64[k<<3>>3] = +FLOAT64[j<<3>>3]-xr;
FLOAT64[(sz+k)<<3>>3] = +FLOAT64[(sz+j)<<3>>3]-xi;
FLOAT64[j<<3>>3] = +FLOAT64[j<<3>>3] + xr;
FLOAT64[(sz+j)<<3>>3] = +FLOAT64[(sz+j)<<3>>3] + xi;
}
}
}
if(normalize) {
r = +(1.0/+(sz|0));
for(i=0;(i|0)<(sz|0);i=(i+1)|0) {
FLOAT64[i<<3>>3]=+FLOAT64[i<<3>>3]*r;
FLOAT64[(sz+i)<<3>>3]=+FLOAT64[(sz+i)<<3>>3]*r;
}
}
}
function mag() {
var i=0,j=0;
for(i=0;(i|0)<(sz|0);i=(i+1)|0) {
j=(sz*2-i-1)|0;
FLOAT64[i<<3>>3]=sqrt(FLOAT64[i<<3>>3]*FLOAT64[i<<3>>3]+FLOAT64[j<<3>>3]*FLOAT64[j<<3>>3]);
}
}
return {
setup:setup,
fft:fft,
mag:mag
};
}