Exemplo n.º 1
0
  // compute the FFT of x[], assuming its length is a power of 2
  public static Complex[] fft(Complex[] x) {
    int n = x.length;

    // base case
    if (n == 1) return new Complex[] {x[0]};

    // radix 2 Cooley-Tukey FFT
    if (n % 2 != 0) {
      throw new RuntimeException("n is not a power of 2");
    }

    // fft of even terms
    Complex[] even = new Complex[n / 2];
    for (int k = 0; k < n / 2; k++) {
      even[k] = x[2 * k];
    }
    Complex[] q = fft(even);

    // fft of odd terms
    Complex[] odd = even; // reuse the array
    for (int k = 0; k < n / 2; k++) {
      odd[k] = x[2 * k + 1];
    }
    Complex[] r = fft(odd);

    // combine
    Complex[] y = new Complex[n];
    for (int k = 0; k < n / 2; k++) {
      double kth = -2 * k * Math.PI / n;
      Complex wk = new Complex(Math.cos(kth), Math.sin(kth));
      y[k] = q[k].plus(wk.times(r[k]));
      y[k + n / 2] = q[k].minus(wk.times(r[k]));
    }
    return y;
  }
Exemplo n.º 2
0
  // sample client for testing
  public static void main(String[] args) {
    Complex a = new Complex(5.0, 6.0);
    Complex b = new Complex(-3.0, 4.0);

    System.out.println("a            = " + a);
    System.out.println("b            = " + b);
    System.out.println("Re(a)        = " + a.re());
    System.out.println("Im(a)        = " + a.im());
    System.out.println("b + a        = " + b.plus(a));
    System.out.println("a - b        = " + a.minus(b));
    System.out.println("a * b        = " + a.times(b));
    System.out.println("b * a        = " + b.times(a));
    System.out.println("a / b        = " + a.divides(b));
    System.out.println("(a / b) * b  = " + a.divides(b).times(b));
    System.out.println("conj(a)      = " + a.conjugate());
    System.out.println("|a|          = " + a.abs());
    System.out.println("tan(a)       = " + a.tan());
  }
Exemplo n.º 3
0
 public Numeric mul(Object y) {
   if (y instanceof Complex) {
     Complex yc = (Complex) y;
     if (yc.unit() == Unit.Empty) {
       double y_re = yc.reValue();
       double y_im = yc.imValue();
       return new DComplex(real * y_re - imag * y_im, real * y_im + imag * y_re);
     }
     return Complex.times(this, yc);
   }
   return ((Numeric) y).mulReversed(this);
 }
Exemplo n.º 4
0
  // compute the FFT of x[], assuming its length is a power of 2
  public static Complex[] fft(Complex[] x) {
    int N = x.length;

    // base case
    if (N == 1) return new Complex[] {x[0]};

    // radix 2 Cooley-Tukey FFT
    if (N % 2 != 0) {
      // throw new RuntimeException("N is not a power of 2");
      Log.e(TAG, "N=" + Integer.toString(N) + " is not a power of 2");
      return null;
    }

    // fft of even terms
    Complex[] even = new Complex[N / 2];
    for (int k = 0; k < N / 2; k++) {
      even[k] = x[2 * k];
    }
    Complex[] q = fft(even);
    if (q == null) return null;

    // fft of odd terms
    Complex[] odd = even; // reuse the array
    for (int k = 0; k < N / 2; k++) {
      odd[k] = x[2 * k + 1];
    }
    Complex[] r = fft(odd);
    if (r == null) return null;

    // combine
    Complex[] y = new Complex[N];
    for (int k = 0; k < N / 2; k++) {
      double kth = -2 * k * Math.PI / N;
      Complex wk = new Complex(Math.cos(kth), Math.sin(kth));
      y[k] = q[k].plus(wk.times(r[k]));
      y[k + N / 2] = q[k].minus(wk.times(r[k]));
    }
    return y;
  }
Exemplo n.º 5
0
 // return a / b
 public Complex divides(Complex b) {
   Complex a = this;
   return a.times(b.reciprocal());
 }