Apache Commons Math3学习笔记(1)- 快速傅立叶变换

傅立叶变换: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。还是挺快的。

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值