Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)

<scбыстрая реализация алгоритма Фурье на java

  1. User

    <scбыстрая реализация алгоритма Фурье на java

  2. Assistant

    Вот быстрая реализация алгоритма Фурье на Java:

    import java.util.Arrays;
    
    public class FastFourierTransform {
        public Complex[] fft(Complex[] x) {
            int n = x.length;
    
            // Base case
            if (n == 1) {
                return new Complex[]{x[0]};
            }
    
            // Splitting the array into even and odd indexed elements
            Complex[] even = new Complex[n/2];
            Complex[] odd = new Complex[n/2];
            for (int i = 0; i < n/2; i++) {
                even[i] = x[2 * i];
                odd[i] = x[2 * i + 1];
            }
    
            // Recursive calls for even and odd arrays
            Complex[] evenResult = fft(even);
            Complex[] oddResult = fft(odd);
    
            // Combining the results
            Complex[] result = 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));
                Complex temp = wk.multiply(oddResult[k]);
                result[k] = evenResult[k].add(temp);
                result[k + n/2] = evenResult[k].subtract(temp);
            }
    
            return result;
        }
    
        public static void main(String[] args) {
            Complex[] x = {
              new Complex(1, 0),
              new Complex(2, 0),
              new Complex(3, 0),
              new Complex(4, 0)
            };
    
            FastFourierTransform fft = new FastFourierTransform();
            Complex[] result = fft.fft(x);
    
            System.out.println(Arrays.toString(result));
        }
    }
    
    class Complex {
        private final double real;
        private final double imaginary;
    
        public Complex(double real, double imaginary) {
            this.real = real;
            this.imaginary = imaginary;
        }
    
        public Complex add(Complex other) {
            double real = this.real + other.real;
            double imaginary = this.imaginary + other.imaginary;
            return new Complex(real, imaginary);
        }
    
        public Complex subtract(Complex other) {
            double real = this.real - other.real;
            double imaginary = this.imaginary - other.imaginary;
            return new Complex(real, imaginary);
        }
    
        public Complex multiply(Complex other) {
            double real = this.real * other.real - this.imaginary * other.imaginary;
            double imaginary = this.real * other.imaginary + this.imaginary * other.real;
            return new Complex(real, imaginary);
        }
    
        @Override
        public String toString() {
            return real + " + " + imaginary + "i";
        }
    }
    

    Этот код реализует быстрое преобразование Фурье (Fast Fourier Transform - FFT), которое работает на входном массиве комплексных чисел и возвращает массив преобразованных значений. В приведенном примере входной массив состоит из 4 комплексных чисел {1, 2, 3, 4}, и код выводит массив преобразованных чисел.

    Обратите внимание, что класс Complex используется для представления комплексных чисел и определения основных операций (сложение, вычитание, умножение).