Class ComplexArrayF

java.lang.Object
org.jamdev.jpamutils.spectrogram.ComplexArrayF
All Implemented Interfaces:
Serializable, Cloneable

public class ComplexArrayF
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.

This version of COmplexArray stores numbers as floats...perhaps a little dangerous but halves memory usage.

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

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

    Constructors 
    Constructor Description
    ComplexArrayF​(float[] complexData)
    Construct a complex array from an existing array of float data.
    ComplexArrayF​(float[] realData, float[] imagData)
    Construct a complex array from two existing arrays of float data, one with real, the other with imaginary parts.
    ComplexArrayF​(int n)
    Construct a complex array.
    ComplexArrayF​(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
    float[] ang()
    Gets the angle of all complex numbers in the array.
    float ang​(int i)
    Gets the angle of a single complex number in the array
    ComplexArrayF[] calcCSDM()
    Calculate the Cross-Spectral Density Matrix (CSDM) from this complex array.
    ComplexArrayF clone()  
    ComplexArrayF conj()
    Calculate the complex conjugate of the complex array
    ComplexArrayF conjTimes​(ComplexArrayF s)
    Multiply this array by the complex conjugate of Array s;
    ComplexArrayF conjTimes​(ComplexArrayF 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]
    ComplexArrayF copyOf​(int newLength)
    Make a copy of the complex array with a new length.
    Complex dotProduct​(ComplexArrayF s)
    Dot product (aka Inner Product) of this array and another complex array 's'.
    ComplexArrayF 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)  
    float[] getData()
    Get the data array of interleaved real / complex elements
    float[] getImag()  
    float getImag​(int i)
    Get a single imaginary element from the array
    float[] getReal()  
    float getReal​(int i)
    Get a real element from the array
    void internalTimes​(float 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
    void internalTimes​(int i, float 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.
    float[] mag()
    Gets the magnitude of all complex numbers in the array.
    float mag​(int i)
    Gets the magnitude of a single complex number
    float[] magsq()
    Gets the squared magnitude of a complex array
    float magsq​(int i)
    Gets the squared magnitude of a complex number
    ComplexArrayF minus​(ComplexArrayF c)
    Subtract a complex array from this array.
    ComplexArrayF plus​(ComplexArrayF c)
    Add a complex array to the current array
    ComplexArrayF pow​(float f)
    Raises the complex array to the power f
    Complex pow​(int i, float f)
    Raises a complex number to a scalar power.
    static ComplexArrayF realToComplex​(float[] 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, float re, float im)
    Set a single complex number in the array
    void set​(int i, Complex complex)  
    void setData​(float[] data)
    Set the data array
    void setImag​(int i, float im)
    Set a single imaginary element in the array
    void setReal​(int i, float re)
    Set a single real element in the array
    void setZero()
    Set the entire data array to zero.
    ComplexArrayF sqrt()
    Calculates the square root of all complex numbers in the array
    Complex sqrt​(int i)
    Gets the square root of a Complex number
    float sumSquared()  
    static float sumSquared​(float[] complexData)  
    ComplexArrayF times​(float f)
    Multiply a complex array by a scaler factor
    Complex times​(int i, float f)  
    ComplexArrayF times​(Complex b)
    return a new ComplexArrayF 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 float[] data
      Main data array of interleaved real / complex data.
  • Constructor Details

    • ComplexArrayF

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

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

      public ComplexArrayF​(float[] realData, float[] imagData)
      Construct a complex array from two existing arrays of float 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.
    • ComplexArrayF

      public ComplexArrayF​(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 ComplexArrayF 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, float re, float 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, double re, double im)
      Set a single complex number in the array. Be careful when using doubles
      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 ComplexArrayF clone()
      Overrides:
      clone in class Object
    • ang

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

      public float 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 float[] magsq()
      Gets the squared magnitude of a complex array
      Returns:
      squared magnitudes
    • magsq

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

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

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

      public ComplexArrayF 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 ComplexArrayF pow​(float 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, float 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 ComplexArrayF plus​(ComplexArrayF c)
      Add a complex array to the current array
      Parameters:
      c - complex array to add
      Returns:
      sum of this and c.
    • minus

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

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

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

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

      public ComplexArrayF conjTimes​(ComplexArrayF 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 ComplexArrayF conjTimes​(ComplexArrayF 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​(ComplexArrayF 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 ComplexArrayF 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 ComplexArrayF to perform the dot product with
      Returns:
      Complex number = this⋅s
    • calcCSDM

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

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

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

      Where the underlined variables indicate the complex conjugate

      Returns:
    • getReal

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

      public float 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, float re)
      Set a single real element in the array
      Parameters:
      i - index of the complex number
      re - real value to set.
    • getImag

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

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

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

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

      public void setData​(float[] 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​(float d)
      Multiply the array internally by a scalar number
      Parameters:
      d - scalar multiplier
    • times

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

      public void internalTimes​(int i, float d)
      Multiple a single element of the array by a scalar
      Parameters:
      i - index of the complex number
      d - scalar factor
    • 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 ComplexArrayF 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:
      float length array with the second half being the complex cunjugate of the first.
    • realToComplex

      public static ComplexArrayF realToComplex​(float[] 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 float sumSquared()
      Returns:
      sum of squares of all data in the array.
    • sumSquared

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