Apache Commons Math3探索之快速傅立葉變換代碼示例
上一篇文章中我們了解了Apache Commons Math3探索之多項(xiàng)式曲線擬合實(shí)現(xiàn)代碼,今天我們就來(lái)看看如何通過(guò)apache commons math3實(shí)現(xiàn)快速傅里葉變換,下面是具體內(nèi)容。
傅立葉變換: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);
使用還是非常簡(jiǎn)單的。首先要?jiǎng)?chuàng)建待計(jì)算數(shù)據(jù)的數(shù)組,可以是double類型,亦可是org.apache.commons.math3.complex.Complex類型,然后創(chuàng)建org.apache.commons.math3.transform.FastFourierTransformer對(duì)象實(shí)例,最后調(diào)用其transform方法即可得到存放于復(fù)數(shù)數(shù)組中的傅立葉變換結(jié)果。
完整的示例代碼如下:
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("本算例用于計(jì)算快速傅立葉變換。正在初始化 計(jì)算數(shù)據(jù)(" + arrayLength + "點(diǎn))... ..."); 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() { } /** * 計(jì)算指定對(duì)象的運(yùn)行時(shí)間開(kāi)銷。 * * @param testCase 指定被測(cè)對(duì)象。 * @return 返回sub.run的時(shí)間開(kāi)銷,單位為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內(nèi)存的臺(tái)式機(jī)上,計(jì)算4百萬(wàn)點(diǎn)FFT,耗時(shí)0.7s。還是挺快的。
總結(jié)
以上就是本文關(guān)于Apache Commons Math3探索之快速傅立葉變換代碼示例的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:Apache Commons Math3學(xué)習(xí)之?dāng)?shù)值積分實(shí)例代碼、apache zookeeper使用方法實(shí)例詳解等,有什么問(wèn)題可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家的。最后推薦幾本有關(guān)Java編程方面不錯(cuò)的書籍,免費(fèi)下載,供廣大編程愛(ài)好及工作者參考,提高!
Java Web開(kāi)發(fā)就該這樣學(xué) (王洋著) pdf掃描版
http://chabaoo.cn/books/561375.html
Spring+MyBatis企業(yè)應(yīng)用實(shí)戰(zhàn) 完整pdf掃描版
http://chabaoo.cn/books/560647.html
希望大家喜歡,更多精彩內(nèi)容,就在http://chabaoo.cn/
相關(guān)文章
java?webservice超時(shí)時(shí)間設(shè)置方法代碼
當(dāng)我們使用WebService進(jìn)行調(diào)用時(shí),有時(shí)會(huì)出現(xiàn)超時(shí)的情況,下面這篇文章主要給大家介紹了關(guān)于java?webservice超時(shí)時(shí)間設(shè)置方法的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-01-01聊聊注解@Aspect的AOP實(shí)現(xiàn)操作
這篇文章主要介紹了聊聊注解@Aspect的AOP實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01Sleuth(Micrometer)+ZipKin分布式鏈路問(wèn)題小結(jié)
在微服務(wù)架構(gòu)中,分布式鏈路追蹤技術(shù)成為了解決系統(tǒng)復(fù)雜調(diào)用問(wèn)題的關(guān)鍵,本文介紹了其他鏈路追蹤方案,如Cat、Pinpoint和Skywalking,展示了分布式鏈路追蹤技術(shù)的多樣化,感興趣的朋友一起看看吧2024-10-10Java如何實(shí)現(xiàn)通過(guò)鍵盤輸入一個(gè)數(shù)組
這篇文章主要介紹了Java實(shí)現(xiàn)通過(guò)鍵盤輸入一個(gè)數(shù)組,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02java isPalindrome方法在密碼驗(yàn)證中的應(yīng)用
這篇文章主要為大家介紹了java isPalindrome方法在密碼驗(yàn)證中的簡(jiǎn)單應(yīng)用技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12