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

