Example usage for org.apache.commons.math3.complex Complex getReal

List of usage examples for org.apache.commons.math3.complex Complex getReal

Introduction

In this page you can find the example usage for org.apache.commons.math3.complex Complex getReal.

Prototype

public double getReal() 

Source Link

Document

Access the real part.

Usage

From source file:com.wwidesigner.modelling.ImpedanceSpectrumPlot.java

/**
 * @param args/*ww  w . j ava  2s.c o m*/
 */
public static void main(String[] args) {
    com.jidesoft.utils.Lm.verifyLicense("Edward Kort", "WWIDesigner", "DfuwPRAUR5KQYgePf:CH0LWIp63V8cs2");
    ImpedanceSpectrumPlot plot = new ImpedanceSpectrumPlot();
    try {
        String inputInstrumentXML = "com/wwidesigner/optimization/example/E4-instrument_actual.xml";
        String inputTuningXML = "com/wwidesigner/optimization/example/E4-tuning_actual.xml";

        PhysicalParameters params = new PhysicalParameters(22.22, TemperatureType.C);
        Instrument instrument = plot.getInstrumentFromXml(inputInstrumentXML);
        InstrumentCalculator calculator = new NAFCalculator(instrument, params);

        Tuning tuning = plot.getTuningFromXml(inputTuningXML);
        // Fingering fingering = tuning.getFingering().get(0);
        Fingering fingering = tuning.getFingering().get(7);

        instrument.convertToMetres();

        double freqRange = 1.1;
        int numberOfFrequencies = 10000;
        double targetFreq = fingering.getNote().getFrequency();
        double freqStart = targetFreq / freqRange;
        double freqEnd = targetFreq * freqRange;
        ImpedanceSpectrum impSpectrum = new ImpedanceSpectrum();

        impSpectrum.calcImpedance(instrument, calculator, freqStart, freqEnd, numberOfFrequencies, fingering,
                params);
        impSpectrum.plotImpedanceSpectrum();

        Complex fluteImpedance = calculator.calcZ(targetFreq, fingering);
        String outStr = "Flute impedance: " + fluteImpedance.getReal() + ", " + fluteImpedance.getImaginary()
                + "at " + targetFreq + " Hz";
        System.out.println(outStr);

        ReflectanceSpectrum reflSpectrum = new ReflectanceSpectrum();
        reflSpectrum.calcReflectance(instrument, calculator, 200., 4000., numberOfFrequencies, fingering,
                params);
        List<Double> magMinima = reflSpectrum.getMagnitudeMinima();
        double predFreq = reflSpectrum.getClosestMinimumFrequency(targetFreq);
        outStr = "Flute impedance magnitude minima for " + fingering.getNote().getName() + " "
                + fingering.toString() + " : " + magMinima.get(0) + " " + magMinima.get(1) + " "
                + magMinima.get(2) + " " + magMinima.get(3) + " at target " + targetFreq + " Hz, and predicted "
                + predFreq + " Hz";
        System.out.println(outStr);
        reflSpectrum.plotReflectanceSpectrum(ReflectanceSpectrum.PLOT_REFL_MAGNITUDE_ONLY);
    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:matrix.MatrixTest.java

/**
 * @param args the command line arguments
 *//*  w  ww  . j  a v a 2s.c o  m*/
public static void main(String[] args) {
    // TODO code application logic here
    Matrix testMatrix = new Matrix();
    System.out.println("My testMatrix (should be identity 2x2):");
    System.out.println(testMatrix);

    Matrix testMatrix2 = Matrix.IdentityMatrix(3);
    System.out.println("My testMatrix2 (should be identity 3x3) : ");
    System.out.println(testMatrix2);

    Matrix testMatrix3 = Matrix.IdentityMatrix(3);
    System.out.println("My testMatrix3 (should be identity 3x3) : ");
    System.out.println(testMatrix3);

    int[][] myArray = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 2 } };

    Matrix testMatrix4 = new Matrix(myArray);

    System.out.println("My testMatrix4 :");
    System.out.println(testMatrix4);

    double[][] mySecondArray = { { 1, 2, 3 }, { 4, 12, 6 }, { 7, 8, 11 } };

    Matrix testMatrix5 = new Matrix(mySecondArray);

    System.out.println("My testMatrix5 :");
    System.out.println(testMatrix5);

    System.out.println("Result of testMatrix2.equals(testMatrix3): " + testMatrix2.equals(testMatrix3));
    System.out.println("Result of testMatrix.equals(testMatrix3): " + testMatrix.equals(testMatrix3));
    System.out.println("Result of testMatrix3.equals(testMatrix4): " + testMatrix3.equals(testMatrix4));
    try {
        System.out.println(
                "\nResult of Matrix.mult(testMatrix4, testMatrix3):\n" + Matrix.mult(testMatrix4, testMatrix3));
        System.out.println("\nResult of testMatrix3.mult(testMatrix4):\n" + testMatrix3.mult(testMatrix4));
        System.out.println("\nResult of testMatrix4.mult(testMatrix5):\n" + testMatrix4.mult(testMatrix5));
        System.out.println("\nResult of testMatrix5.mult(testMatrix4):\n" + testMatrix5.mult(testMatrix4));
    } catch (DimensionMismatchException ex) {
        System.out.println(ex.getMessage());
    }

    SquareMatrix testSquare1 = new SquareMatrix(testMatrix5);
    try {
        LUPDecomposition testDecompose = new LUPDecomposition();
        testDecompose.decompose(testSquare1);
    } catch (NonInvertibleMatrixException ex) {
        System.out.println("LUDecomposition of testDecompose object threw exception:\n" + ex.getMessage());
    }

    double[][] myThirdArray = { { 7, 4, 2, 0 }, { 6, 3, -1, 2 }, { 4, 6, 2, 5 }, { 8, 2, -7, 1 } };

    SquareMatrix testSquare2 = new SquareMatrix(new Matrix(myThirdArray));

    try {
        LUPDecomposition testDecompose2 = new LUPDecomposition();
        testDecompose2.decompose(testSquare2);
    } catch (NonInvertibleMatrixException ex) {
        System.out.println("LUDecomposition of testDecompose2 object threw exception:\n" + ex.getMessage());
    }

    System.out.println("Smoke test for testSquare2.determinant():\t" + testSquare2.determinant());
    System.out.println("Second smoke test for testSquare1.determinant():\t" + testSquare1.determinant() + "\n");
    try {
        SquareMatrix InvertedTestSquare2 = SquareMatrix.invert(testSquare2);
        System.out.println("InvertedTestSquare2:\n" + InvertedTestSquare2);
        System.out.println("Result of testSquare2.mult(InvertedTestSquare2): (should be identity matrix)\n"
                + testSquare2.mult(InvertedTestSquare2));
        System.out.println("Result of InvertedTestSquare2.mult(TestSquare2): (should be identity matrix)\n"
                + InvertedTestSquare2.mult(testSquare2));
    } catch (NonInvertibleMatrixException ex) {
        System.out.println("Inverting matrix testSquare2 threw exception:\n" + ex.getMessage());
    } catch (DimensionMismatchException ex) {
        System.out.println("Multiplying Matrix failed. (shouldn't have happened, exiting");
        System.exit(-1);
    }

    SquareMatrix testSquare3 = new SquareMatrix(Matrix.transpose(testSquare2));

    System.out.println(
            "testSquare2:\n" + testSquare2 + "\ntestSquare3: (transpose of testSquare2)\n" + testSquare3);
    try {
        System.out.println("Result of testSquare2.subtract(testSquare3)\n" + testSquare2.subtract(testSquare3));
    } catch (DimensionMismatchException ex) {
        System.out.println("Subtracting Matrix failed. (shouldn't have happened, exiting");
        System.exit(-1);
    }

    /*float[][] fail = {
     {1, 2, 3},
     {4, 5, 7},
     {3, 6, 8},
     {4, 5, 9}
     };*/
    //Matrix goodMatrix = new Matrix(fail);
    //SquareMatrix badMatrix = new SquareMatrix(goodMatrix);
    //System.out.print(badMatrix);
    Matrix random = Matrix.random(3, 5, 100, 1);

    System.out.println("random matrix: (should be 3x5)\n" + random);

    int BMFsize = 500;
    System.out.println("smoke checking my mult method with " + BMFsize + "x" + BMFsize + " matrices.\n");

    Matrix big1 = Matrix.random(BMFsize, BMFsize, 2, -2);
    Matrix big2 = Matrix.random(BMFsize, BMFsize, 2, -2);
    SquareMatrix big3 = null;

    try {
        big3 = new SquareMatrix(big1.mult(big2));
    } catch (DimensionMismatchException ex) {
        System.out.println("this exception shouldn't have happened, bailing");
        System.exit(-1);
    }

    System.out.println("smoke checking determinant method with " + BMFsize + "x" + BMFsize + " matrix.\n");
    System.out.println("Result of big3.determinant():\t" + big3.determinant() + "\n");

    System.out.println("Performing basic check of QR Decomp with known 4x4 matrix.\n");

    double[][] qrTest = { { 4, 8, 9, 1 }, { 2, -3, 1, -1 }, { 8, 15, 3, 1 }, { 7, 1, 2, 3 } };

    Matrix QRTest = new Matrix(qrTest);

    QRDecomposition qr = new QRDecomposition();

    qr.decompose(QRTest);

    System.out.println("\nQ:\n" + qr.Q + "\nR:\n" + qr.R + "\nQRtest: (original matrix)\n" + QRTest);

    try {
        System.out.println("Result of Q.mult(R): (should be the original matrix)\n" + qr.Q.mult(qr.R));
    } catch (DimensionMismatchException ex) {
        System.out.println("this exception shouldn't have happened, bailing");
        System.exit(-1);
    }

    try {
        System.out.println("Result of Q.mult(Q.transpose()): (should be the identity matrix)\n"
                + qr.Q.mult(qr.Q.transpose()));
    } catch (DimensionMismatchException ex) {
        System.out.println("this exception shouldn't have happened, bailing");
        System.exit(-1);
    }

    /*System.out.println("Beginning initial testing of eigenvalue function on QRTest:\n" + QRTest);
            
     Complex[] eigen = SquareMatrix.eigenvalues(new SquareMatrix(QRTest));
            
     for (Complex e : eigen) {
     System.out.println("eigenvalue:" + e);
     }*/
    int SMFSize = 10;

    //SquareMatrix big4 = new SquareMatrix(Matrix.random(SMFSize, SMFSize, -5, 5));
    //this matrix is causing a bug, using it to troubleshooot
    double[][] big4Matrix = { { -2.476, -2.814, 4.29, -3.649 }, { 2.839, -2.859, 1.623, -2.926 },
            { -0.392, -3.206, -0.401, -2.174 }, { 2.241, -4.435, -3.963, 4.102 } };
    SquareMatrix big4 = new SquareMatrix(big4Matrix);

    if (SMFSize < 6) {
        System.out.println(
                "Smoke test of eigenvalue function with " + SMFSize + "x" + SMFSize + " matrix:\n" + big4);
    } //only print it if it will fit on the screen
    Complex eigen2[] = big4.eigenvalues();

    //System.out.println(big4.toCopyableString());//used for debugging
    int eig = 1;
    for (Complex e : eigen2) {
        System.out.println("eigenvalue #" + eig++ + ":\t" + Precision.round(e.getReal(), 3) + " + "
                + Precision.round(e.getImaginary(), 3) + "i");
    }

    double[] vector = new double[5];
    double[] vector2 = new double[5];

    Matrix v1 = new Matrix(vector, MatrixConstants.ROW_VECTOR);
    Matrix v2 = new Matrix(vector2, MatrixConstants.COLUMN_VECTOR);

    System.out.println("\nv1: (should be 1x5)\n" + v1 + "\nv2: (should be 5x1)\n" + v2);

    double[][] qrTest2 = { { 4, 2, 2, 1 }, { 2, -3, 1, 1 }, { 2, 1, 3, 1 }, { 1, 1, 1, 2 } };

    Matrix QRTest2 = new Matrix(qrTest2);

    System.out.println("Testing hessenberg() with QRTest2:\n" + QRTest2);

    System.out.println("Hessenberg output for QRTest2:\n" + qr.hessenberg(QRTest2));

    int big6size = 5;
    SquareMatrix big6 = new SquareMatrix(Matrix.random(big6size, big6size, -5, 5));

    System.out.println("Testing hessenberg() with big6:\n" + big6);

    System.out.println("Hessenberg output with big6:\n" + qr.hessenberg(big6));

    //System.out.println(big6.toCopyableString());
}

From source file:com.wwidesigner.math.TransferMatrix.java

public static Complex copyComplex(Complex in) {
    return new Complex(in.getReal(), in.getImaginary());
}

From source file:edu.ucsf.valelab.saim.calculations.SaimCalc.java

public static double fieldStrength(final double wavelength, final double angle, final double nSample,
        final double dOx, final double distance) {
    Complex rTE = fresnelTE(wavelength, angle, dOx, nSample);
    double phaseDiff = PhaseDiff(wavelength, angle, nSample, distance);
    Complex tmp = new Complex(Math.cos(phaseDiff), Math.sin(phaseDiff));
    Complex fieldStrength = rTE.multiply(tmp);
    fieldStrength = fieldStrength.add(1.0);

    return fieldStrength.getReal() * fieldStrength.getReal()
            + fieldStrength.getImaginary() * fieldStrength.getImaginary();
}

From source file:com.thalespf.dip.DeblurringTest.java

public static void normalizeToMaxAbsValue(Complex[] data, int width, int height, double value) {
    double max = Double.MIN_VALUE;

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            Complex c = data[x + y * width];
            double abs = Math.sqrt(c.getReal() * c.getReal() + c.getImaginary() * c.getImaginary());
            max = Math.max(max, abs);
        }//  w  ww .j ava  2s  . com
    }

    for (int x = 0; x < data.length; x++) {
        data[x] = data[x].multiply(value / max);
    }
}

From source file:com.thalespf.dip.DeblurringTest.java

private static Complex deconvolutionByWiener(Complex imagem, Complex psf) {
    double K = Math.pow(1.07, 32) / 10000.0;
    double energyValue = Math.pow(psf.getReal(), 2) + Math.pow(psf.getImaginary(), 2);
    double wienerValue = energyValue / (energyValue + K);

    Complex divided = imagem.divide(psf);
    Complex c = divided.multiply(wienerValue);

    return c;/*w  w w  .j a v a2  s .  c o m*/
}

From source file:com.thalespf.dip.DeblurringTest.java

private static Complex[] motionBlur(double[] degradation, int width, int height) {
    Complex[] complex = new Complex[width * height];

    double[] temp = new double[2 * width * height];

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {

            double teta = Math.PI * ((x - width / 2) % width * A + (y - height / 2) % height * B);

            Sinc sinc = new Sinc();

            double real = Math.cos(teta) * sinc.value(teta) * T;
            double imaginary = Math.sin(teta) * sinc.value(teta) * T;

            Complex c = new Complex(real, imaginary);
            Complex cConj = c.conjugate();

            temp[2 * (x + y * width)] = cConj.getReal();
            temp[2 * (x + y * width) + 1] = cConj.getImaginary();
        }/*from ww  w  . j a v  a2s  .  c o m*/
    }

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            int xTranslated = (x + width / 2) % width;
            int yTranslated = (y + height / 2) % height;

            double real = temp[2 * (yTranslated * width + xTranslated)];
            double imaginary = temp[2 * (yTranslated * width + xTranslated) + 1];

            degradation[2 * (x + y * width)] = real;
            degradation[2 * (x + y * width) + 1] = imaginary;

            Complex c = new Complex(real, imaginary);
            complex[y * width + x] = c;
        }
    }

    return complex;
}

From source file:com.thalespf.dip.DeblurringTest.java

private static void blurTest() throws IOException {
    String imagePath = "image.jpg";
    IImageIO imageIO = ImageIODesktop.createImageIO(imagePath);
    Object imageObject = imageIO.getImageObject();
    if (imageObject == null || !(imageObject instanceof BufferedImage)) {
        throw new IllegalStateException("Nao foi possivel criar a imagem.");
    }/*from  ww  w  .j av a  2s .  c o  m*/

    BufferedImage bufferedImage = (BufferedImage) imageObject;
    WritableRaster raster = bufferedImage.getRaster();

    int width = bufferedImage.getWidth();
    int height = bufferedImage.getHeight();

    double[] fft = new double[2 * width * height];
    FFTForwardInverseTest.fftForward(raster, width, height, fft);

    Complex[] ci = new Complex[width * height];

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {

            Complex c = new Complex(fft[2 * (x + y * width)], fft[2 * (x + y * width) + 1]);
            ci[x + y * width] = c;

        }
    }

    normalizeToMaxAbsValue(ci, width, height, 1);

    double[] degradation = new double[2 * width * height];
    //motionBlur (funcao de transferencia)
    Complex[] complexPsf = motionBlur(degradation, width, height);

    normalizeToMaxAbsValue(complexPsf, width, height, 1);

    //convolve data
    double[] convoluted = new double[2 * width * height];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            double realImg = fft[2 * (x + y * width)];
            double imgImg = fft[2 * (x + y * width) + 1];
            Complex complexImage = new Complex(realImg, imgImg);

            double realDeg = degradation[2 * (x + y * width)];
            double imgDeg = degradation[2 * (x + y * width) + 1];
            Complex complexDeg = new Complex(realDeg, imgDeg);

            Complex m = complexImage.multiply(complexDeg);
            convoluted[2 * (x + y * width)] = m.getReal();
            convoluted[2 * (x + y * width) + 1] = m.getImaginary();
        }
    }

    double[] imageResult = new double[width * height];
    FFTForwardInverseTest.fftInverse(convoluted, imageResult, width, height);

    int[] image = new int[imageResult.length];
    Expansion.globalExpansion2(imageResult, image, width, height);

    ImageIODesktop imageIODesktop = new ImageIODesktop();
    imageIODesktop.savePixels(image, width, height, null, "image_blurred");
}

From source file:com.thalespf.dip.DeblurringTest.java

private static void deblurTest() throws IOException {
    String imagePath = "image_blurred.png";
    IImageIO imageIO = ImageIODesktop.createImageIO(imagePath);
    Object imageObject = imageIO.getImageObject();
    if (imageObject == null || !(imageObject instanceof BufferedImage)) {
        throw new IllegalStateException("Nao foi possivel criar a imagem.");
    }/* ww  w. j av  a  2  s .c  o m*/

    BufferedImage bufferedImage = (BufferedImage) imageObject;
    WritableRaster raster = bufferedImage.getRaster();

    int width = bufferedImage.getWidth();
    int height = bufferedImage.getHeight();

    double[] fft = new double[2 * width * height];
    FFTForwardInverseTest.fftForward(raster, width, height, fft);

    Complex[] complexImage = new Complex[width * height];

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {

            Complex c = new Complex(fft[2 * (x + y * width)], fft[2 * (x + y * width) + 1]);
            complexImage[x + y * width] = c;

        }
    }

    normalizeToMaxAbsValue(complexImage, width, height, 1);

    double[] degradation = new double[2 * width * height];
    //motionBlur (funcao de transferencia)
    Complex[] complexPsf = motionBlur(degradation, width, height);

    normalizeToMaxAbsValue(complexPsf, width, height, 1);

    //deconvolve data
    double[] convoluted = new double[2 * width * height];
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            Complex complexImg = complexImage[x + y * width];

            Complex complexDeg = complexPsf[x + y * width];

            Complex m = deconvolutionByWiener(complexImg, complexDeg);

            convoluted[2 * (x + y * width)] = m.getReal();
            convoluted[2 * (x + y * width) + 1] = m.getImaginary();
        }
    }

    double[] imageResult = new double[width * height];
    FFTForwardInverseTest.fftInverse(convoluted, imageResult, width, height);

    int[] image = new int[imageResult.length];
    Expansion.globalExpansion2(imageResult, image, width, height);

    ImageIODesktop imageIODesktop = new ImageIODesktop();
    imageIODesktop.savePixels(image, width, height, null, "image_deblurred");
}

From source file:com.wwidesigner.modelling.SupplementaryInfoTable.java

/**
 * Estimate Q factor using:<br/>/* w  ww  .j  a  v  a2s.  c  o m*/
 * Q = f0/2 * d/df (Im(z)/Re(z))<br/>
 * cf. Arthur D. Yaghjian, Steven R. Best, "Impedance, Bandwidth, and Q of Antennas,"
 * IEEE Transactions on Antennas and Propagation, V 53, n 4, April 2005.
 */
protected static double Q(double freq, Complex z, InstrumentCalculator calculator, Fingering fingering) {
    double freqPlus = freq * (1 + DeltaF);
    Complex zPlus = calculator.calcZ(freqPlus, fingering);
    return 0.25 * (freq + freqPlus) * (zPlus.getImaginary() / zPlus.getReal() - z.getImaginary() / z.getReal())
            / (freqPlus - freq);
}