JAVA实现FFT算法

本文介绍了如何使用JAVA实现快速傅里叶变换(FFT),提供了FFT的代码实现,并解释了代码工作原理。文章还提及了实现过程中需要的复数类以及依赖的StdOut类,提供了相关资源下载链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JAVA实现FFT算法

关于快速傅里叶变换(FFT)和傅里叶变换的理论知识这里我就不提了,本文主要讲解FFT实现:

之前想找一个FFT代码,在网上找了很多都是有问题的,下面我完善了一个供大家学习交流;

首先粘贴FFT的代码如下:
/******************************************************************************

  • Compilation: javac FFT.java

  • Execution: java FFT n

  • Dependencies: Complex.java

  • Compute the FFT and inverse FFT of a length n complex sequence

  • using the radix 2 Cooley-Tukey algorithm.

  • Bare bones implementation that runs in O(n log n) time. Our goal

  • is to optimize the clarity of the code, rather than performance.

  • Limitations


    • assumes n is a power of 2
    • not the most memory efficient algorithm (because it uses
  •  an object type for representing complex numbers and because
    
  •  it re-allocates memory for the subarray, instead of doing
    
  •  in-place or reusing a single temporary array)
    
  • For an in-place radix 2 Cooley-Tukey FFT, see

  • https://introcs.cs.princeton.edu/java/97data/InplaceFFT.java.html

******************************************************************************/
import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;
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 IllegalArgumentException("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), 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值