Java实现的傅里叶变化算法示例
本文实例讲述了Java实现的傅里叶变化算法。分享给大家供大家参考,具体如下:
用JAVA实现傅里叶变化结果为复数形式a+bi
废话不多说,实现代码如下,共两个class
FFT.class傅里叶变化功能实现代码
packagefft.test;
/*************************************************************************
*Compilation:javacFFT.javaExecution:javaFFTNDependencies:Complex.java
*
*ComputetheFFTandinverseFFTofalengthNcomplexsequence.Barebones
*implementationthatrunsinO(NlogN)time.Ourgoalistooptimizethe
*clarityofthecode,ratherthanperformance.
*
*Limitations------------assumesNisapowerof2
*
*-notthemostmemoryefficientalgorithm(becauseitusesanobjecttypefor
*representingcomplexnumbersandbecauseitre-allocatesmemoryforthe
*subarray,insteadofdoingin-placeorreusingasingletemporaryarray)
*
*************************************************************************/
publicclassFFT{
//computetheFFTofx[],assumingitslengthisapowerof2
publicstaticComplex[]fft(Complex[]x){
intN=x.length;
//basecase
if(N==1)
returnnewComplex[]{x[0]};
//radix2Cooley-TukeyFFT
if(N%2!=0){
thrownewRuntimeException("Nisnotapowerof2");
}
//fftofeventerms
Complex[]even=newComplex[N/2];
for(intk=0;k
Complex.class复数类
packagefft.test;
/******************************************************************************
*Compilation:javacComplex.java
*Execution:javaComplex
*
*Datatypeforcomplexnumbers.
*
*Thedatatypeis"immutable"soonceyoucreateandinitialize
*aComplexobject,youcannotchangeit.The"final"keyword
*whendeclaringreandimenforcesthisrule,makingita
*compile-timeerrortochangethe.reor.iminstancevariablesafter
*they'vebeeninitialized.
*
*%javaComplex
*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
*
******************************************************************************/
importjava.util.Objects;
publicclassComplex{
privatefinaldoublere;//therealpart
privatefinaldoubleim;//theimaginarypart
//createanewobjectwiththegivenrealandimaginaryparts
publicComplex(doublereal,doubleimag){
re=real;
im=imag;
}
//returnastringrepresentationoftheinvokingComplexobject
publicStringtoString(){
if(im==0)
returnre+"";
if(re==0)
returnim+"i";
if(im<0)
returnre+"-"+(-im)+"i";
returnre+"+"+im+"i";
}
//returnabs/modulus/magnitude
publicdoubleabs(){
returnMath.hypot(re,im);
}
//returnangle/phase/argument,normalizedtobebetween-piandpi
publicdoublephase(){
returnMath.atan2(im,re);
}
//returnanewComplexobjectwhosevalueis(this+b)
publicComplexplus(Complexb){
Complexa=this;//invokingobject
doublereal=a.re+b.re;
doubleimag=a.im+b.im;
returnnewComplex(real,imag);
}
//returnanewComplexobjectwhosevalueis(this-b)
publicComplexminus(Complexb){
Complexa=this;
doublereal=a.re-b.re;
doubleimag=a.im-b.im;
returnnewComplex(real,imag);
}
//returnanewComplexobjectwhosevalueis(this*b)
publicComplextimes(Complexb){
Complexa=this;
doublereal=a.re*b.re-a.im*b.im;
doubleimag=a.re*b.im+a.im*b.re;
returnnewComplex(real,imag);
}
//returnanewobjectwhosevalueis(this*alpha)
publicComplexscale(doublealpha){
returnnewComplex(alpha*re,alpha*im);
}
//returnanewComplexobjectwhosevalueistheconjugateofthis
publicComplexconjugate(){
returnnewComplex(re,-im);
}
//returnanewComplexobjectwhosevalueisthereciprocalofthis
publicComplexreciprocal(){
doublescale=re*re+im*im;
returnnewComplex(re/scale,-im/scale);
}
//returntherealorimaginarypart
publicdoublere(){
returnre;
}
publicdoubleim(){
returnim;
}
//returna/b
publicComplexdivides(Complexb){
Complexa=this;
returna.times(b.reciprocal());
}
//returnanewComplexobjectwhosevalueisthecomplexexponentialof
//this
publicComplexexp(){
returnnewComplex(Math.exp(re)*Math.cos(im),Math.exp(re)*Math.sin(im));
}
//returnanewComplexobjectwhosevalueisthecomplexsineofthis
publicComplexsin(){
returnnewComplex(Math.sin(re)*Math.cosh(im),Math.cos(re)*Math.sinh(im));
}
//returnanewComplexobjectwhosevalueisthecomplexcosineofthis
publicComplexcos(){
returnnewComplex(Math.cos(re)*Math.cosh(im),-Math.sin(re)*Math.sinh(im));
}
//returnanewComplexobjectwhosevalueisthecomplextangentofthis
publicComplextan(){
returnsin().divides(cos());
}
//astaticversionofplus
publicstaticComplexplus(Complexa,Complexb){
doublereal=a.re+b.re;
doubleimag=a.im+b.im;
Complexsum=newComplex(real,imag);
returnsum;
}
//SeeSection3.3.
publicbooleanequals(Objectx){
if(x==null)
returnfalse;
if(this.getClass()!=x.getClass())
returnfalse;
Complexthat=(Complex)x;
return(this.re==that.re)&&(this.im==that.im);
}
//SeeSection3.3.
publicinthashCode(){
returnObjects.hash(re,im);
}
//sampleclientfortesting
publicstaticvoidmain(String[]args){
Complexa=newComplex(3.0,4.0);
Complexb=newComplex(-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程序设计有所帮助。