Gruppe 1
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 7.6KB

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