-
Notifications
You must be signed in to change notification settings - Fork 0
/
fft-asm-lib.ts
62 lines (59 loc) · 1.37 KB
/
fft-asm-lib.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
55
56
57
58
59
60
import FftModuleAsm from "./fft-asm"
import FftModuleNoAsm from "./fft-asm-noasm"
export default function FftModule(sz,asm) {
var i,j,k:number;
this.sz=sz;
this.bufsz=sz*32;
if(this.bufsz<4096)
this.bufsz=4096;
this.heap=new ArrayBuffer(this.bufsz);
this.foreign=new ArrayBuffer(4096);
this.flt64=new Float64Array(this.heap);
if(asm)
this.fftasm=FftModuleAsm(window,this.foreign,this.heap);
else
this.fftasm=FftModuleNoAsm(window,this.foreign,this.heap);
this.fftasm.setup(sz);
var t,th=Math.PI;
for(i=1,j=0;i<sz;i<<=1) {
t=0.0;
for(k=0;k<i;++k,j+=2) {
t+=th;
this.flt64[sz*2+j]=Math.cos(t);
this.flt64[sz*2+j+1]=Math.sin(t);
}
th*=0.5;
}
this.bitrev=new Array(sz);
this.bitrev[0]=0;
this.bitrev[sz-1]=sz-1;
for(j=1,i=0;j<sz-1;++j) {
for(var k=sz>>1;k>(i^=k);k>>=1)
;
this.bitrev[j]=i;
}
this.fft=function(real,imag,normalize,asm) {
var i;
for(i=0;i<this.sz;++i) {
this.flt64[this.bitrev[i]]=real[i];
this.flt64[this.sz+this.bitrev[i]]=imag[i];
}
this.fftasm.fft(normalize);
for(i=0;i<this.sz;++i) {
real[i]=this.flt64[i];
imag[i]=this.flt64[this.sz+i];
}
}
this.fftmag=function(real,imag) {
var i;
for(i=0;i<this.sz;++i) {
this.flt64[this.bitrev[i]]=real[i];
this.flt64[this.sz+this.bitrev[i]]=imag[i];
}
this.fftasm.fft(1);
this.fftasm.mag();
for(i=0;i<this.sz;++i) {
real[i]=this.flt64[i];
}
}
}