Class ComplexArray

java.lang.Object
PamUtils.complex.ComplexArray
All Implemented Interfaces:
Serializable, Cloneable

public class ComplexArray 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:
  • Constructor Summary

    Constructors
    Constructor
    Description
    ComplexArray(double[] complexData)
    Construct a complex array from an existing array of double data.
    ComplexArray(double[] realData, double[] imagData)
    Construct a complex array from two existing arrays of double data, one with real, the other with imaginary parts.
    ComplexArray(int n)
    Construct a complex array.
  • 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
    Calculate the Cross-Spectral Density Matrix (CSDM) from this complex array.
     
    Calculate the complex conjugate of the complex array
    Multiply this array by the complex conjugate of Array s;
    conjTimes(ComplexArray 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 invalid input: '<' binRange[1]
    copyOf(int newLength)
    Make a copy of the complex array with a new length.
    Dot product (aka Inner Product) of this array and another complex array 's'.
    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.
    get(int i)
     
    double[]
    Get the data array of interleaved real / complex elements
    double[]
     
    double
    getImag(int i)
    Get a single imaginary element from the array
    double[]
     
    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
    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[]
    Gets the squared magnitude of a complex array
    double
    magsq(int i)
    Gets the squared magnitude of a complex number
    double[]
    magsq(int loBin, int hiBin)
    Gets the squared magnitude of a complex array between binLo and binHi-1
    Subtract a complex array from this array.
    Add a complex array to the current array
    pow(double f)
    Raises the complex array to the power f
    pow(int i, double f)
    Raises a complex number to a scalar power.
    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
    Set the entire data array to zero.
    Calculates the square root of all complex numbers in the array
    sqrt(int i)
    Gets the square root of a Complex number
    double
     
    static double
    sumSquared(double[] complexData)
     
    double
    sumSquared(int loBin, int hiBin)
    Calculate sum of squares between two specified bins.
    times(double f)
    Multiply a complex array by a scaler factor
    times(int i, double f)
     
    return a new ComplexArray whose value is (this * b)
     

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ComplexArray

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

      public ComplexArray(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.
    • ComplexArray

      public ComplexArray(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.
  • Method Details

    • copyOf

      public ComplexArray 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 ComplexArray clone()
    • 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 loBin, int hiBin)
      Gets the squared magnitude of a complex array between binLo and binHi-1
      Parameters:
      loBin - bin to start at
      hiBin - bin to stop before
      Returns:
      array of length hiBin-loBin of 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 ComplexArray 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 ComplexArray 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 ComplexArray plus(ComplexArray c)
      Add a complex array to the current array
      Parameters:
      c - complex array to add
      Returns:
      sum of this and c.
    • minus

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

      public ComplexArray 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 ComplexArray conj()
      Calculate the complex conjugate of the complex array
      Returns:
      complex conjugate of the complex array.
    • conjTimes

      public ComplexArray conjTimes(ComplexArray 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 ComplexArray conjTimes(ComplexArray 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 invalid input: '<' 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(ComplexArray s)
      Dot product (aka Inner Product) of this array and another complex array 's'. The order of the equation is thisinvalid input: '&#8901's.

      For example, if this ComplexArray 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 ComplexArray to perform the dot product with
      Returns:
      Complex number = thisinvalid input: '&#8901's
    • calcCSDM

      public ComplexArray[] 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 ComplexArray column vector with length() number of rows, and each row will be a ComplexArray object containing a row vector with length() number of columns.

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

      • ComplexArray[0] = [u0u0 u0u1 u0u2]
      • ComplexArray[1] = [u1u0 u1u1 u1u2]
      • ComplexArray[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 ComplexArray times(Complex b)
      return a new ComplexArray 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 ComplexArray 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 ComplexArray 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 double sumSquared(int loBin, int hiBin)
      Calculate sum of squares between two specified bins. Sum is from loBin to hiBin-1 inclusive.
      No checking of limits so with throw IndexOutOfBoundsException if bins are invalid input: '<'0 or > length.
      Parameters:
      loBin - first bin (>=0)
      hiBin - last bin (or rather, the first bin that isn't included; invalid input: '<'= Length)
      Returns:
      sum of squares between those bins.
    • sumSquared

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

      public String toString()
      Overrides:
      toString in class Object