- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathFFT.java
296 lines (271 loc) · 8.52 KB
/
FFT.java
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
packagecom.thealgorithms.maths;
importjava.util.ArrayList;
importjava.util.Collection;
importjava.util.Collections;
/**
* Class for calculating the Fast Fourier Transform (FFT) of a discrete signal
* using the Cooley-Tukey algorithm.
*
* @author Ioannis Karavitsis
* @version 1.0
*/
publicfinalclassFFT {
privateFFT() {
}
/**
* This class represents a complex number and has methods for basic
* operations.
*
* <p>
* More info:
* https://introcs.cs.princeton.edu/java/32class/Complex.java.html
*/
staticclassComplex {
privatedoublereal;
privatedoubleimg;
/**
* Default Constructor. Creates the complex number 0.
*/
Complex() {
real = 0;
img = 0;
}
/**
* Constructor. Creates a complex number.
*
* @param r The real part of the number.
* @param i The imaginary part of the number.
*/
Complex(doubler, doublei) {
real = r;
img = i;
}
/**
* Returns the real part of the complex number.
*
* @return The real part of the complex number.
*/
publicdoublegetReal() {
returnreal;
}
/**
* Returns the imaginary part of the complex number.
*
* @return The imaginary part of the complex number.
*/
publicdoublegetImaginary() {
returnimg;
}
/**
* Adds this complex number to another.
*
* @param z The number to be added.
* @return The sum.
*/
publicComplexadd(Complexz) {
Complextemp = newComplex();
temp.real = this.real + z.real;
temp.img = this.img + z.img;
returntemp;
}
/**
* Subtracts a number from this complex number.
*
* @param z The number to be subtracted.
* @return The difference.
*/
publicComplexsubtract(Complexz) {
Complextemp = newComplex();
temp.real = this.real - z.real;
temp.img = this.img - z.img;
returntemp;
}
/**
* Multiplies this complex number by another.
*
* @param z The number to be multiplied.
* @return The product.
*/
publicComplexmultiply(Complexz) {
Complextemp = newComplex();
temp.real = this.real * z.real - this.img * z.img;
temp.img = this.real * z.img + this.img * z.real;
returntemp;
}
/**
* Multiplies this complex number by a scalar.
*
* @param n The real number to be multiplied.
* @return The product.
*/
publicComplexmultiply(doublen) {
Complextemp = newComplex();
temp.real = this.real * n;
temp.img = this.img * n;
returntemp;
}
/**
* Finds the conjugate of this complex number.
*
* @return The conjugate.
*/
publicComplexconjugate() {
Complextemp = newComplex();
temp.real = this.real;
temp.img = -this.img;
returntemp;
}
/**
* Finds the magnitude of the complex number.
*
* @return The magnitude.
*/
publicdoubleabs() {
returnMath.hypot(this.real, this.img);
}
/**
* Divides this complex number by another.
*
* @param z The divisor.
* @return The quotient.
*/
publicComplexdivide(Complexz) {
Complextemp = newComplex();
doubled = z.abs() * z.abs();
d = (double) Math.round(d * 1000000000d) / 1000000000d;
temp.real = (this.real * z.real + this.img * z.img) / (d);
temp.img = (this.img * z.real - this.real * z.img) / (d);
returntemp;
}
/**
* Divides this complex number by a scalar.
*
* @param n The divisor which is a real number.
* @return The quotient.
*/
publicComplexdivide(doublen) {
Complextemp = newComplex();
temp.real = this.real / n;
temp.img = this.img / n;
returntemp;
}
publicdoublereal() {
returnreal;
}
publicdoubleimaginary() {
returnimg;
}
}
/**
* Iterative In-Place Radix-2 Cooley-Tukey Fast Fourier Transform Algorithm
* with Bit-Reversal. The size of the input signal must be a power of 2. If
* it isn't then it is padded with zeros and the output FFT will be bigger
* than the input signal.
*
* <p>
* More info:
* https://www.algorithm-archive.org/contents/cooley_tukey/cooley_tukey.html
* https://www.geeksforgeeks.org/iterative-fast-fourier-transformation-polynomial-multiplication/
* https://en.wikipedia.org/wiki/Cooley%E2%80%93Tukey_FFT_algorithm
* https://cp-algorithms.com/algebra/fft.html
* @param x The discrete signal which is then converted to the FFT or the
* IFFT of signal x.
* @param inverse True if you want to find the inverse FFT.
* @return
*/
publicstaticArrayList<Complex> fft(ArrayList<Complex> x, booleaninverse) {
/* Pad the signal with zeros if necessary */
paddingPowerOfTwo(x);
intn = x.size();
intlog2n = findLog2(n);
x = fftBitReversal(n, log2n, x);
intdirection = inverse ? -1 : 1;
/* Main loop of the algorithm */
for (intlen = 2; len <= n; len *= 2) {
doubleangle = -2 * Math.PI / len * direction;
Complexwlen = newComplex(Math.cos(angle), Math.sin(angle));
for (inti = 0; i < n; i += len) {
Complexw = newComplex(1, 0);
for (intj = 0; j < len / 2; j++) {
Complexu = x.get(i + j);
Complexv = w.multiply(x.get(i + j + len / 2));
x.set(i + j, u.add(v));
x.set(i + j + len / 2, u.subtract(v));
w = w.multiply(wlen);
}
}
}
x = inverseFFT(n, inverse, x);
returnx;
}
/* Find the log2(n) */
publicstaticintfindLog2(intn) {
intlog2n = 0;
while ((1 << log2n) < n) {
log2n++;
}
returnlog2n;
}
/* Swap the values of the signal with bit-reversal method */
publicstaticArrayList<Complex> fftBitReversal(intn, intlog2n, ArrayList<Complex> x) {
intreverse;
for (inti = 0; i < n; i++) {
reverse = reverseBits(i, log2n);
if (i < reverse) {
Collections.swap(x, i, reverse);
}
}
returnx;
}
/* Divide by n if we want the inverse FFT */
publicstaticArrayList<Complex> inverseFFT(intn, booleaninverse, ArrayList<Complex> x) {
if (inverse) {
for (inti = 0; i < x.size(); i++) {
Complexz = x.get(i);
x.set(i, z.divide(n));
}
}
returnx;
}
/**
* This function reverses the bits of a number. It is used in Cooley-Tukey
* FFT algorithm.
*
* <p>
* E.g. num = 13 = 00001101 in binary log2n = 8 Then reversed = 176 =
* 10110000 in binary
*
* <p>
* More info: https://cp-algorithms.com/algebra/fft.html
* https://www.geeksforgeeks.org/write-an-efficient-c-program-to-reverse-bits-of-a-number/
*
* @param num The integer you want to reverse its bits.
* @param log2n The number of bits you want to reverse.
* @return The reversed number
*/
privatestaticintreverseBits(intnum, intlog2n) {
intreversed = 0;
for (inti = 0; i < log2n; i++) {
if ((num & (1 << i)) != 0) {
reversed |= 1 << (log2n - 1 - i);
}
}
returnreversed;
}
/**
* This method pads an ArrayList with zeros in order to have a size equal to
* the next power of two of the previous size.
*
* @param x The ArrayList to be padded.
*/
privatestaticvoidpaddingPowerOfTwo(Collection<Complex> x) {
intn = 1;
intoldSize = x.size();
while (n < oldSize) {
n *= 2;
}
for (inti = 0; i < n - oldSize; i++) {
x.add(newComplex());
}
}
}