Class ComplexArray
- All Implemented Interfaces:
Serializable
,Cloneable
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
ConstructorDescriptionComplexArray
(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 TypeMethodDescriptiondouble[]
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 arraycalcCSDM()
Calculate the Cross-Spectral Density Matrix (CSDM) from this complex array.clone()
conj()
Calculate the complex conjugate of the complex arrayMultiply 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[]
getData()
Get the data array of interleaved real / complex elementsdouble[]
getImag()
double
getImag
(int i) Get a single imaginary element from the arraydouble[]
getReal()
double
getReal
(int i) Get a real element from the arrayvoid
internalTimes
(double d) Multiply the array internally by a scalar numbervoid
internalTimes
(int i, double d) Multiple a single element of the array by a scalarboolean
isNaN
(int i) Is either the real or imaginary part of a specifiec element NaNint
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 numberdouble[]
magsq()
Gets the squared magnitude of a complex arraydouble
magsq
(int i) Gets the squared magnitude of a complex numberdouble[]
magsq
(int loBin, int hiBin) Gets the squared magnitude of a complex array between binLo and binHi-1Subtract a complex array from this array.plus
(ComplexArray c) Add a complex array to the current arraypow
(double f) Raises the complex array to the power fpow
(int i, double f) Raises a complex number to a scalar power.static ComplexArray
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 arrayvoid
void
setData
(double[] data) Set the data arrayvoid
setImag
(int i, double im) Set a single imaginary element in the arrayvoid
setReal
(int i, double re) Set a single real element in the arrayvoid
setZero()
Set the entire data array to zero.sqrt()
Calculates the square root of all complex numbers in the arraysqrt
(int i) Gets the square root of a Complex numberdouble
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 factortimes
(int i, double f) return a new ComplexArray whose value is (this * b)toString()
-
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
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 numberre
- real partim
- imaginary part
-
set
-
get
-
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
-
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 athiBin
- 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
Calculates the square root of all complex numbers in the array- Returns:
- square roots of complex numbers.
-
sqrt
Gets the square root of a Complex number- Parameters:
i
- index of the complex number- Returns:
- square root of the Complex number
-
pow
Raises the complex array to the power f- Parameters:
f
- power factor- Returns:
- array of complex numbers raised to the power f.
-
pow
Raises a complex number to a scalar power.- Parameters:
i
- index of the complex numberf
- power to raise number to- Returns:
- new Complex number
-
plus
Add a complex array to the current array- Parameters:
c
- complex array to add- Returns:
- sum of this and c.
-
minus
Subtract a complex array from this array.- Parameters:
c
- complex array to subtract.- Returns:
- this minus c.
-
times
Multiply a complex array by a scaler factor- Parameters:
f
- multiplication factor.- Returns:
- new complex array
-
times
-
conj
Calculate the complex conjugate of the complex array- Returns:
- complex conjugate of the complex array.
-
conjTimes
Multiply this array by the complex conjugate of Array s;- Parameters:
s
- Array to multiply by- Returns:
- new array = this*conj(other)
-
conjTimes
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 bebinRange
- range of frequency bins to include.- Returns:
- new array = this*conj(other)
-
dotProduct
Dot product (aka Inner Product) of this array and another complex array 's'. The order of the equation is thisinvalid input: '⋅'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: '⋅'s
-
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 numberre
- 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 numberre
- 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
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 numberd
- scalar factor
-
setZero
public void setZero()Set the entire data array to zero. -
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
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
-