# Complete java version of Fourier transform function (FFT)

Posted by big_c147 on Sun, 06 Mar 2022 10:43:43 +0100

1. Preface
This function is completely based on Java language and its related computing toolkit, and has been applied to practice.
As we all know, when we need to analyze the signal, we basically use the Fourier change function, but based on the Java platform, there is a lack of relevant Fourier function, or although there are packages in some toolkits, there are problems in the actual calculation. Therefore, we need to write the correlation function according to the principle of Fourier transform, which is more reliable. The function of Fourier transform is to convert the time domain waveform to the frequency domain to observe the law of the signal.
This function first contains a calculation tool class, Complex class, which contains some calculation tools needed for Fourier transform, followed by Fourier transform class FFT, which contains the specific implementation method of Fourier transform getFFT().
Less nonsense. Let's look at the code and detailed explanation of the algorithm.
2.Complex class
Specific code:

```public class Complex {
public double i;
public double j;// imaginary number
public Complex(double i, double j) {
this.i = i;
this.j = j;
}

public double getMod() {// Modulus of complex number
return Math.sqrt(i * i + j * j);
}

public static Complex Add(Complex a, Complex b) {
return new Complex(a.i + b.i, a.j + b.j);
}

public static Complex Subtract(Complex a, Complex b) {
return new Complex(a.i - b.i, a.j - b.j);
}

public static Complex Mul(Complex a, Complex b) {// multiplication
return new Complex(a.i * b.i - a.j * b.j, a.i * b.j + a.j * b.i);
}

public static Complex GetW(int k, int N) {
return new Complex(Math.cos(-2 * Math.PI * k / N), Math.sin(-2 * Math.PI * k / N));
}

public static Complex[] butterfly(Complex a, Complex b, Complex w) {
return new Complex[] { Add(a, Mul(w, b)), Subtract(a, Mul(w, b)) };
}

public static Double[] toModArray(Complex[] complex) {
Double[] res = new Double[complex.length];
for (int i = 0; i < complex.length; i++) {
res[i] = complex[i].getMod();
}
return res;
}

}
```

Class name: Complex
Function name 1: Complex(double i, double j)
Variable description: i is the real part of the data and j is the imaginary part of the data. The return value is an imaginary value
Function: output imaginary numbers (because Fourier transform is to process imaginary numbers, the initial data needs to be processed into imaginary numbers)
Function name 2: public static Complex Add(Complex a, Complex b)
Variable description: a and b are different complex values respectively. The return value is the result of the addition of two complex values, and the return value type is also complex type
Function name 3: public static Complex Subtract(Complex a, Complex b)
Variable description: a and b are different complex values respectively. The return value is the result of subtracting two complex values, and the return value type is also complex type
Function: subtract two complex numbers
Function name 4: public static Complex Mul(Complex a, Complex b)
Variable description: a and b are different complex values respectively. The return value is the result of multiplying two complex values, and the return value type is also complex type
Function: multiply two complex numbers
Function name 5: public static Complex GetW(int k, int N)
Variable description: k and N are the coefficients in Fourier transform. The return value is the result of two complex values calculated by Euler formula. The return value type is also complex type
Function: Euler formula transforms the exponent of e into the form of complex number.
Function name 6: public static Complex[] butterfly(Complex a, Complex b, Complex w)
Variable description: a is the even number sequence (type is complex), b is the odd number sequence (type is complex), w is the exponential expansion complex of e (type is complex), and the return value is the complex array type
Function: Butterfly calculation formula of Fourier transform
Function name 7: public static double [] to modarray (complex [] complex)
Variable description: complex is the incoming complex array, and the return value is the calculated complex modulus (type is Double [])
Function: calculate the modulus of the complex number

3.FFT class

Specific code:

```import complex_1.Complex;

public class FFT{
public static Complex[] getFFT(Complex[] input, int N) {
if ((N / 2) % 2 == 0) {
Complex[] even = new Complex[N / 2];// even numbers
Complex[] odd = new Complex[N / 2];// Odd number
for (int i = 0; i < N / 2; i++) {
even[i] = input[2 * i];
odd[i] = input[2 * i + 1];
}
even = getFFT(even, N / 2);
odd = getFFT(odd, N / 2);
for (int i = 0; i < N / 2; i++) {
Complex[] res = Complex.butterfly(even[i], odd[i], Complex.GetW(i, N));
input[i] = res[0];
input[i + N / 2] = res[1];
}
return input;
} else {// Two point DFT, direct dish operation
Complex[] res = Complex.butterfly(input[0], input[1], Complex.GetW(0, N));
return res;
}
}
}

```

Class name: FFT
Function name: public static Complex[] getFFT(Complex[] input, int N)
Variable description: input is the input data (the data type must be Complex type, and the Complex () method in the Complex class needs to be called to convert the data into a Complex number), N is the length of the data (the data length must be a power of 2), and the return value is a Complex array
Call the Complex() method to convert data into a Complex number. For example:

```double[] data= {-0.35668879080953375, -0.6118094913035987, 0.8534269560320435, -0.6699697478438837, 0.35425500561437717,
0.8910250650549392, -0.025718699518642918, 0.07649691490732002};
N=8;
Complex[] input = new Complex[N];
for (int i = 0; i <= N-1; i++){
input[i] = new Complex(data[i], 0);}
```

Note: 1 The Complex array is still obtained after Fourier transform. You need to call the toModArray(input) method in the Complex class to convert the data into real numbers.
Example: x = complex toModArray(input);
2. After obtaining the real number, you need to divide the number in the obtained real number array by 2N (N is the data length)

The above is all the Fourier transform functions and tool functions written in Java. The specific mathematical principles will not be introduced in detail here. If you want to know how to calculate, you can refer to the specific mathematical reasoning about Fourier transform.

Topics: Java Algorithm