Example usage for org.apache.commons.math3.transform FastFourierTransformer FastFourierTransformer

List of usage examples for org.apache.commons.math3.transform FastFourierTransformer FastFourierTransformer

Introduction

In this page you can find the example usage for org.apache.commons.math3.transform FastFourierTransformer FastFourierTransformer.

Prototype

public FastFourierTransformer(final DftNormalization normalization) 

Source Link

Document

Creates a new instance of this class, with various normalization conventions.

Usage

From source file:com.mycompany.semconsolewebapp.FFT.java

public static double[] forward(double[] data) {
    FastFourierTransformer f = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] fftC = f.transform(data, TransformType.FORWARD);

    for (int i = 0; i < data.length; i++) {

        data[i] = fftC[i].abs();// www  . ja  va2 s .  c  om

        if (Double.isNaN(data[i])) {
            System.out.println("C " + fftC[i].getReal() + " + i" + fftC[i].getImaginary());
            System.out.println("D " + data[i]);
            break;
        }
    }

    return data;
}

From source file:br.prof.salesfilho.oci.util.OCIUtils.java

public static Complex[] fft(double[] input, TransformType type) {
    FastFourierTransformer fft = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] resultComplex = fft.transform(input, type);
    return resultComplex;
}

From source file:com.fpuna.preproceso.util.Util.java

public static double[] transform(double[] input) {
    //double[] tempConversion = new double[input.length];
    double[] tempConversion = new double[2048];

    FastFourierTransformer transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    try {/*  ww  w.  j  ava  2 s. c  o  m*/
        Complex[] complx = transformer.transform(input, TransformType.FORWARD);

        for (int i = 0; i < complx.length; i++) {
            double rr = (complx[i].getReal());
            double ri = (complx[i].getImaginary());

            tempConversion[i] = Math.sqrt((rr * rr) + (ri * ri));
        }

    } catch (IllegalArgumentException e) {
        System.out.println(e);
    }

    return tempConversion;
}

From source file:br.prof.salesfilho.oci.util.OCIUtils.java

public static Complex[] fft(Complex[] input, TransformType type) {
    FastFourierTransformer fft = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] resultComplex = fft.transform(input, type);
    return resultComplex;
}

From source file:com.bleedobsidian.datawave.utils.Sinewave.java

/**
 * Calculate the frequency of sine wave from sound data.
 * //from   w  ww. j  a  va2  s .co  m
 * @param sampleRate Sample rate.
 * @param samples Samples.
 * 
 * @return Frequency.
 */
public static double calculateFrequency(double sampleRate, double[] samples) {
    FastFourierTransformer transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] complex = transformer.transform(samples, TransformType.FORWARD);

    double real;
    double im;
    double mag[] = new double[complex.length];

    for (int i = 0; i < complex.length; i++) {
        real = complex[i].getReal();
        im = complex[i].getImaginary();
        mag[i] = Math.sqrt((real * real) + (im * im));
    }

    double peak = 0.2;
    int index = -1;
    for (int i = 0; i < complex.length; i++) {
        if (peak < mag[i]) {
            index = i;
            peak = mag[i];
        }
    }

    return ((sampleRate * index) / Audio.SAMPLE_BUFFER_SIZE) * 2;
}

From source file:br.prof.salesfilho.oci.util.OCIUtils.java

public static double[] fft_forward_double(double[] input) {
    FastFourierTransformer fft = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] complexTransInput = fft.transform(input, TransformType.FORWARD);
    double[] result = new double[complexTransInput.length];
    for (int i = 0; i < complexTransInput.length; i++) {
        result[i] = (complexTransInput[i].getReal());
    }/*from  w  ww  .j  a  v a 2s  .c  om*/
    return result;
}

From source file:br.prof.salesfilho.oci.util.OCIUtils.java

public static double[] fft_magnitude(double[] input) {
    FastFourierTransformer fft = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] complexTransInput = fft.transform(input, TransformType.FORWARD);
    for (int i = 0; i < complexTransInput.length; i++) {
        double real = (complexTransInput[i].getReal());
        double img = (complexTransInput[i].getImaginary());
        input[i] = (Math.pow(real, 2) + Math.pow(img, 2));
    }// w  w  w.ja  va 2 s  .  c o m
    return input;
}

From source file:net.sf.dsp4j.octave.packages.signal_1_0_11.Freqz.java

public Freqz(double[] b, double[] a, int n) {
    FastFourierTransformer fft = new FastFourierTransformer(DftNormalization.STANDARD);

    Complex[] hb = fft.transform(Arrays.copyOf(b, 2 * n), TransformType.FORWARD);
    Complex[] ha = fft.transform(Arrays.copyOf(a, 2 * n), TransformType.FORWARD);

    H = new Complex[n];
    w = new double[n];

    for (int i = 0; i < H.length; i++) {
        H[i] = hb[i].divide(ha[i]);/* w w  w .  j a  v a 2  s . c o m*/
        w[i] = Math.PI / n * i;
    }

}

From source file:net.sf.dsp4j.octave.packages.signal_1_0_11.Freqz.java

public Freqz(double[] b, int n) {
    FastFourierTransformer fft = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] hb = fft.transform(Arrays.copyOf(b, 2 * n), TransformType.FORWARD);

    H = Arrays.copyOfRange(hb, 0, n);
    w = new double[n];

    for (int i = 0; i < H.length; i++) {
        w[i] = Math.PI / n * i;// w  w w .  ja  v a 2 s .c o  m
    }
}

From source file:gedi.util.math.stat.distributions.PoissonBinomial.java

private void preprocess() {
    int n = pp.length;
    m = n + 1;//from  w  w w  .j a  v  a2 s  .c  om
    int nextPowerOf2 = Integer.highestOneBit(m);
    if (nextPowerOf2 != m)
        nextPowerOf2 <<= 1;
    m = nextPowerOf2;
    n = m - 1;

    int ins = 0;
    int start = 0;
    for (int i = 1; i < pp.length; i++) {
        if (Math.abs(pp[i] - pp[start]) > 1E-10) {
            if (i - start > 1) {
                double p = pp[start];
                pp[ins++] = -(i - start);
                pp[ins++] = p;
            } else {
                pp[ins++] = pp[i - 1];
            }
            start = i;
        }
    }

    if (pp.length - start > 1) {
        double p = pp[start];
        pp[ins++] = -(pp.length - start);
        pp[ins++] = p;
    } else {
        pp[ins++] = pp[pp.length - 1];
    }

    double delta = 2 * Math.PI / m;
    z = new Complex[m];
    z[0] = new Complex(1, 0);

    for (int i = 1; i <= Math.ceil(n / 2.0); i++) {
        double tt = i * delta;

        //         for(int j=0;j<pp.length;j++)
        //         {
        //            double pj=j<opp.length?opp[j]:0;
        //            double ax=1-pj+pj*Math.cos(tt);
        //            double bx=pj*Math.sin(tt);
        //            double tmp1=Math.sqrt(ax*ax+bx*bx);
        //            double tmp2=Math.atan2(bx,ax); //atan2(x,y)
        //            c1o+=Math.log(tmp1);
        //            c2o+=tmp2;
        //         }

        double c1 = 0.00;
        double c2 = 0.00;
        for (int j = 0; j < ins; j++) {
            double pj = pp[j];
            double f = 1;
            if (pj < 0) {
                f = -pj;
                pj = pp[++j];
            }

            double ax = 1 - pj + pj * Math.cos(tt);
            double bx = pj * Math.sin(tt);
            double tmp1 = Math.sqrt(ax * ax + bx * bx);
            double tmp2 = Math.atan2(bx, ax); //atan2(x,y)
            c1 += Math.log(tmp1) * f;
            c2 += tmp2 * f;
        }
        z[i] = new Complex(Math.exp(c1) * Math.cos(c2), Math.exp(c1) * Math.sin(c2));
        z[z.length - i] = z[i].conjugate();
    }
    FastFourierTransformer fft = new FastFourierTransformer(DftNormalization.STANDARD);
    z = fft.transform(z, TransformType.FORWARD);
}