java code
compex class
public class Complex { private double a, b; public Complex(){ this.a = 0.0; this.b = 0.0; } public Complex(double a, double b){ this.a = a; this.b = b; } public Complex(Complex C){ this.a = C.a; this.b = C.b; } public double getRealPart(){ //返回实部 return a; } // Get real part public double getImaginaryPart () { return b; } // Get imaginary part public String toString () { if (b! = 0 ) { if (a == 0 ) return b + "i " ; else return a +" + "+ b +" i " ; } else return a +" " ; } // The string represents complex public complex plus (Complex C) { if (C ==null){ System.out.println("对象为null"); return new Complex(); } return new Complex(this.a + C.a, this.b + C.b); } // 加法 public Complex minus(Complex C){ if (C == null){ System.out.println("对象为null"); return new Complex(); } return new Complex(this.a - C.a, this.b - C.b); } // 减法 public Complex multiply(Complex C){ if (C == null){ System.out.println("对象为null"); return new Complex(); } double newa = this.a * C.a - this.b * C.b; double newb = this.a * C.b + this.b * C.a; return new Complex(newa, newb); } // 乘法 public Complex divide(Complex C){ if (C == null){ System.out.println("对象为null"); return new Complex(); } if(C.a == 0 && b == 0){ System.out.println("除数不能为0!"); return new Complex(); } double newa = (this.a * C.a + this.b * C.b) / (C.a * C.a + C.b * C.b); double newb = (this.a * C.a - this.b * C.b) / (C.a * C.a + C.b * C.b); return new Complex(newa, newb); } // 除法 public static void main(String[] args) { Complex x = new Complex(3,4); Complex y = new Complex(5,7); System.out.println("x:" + x.toString()); System.out.println("y:" + y.toString()); System.out.println("(x+y) = " + x.plus(y).toString()); System.out.println("(x-y) = " + x.minus(y).toString()); System.out.println("(x*y) = " + x.multiply(y).toString()); System.out.println("(x/y) = " + x.divide(y).toString()); } }
FFT
import java.io. * ; public class FFT { public static void CreateSignal ( int length, Complex [] x) { double Fs = 1000; // sampling frequency double T = 1 / Fs; // sampling period System.out.println ("Generate a 50 Hz sine quantity with an amplitude of 0.7 and a 120 Hz sine quantity with an amplitude of 1 (total" + length + ") )); for ( int i = 0; i <length; i ++ ) { // Generate A 50 Hz sine wave with an amplitude of 0.7 and a 120 Hz sine wave with an amplitude of 1. double S = 0.7 * Math.sin (2 * Math.PI * 50 * i * T) + Math.sin (2 * Math.PI * 120 * i * T); x [i]= new Complex(S, 0); } } public static Complex[] dft(Complex[] x) { int n = x.length; if (n == 1) // exp(-2pi*j) = cos(-2pi)+j * sin(-2pi) = 1 return x; Complex[] result = new Complex[n]; for (int i = 0; i < n; i++) { result[i] = new Complex(); for (int k = 0; k < n; k++) { doublep = -2 * k * Math.PI * i / n; // Eulerian formulae ^ (-j (2pi * k * i) / N) = cos (-2pi * k * i / N) + i * sin (-2pi * k * i / N) Complex e = new Complex (Math.cos (p), Math.sin (p)); result [i] = result [i] .plus (x [k] .multiply (e)); } } return result; } public static Complex [] fft (Complex [] x) { int n = x.length; if (n == 1 ) return x; // The number of signals is odd and does not satisfy symmetry Sex, use dft to calculate if (n% 2! = 0) return dft (x); // Remove the even-numbered signal Complex [] even = new Complex [n / 2 ]; for ( int i = 0; i <n / 2; i ++ ) { even [i] = x [2 * i]; } Complex [] evenValue = fft (even); // Remove the signal marked as odd Complex [] odd = new Complex [n / 2 ]; for ( int i = 0; i <n / 2; i ++ ) { odd [i] = x [2 * i + 1 ]; } Complex [] oddValue = fft(odd); // 相加 Complex[] result = new Complex[n]; for (int i = 0; i < n / 2; i++) { double p = -2 * Math.PI * i / n ; Complex e = new Complex(Math.cos(p), Math.sin(p)); result[i] = evenValue[i].plus(e.multiply(oddValue[i])); result[i + n / 2] = evenValue[i].minus(e.multiply(oddValue[i])); } return result; } public static voidwriteText (String filename, Complex [] result) { try { File writeName = new File (filename); // relative path, if not, create a new file writeName.createNewFile (); // Create a new file, with the same name File words directly overwrite try (FileWriter writer = new FileWriter (writeName); BufferedWriter out = new BufferedWriter (writer) ) { for ( int i = 0; i <result.length; i ++ ) { out.write (result [i]. toString () + "\ n"); // \ r \ n is the newline } } } catch (IOException e) { e.printStackTrace(); } System.out.println("数据写入成功!"); } public static void main(String[] args) { int length = 1500; Complex[] x = new Complex[length]; Complex[] result; CreateSignal(length, x); long start1 = System.currentTimeMillis(); result = dft(x); long end1 = System.currentTimeMillis(); long start2 = System.currentTimeMillis(); result = fft(x); long end2 = System.currentTimeMillis(); System.out.println("FFT结果:"); for (int i = 0; i < length; i++) { System.out.println("第"+(i+1)+"个数据:"+result[i].toString()); } System.out.println("------------------------------------------------"); System.out.println("DFT运行时间: " + (end1 - start1) + "ms"); System.out.println("FFT运行时间: " + (end2 - start2) + "ms"); String filename = "C:\\Users\\xxx\\Desktop\\算法课程\\xxx\\shuju.txt"; writeText(filename, result); } }
result
Verification (matlab)
clc clear close all; Fs = 1000 ; T = 1 / Fs; L = 1500 ; t = (0: L-1) * T; S = 0.7 * sin (2 * pi * 50 * t) + sin (2 * pi * 120 * t); % matlab fft function Y = fft (S); % import data generated by java file = fopen ('shuju.txt' ); p = textscan (file, '% s' ); pp = ( str2double (p {:})] '; P2 = abs (Y / L); P1 = P2 (1: L / 2 + 1 ); P1 ( 2: end-1) = 2 * P1 (2: end-1); f = Fs*(0:(L/2))/L; subplot(1,2,1) plot(f,P1) hold on; title('FFT of Matlab') xlabel('f (Hz)') ylabel('|P1(f)|') X2 = abs(pp/L); X1 = X2(1:L/2+1); X1(2:end-1) = 2*X1(2:end-1); f1 = Fs*(0:(L/2))/L; subplot(1,2,2); plot(f1,X1) title('FFT of Java') xlabel('f (Hz)') ylabel('|X1(f)|') hold off;
result: