注:转载大神的代码,只增加了一个函数,其余未改动。
1 FFT
package com.xu.music.player.fft;
import java.util.stream.Stream;
public class FFT {
public static Complex[] fft(Complex[] x) {
int N = x.length;
if (N == 1)
return new Complex[]{x[0]};
if (N % 2 != 0) {
throw new RuntimeException("N is not a power of 2");
}
Complex[] even = new Complex[N / 2];
for (int k = 0; k < N / 2; k++) {
even[k] = x[2 * k];
}
Complex[] q = fft(even);
Complex[] odd = even;
for (int k = 0; k < N / 2; k++) {
odd[k] = x[2 * k + 1];
}
Complex[] r = fft(odd);
Complex[] y = new Complex[N];
for (int k = 0; k < N / 2; k++) {
double kth = -2 * k * Math.PI / N;
Complex wk = new Complex(Math.cos(kth), Math.sin(kth));
y[k] = q[k].plus(wk.times(r[k]));
y[k + N / 2] = q[k].minus(wk.times(r[k]));
}
return y;
}
public static Complex[] ifft(Complex[] x) {
int N = x.length;
Complex[] y = new Complex[N];
for (int i = 0; i < N; i++) {
y[i] = x[i].conjugate();
}
y = fft(y);
for (int i = 0; i < N; i++) {
y[i] = y[i].conjugate();
}
for (int i = 0; i < N; i++) {
y[i] = y[i].scale(1.0 / N);
}
return y;
}
public static Complex[] cconvolve(Complex[] x, Complex[] y) {
if (x.length != y.length) {
throw new RuntimeException("Dimensions don't agree");
}
int N = x.length;
Complex[] a = fft(x);
Complex[] b = fft(y);
Complex[] c = new Complex[N];
for (int i = 0; i < N; i++) {
c[i] = a[i].times(b[i]);
}
return ifft(c);
}
public static Complex[] convolve(Complex[] x, Complex[] y) {
Complex ZERO = new Complex(0, 0);
Complex[] a = new Complex[2 * x.length];
for (int i = 0; i < x.length; i++)
a[i] = x[i];
for (int i = x.length; i < 2 * x.length; i++)
a[i] = ZERO;
Complex[] b = new Complex[2 * y.length];
for (int i = 0; i < y.length; i++)
b[i] = y[i];
for (int i = y.length; i < 2 * y.length; i++)
b[i] = ZERO;
return cconvolve(a, b);
}
public static Double[] array(Complex[] x) {
int len = x.length;
return Stream.of(x).map(a -> a.abs() * 2 / len * 50).toArray(Double[]::new);
}
public static void show(Double[] x, String... title) {
for (String s : title) {
System.out.print(s);
}
System.out.println();
System.out.println("-------------------");
for (int i = 0, len = x.length; i < len; i++) {
System.out.println(x[i]);
}
System.out.println();
}
public static void show(Complex[] x, String title) {
System.out.println(title);
System.out.println("-------------------");
for (int i = 0, len = x.length; i < len; i++) {
System.out.println(x[i].abs() * 2 / len);
}
System.out.println();
}
public static Double[] pow2DoubleArr(Double[] data) {
Double[] newData = null;
int dataLength = data.length;
int sumNum = 2;
while (sumNum < dataLength) {
sumNum = sumNum * 2;
}
int addLength = sumNum - dataLength;
if (addLength != 0) {
newData = new Double[sumNum];
System.arraycopy(data, 0, newData, 0, dataLength);
for (int i = dataLength; i < sumNum; i++) {
newData[i] = 0d;
}
} else {
newData = data;
}
return newData;
}
public static Double[] deskew(Double[] originalArr) {
if (originalArr == null || originalArr.length <= 0) {
return null;
}
Double[] resArr = new Double[originalArr.length];
Double sum = 0D;
for (int i = 0; i < originalArr.length; i++) {
sum += originalArr[i];
}
Double aver = sum / originalArr.length;
for (int i = 0; i < originalArr.length; i++) {
resArr[i] = originalArr[i] - aver;
}
return resArr;
}
public static void main(String[] args) {
Double[] data = {-8.35668879080953375, 4.6118094913035987, 5.8534269560320435, -4.6699697478438837, 3.35425500561437717,
1.8910250650549392, -9.025718699518642918, 2.07649691490732002};
data = deskew(data);
data = pow2DoubleArr(data);
int N = data.length;
System.out.println(N + "数组N中数量....");
Complex[] x = new Complex[N];
for (int i = 0; i < N; i++) {
x[i] = new Complex(data[i], 0);
}
show(x, "x");
Complex[] y = fft(x);
show(array(y), "MusicPlayer", " Test Data");
show(y, "y = fft(x)");
Complex[] z = ifft(y);
show(z, "z = ifft(y)");
Complex[] c = cconvolve(x, x);
show(c, "c = cconvolve(x, x)");
Complex[] d = convolve(x, x);
show(d, "d = convolve(x, x)");
}
}
2 Complex
package com.xu.music.player.fft;
import java.util.Objects;
public class Complex {
private final double re;
private final double im;
public Complex(double real, double imag) {
re = real;
im = imag;
}
public static Complex plus(Complex a, Complex b) {
double real = a.re + b.re;
double imag = a.im + b.im;
Complex sum = new Complex(real, imag);
return sum;
}
public static void main(String[] args) {
Complex a = new Complex(3.0, 4.0);
Complex b = new Complex(-3.0, 4.0);
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("Re(a) = " + a.re());
System.out.println("Im(a) = " + a.im());
System.out.println("b + a = " + b.plus(a));
System.out.println("a - b = " + a.minus(b));
System.out.println("a * b = " + a.times(b));
System.out.println("b * a = " + b.times(a));
System.out.println("a / b = " + a.divides(b));
System.out.println("(a / b) * b = " + a.divides(b).times(b));
System.out.println("conj(a) = " + a.conjugate());
System.out.println("|a| = " + a.abs());
System.out.println("tan(a) = " + a.tan());
}
public String toString() {
if (im == 0)
return re + "";
if (re == 0)
return im + "i";
if (im < 0)
return re + " - " + (-im) + "i";
return re + " + " + im + "i";
}
public double abs() {
return Math.hypot(re, im);
}
public double phase() {
return Math.atan2(im, re);
}
public Complex plus(Complex b) {
Complex a = this;
double real = a.re + b.re;
double imag = a.im + b.im;
return new Complex(real, imag);
}
public Complex minus(Complex b) {
Complex a = this;
double real = a.re - b.re;
double imag = a.im - b.im;
return new Complex(real, imag);
}
public Complex times(Complex b) {
Complex a = this;
double real = a.re * b.re - a.im * b.im;
double imag = a.re * b.im + a.im * b.re;
return new Complex(real, imag);
}
public Complex scale(double alpha) {
return new Complex(alpha * re, alpha * im);
}
public Complex conjugate() {
return new Complex(re, -im);
}
public Complex reciprocal() {
double scale = re * re + im * im;
return new Complex(re / scale, -im / scale);
}
public double re() {
return re;
}
public double im() {
return im;
}
public Complex divides(Complex b) {
Complex a = this;
return a.times(b.reciprocal());
}
public Complex exp() {
return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
}
public Complex sin() {
return new Complex(Math.sin(re) * Math.cosh(im), Math.cos(re) * Math.sinh(im));
}
public Complex cos() {
return new Complex(Math.cos(re) * Math.cosh(im), -Math.sin(re) * Math.sinh(im));
}
public Complex tan() {
return sin().divides(cos());
}
public boolean equals(Object x) {
if (x == null)
return false;
if (this.getClass() != x.getClass())
return false;
Complex that = (Complex) x;
return (this.re == that.re) && (this.im == that.im);
}
public int hashCode() {
return Objects.hash(re, im);
}
}