Guest User

Untitled

a guest
Oct 5th, 2014
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package com.corticalcafe.primitives.math;
  7.  
  8. /**
  9.  *
  10.  */
  11.  
  12. public class FFT {
  13.  
  14.  
  15.     /*
  16.      * Compute an FFT of an array of length 2^x
  17.      *
  18.      */
  19.     public static Complex[] fftPower2(Complex[] x) {
  20.         int N = x.length;
  21.  
  22.         // base case
  23.         if (N == 1) return new Complex[] { x[0] };
  24.  
  25.         // radix 2 Cooley-Tukey FFT
  26.         if (N % 2 != 0) { throw new RuntimeException("N is not a power of 2"); }
  27.  
  28.         // fft of even terms
  29.         Complex[] even = new Complex[N/2];
  30.         for (int k = 0; k < N/2; k++) {
  31.             even[k] = x[2*k];
  32.         }
  33.         Complex[] q = fftPower2(even);
  34.  
  35.         // fft of odd terms
  36.         Complex[] odd  = even;  // reuse the array
  37.         for (int k = 0; k < N/2; k++) {
  38.             odd[k] = x[2*k + 1];
  39.         }
  40.         Complex[] r = fftPower2(odd);
  41.  
  42.         // combine
  43.         Complex[] y = new Complex[N];
  44.  
  45.         for (int k = 0; k < N/2; k++) {
  46.             double kth = -2 * k * Math.PI / N;
  47.             Complex wk = new Complex(Math.cos(kth), Math.sin(kth));
  48.             y[k]       = q[k].plus(wk.times(r[k]));
  49.             y[k + N/2] = q[k].minus(wk.times(r[k]));
  50.         }
  51.         return y;
  52.     }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment