You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FFT.java 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package com.feemers.android.fftdrawer.SignalProcessing;
  2. /******************************************************************************
  3. * Compilation: javac FFT.java
  4. * Execution: java FFT n
  5. * Dependencies: Complex.java
  6. *
  7. * Compute the FFT and inverse FFT of a length n complex sequence
  8. * using the radix 2 Cooley-Tukey algorithm.
  9. * Bare bones implementation that runs in O(n log n) time and O(n)
  10. * space. Our goal is to optimize the clarity of the code, rather
  11. * than performance.
  12. *
  13. * This implementation uses the primitive root of unity w = e^(-2 pi i / n).
  14. * Some resources use w = e^(2 pi i / n).
  15. *
  16. * Reference: https://www.cs.princeton.edu/~wayne/kleinberg-tardos/pdf/05DivideAndConquerII.pdf
  17. *
  18. * Limitations
  19. * -----------
  20. * - assumes n is a power of 2
  21. *
  22. * - not the most memory efficient algorithm (because it uses
  23. * an object type for representing complex numbers and because
  24. * it re-allocates memory for the subarray, instead of doing
  25. * in-place or reusing a single temporary array)
  26. *
  27. * For an in-place radix 2 Cooley-Tukey FFT, see
  28. * https://introcs.cs.princeton.edu/java/97data/InplaceFFT.java.html
  29. *
  30. ******************************************************************************/
  31. public class FFT {
  32. // compute the FFT of x[], assuming its length n is a power of 2
  33. public static Complex[] fft(Complex[] x) {
  34. int n = x.length;
  35. // base case
  36. if (n == 1) return new Complex[] { x[0] };
  37. // radix 2 Cooley-Tukey FFT
  38. if (n % 2 != 0) {
  39. throw new IllegalArgumentException("n is not a power of 2");
  40. }
  41. // compute FFT of even terms
  42. Complex[] even = new Complex[n/2];
  43. for (int k = 0; k < n/2; k++) {
  44. even[k] = x[2*k];
  45. }
  46. Complex[] evenFFT = fft(even);
  47. // compute FFT of odd terms
  48. Complex[] odd = even; // reuse the array (to avoid n log n space)
  49. for (int k = 0; k < n/2; k++) {
  50. odd[k] = x[2*k + 1];
  51. }
  52. Complex[] oddFFT = fft(odd);
  53. // combine
  54. Complex[] y = new Complex[n];
  55. for (int k = 0; k < n/2; k++) {
  56. double kth = -2 * k * Math.PI / n;
  57. Complex wk = new Complex(Math.cos(kth), Math.sin(kth));
  58. y[k] = evenFFT[k].plus (wk.times(oddFFT[k]));
  59. y[k + n/2] = evenFFT[k].minus(wk.times(oddFFT[k]));
  60. }
  61. return y;
  62. }
  63. // compute the inverse FFT of x[], assuming its length n is a power of 2
  64. public static Complex[] ifft(Complex[] x) {
  65. int n = x.length;
  66. Complex[] y = new Complex[n];
  67. // take conjugate
  68. for (int i = 0; i < n; i++) {
  69. y[i] = x[i].conjugate();
  70. }
  71. // compute forward FFT
  72. y = fft(y);
  73. // take conjugate again
  74. for (int i = 0; i < n; i++) {
  75. y[i] = y[i].conjugate();
  76. }
  77. // divide by n
  78. for (int i = 0; i < n; i++) {
  79. y[i] = y[i].scale(1.0 / n);
  80. }
  81. return y;
  82. }
  83. // compute the circular convolution of x and y
  84. public static Complex[] cconvolve(Complex[] x, Complex[] y) {
  85. // should probably pad x and y with 0s so that they have same length
  86. // and are powers of 2
  87. if (x.length != y.length) {
  88. throw new IllegalArgumentException("Dimensions don't agree");
  89. }
  90. int n = x.length;
  91. // compute FFT of each sequence
  92. Complex[] a = fft(x);
  93. Complex[] b = fft(y);
  94. // point-wise multiply
  95. Complex[] c = new Complex[n];
  96. for (int i = 0; i < n; i++) {
  97. c[i] = a[i].times(b[i]);
  98. }
  99. // compute inverse FFT
  100. return ifft(c);
  101. }
  102. // compute the linear convolution of x and y
  103. public static Complex[] convolve(Complex[] x, Complex[] y) {
  104. Complex ZERO = new Complex(0, 0);
  105. Complex[] a = new Complex[2*x.length];
  106. for (int i = 0; i < x.length; i++) a[i] = x[i];
  107. for (int i = x.length; i < 2*x.length; i++) a[i] = ZERO;
  108. Complex[] b = new Complex[2*y.length];
  109. for (int i = 0; i < y.length; i++) b[i] = y[i];
  110. for (int i = y.length; i < 2*y.length; i++) b[i] = ZERO;
  111. return cconvolve(a, b);
  112. }
  113. // compute the DFT of x[] via brute force (n^2 time)
  114. public static Complex[] dft(Complex[] x) {
  115. int n = x.length;
  116. Complex ZERO = new Complex(0, 0);
  117. Complex[] y = new Complex[n];
  118. for (int k = 0; k < n; k++) {
  119. y[k] = ZERO;
  120. for (int j = 0; j < n; j++) {
  121. int power = (k * j) % n;
  122. double kth = -2 * power * Math.PI / n;
  123. Complex wkj = new Complex(Math.cos(kth), Math.sin(kth));
  124. y[k] = y[k].plus(x[j].times(wkj));
  125. }
  126. }
  127. return y;
  128. }
  129. /***************************************************************************
  130. * Test client and sample execution
  131. *
  132. * % java FFT 4
  133. * x
  134. * -------------------
  135. * -0.03480425839330703
  136. * 0.07910192950176387
  137. * 0.7233322451735928
  138. * 0.1659819820667019
  139. *
  140. * y = fft(x)
  141. * -------------------
  142. * 0.9336118983487516
  143. * -0.7581365035668999 + 0.08688005256493803i
  144. * 0.44344407521182005
  145. * -0.7581365035668999 - 0.08688005256493803i
  146. *
  147. * z = ifft(y)
  148. * -------------------
  149. * -0.03480425839330703
  150. * 0.07910192950176387 + 2.6599344570851287E-18i
  151. * 0.7233322451735928
  152. * 0.1659819820667019 - 2.6599344570851287E-18i
  153. *
  154. * c = cconvolve(x, x)
  155. * -------------------
  156. * 0.5506798633981853
  157. * 0.23461407150576394 - 4.033186818023279E-18i
  158. * -0.016542951108772352
  159. * 0.10288019294318276 + 4.033186818023279E-18i
  160. *
  161. * d = convolve(x, x)
  162. * -------------------
  163. * 0.001211336402308083 - 3.122502256758253E-17i
  164. * -0.005506167987577068 - 5.058885073636224E-17i
  165. * -0.044092969479563274 + 2.1934338938072244E-18i
  166. * 0.10288019294318276 - 3.6147323062478115E-17i
  167. * 0.5494685269958772 + 3.122502256758253E-17i
  168. * 0.240120239493341 + 4.655566391833896E-17i
  169. * 0.02755001837079092 - 2.1934338938072244E-18i
  170. * 4.01805098805014E-17i
  171. *
  172. ***************************************************************************/
  173. }