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);
使用還是非常簡單的。首先要創(chuàng)建待計算數據的數組,可以是double類型,亦可是org.apache.commons.math3.complex.Complex類型,然后創(chuàng)建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探索之快速傅立葉變換代碼示例的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:Apache Commons Math3學習之數值積分實例代碼、apache zookeeper使用方法實例詳解等,有什么問題可以隨時留言,小編會及時回復大家的。最后推薦幾本有關Java編程方面不錯的書籍,免費下載,供廣大編程愛好及工作者參考,提高!
Java Web開發(fā)就該這樣學 (王洋著) pdf掃描版
http://www.dhdzp.com/books/561375.html
Spring+MyBatis企業(yè)應用實戰(zhàn) 完整pdf掃描版
http://www.dhdzp.com/books/560647.html
希望大家喜歡,更多精彩內容,就在http://www.dhdzp.com/
相關文章
Spring Boot整合MyBatis-Plus實現CRUD操作的示例代碼
本文主要介紹了Spring Boot整合MyBatis-Plus實現CRUD操作,可以快速實現數據庫的增刪改查操作,文中通過示例代碼介紹的非常詳細,需要的朋友們下面隨著小編來一起學習學習吧2025-04-04
Java實現使用Websocket發(fā)送消息詳細代碼舉例
這篇文章主要給大家介紹了關于Java實現使用Websocket發(fā)送消息的相關資料,WebSocket是一種協(xié)議,用于在Web應用程序和服務器之間建立實時、雙向的通信連接,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2024-05-05
Sentinel網關限流與SpringCloud Gateway整合過程
本文介紹了如何通過SpringCloudGateway集成阿里的Sentinel進行網關限流,Sentinel作為流量防衛(wèi)兵,提供了豐富的應用場景和完備的實時監(jiān)控功能,通過配置路由維度和自定義API維度的限流規(guī)則,實現了對微服務的保護2024-11-11
解讀@ResponseBody與@RequestBody注解的用法
這篇文章主要介紹了Spring MVC中的@ResponseBody和@RequestBody注解的用法,@ResponseBody注解用于將Controller方法的返回對象轉換為指定格式(如JSON)并通過Response響應給客戶端,@RequestBody注解用于讀取HTTP請求的內容2024-11-11
使用Stargate訪問K8ssandra的過程之Springboot整合Cassandra
這篇文章主要介紹了使用Stargate訪問K8ssandra的過程之Springboot整合Cassandra,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-10-10

