• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

java 搞代码 4年前 (2022-01-05) 23次浏览 已收录 0个评论

这篇文章主要介绍了Apache Commons Math3探索之快速傅立叶变换代码示例,具有一定参考价值,需要的朋友可以了解下。

上一篇文章中我们了解了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 params) throws Exception; public List 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 getParams() { return null; } @Override public Object run(List 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 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("------------------<div style="color:transparent">来源gaodai.ma#com搞#代!码网</div>--------------------------------------------------------"); } } 

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

总结

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

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

https://www.gaodaima.com/books/561375.html

Spring+MyBatis企业应用实战 完整pdf扫描版

https://www.gaodaima.com/books/560647.html

希望大家喜欢,更多精彩内容,就在https://www.gaodaima.com/

以上就是Apache Commons Math3探索之快速傅立叶变换代码示例的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Apache Commons Math3探索之快速傅立叶变换代码示例

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址