要结合算法导论理解,参考:http://blog.csdn.net/fjssharpsword/article/details/53281889

代码中算法思路:输入n位(2的幂)向量,分别求值FFT和插值逆FFT,并计算卷积。

package sk.mlib;
/**************************************************************************  Compilation:  javac FFT.java*  Execution:    java FFT N*  Dependencies: Complex.java**  Compute the FFT and inverse FFT of a length N complex sequence.*  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)*  *************************************************************************/
public class FFT {// compute the FFT of x[], assuming its length is a power of 2public static Complex[] fft(Complex[] x) {int N = x.length;// base caseif (N == 1) return new Complex[] { x[0] };// radix 2 Cooley-Tukey FFTif (N % 2 != 0) { throw new RuntimeException("N is not a power of 2"); }// fft of even termsComplex[] 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 termsComplex[] odd  = even;  // reuse the arrayfor (int k = 0; k < N/2; k++) {odd[k] = x[2*k + 1];}Complex[] r = fft(odd);// combineComplex[] 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 2public static Complex[] ifft(Complex[] x) {int N = x.length;Complex[] y = new Complex[N];// take conjugatefor (int i = 0; i < N; i++) {y[i] = x[i].conjugate();}// compute forward FFTy = fft(y);// take conjugate againfor (int i = 0; i < N; i++) {y[i] = y[i].conjugate();}// divide by Nfor (int i = 0; i < N; i++) {y[i] = y[i].scale(1.0 / N);}return y;}// compute the circular convolution of x and ypublic 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 2if (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 ypublic 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 outputpublic static void show(Complex[] x, String title) {System.out.println(title);System.out.println("-------------------");for (int i = 0; i < x.length; i++) {System.out.println(x[i]);}System.out.println();}public static void main(String[] args) { //int N = Integer.parseInt(args[0]);int N=8;Complex[] x = new Complex[N];// original datafor (int i = 0; i < N; i++) {x[i] = new Complex(i, 0);x[i] = new Complex(-2*Math.random() + 1, 0);}show(x, "x");// FFT of original dataComplex[] y = fft(x);show(y, "y = fft(x)");// take inverse FFTComplex[] z = ifft(y);show(z, "z = ifft(y)");// circular convolution of x with itselfComplex[] c = cconvolve(x, x);show(c, "c = cconvolve(x, x)");// linear convolution of x with itselfComplex[] d = convolve(x, x);show(d, "d = convolve(x, x)");}
}
/*********************************************************************% java FFT 8x
-------------------
-0.35668879080953375
-0.6118094913035987
0.8534269560320435
-0.6699697478438837
0.35425500561437717
0.8910250650549392
-0.025718699518642918
0.07649691490732002y = fft(x)
-------------------
0.5110172121330208
-1.245776663065442 + 0.7113504894129803i
-0.8301420417085572 - 0.8726884066879042i
-0.17611092978238008 + 2.4696418005143532i
1.1395317305034673
-0.17611092978237974 - 2.4696418005143532i
-0.8301420417085572 + 0.8726884066879042i
-1.2457766630654419 - 0.7113504894129803iz = ifft(y)
-------------------
-0.35668879080953375
-0.6118094913035987 + 4.2151962932466006E-17i
0.8534269560320435 - 2.691607282636124E-17i
-0.6699697478438837 + 4.1114763914420734E-17i
0.35425500561437717
0.8910250650549392 - 6.887033953004965E-17i
-0.025718699518642918 + 2.691607282636124E-17i
0.07649691490732002 - 1.4396387316837096E-17ic = cconvolve(x, x)
-------------------
-1.0786973139009466 - 2.636779683484747E-16i
1.2327819138980782 + 2.2180047699856214E-17i
0.4386976685553382 - 1.3815636262919812E-17i
-0.5579612069781844 + 1.9986455722517509E-16i
1.432390480003344 + 2.636779683484747E-16i
-2.2165857430333684 + 2.2180047699856214E-17i
-0.01255525669751989 + 1.3815636262919812E-17i
1.0230680492494633 - 2.4422465262488753E-16id = convolve(x, x)
-------------------
0.12722689348916738 + 3.469446951953614E-17i
0.43645117531775324 - 2.78776395788635E-18i
-0.2345048043334932 - 6.907818131459906E-18i
-0.5663280251946803 + 5.829891518914417E-17i
1.2954076913348198 + 1.518836016779236E-16i
-2.212650940696159 + 1.1090023849928107E-17i
-0.018407034687857718 - 1.1306778366296569E-17i
1.023068049249463 - 9.435675069681485E-17i
-1.205924207390114 - 2.983724378680108E-16i
0.796330738580325 + 2.4967811657742562E-17i
0.6732024728888314 - 6.907818131459906E-18i
0.00836681821649593 + 1.4156564203603091E-16i
0.1369827886685242 + 1.1179436667055108E-16i
-0.00393480233720922 + 1.1090023849928107E-17i
0.005851777990337828 + 2.512241462921638E-17i
1.1102230246251565E-16 - 1.4986790192807268E-16i*********************************************************************/

依赖Complex.java 复数操作类的实现:

package sk.mlib;
/*******************************************************************************  Compilation:  javac Complex.java*  Execution:    java Complex**  Data type for complex numbers.**  The data type is "immutable" so once you create and initialize*  a Complex object, you cannot change it. The "final" keyword*  when declaring re and im enforces this rule, making it a*  compile-time error to change the .re or .im instance variables after*  they've been initialized.**  % java Complex*  a            = 5.0 + 6.0i*  b            = -3.0 + 4.0i*  Re(a)        = 5.0*  Im(a)        = 6.0*  b + a        = 2.0 + 10.0i*  a - b        = 8.0 + 2.0i*  a * b        = -39.0 + 2.0i*  b * a        = -39.0 + 2.0i*  a / b        = 0.36 - 1.52i*  (a / b) * b  = 5.0 + 6.0i*  conj(a)      = 5.0 - 6.0i*  |a|          = 7.810249675906654*  tan(a)       = -6.685231390246571E-6 + 1.0000103108981198i*******************************************************************************/
import java.util.Objects;
public class Complex {private final double re;   // the real partprivate final double im;   // the imaginary part// create a new object with the given real and imaginary partspublic Complex(double real, double imag) {re = real;im = imag;}// return a string representation of the invoking Complex objectpublic 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/magnitudepublic double abs() {return Math.hypot(re, im);}// return angle/phase/argument, normalized to be between -pi and pipublic 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 objectdouble 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 thispublic Complex conjugate() {return new Complex(re, -im);}// return a new Complex object whose value is the reciprocal of thispublic Complex reciprocal() {double scale = re*re + im*im;return new Complex(re / scale, -im / scale);}// return the real or imaginary partpublic double re() { return re; }public double im() { return im; }// return a / bpublic Complex divides(Complex b) {Complex a = this;return a.times(b.reciprocal());}// return a new Complex object whose value is the complex exponential of thispublic 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 thispublic 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 thispublic 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 thispublic Complex tan() {return sin().divides(cos());}// a static version of pluspublic 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 testingpublic 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());}
}

Java实现算法导论中快速傅里叶变换FFT递归算法相关推荐

  1. Java实现算法导论中快速傅里叶变换FFT迭代算法

    要结合算法导论理解,参考:http://blog.csdn.NET/fjssharpsword/article/details/53281889 FFT的迭代实现,可以实现并行电路,和比较网络中的比较 ...

  2. Java实现算法导论中Miller-Rabin随机性素数测试

    Miller-Rabin测试: 费马小定理:对于素数p和任意整数a,有ap ≡ a(mod p)(同余).反过来,满足ap ≡ a(mod p),p也几乎一定是素数. 伪素数:如果n是一个正整数,如果 ...

  3. Java实现算法导论中图的广度优先搜索(BFS)和深度优先搜索(DFS)

    对算法导论中图的广度优先搜索(BFS)和深度优先搜索(DFS)用Java实现其中的伪代码算法,案例也采用算法导论中的图. import java.util.ArrayList; import java ...

  4. Java实现算法导论中Rabin-Karp字符串匹配算法

    Rabin-Karp算法的思想: 假设子串的长度为M,目标字符串的长度为N 计算子串的hash值 计算目标字符串中每个长度为M的子串的hash值(共需要计算N-M+1次) 比较hash值 如果hash ...

  5. Java实现算法导论中朴素字符串匹配算法

    朴素字符串匹配算法沿着主串滑动子串来循环匹配,算法时间性能是O((n-m+1)m),n是主串长度,m是字串长度,结合算法导论中来理解,具体代码参考: package cn.ansj;public cl ...

  6. Java实现算法导论中反复平方法模取幂

    在众多的加密算法中都需要进行幂的取模运算,比如在RSA算法中需要计算d=ne mod N,我们称之为幂模算法,其中: N=p*q(p,q为大素数) n为加密数据,n<N e为公钥,d为私钥,满足 ...

  7. Java实现算法导论中求解模线性方程解(基于最大公约数欧几里得扩展算法)

    基于最大公约数欧几里得扩展算法求解算法导论中模线性方程解.具体要结合算法导论中的有关数论算法章节理解,具体代码如下: package cn.ansj;/*假设方程ax=b(mod n)有解,且x0是方 ...

  8. python fft库有哪些_Python图像处理库PIL中快速傅里叶变换FFT的实现(一)

    离散傅里叶变换(discrete Fouriertransform)傅里叶分析方法是信号分析的最基本方法,傅里叶变换是傅里叶分析的核心,通过它把信号从时间域变换到频率域,进而研究信号的频谱结构和变化规 ...

  9. Java实现算法导论中有限自动机字符串匹配算法

    这里实现了基于有限自动机(Finite Automaton,FA)的模式匹配算法,算法的重点在于利用字符串的前后缀构造模式P的自动机,具体结合导论中的说明来理解,可参考http://www.geeks ...

最新文章

  1. POJ-1273(最大流-Augment Path,EK,BFS)
  2. confusion_matrix函数的使用
  3. linux上使用git把代码push到gitee上
  4. python语言的单行注释以井号开头_推荐|零基础学习Python基础知识
  5. ssm(Spring+Spring mvc+mybatis)Service层接口——IDeptService
  6. 快速打开 Mac OS X 隐藏的用户资源库文件夹
  7. 如何读取H264文件获得每一帧的数据(VsParserPro)
  8. 【电力电子】【2020.02】利用导抗式三相双有源桥DC-DC变换器实现宽范围高效率的拓扑结构和调制方案
  9. 计算机什么快捷键是睡眠,电脑睡眠快捷键(ctrl加哪个键是睡眠)
  10. app android切图工具,小白自学APP切图:APP切图工具Cutterman的参数设置
  11. 2015-2020: 5年,不问归期,奋斗没有终点
  12. Notepad++的JsonViewer 插件安装失败的解决
  13. 根下有长长的白色根须
  14. 一个IP可以登几个拼多多后台 拼多多如何推广营销
  15. java writeint_java的writeInt() 函数怎么用啊
  16. 【JavaEE】文件
  17. 淘宝新开店铺容易忽略的地方,如何安全提升宝贝排名
  18. 图片资源中总会出现thumb.db文件
  19. Symfony 介绍
  20. python文件相对路径是什么意思_python相对路径表示_什么是绝对路径和相对路径,举例说明...

热门文章

  1. 客服机器人源码_快速搭建对话机器人,就用这一招!
  2. 可持久化线段树——主席树
  3. python 入门DAY1
  4. 通过cordova将vue项目打包
  5. sql中存储过程打印返回的记录集
  6. PYTHON_DACORATOR
  7. (C#)设计模式之装饰模式
  8. 字符串的最大最小表示法 模板
  9. Java实现的简单的WebService服务发布和Client调用例子
  10. 【转】 ASP.NET 3.5中使用新的ListView控件