当前位置: 移动技术网 > IT编程>开发语言>Java > Apache Commons Math3探索之快速傅立叶变换代码示例

Apache Commons Math3探索之快速傅立叶变换代码示例

2019年07月19日  | 移动技术网IT编程  | 我要评论

上一篇文章中我们了解了apache commons math3探索之多项式曲线拟合实现代码,今天我们就来看看如何通过apache commons math3实现快速傅里叶变换,下面是具体内容。

傅立叶变换:org.apache.commons.math3.transform.fastfouriertransformer类。

用法示例代码:

double inputdata = new double[arraylength]; 
// ... 给inputdata赋值 
fastfouriertransformer fft = new fastfouriertransformer(dftnormalization.standard); 
complex[] result = fft.transform(inputdata, transformtype.forward); 

使用还是非常简单的。首先要创建待计算数据的数组,可以是double类型,亦可是org.apache.commons.math3.complex.complex类型,然后创建org.apache.commons.math3.transform.fastfouriertransformer对象实例,最后调用其transform方法即可得到存放于复数数组中的傅立叶变换结果。
完整的示例代码如下:

import org.apache.commons.math3.transform.dftnormalization; 
import org.apache.commons.math3.transform.fastfouriertransformer; 
import org.apache.commons.math3.transform.transformtype; 
interface testcase 
{ 
  public object run(list<object> params) throws exception; 
  public list<object> getparams(); 
} 
class calcfft implements testcase 
{ 
  public calcfft() 
  { 
   system.out.print("本算例用于计算快速傅立叶变换。正在初始化 计算数据(" + arraylength + "点)... ..."); 
   inputdata = new double[arraylength]; 
   for (int index = 0; index < inputdata.length; index++) 
   { 
     inputdata[index] = (math.random() - 0.5) * 100.0; 
   } 
   system.out.println("初始化完成"); 
  } 
  @override 
  public list<object> getparams() 
  { 
   return null; 
  } 
  @override 
  public object run(list<object> params) throws exception 
  { 
   fastfouriertransformer fft = new fastfouriertransformer(dftnormalization.standard); 
   complex[] result = fft.transform(inputdata, transformtype.forward); 
   return result; 
  } 
  private double[] inputdata = null; 
  private final int arraylength = 4 * 1024*1024; 
} 
public class timecostcalculator 
{ 
  public timecostcalculator() 
  { 
  } 
  /** 
  * 计算指定对象的运行时间开销。 
  * 
  * @param testcase 指定被测对象。 
  * @return 返回sub.run的时间开销,单位为s。 
  * @throws exception 
  */ 
  public double calctimecost(testcase testcase) throws exception 
  { 
   list<object> params = testcase.getparams(); 
   long starttime = system.nanotime(); 
   testcase.run(params); 
   long stoptime = system.nanotime(); 
   system.out.println("start: " + starttime + " / stop: " + stoptime); 
   double timecost = (stoptime - starttime) * 1.0e-9; 
   //   double timecost = bigdecimal.valueof(stoptime - starttime, 9).doublevalue(); 
   return timecost; 
  } 
  public static void main(string[] args) throws exception 
  { 
   timecostcalculator tcc = new timecostcalculator(); 
   double timecost; 
   system.out.println("--------------------------------------------------------------------------"); 
   timecost = tcc.calctimecost(new calcfft()); 
   system.out.println("time cost is: " + timecost + "s"); 
   system.out.println("--------------------------------------------------------------------------"); 
  } 
} 

在i5四核处理器+16gb内存的台式机上,计算4百万点fft,耗时0.7s。还是挺快的。

总结

以上就是本文关于apache commons math3探索之快速傅立叶变换代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:apache commons math3学习之数值积分实例代码、等,有什么问题可以随时留言,小编会及时回复大家的。最后推荐几本有关java编程方面不错的书籍,免费下载,供广大编程爱好及工作者参考,提高!

java web开发就该这样学 (王洋著) pdf扫描版

spring+mybatis企业应用实战 完整pdf扫描版

希望大家喜欢,更多精彩内容,就在

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网