package party.bami.eqfix.analysis;

import org.jtransforms.fft.FloatFFT_1D;


public class Split {
    public static float[][] splitPCM(float[] pcm, int sSplit) {
        int nSplit = pcm.length / sSplit;
        float[][] pcmSplit = new float[nSplit][sSplit];

        for (int i = 0; i < nSplit; i++) {
            for (int k = 0; k < sSplit; k++) {
                pcmSplit[i][k] = pcm[i * sSplit + k];
            }
        }

        return pcmSplit;
    }

    public static float[][] getSplitFFTComplex(float[][] pcmSplit) {
        float[][] FFTC = new float[pcmSplit.length][];
        FloatFFT_1D FFT = new FloatFFT_1D(pcmSplit[0].length);
        for (int i = 0; i < pcmSplit.length; i++) {
            FFTC[i] = pcmSplit[i].clone();
            FFT.complexForward(FFTC[i]);
        }

        return FFTC;
    }

    public static float[][] getSplitFFTReal(float[][] pcmSplit) {
        float[][] FFTR = new float[pcmSplit.length][];
        FloatFFT_1D FFT = new FloatFFT_1D(pcmSplit[0].length);
        for (int i = 0; i < pcmSplit.length; i++) {
            FFTR[i] = pcmSplit[i].clone();
            FFT.realForward(FFTR[i]);
        }

        return FFTR;
    }

    public static float[] averageSpilt(float[][] pcmSplit) {
        float[] pcmAverage = new float[pcmSplit[0].length];

        for (int i = 0; i < pcmSplit.length; i++) {
            for (int k = 0; k < pcmSplit[0].length; k++) {
                pcmAverage[k] += pcmSplit[i][k];
            }
        }

        for (int i = 0; i < pcmAverage.length; i++) {
            pcmAverage[i] /= pcmSplit.length;
        }

        return pcmAverage;
    }
}
