栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java实现的傅里叶变化算法示例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java实现的傅里叶变化算法示例

本文实例讲述了Java实现的傅里叶变化算法。分享给大家供大家参考,具体如下:

用JAVA实现傅里叶变化 结果为复数形式 a+bi

废话不多说,实现代码如下,共两个class

FFT.class 傅里叶变化功能实现代码

package fft.test;

public class FFT {
  // 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;
  }
  // compute the inverse FFT of x[], assuming its length is a power of 2
  public static Complex[] ifft(Complex[] x) {
    int N = x.length;
    Complex[] y = new Complex[N];
    // take conjugate
    for (int i = 0; i < N; i++) {
      y[i] = x[i].conjugate();
    }
    // compute forward FFT
    y = fft(y);
    // take conjugate again
    for (int i = 0; i < N; i++) {
      y[i] = y[i].conjugate();
    }
    // divide by N
    for (int i = 0; i < N; i++) {
      y[i] = y[i].scale(1.0 / N);
    }
    return y;
  }
  // compute the circular convolution of x and y
  public static Complex[] cconvolve(Complex[] x, Complex[] y) {
    // should probably pad x and y with 0s so that they have same length
    // and are powers of 2
    if (x.length != y.length) {
      throw new RuntimeException("Dimensions don't agree");
    }
    int N = x.length;
    // compute FFT of each sequence,求值
    Complex[] a = fft(x);
    Complex[] b = fft(y);
    // point-wise multiply,点值乘法
    Complex[] c = new Complex[N];
    for (int i = 0; i < N; i++) {
      c[i] = a[i].times(b[i]);
    }
    // compute inverse FFT,插值
    return ifft(c);
  }
  // compute the linear convolution of x and y
  public static Complex[] convolve(Complex[] x, Complex[] y) {
    Complex ZERO = new Complex(0, 0);
    Complex[] a = new Complex[2 * x.length];// 2n次数界,高阶系数为0.
    for (int i = 0; i < x.length; i++)
      a[i] = x[i];
    for (int i = x.length; i < 2 * x.length; i++)
      a[i] = ZERO;
    Complex[] b = new Complex[2 * y.length];
    for (int i = 0; i < y.length; i++)
      b[i] = y[i];
    for (int i = y.length; i < 2 * y.length; i++)
      b[i] = ZERO;
    return cconvolve(a, b);
  }
  // display an array of Complex numbers to standard output
  public static void show(Complex[] x, String title) {
    System.out.println(title);
    System.out.println("-------------------");
    int complexLength = x.length;
    for (int i = 0; i < complexLength; i++) {
      // 输出复数
      // System.out.println(x[i]);
      // 输出幅值需要 * 2 / length
      System.out.println(x[i].abs() * 2 / complexLength);
    }
    System.out.println();
  }

  public static Double[] pow2DoubleArr(Double[] data) {
    // 创建新数组
    Double[] newData = null;
    int dataLength = data.length;
    int sumNum = 2;
    while (sumNum < dataLength) {
      sumNum = sumNum * 2;
    }
    int addLength = sumNum - dataLength;
    if (addLength != 0) {
      newData = new Double[sumNum];
      System.arraycopy(data, 0, newData, 0, dataLength);
      for (int i = dataLength; i < sumNum; i++) {
 newData[i] = 0d;
      }
    } else {
      newData = data;
    }
    return newData;
  }
  
  public static Double[] deskew(Double[] originalArr) {
    // 过滤不正确的参数
    if (originalArr == null || originalArr.length <= 0) {
      return null;
    }
    // 定义目标数组
    Double[] resArr = new Double[originalArr.length];
    // 求数组总和
    Double sum = 0D;
    for (int i = 0; i < originalArr.length; i++) {
      sum += originalArr[i];
    }
    // 求数组平均值
    Double aver = sum / originalArr.length;
    // 去除偏移值
    for (int i = 0; i < originalArr.length; i++) {
      resArr[i] = originalArr[i] - aver;
    }
    return resArr;
  }
  public static void main(String[] args) {
    // int N = Integer.parseInt(args[0]);
    Double[] data = { -0.35668879080953375, -0.6118094913035987, 0.8534269560320435, -0.6699697478438837, 0.35425500561437717,
 0.8910250650549392, -0.025718699518642918, 0.07649691490732002 };
    // 去除偏移量
    data = deskew(data);
    // 个数为2的幂次方
    data = pow2DoubleArr(data);
    int N = data.length;
    System.out.println(N + "数组N中数量....");
    Complex[] x = new Complex[N];
    // original data
    for (int i = 0; i < N; i++) {
      // x[i] = new Complex(i, 0);
      // x[i] = new Complex(-2 * Math.random() + 1, 0);
      x[i] = new Complex(data[i], 0);
    }
    show(x, "x");
    // FFT of original data
    Complex[] y = fft(x);
    show(y, "y = fft(x)");
    // take inverse FFT
    Complex[] z = ifft(y);
    show(z, "z = ifft(y)");
    // circular convolution of x with itself
    Complex[] c = cconvolve(x, x);
    show(c, "c = cconvolve(x, x)");
    // linear convolution of x with itself
    Complex[] d = convolve(x, x);
    show(d, "d = convolve(x, x)");
  }
}


Complex.class 复数类

package fft.test;

import java.util.Objects;
public class Complex {
  private final double re; // the real part
  private final double im; // the imaginary part
  // create a new object with the given real and imaginary parts
  public Complex(double real, double imag) {
    re = real;
    im = imag;
  }
  // return a string representation of the invoking Complex object
  public String toString() {
    if (im == 0)
      return re + "";
    if (re == 0)
      return im + "i";
    if (im < 0)
      return re + " - " + (-im) + "i";
    return re + " + " + im + "i";
  }
  // return abs/modulus/magnitude
  public double abs() {
    return Math.hypot(re, im);
  }
  // return angle/phase/argument, normalized to be between -pi and pi
  public double phase() {
    return Math.atan2(im, re);
  }
  // return a new Complex object whose value is (this + b)
  public Complex plus(Complex b) {
    Complex a = this; // invoking object
    double real = a.re + b.re;
    double imag = a.im + b.im;
    return new Complex(real, imag);
  }
  // return a new Complex object whose value is (this - b)
  public Complex minus(Complex b) {
    Complex a = this;
    double real = a.re - b.re;
    double imag = a.im - b.im;
    return new Complex(real, imag);
  }
  // return a new Complex object whose value is (this * b)
  public Complex times(Complex b) {
    Complex a = this;
    double real = a.re * b.re - a.im * b.im;
    double imag = a.re * b.im + a.im * b.re;
    return new Complex(real, imag);
  }
  // return a new object whose value is (this * alpha)
  public Complex scale(double alpha) {
    return new Complex(alpha * re, alpha * im);
  }
  // return a new Complex object whose value is the conjugate of this
  public Complex conjugate() {
    return new Complex(re, -im);
  }
  // return a new Complex object whose value is the reciprocal of this
  public Complex reciprocal() {
    double scale = re * re + im * im;
    return new Complex(re / scale, -im / scale);
  }
  // return the real or imaginary part
  public double re() {
    return re;
  }
  public double im() {
    return im;
  }
  // return a / b
  public Complex divides(Complex b) {
    Complex a = this;
    return a.times(b.reciprocal());
  }
  // return a new Complex object whose value is the complex exponential of
  // this
  public Complex exp() {
    return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
  }
  // return a new Complex object whose value is the complex sine of this
  public Complex sin() {
    return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im));
  }
  // return a new Complex object whose value is the complex cosine of this
  public Complex cos() {
    return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im));
  }
  // return a new Complex object whose value is the complex tangent of this
  public Complex tan() {
    return sin().divides(cos());
  }
  // a static version of plus
  public static Complex plus(Complex a, Complex b) {
    double real = a.re + b.re;
    double imag = a.im + b.im;
    Complex sum = new Complex(real, imag);
    return sum;
  }
  // See Section 3.3.
  public boolean equals(Object x) {
    if (x == null)
      return false;
    if (this.getClass() != x.getClass())
      return false;
    Complex that = (Complex) x;
    return (this.re == that.re) && (this.im == that.im);
  }
  // See Section 3.3.
  public int hashCode() {
    return Objects.hash(re, im);
  }
  // sample client for testing
  public static void main(String[] args) {
    Complex a = new Complex(3.0, 4.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());
  }
}

更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》、《Java文件与目录操作技巧汇总》和《Java缓存操作技巧汇总》

希望本文所述对大家java程序设计有所帮助。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/140657.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号