Example usage for org.apache.commons.math3.transform TransformType FORWARD

List of usage examples for org.apache.commons.math3.transform TransformType FORWARD

Introduction

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

Prototype

TransformType FORWARD

To view the source code for org.apache.commons.math3.transform TransformType FORWARD.

Click Source Link

Document

The type to be specified for forward transforms.

Usage

From source file:br.prof.salesfilho.oci.service.ImageDescriptorService.java

/**
 * @param signal/*from  ww  w  .jav a2 s .c  o m*/
 * @param channel RGB and grayscale (1 = RED, 2 = GREEN, 3 = BLUE and 4 =
 * GRAYSCALE, diferent value GRAYSCALE is returned)
 * @return array of abs FFT
 */
public double[] fft(double[] signal, int channel) {
    double[] result = new double[signal.length];

    FastFourierTransformer fft = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] complexTransInput = fft.transform(signal, TransformType.FORWARD);
    for (int i = 0; i < complexTransInput.length; i++) {
        result[i] = complexTransInput[i].getReal() + complexTransInput[i].getImaginary();
    }
    return result;
}

From source file:experiment.FastSineTransformer_bug.java

/**
 * {@inheritDoc}// w  w  w  .j ava 2  s .c  o m
 *
 * The first element of the specified data set is required to be {@code 0}.
 *
 * @throws MathIllegalArgumentException if the length of the data array is
 *   not a power of two, or the first element of the data array is not zero
 */
public double[] transform(final double[] f, final TransformType type) {
    if (normalization == DstNormalization.ORTHOGONAL_DST_I) {
        final double s = FastMath.sqrt(2.0 / f.length);
        return TransformUtils.scaleArray(fst(f), s);
    }
    if (type == TransformType.FORWARD) {
        return fst(f);
    }
    final double s = 2.0 / f.length;
    return TransformUtils.scaleArray(fst(f), s);
}

From source file:br.prof.salesfilho.oci.service.ImageDescriptorService.java

/**
 * @param image input image (signal)/*from   ww  w  .j  a va 2 s.com*/
 * @param channel RGB and grayscale (1 = RED, 2 = GREEN, 3 = BLUE and 4 =
 * GRAYSCALE, diferent value GRAYSCALE is returned)
 * @return array of abs FFT
 */
public double[] fft(BufferedImage image, int channel) {
    double[] result = OCIUtils.vetorizeWithSpatialEntropySequence(this.getColorMatrix(image, channel));

    FastFourierTransformer fft = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] complexTransInput = fft.transform(result, TransformType.FORWARD);
    for (int i = 0; i < complexTransInput.length; i++) {
        result[i] = complexTransInput[i].getReal() + complexTransInput[i].getImaginary();
    }
    return result;
}

From source file:br.prof.salesfilho.oci.service.ImageDescriptorService.java

/**
 * @param image input image (signal)// w  w  w. j a v a2s.  com
 * @param channel RGB and grayscale (1 = RED, 2 = GREEN, 3 = BLUE and 4 =
 * GRAYSCALE, diferent value GRAYSCALE is returned)
 * @param kernel Kernel size
 * @return getMagnitude/modulo = fft(autocorrentropia)) as array
 */
public double[] magnitude(BufferedImage image, int channel, double kernel) {
    double[] result = this.autoCorrentropy(image, channel, kernel);

    FastFourierTransformer fft = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] complexTransInput = fft.transform(result, TransformType.FORWARD);
    for (int i = 0; i < complexTransInput.length; i++) {
        double real = (complexTransInput[i].getReal());
        double img = (complexTransInput[i].getImaginary());
        result[i] = Math.sqrt(Math.pow(real, 2) + Math.pow(img, 2));
    }
    return result;
}

From source file:gr.iti.mklab.reveal.forensics.maps.dq.DQExtractor.java

public void detectDQDiscontinuities() {

    int imWidth = dcts.length;
    int imHeight = dcts[0].length;

    int[] p_h_avg = new int[maxCoeffs];
    int[] p_h_fft = new int[maxCoeffs];
    int[] p_final = new int[maxCoeffs];

    double[][] pTampered = new double[maxCoeffs][];
    double[][] pUntampered = new double[maxCoeffs][];

    for (int coeffIndex = 0; coeffIndex < maxCoeffs; coeffIndex++) {

        int coe = coeff[coeffIndex];
        int startY = coe % 8 - 1;
        if (startY == -1) {
            startY = 8;//from  ww  w .j  a  v a  2  s. co m
        }
        int startX = (int) Math.floor((coe - 1) / 8);

        List<Integer> selectedCoeffs = new ArrayList<Integer>();
        for (int ii = startX; ii < imWidth; ii += 8) {
            for (int jj = startY; jj < imHeight; jj += 8) {
                selectedCoeffs.add(dcts[ii][jj]);
            }
        }

        int minCoeffValue = Collections.min(selectedCoeffs);
        int maxCoeffValue = Collections.max(selectedCoeffs);
        int s_0;
        Double[] coeffHist = new Double[0];
        if (maxCoeffValue - minCoeffValue > 0) {
            //will be a power of 2 to allow for fft (zero padded)
            int trueHistRange = maxCoeffValue - minCoeffValue + 1;
            //int histLength = trueHistRange;
            int histLength = (int) Math.pow(2, Math.ceil(Math.log(trueHistRange) / Math.log(2)));

            coeffHist = new Double[histLength];

            for (int ii = 0; ii < coeffHist.length; ii++) {
                coeffHist[ii] = 0.0;
            }

            for (Integer selectedCoeff : selectedCoeffs) {
                coeffHist[selectedCoeff - minCoeffValue] += 1;
            }

            List<Double> coeffHistList = Arrays.asList(coeffHist);
            s_0 = coeffHistList.indexOf(Collections.max(coeffHistList));

            List<Double> h = new ArrayList<>();
            DescriptiveStatistics vals;
            for (int coeffInd = 1; coeffInd < coeffHistList.size(); coeffInd++) {
                vals = new DescriptiveStatistics();
                for (int leapInd = s_0; leapInd < coeffHistList.size(); leapInd += coeffInd) {
                    vals.addValue(coeffHistList.get(leapInd));
                }
                for (int leapInd = s_0 - coeffInd; leapInd >= 0; leapInd -= coeffInd) {
                    vals.addValue(coeffHistList.get(leapInd));
                }
                h.add(vals.getMean());
            }
            p_h_avg[coeffIndex] = (h.indexOf(Collections.max(h)));

            FastFourierTransformer fastFourierTransformer = new FastFourierTransformer(
                    DftNormalization.STANDARD);
            Complex[] fft = fastFourierTransformer.transform(ArrayUtils.toPrimitive(coeffHist),
                    TransformType.FORWARD);

            double[] power = new double[fft.length];
            for (int ii = 0; ii < power.length; ii++) {
                power[ii] = fft[ii].abs();
            }

            //Find first local minimum, to bypass DC peak
            double DC = power[0];
            int FreqValley = 1;
            while (FreqValley < power.length - 1 & power[FreqValley] >= power[FreqValley + 1]) {
                FreqValley++;
            }

            int maxFFTInd = 0;
            double maxFFTVal = 0;
            double minFFTVal = Double.MAX_VALUE;
            for (int ii = FreqValley; ii < power.length / 2; ii++) {
                if (power[ii] > maxFFTVal) {
                    maxFFTInd = ii;
                    maxFFTVal = power[ii];
                }
                if (power[ii] < minFFTVal) {
                    minFFTVal = power[ii];
                }
            }
            if (maxFFTInd == 0 | maxFFTVal < (DC / 5) | minFFTVal / maxFFTVal > 0.9) {
                p_h_fft[coeffIndex] = 1;
            } else {
                p_h_fft[coeffIndex] = Math.round(coeffHist.length / maxFFTInd);
            }

        } else {
            p_h_avg[coeffIndex] = 1;
            p_h_fft[coeffIndex] = 1;
            s_0 = 0;
        }
        if (p_h_avg[coeffIndex] < p_h_fft[coeffIndex]) {
            p_final[coeffIndex] = p_h_avg[coeffIndex];
        } else {
            p_final[coeffIndex] = p_h_fft[coeffIndex];
        }

        pTampered[coeffIndex] = new double[selectedCoeffs.size()];
        pUntampered[coeffIndex] = new double[selectedCoeffs.size()];
        int[] adjustedCoeffs = new int[selectedCoeffs.size()];
        int[] period_start = new int[selectedCoeffs.size()];
        int[] period;
        int[] num = new int[selectedCoeffs.size()];
        int[] denom = new int[selectedCoeffs.size()];
        double[] P_u = new double[selectedCoeffs.size()];
        double[] P_t = new double[selectedCoeffs.size()];

        if (p_final[coeffIndex] != 1) {
            for (int ii = 0; ii < adjustedCoeffs.length; ii++) {
                adjustedCoeffs[ii] = selectedCoeffs.get(ii) - minCoeffValue;
                period_start[ii] = adjustedCoeffs[ii] - rem(adjustedCoeffs[ii] - s_0, p_final[coeffIndex]);
            }
            for (int kk = 0; kk < selectedCoeffs.size(); kk++) {
                if (period_start[kk] > s_0) {
                    period = new int[p_final[coeffIndex]];
                    for (int ii = 0; ii < p_final[coeffIndex]; ii++) {
                        period[ii] = period_start[kk] + ii;
                        if (period[ii] >= coeffHist.length) {
                            period[ii] = period[ii] - p_final[coeffIndex];
                        }
                    }
                    num[kk] = (int) coeffHist[adjustedCoeffs[kk]].doubleValue();
                    denom[kk] = 0;
                    for (int ll = 0; ll < period.length; ll++) {
                        denom[kk] = denom[kk] + (int) coeffHist[period[ll]].doubleValue();
                    }
                } else {
                    period = new int[p_final[coeffIndex]];
                    for (int ii = 0; ii < p_final[coeffIndex]; ii++) {
                        period[ii] = period_start[kk] - ii;
                        if (period_start[kk] - p_final[coeffIndex] + 1 <= 0) {
                            if (period[ii] <= 0) {
                                period[ii] = period[ii] + p_final[coeffIndex];
                            }
                        }
                    }
                    num[kk] = (int) coeffHist[adjustedCoeffs[kk]].doubleValue();
                    denom[kk] = 0;
                    for (int ll = 0; ll < period.length; ll++) {
                        denom[kk] = denom[kk] + (int) coeffHist[period[ll]].doubleValue();
                    }
                }

                P_u[kk] = ((double) num[kk] / denom[kk]);
                P_t[kk] = (1.0 / p_final[coeffIndex]);
                if (P_u[kk] + P_t[kk] != 0) {
                    pTampered[coeffIndex][kk] = P_t[kk] / (P_u[kk] + P_t[kk]);
                    pUntampered[coeffIndex][kk] = P_u[kk] / (P_u[kk] + P_t[kk]);

                } else {
                    pTampered[coeffIndex][kk] = 0.5;
                    pUntampered[coeffIndex][kk] = 0.5;
                }
            }

        } else {
            for (int kk = 0; kk < selectedCoeffs.size(); kk++) {
                pTampered[coeffIndex][kk] = 0.5;
                pUntampered[coeffIndex][kk] = 0.5;
            }
        }

    }
    double[] pTamperedOverall = new double[pTampered[0].length];
    double pTamperedProd;
    double pUntamperedProd;

    for (int locationIndex = 0; locationIndex < pTampered[0].length; locationIndex++) {
        pTamperedProd = 1;
        pUntamperedProd = 1;
        for (int coeffIndex = 0; coeffIndex < pTampered.length; coeffIndex++) {
            pTamperedProd = pTamperedProd * pTampered[coeffIndex][locationIndex];
            pUntamperedProd = pUntamperedProd * pUntampered[coeffIndex][locationIndex];
        }
        if (pTamperedProd + pUntamperedProd != 0) {
            pTamperedOverall[locationIndex] = pTamperedProd / (pTamperedProd + pUntamperedProd);
        } else {
            pTamperedOverall[locationIndex] = 0;
        }
    }

    int blocksH = imWidth / 8;
    int blocksV = imHeight / 8;
    double[][] outputMap = new double[blocksV][blocksH];
    for (int kk = 0; kk < pTamperedOverall.length; kk++) {
        outputMap[kk % blocksV][(int) Math.floor(kk / blocksV)] = pTamperedOverall[kk];
        if (pTamperedOverall[kk] > maxProbValue) {
            maxProbValue = pTamperedOverall[kk];
        }
        if (pTamperedOverall[kk] < minProbValue) {
            minProbValue = pTamperedOverall[kk];
        }
    }
    probabilityMap = outputMap;
    BufferedImage outputIm = visualizeWithJet(outputMap);
    // output
    displaySurface = outputIm;
}

From source file:br.prof.salesfilho.oci.service.ImageDescriptorService.java

/**
 * @param signal/*from w  ww  .j a  v  a2 s .  com*/
 * @param kernel Kernel size
 * @return getMagnitude/modulo = fft(autocorrentropia)) as array
 */
public double[] magnitude(double[] signal, double kernel) {
    double[] result = this.autoCorrentropy(signal, kernel);

    FastFourierTransformer fft = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] complexTransInput = fft.transform(result, TransformType.FORWARD);
    for (int i = 0; i < complexTransInput.length; i++) {
        double real = (complexTransInput[i].getReal());
        double img = (complexTransInput[i].getImaginary());
        result[i] = Math.sqrt(Math.pow(real, 2) + Math.pow(img, 2));
    }
    return result;
}

From source file:br.prof.salesfilho.oci.image.ImageProcessor.java

/**
 * @param channel RGB and grayscale (1 = RED, 2 = GREEN, 3 = BLUE and 4 =
 * GRAYSCALE, diferent value GRAYSCALE is returned)
 * @param kernel Kernel size to be used by correntropy calculation
 * @return getMagnitude/modulo = fft(autocorrentropia)) as array
 *//*w ww.j  a v a 2 s  .co  m*/
public double[] getMagnitude(int channel, double kernel) {

    double[] result = this.getAutoCorrentropy(channel, kernel);

    FastFourierTransformer fft = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] complexTransInput = fft.transform(result, TransformType.FORWARD);
    for (int i = 0; i < complexTransInput.length; i++) {
        double real = (complexTransInput[i].getReal());
        double img = (complexTransInput[i].getImaginary());
        result[i] = Math.sqrt(Math.pow(real, 2) + Math.pow(img, 2));
    }
    return result;
}

From source file:experiment.FastCosineTransformer_bug2.java

/**
 * Perform the FCT algorithm (including inverse).
 * /*from  w w  w. j ava2 s .c  o m*/
 * @param f
 *            the real data array to be transformed
 * @return the real transformed array
 * @throws MathIllegalArgumentException
 *             if the length of the data array is not a power of two plus
 *             one
 */
protected double[] fct(double[] f) throws MathIllegalArgumentException {
    final double[] transformed = new double[f.length];
    final int n = f.length - 1;
    if (!ArithmeticUtils.isPowerOfTwo(n)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NOT_POWER_OF_TWO_PLUS_ONE,
                Integer.valueOf(f.length));
    }
    if (n == 1) { // trivial case
        transformed[0] = 0.5 * (f[0] + f[1]);
        transformed[1] = 0.5 * (f[0] - f[1]);
        return transformed;
    }
    test test1 = new test();
    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.5 * (f[0] + f[n]);
    String funname = "cosh/";
    double tempexpression = 0;
    double ta = 3.24, tb = 2.31, tc = 7.86, td = 5.12;
    int te = 2;
    boolean tf = false;
    x[n >> 1] = f[n >> 1];
    ta = tb + tc + mid((int) ta + 1, (int) tb, (int) tc) + td;
    // temporary variable for transformed[1]
    double t1 = 0.5 * (f[0] - f[n]);
    ta = (te >> 2) + tc % tb + td;
    ta = tb + tc + td;
    ta = tb + tc - td;
    ta = tb + tc + td + te;
    ta = tb * tc * td;
    ta = tb * tc / td;
    ta = tb * tc * td * te;
    ta = ta * ta + tb * tb + tc * tc;
    ta = tc - (td + te);
    ta = tc + tb - (td + te + tc);
    ta = tc * tb / tc + test1.a + 3;
    ta = tc + tb / td - test1.f.a;
    ta = td + Math.cos(ta + tc - td - te * tb) + tb;
    ta = Math.min(tc, td + 1) + 1;
    for (int i = 1; i < (n >> 1); i++) {
        final double a = 0.5 * (f[i] + f[n - i]);

        final double b = FastMath.sin(i * FastMath.PI / n) * (f[i] - f[n - i]);

        /*****
         * bug2 store in Data2 FastMath.sin(i * FastMath.PI / n) to
         * FastMath.sin(2*i * FastMath.PI / n)
         *******/

        final double c = FastMath.cos(i * FastMath.PI / n) * (f[i] - f[n - i]);

        x[i] = a + b;

        x[n - i] = a - b;

        tempexpression = t1;

        t1 = t1 + c;

    }

    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FCT result for the original array
    transformed[0] = y[0].getReal();

    transformed[1] = t1;

    for (int i = 1; i < (n >> 1); i++) {

        transformed[2 * i] = y[i].getReal();

        /***
         * bug 1, store in Data1, add Math.abs() on transformed[2 * i - 1] -
         * y[i].getImaginary()
         ***/

        transformed[2 * i + 1] = transformed[2 * i - 1] - y[i].getImaginary();

    }

    transformed[n] = y[n >> 1].getReal();

    return transformed;
}

From source file:experiment.FastSineTransformer_bug.java

/**
 * Perform the FST algorithm (including inverse). The first element of the
 * data set is required to be {@code 0}.
 *
 * @param f the real data array to be transformed
 * @return the real transformed array/*from   ww  w.  j a  v a2s. c  o  m*/
 * @throws MathIllegalArgumentException if the length of the data array is
 *   not a power of two, or the first element of the data array is not zero
 */
protected double[] fst(double[] f) throws MathIllegalArgumentException {

    final double[] transformed = new double[f.length];

    if (!ArithmeticUtils.isPowerOfTwo(f.length)) {
        throw new MathIllegalArgumentException(LocalizedFormats.NOT_POWER_OF_TWO_CONSIDER_PADDING,
                Integer.valueOf(f.length));
    }
    if (f[0] != 0.0) {
        throw new MathIllegalArgumentException(LocalizedFormats.FIRST_ELEMENT_NOT_ZERO, Double.valueOf(f[0]));
    }
    final int n = f.length;
    if (n == 1) { // trivial case
        transformed[0] = 0.0;
        return transformed;
    }

    // construct a new array and perform FFT on it
    final double[] x = new double[n];
    x[0] = 0.0;

    x[n >> 1] = 2.0 * f[n >> 1];
    double[] data1 = { x[n >> 1], f[n >> 1] };
    log.add(1, data1, "x[n>>1] f[n>>1]");//x[n>>1] f[n>>1] exp1
    double[] data2 = { f[n >> 1] };
    log.add(2, data2, "f[n>>1]");//f[n>>1] exp2
    double tempa = 0;
    double tempb = 0;
    int tempi = 0;
    /*Bug2 change FastMath.PI/n to FastMath.PI/(n-1)+1E-10 store in Data1
     * Bug 3 change 0.5 to 0.499 for b store in Data2
     */
    /*Bug1 add a small value*/
    for (int i = 1; i < (n >> 1); i++) {
        final double a = FastMath.sin(i * FastMath.PI / n) * (f[i] + f[n - i]);
        final double b = 0.499 * (f[i] - f[n - i]);
        x[i] = a + b;
        x[n - i] = a - b;
        tempa = a;
        tempb = b;
        tempi = i;
    }

    double[] data3 = { tempa, FastMath.sin(tempi * FastMath.PI / n), f[tempi] + f[n - tempi] };
    log.add(3, data3, "a");// a FastMath.sin(i*FastMath.PI/n)  (f[i]+f[n-i])
    double[] data4 = { FastMath.sin(tempi * FastMath.PI / n), (double) tempi };// FastMath.sin(i * FastMath.PI / n) i
    log.add(4, data4, "FastMath.sin(temp * FastMath.PI / n)");
    double[] data5 = { f[tempi] + f[n - tempi], f[tempi], f[n - tempi] };
    log.add(5, data5, "f[temp] + f[n - temp]");// (f[i] + f[n - i]) f[i] f[n-i]
    double[] data6 = { f[tempi] };
    log.add(6, data6, "f[temp]");//f[i]
    double[] data7 = { f[n - tempi] };
    log.add(7, data7, "f[n-temp]");//f[n-i]
    double[] data8 = { x[tempi], tempa, tempb };
    log.add(8, data8, "x[i]");//x[i] a b
    double[] data9 = { x[n - tempi], tempa, tempb };
    log.add(9, data9, "x[n-i]");//x[n-i] a b
    double[] data10 = { tempa };
    log.add(10, data10, "a");//a
    double[] data11 = { tempb, f[tempi], f[n - tempi] };
    log.add(11, data11, "b");//b
    FastFourierTransformer transformer;
    transformer = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] y = transformer.transform(x, TransformType.FORWARD);

    // reconstruct the FST result for the original array
    transformed[0] = 0.0;
    /*Bug 1 add a small number 0.03 to transformed[1]  */
    transformed[1] = 0.5 * y[0].getReal();
    double[] data12 = { transformed[1], y[0].getReal() };
    log.add(12, data12, "transformed[1]");//transformed[1] y[0].getReal()
    double[] data13 = { y[0].getReal() };
    log.add(13, data13, "y[0].getReal()");//y[0].getReal()
    for (int i = 1; i < (n >> 1); i++) {
        /*Bug 4 add abs to y[i].getImaginary  */
        transformed[2 * i] = -y[i].getImaginary();
        /*Bug 5 change 2*i-1 to 2*i           */
        transformed[2 * i + 1] = y[i].getReal() + transformed[2 * i - 1];
        tempi = i;
    }
    double[] data14 = { transformed[2 * tempi] };
    log.add(14, data14, "transformed[2*i]");// transformed[2*i]  -y[i].getImaginary();
    double[] data15 = { transformed[2 * tempi + 1], y[tempi].getReal(), transformed[2 * tempi - 1] };// transformed[2*i+1]  y[i].getReal()  transformed[2 * i - 1]
    log.add(15, data15, "transformed[2*ti+1]");
    double[] data16 = { y[tempi].getReal() };
    log.add(16, data16, "y[i].getReal()");// y[i].getReal()
    double[] data17 = { transformed[2 * tempi - 1] };
    log.add(17, data17, "transformed[2 * i - 1]");// transformed[2 * i - 1]
    log.logFile();
    log.clear();
    return transformed;
}

From source file:com.hurence.logisland.botsearch.Trace.java

/**
 *
 * In the next step, we compute the Power Spectral Density (PSD) of the Fast
 * Fourier Transformation over our sampled trace and extract the most
 * significant frequency. The FFT peaks are corralated with time
 * periodicities and resistant against irregular large gaps in the trace. We
 * observed the introduction of gaps in the wild for bots in which
 * communication with the C&C server is periodic and then pauses for a
 * while. When malware authors randomly vary the C&C connection frequency
 * within a certain window, the random variation lowers the FFT peak.
 * However, the peak remains detectable and at the same frequency, enabling
 * the detection of the malware communication.
 *
 *///  ww w  .  j a va  2s.c  o  m
double[] computePowerSpectralDensity(double[] samples) {

    // compute FFT
    FastFourierTransformer fft = new FastFourierTransformer(DftNormalization.STANDARD);
    Complex[] frequencies = fft.transform(samples, TransformType.FORWARD);

    // take the highest magnitude of power spectral density
    double[] magnitudes = new double[frequencies.length / 2];
    for (int i = 0; i < magnitudes.length; i++) {
        // Convert to db
        magnitudes[i] = 10 * Math.log10(frequencies[i].abs());
    }

    // apply a low pass filter to smooth high frequency magnitudes
    smoothArray(magnitudes, 2.0);

    return magnitudes;
}