Class ComplexArrayF
- All Implemented Interfaces:
Serializable,Cloneable
public class ComplexArrayF extends Object implements Cloneable, Serializable
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[]dataMain 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.floatang(int i)Gets the angle of a single complex number in the arrayComplexArrayF[]calcCSDM()Calculate the Cross-Spectral Density Matrix (CSDM) from this complex array.ComplexArrayFclone()ComplexArrayFconj()Calculate the complex conjugate of the complex arrayComplexArrayFconjTimes(ComplexArrayF s)Multiply this array by the complex conjugate of Array s;ComplexArrayFconjTimes(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]ComplexArrayFcopyOf(int newLength)Make a copy of the complex array with a new length.ComplexdotProduct(ComplexArrayF s)Dot product (aka Inner Product) of this array and another complex array 's'.ComplexArrayFfillConjugateHalf()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.Complexget(int i)float[]getData()Get the data array of interleaved real / complex elementsfloat[]getImag()floatgetImag(int i)Get a single imaginary element from the arrayfloat[]getReal()floatgetReal(int i)Get a real element from the arrayvoidinternalTimes(float d)Multiply the array internally by a scalar numbervoidinternalTimes(int i, double d)Multiple a single element of the array by a scalarvoidinternalTimes(int i, float d)Multiple a single element of the array by a scalarbooleanisNaN(int i)Is either the real or imaginary part of a specifiec element NaNintlength()The length of the complex array, i.e.float[]mag()Gets the magnitude of all complex numbers in the array.floatmag(int i)Gets the magnitude of a single complex numberfloat[]magsq()Gets the squared magnitude of a complex arrayfloatmagsq(int i)Gets the squared magnitude of a complex numberComplexArrayFminus(ComplexArrayF c)Subtract a complex array from this array.ComplexArrayFplus(ComplexArrayF c)Add a complex array to the current arrayComplexArrayFpow(float f)Raises the complex array to the power fComplexpow(int i, float f)Raises a complex number to a scalar power.static ComplexArrayFrealToComplex(float[] realArray)Create a complex array from a real array.voidset(int i, double re, double im)Set a single complex number in the array.voidset(int i, float re, float im)Set a single complex number in the arrayvoidset(int i, Complex complex)voidsetData(float[] data)Set the data arrayvoidsetImag(int i, float im)Set a single imaginary element in the arrayvoidsetReal(int i, float re)Set a single real element in the arrayvoidsetZero()Set the entire data array to zero.ComplexArrayFsqrt()Calculates the square root of all complex numbers in the arrayComplexsqrt(int i)Gets the square root of a Complex numberfloatsumSquared()static floatsumSquared(float[] complexData)ComplexArrayFtimes(float f)Multiply a complex array by a scaler factorComplextimes(int i, float f)ComplexArrayFtimes(Complex b)return a new ComplexArrayF whose value is (this * b)
-
Field Details
-
data
protected float[] dataMain 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
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 numberre- real partim- 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 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 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
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 < 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 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
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 numberre- 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 numberre- 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
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 numberd- 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 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:
- float 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 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
-