Class ComplexArrayD

java.lang.Object
org.jamdev.jpamutils.spectrogram.ComplexArrayD
All Implemented Interfaces:
Serializable, Cloneable
Direct Known Subclasses:
ComplexArray

public class ComplexArrayD
extends Object
implements Cloneable, Serializable
Class for handling arrays of Complex data.

This class should be used wherever possible in preference to arrays of Complex objects since this class realised purely on an array of primitives, assumed to be in real, imaginary order which should give a speed improvement over previous methods.

functions exist within this to convert to an array of Complex objects to support current PAMGUARD functionality but it is hoped that modules will gradually be rewritten so they don't need this.

Author:
Doug Gillespie
See Also:
Serialized Form
  • Field Summary

    Fields 
    Modifier and Type Field Description
    protected double[] data
    Main data array of interleaved real / complex data.
  • Constructor Summary

    Constructors 
    Constructor Description
    ComplexArrayD​(double[] complexData)
    Construct a complex array from an existing array of double data.
    ComplexArrayD​(double[] realData, double[] imagData)
    Construct a complex array from two existing arrays of double data, one with real, the other with imaginary parts.
    ComplexArrayD​(int n)
    Construct a complex array.
    ComplexArrayD​(org.apache.commons.math3.complex.Complex[] complexArr)
    Construct a complex array from an array of Apache Complex objects.
  • Method Summary

    Modifier and Type Method Description
    double[] ang()
    Gets the angle of all complex numbers in the array.
    double ang​(int i)
    Gets the angle of a single complex number in the array
    ComplexArrayD[] calcCSDM()
    Calculate the Cross-Spectral Density Matrix (CSDM) from this complex array.
    ComplexArrayD clone()  
    ComplexArrayD conj()
    Calculate the complex conjugate of the complex array
    ComplexArrayD conjTimes​(ComplexArrayD s)
    Multiply this array by the complex conjugate of Array s;
    ComplexArrayD conjTimes​(ComplexArrayD s, int[] binRange)
    Multiply this array by the complex conjugate of Array s but only using data within the complex bin range >= binRange[0] to < binRange[1]
    ComplexArrayD copyOf​(int newLength)
    Make a copy of the complex array with a new length.
    Complex dotProduct​(ComplexArrayD s)
    Dot product (aka Inner Product) of this array and another complex array 's'.
    ComplexArrayD fillConjugateHalf()
    FFT's of real data often only contain the first half of the data since the second half is just a flipped complex conjugate of the first half.
    Complex get​(int i)  
    double[] getData()
    Get the data array of interleaved real / complex elements
    double[] getImag()  
    double getImag​(int i)
    Get a single imaginary element from the array
    double[] getReal()  
    double getReal​(int i)
    Get a real element from the array
    void internalTimes​(double d)
    Multiply the array internally by a scalar number
    void internalTimes​(int i, double d)
    Multiple a single element of the array by a scalar
    boolean isNaN​(int i)
    Is either the real or imaginary part of a specifiec element NaN
    int length()
    The length of the complex array, i.e.
    double[] mag()
    Gets the magnitude of all complex numbers in the array.
    double mag​(int i)
    Gets the magnitude of a single complex number
    double[] magsq()
    Gets the squared magnitude of a complex array
    double magsq​(int i)
    Gets the squared magnitude of a complex number
    ComplexArrayD minus​(ComplexArrayD c)
    Subtract a complex array from this array.
    ComplexArrayD plus​(ComplexArrayD c)
    Add a complex array to the current array
    ComplexArrayD pow​(double f)
    Raises the complex array to the power f
    Complex pow​(int i, double f)
    Raises a complex number to a scalar power.
    static ComplexArrayD realToComplex​(double[] realArray)
    Create a complex array from a real array.
    void set​(int i, double re, double im)
    Set a single complex number in the array
    void set​(int i, Complex complex)  
    void setData​(double[] data)
    Set the data array
    void setImag​(int i, double im)
    Set a single imaginary element in the array
    void setReal​(int i, double re)
    Set a single real element in the array
    void setZero()
    Set the entire data array to zero.
    ComplexArrayD sqrt()
    Calculates the square root of all complex numbers in the array
    Complex sqrt​(int i)
    Gets the square root of a Complex number
    double sumSquared()  
    static double sumSquared​(double[] complexData)  
    ComplexArrayD times​(double f)
    Multiply a complex array by a scaler factor
    Complex times​(int i, double f)  
    ComplexArrayD times​(Complex b)
    return a new ComplexArrayD whose value is (this * b)

    Methods inherited from class java.lang.Object

    equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • data

      protected double[] data
      Main data array of interleaved real / complex data.
  • Constructor Details

    • ComplexArrayD

      public ComplexArrayD​(int n)
      Construct a complex array. Length of allocated data array will be 2*n
      Parameters:
      n - Number of real / imaginary pairs.
    • ComplexArrayD

      public ComplexArrayD​(double[] complexData)
      Construct a complex array from an existing array of double data. Data must be interleaved real / imaginary pairs so the length of data MUST be even.
      Parameters:
      complexData - interleaved real and imaginary data.
    • ComplexArrayD

      public ComplexArrayD​(double[] realData, double[] imagData)
      Construct a complex array from two existing arrays of double data, one with real, the other with imaginary parts. Make sure imagData is same length as realData or else...
      Parameters:
      complexData - interleaved real and imaginary data.
    • ComplexArrayD

      public ComplexArrayD​(org.apache.commons.math3.complex.Complex[] complexArr)
      Construct a complex array from an array of Apache Complex objects.
      Parameters:
      complexArr - an array of Apache Complex objects.
  • Method Details

    • copyOf

      public ComplexArrayD copyOf​(int newLength)
      Make a copy of the complex array with a new length. Wraps around the Arrays.copyof function
      Parameters:
      newLength - new number of complex objects.
      Returns:
      Complex array with the new length.
    • set

      public void set​(int i, double re, double im)
      Set a single complex number in the array
      Parameters:
      i - index of the complex number
      re - real part
      im - imaginary part
    • set

      public void set​(int i, Complex complex)
    • get

      public Complex get​(int i)
    • length

      public int length()
      The length of the complex array, i.e. the number of complex numbers. This is half the length of the internal data array.
      Returns:
      The number of complex numbers in the array
    • clone

      public ComplexArrayD clone()
      Overrides:
      clone in class Object
    • ang

      public double[] ang()
      Gets the angle of all complex numbers in the array.
      Returns:
      angles in radians
    • ang

      public double ang​(int i)
      Gets the angle of a single complex number in the array
      Parameters:
      i - index in the array
      Returns:
      angle in radians.
    • magsq

      public double[] magsq()
      Gets the squared magnitude of a complex array
      Returns:
      squared magnitudes
    • magsq

      public double magsq​(int i)
      Gets the squared magnitude of a complex number
      Parameters:
      i - index of the complex number
      Returns:
      squared magnitude
    • mag

      public double[] mag()
      Gets the magnitude of all complex numbers in the array.
      Returns:
      Array of magnitudes
    • mag

      public double mag​(int i)
      Gets the magnitude of a single complex number
      Parameters:
      i - index of the complex number
      Returns:
      magnitude magnitude
    • sqrt

      public ComplexArrayD sqrt()
      Calculates the square root of all complex numbers in the array
      Returns:
      square roots of complex numbers.
    • sqrt

      public Complex sqrt​(int i)
      Gets the square root of a Complex number
      Parameters:
      i - index of the complex number
      Returns:
      square root of the Complex number
    • pow

      public ComplexArrayD pow​(double f)
      Raises the complex array to the power f
      Parameters:
      f - power factor
      Returns:
      array of complex numbers raised to the power f.
    • pow

      public Complex pow​(int i, double f)
      Raises a complex number to a scalar power.
      Parameters:
      i - index of the complex number
      f - power to raise number to
      Returns:
      new Complex number
    • plus

      public ComplexArrayD plus​(ComplexArrayD c)
      Add a complex array to the current array
      Parameters:
      c - complex array to add
      Returns:
      sum of this and c.
    • minus

      public ComplexArrayD minus​(ComplexArrayD c)
      Subtract a complex array from this array.
      Parameters:
      c - complex array to subtract.
      Returns:
      this minus c.
    • times

      public ComplexArrayD times​(double f)
      Multiply a complex array by a scaler factor
      Parameters:
      f - multiplication factor.
      Returns:
      new complex array
    • times

      public Complex times​(int i, double f)
    • conj

      public ComplexArrayD conj()
      Calculate the complex conjugate of the complex array
      Returns:
      complex conjugate of the complex array.
    • conjTimes

      public ComplexArrayD conjTimes​(ComplexArrayD s)
      Multiply this array by the complex conjugate of Array s;
      Parameters:
      s - Array to multiply by
      Returns:
      new array = this*conj(other)
    • conjTimes

      public ComplexArrayD conjTimes​(ComplexArrayD s, int[] binRange)
      Multiply this array by the complex conjugate of Array s but only using data within the complex bin range >= binRange[0] to < binRange[1]
      Parameters:
      s - Array to multiply be
      binRange - range of frequency bins to include.
      Returns:
      new array = this*conj(other)
    • dotProduct

      public Complex dotProduct​(ComplexArrayD s)
      Dot product (aka Inner Product) of this array and another complex array 's'. The order of the equation is this⋅s.

      For example, if this ComplexArrayD u = [u0 u1 u2] and the passed array is v = [v0 v1 v2], the number returned would be:

      • Complex newVal = u0v0 + u1v1 + u2v2

      Where the underlined variables indicate the complex conjugate. This method is similar to the conjTimes method, but adds Complex Numbers together and returns the resultant Complex number

      Parameters:
      s - the ComplexArrayD to perform the dot product with
      Returns:
      Complex number = this⋅s
    • calcCSDM

      public ComplexArrayD[] calcCSDM()

      Calculate the Cross-Spectral Density Matrix (CSDM) from this complex array. This array is assumed to be a column vector with number of rows = length(). The returned object will be a ComplexArrayD column vector with length() number of rows, and each row will be a ComplexArrayD object containing a row vector with length() number of columns.

      For example, if this ComplexArrayD u = [u0 u1 u2], then CSDM(u) = ComplexArrayD[] of length 3, where:

      • ComplexArrayD[0] = [u0u0 u0u1 u0u2]
      • ComplexArrayD[1] = [u1u0 u1u1 u1u2]
      • ComplexArrayD[2] = [u2u0 u2u1 u2u2]

      Where the underlined variables indicate the complex conjugate

      Returns:
    • getReal

      public double[] getReal()
      Returns:
      the entire real part of the array
    • getReal

      public double getReal​(int i)
      Get a real element from the array
      Parameters:
      i - index of the complex number
      Returns:
      single real element
    • setReal

      public void setReal​(int i, double re)
      Set a single real element in the array
      Parameters:
      i - index of the complex number
      re - real value to set.
    • getImag

      public double getImag​(int i)
      Get a single imaginary element from the array
      Parameters:
      i - index of the complex number
      Returns:
      single imaginary number
    • getImag

      public double[] getImag()
      Returns:
      the entire imag part of the array
    • setImag

      public void setImag​(int i, double im)
      Set a single imaginary element in the array
      Parameters:
      i - index of the complex number
      re - imaginary value to set.
    • getData

      public double[] getData()
      Get the data array of interleaved real / complex elements
      Returns:
      data array
    • setData

      public void setData​(double[] data)
      Set the data array
      Parameters:
      data - array of interleaved real / imaginary pairs.
    • isNaN

      public boolean isNaN​(int i)
      Is either the real or imaginary part of a specifiec element NaN
      Parameters:
      i - index of the complex number
      Returns:
      true if either the real or imaginary part is NaN
    • internalTimes

      public void internalTimes​(double d)
      Multiply the array internally by a scalar number
      Parameters:
      d - scalar multiplier
    • times

      public ComplexArrayD times​(Complex b)
      return a new ComplexArrayD whose value is (this * b)
      Parameters:
      b -
      Returns:
    • internalTimes

      public void internalTimes​(int i, double d)
      Multiple a single element of the array by a scalar
      Parameters:
      i - index of the complex number
      d - scalar factor
    • setZero

      public void setZero()
      Set the entire data array to zero.
    • fillConjugateHalf

      public ComplexArrayD fillConjugateHalf()
      FFT's of real data often only contain the first half of the data since the second half is just a flipped complex conjugate of the first half. This function will fill back in the second half of the data.
      Returns:
      double length array with the second half being the complex cunjugate of the first.
    • realToComplex

      public static ComplexArrayD realToComplex​(double[] realArray)
      Create a complex array from a real array.
      Parameters:
      realArray - array of real data.
      Returns:
      a complex array - twice the length of realArray with imag parts = 0;
    • sumSquared

      public double sumSquared()
      Returns:
      sum of squares of all data in the array.
    • sumSquared

      public static double sumSquared​(double[] complexData)
      Parameters:
      complexData -
      Returns:
      sum of squares of the complex array