Advertisement
Guest User

FFT.java

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