1. 程式人生 > >使用Apache commons-maths3-3.6.1.jar包實現快速傅立葉變換(java)

使用Apache commons-maths3-3.6.1.jar包實現快速傅立葉變換(java)

    快速傅立葉變換 (fast Fourier transform), 即利用計算機計算離散傅立葉變換(DFT)的高效、快速計算方法的統稱,簡稱FFT。

 

 1 package com;
 2 
 3 import org.apache.commons.math3.complex.Complex;
 4 import org.apache.commons.math3.transform.DftNormalization;
 5 import org.apache.commons.math3.transform.FastFourierTransformer;
6 import org.apache.commons.math3.transform.TransformType; 7 /* 8 * @description:快速傅立葉變換 9 * */ 10 11 public class MathTransform{ 12 public static void main(String[] args){ 13 //定義輸入資料型別,並初始化 14 double[] inputData = null; 15 //定義陣列長度 16 int arrayLength = 4 * 1024;
17 inputData = new double[arrayLength]; 18 for (int index = 0; index < inputData.length; index++){ 19 //將經過運算的隨機數賦值給inputdata 20 inputData[index] = (Math.random() - 0.5) * 100.0; 21 } 22 System.out.println("初始化完成"); 23 //建立傅立葉方法例項 24 FastFourierTransformer fft = new
FastFourierTransformer(DftNormalization.STANDARD); 25 Complex[] result = fft.transform(inputData, TransformType.FORWARD); 26 //將傅立葉變換前資料和變換後資料打印出來,顯示前200 27 for(int i=0;i<200;i++){ 28 System.out.print("第"+i+"個變換前資料為:"+inputData[i]+"\t"); 29 System.out.println("第"+i+"個變換後資料為:"+result[i]); 30 } 31 } 32 }