-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgs-utils-fft.js
164 lines (138 loc) · 4.47 KB
/
gs-utils-fft.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
"use strict";
function GSUfft( signal ) {
let complexSignal = {};
if ( signal.real === undefined || signal.imag === undefined ) {
complexSignal = GSUfftConstructComplexArray_( signal );
} else {
complexSignal.real = signal.real.slice();
complexSignal.imag = signal.imag.slice();
}
const N = complexSignal.real.length;
const logN = Math.log2( N );
if ( Math.round( logN ) !== logN ) {
throw new Error( "GSUfft: Input size must be a power of 2." );
}
if ( complexSignal.real.length !== complexSignal.imag.length ) {
throw new Error( "GSUfft: Real and imaginary components must have the same length." );
}
const bitReversedIndices = GSUfftBitReverseArray_( N );
// sort array
const ordered = {
real: [],
imag: [],
};
for ( let i = 0; i < N; ++i ) {
ordered.real[ bitReversedIndices[ i ] ] = complexSignal.real[ i ];
ordered.imag[ bitReversedIndices[ i ] ] = complexSignal.imag[ i ];
}
for ( let i = 0; i < N; ++i ) {
complexSignal.real[ i ] = ordered.real[ i ];
complexSignal.imag[ i ] = ordered.imag[ i ];
}
// iterate over the number of stages
for ( let n = 1; n <= logN; ++n ) {
const currN = 2 ** n;
// find twiddle factors
for ( let k = 0; k < currN / 2; ++k ) {
const twiddle = GSUfftEuler_(k, currN);
// on each block of FT, implement the butterfly diagram
for ( let m = 0; m < N / currN; ++m ) {
const currEvenIndex = ( currN * m ) + k;
const currOddIndex = ( currN * m ) + k + ( currN / 2 );
const currEvenIndexSample = {
real: complexSignal.real[ currEvenIndex ],
imag: complexSignal.imag[ currEvenIndex ],
};
const currOddIndexSample = {
real: complexSignal.real[ currOddIndex ],
imag: complexSignal.imag[ currOddIndex ],
};
const odd = GSUfftMultiply_( twiddle, currOddIndexSample );
const subtractionResult = GSUfftSubtract_( currEvenIndexSample, odd );
complexSignal.real[ currOddIndex ] = subtractionResult.real;
complexSignal.imag[ currOddIndex ] = subtractionResult.imag;
const additionResult = GSUfftAdd_( odd, currEvenIndexSample );
complexSignal.real[ currEvenIndex ] = additionResult.real;
complexSignal.imag[ currEvenIndex ] = additionResult.imag;
}
}
}
return complexSignal;
}
// .............................................................................
function GSUifft( signal ) {
if ( !signal.real || !signal.imag ) {
throw new Error( "GSUifft: only accepts a complex input." );
}
const N = signal.real.length;
const complexSignal = {
real: [],
imag: [],
};
// take complex conjugate in order to be able to use the regular FFT for IFFT
for ( let i = 0; i < N; ++i ) {
const currentSample = {
real: signal.real[ i ],
imag: signal.imag[ i ],
};
const conjugateSample = GSUfftConj_( currentSample );
complexSignal.real[ i ] = conjugateSample.real;
complexSignal.imag[ i ] = conjugateSample.imag;
}
const X = GSUfft( complexSignal );
complexSignal.real = X.real.map( val => val / N );
complexSignal.imag = X.imag.map( val => val / N );
return complexSignal;
}
// .............................................................................
function GSUfftConstructComplexArray_( signal ) {
const real = signal.real
? signal.real.slice()
: signal.slice();
return {
real,
imag: GSUnewArray( real.length, 0 ),
};
}
function GSUfftBitReverseArray_( N ) {
const maxBinaryLength = ( N - 1 ).toString( 2 ).length; // get the binary length of the largest index.
const templateBinary = "0".repeat( maxBinaryLength ); // create a template binary of that length.
const reversed = {};
for ( let n = 0; n < N; ++n ) {
let currBinary = n.toString( 2 ); // get binary value of current index.
// prepend zeros from template to current binary. This makes binary values of all indices have the same length.
currBinary = templateBinary.substr( currBinary.length ) + currBinary;
currBinary = [ ...currBinary ].reverse().join( "" ); // reverse
reversed[ n ] = parseInt( currBinary, 2 ); // convert to decimal
}
return reversed;
}
function GSUfftMultiply_( a, b ) {
return {
real: a.real * b.real - a.imag * b.imag,
imag: a.real * b.imag + a.imag * b.real,
};
}
function GSUfftAdd_( a, b ) {
return {
real: a.real + b.real,
imag: a.imag + b.imag,
};
}
function GSUfftSubtract_( a, b ) {
return {
real: a.real - b.real,
imag: a.imag - b.imag,
};
}
function GSUfftEuler_( kn, N ) {
const x = -2 * Math.PI * kn / N;
return {
real: Math.cos( x ),
imag: Math.sin( x ),
};
}
function GSUfftConj_( a ) {
a.imag *= -1;
return a;
}