com.tagtraum.jipes.math

Class Floats

• Method Summary

All Methods
Modifier and Type Method and Description
static float[] abs(float[] array)
Changes all values to their absolute values.
static float[] add(float[] a, float[] b)
Adds the corresponding elements of two arrays.
static float arithmeticMean(float[] array)
Computes the arithmetic mean of the array.
static float arithmeticMean(float[] array, int offset, int length)
Computes the arithmetic mean of a portion of an array.
static float[] autoCorrelation(float[] samples)
Computes the values of the auto-correlation function with a min delay of 0 and a max delay of samples.length/2.
static float[] autoCorrelation(float[] samples, int minDelay, int maxDelay)
Computes the values of the auto-correlation function.
static float[] autoCorrelation(float[] samples, int minDelay, int maxDelay, float magnitudeCompression)
Computes the values of the auto-correlation function.
static float[] autoCorrelationNaive(float[] samples, int minDelay, int maxDelay)
Naive implementation of the auto-correlation function.
static double cityBlockDistance(float[] a, float[] b)
Computes the city block distance (aka Manhattan distance) between two points, represented as arrays.
static double cityBlockDistance(float[] a, float[] b, boolean ignoreNegativeDiffs)
Computes the city block distance (a.k.a.
static double cityBlockNorm(float[] data)
Computes the city block norm (also known as 1-norm or Manhattan norm) for the given data.
static float[] convolve(float[] f, float[] g)
Fully convolves two vectors.
static float[] convolveSame(float[] f, float[] g)
Convolves two vectors and returns the central part of the convolution, which is as long as the first vector.
static float[] convolveValid(float[] f, float[] g)
Convolves two vectors and returns only those parts of the convolution that are computed without zero-padding.
static float correctedStandardDeviation(float... data)
Corrected standard deviation using unbiasedVariance(float...).
static float correlation(float[] a, float[] b)
Computes the Pearson correlation coefficient between two vectors.
static double cosineDistance(float[] a, float[] b)
Computes the cosine distance between two vectors.
static double cosineDistance(float[] a, float[] b, int offset, int length)
Computes the cosine distance between two vectors.
static double cosineSimilarity(float[] a, float[] b)
Computes the cosine similarity between two vectors.
static double cosineSimilarity(float[] a, float[] b, int offset, int length)
Computes the cosine similarity between two vectors.
static double cosineSimilarity(float[] a, float[] b, int offset, int length, double euclideanNormA, double euclideanNormB)
Computes the cosine similarity between two vectors, taking advantage of already known norms.
static float[] deltas(float[] data, int n)
Compute deltas for a point t that takes ±n points around t into account.
static double dotProduct(float[] a, float[] b)
Computes the dot product.
static double dotProduct(float[] a, float[] b, int offset, int length)
Computes the dot product.
static double euclideanDistance(float[] a, float[] b)
Computes the Euclidean distance between two points, represented as arrays.
static double euclideanDistance(float[] a, float[] b, boolean ignoreNegativeDiffs)
Computes the Euclidean distance between two points, represented as arrays.
static double euclideanNorm(float[] data)
Computes the Euclidean norm (root sum of the squared data points, also known as 2-norm) for the given data.
static double euclideanNorm(float[] data, int offset, int length)
Computes the Euclidean norm (root sum of the squared data points, also known as 2-norm) for the given data taking only a part of the vector into account (defined by offset and length).
static float geometricMean(float[] array)
Computes the geometric mean of the array.
static float geometricMean(float[] array, int offset, int length)
Computes the geometric mean of a portion of an array.
static float[] interpolate(float[] data, float shift, int indicesPerOneShift)
Linearly interpolates a given array by shifting its contents by the amount shift.
static float log2(float n)
Base 2 logarithm.
static float max(float[] array)
Calculates the maximum value of a given array.
static float max(float[] array, int offset, int length)
Calculates the maximum value of a given array.
static int maxIndex(float... array)
Calculates the index of the max value of a given array.
static int maxIndex(float[] array, int offset, int length)
Calculates the index of the max value of a given array.
static int[] maxIndices(float[] array)
Calculates the indices of the max values of a given array in descending value order.
static int[] maxIndices(float[] array, int offset, int length)
Calculates the indices of the max values of a given array in descending value order.
static float meanAbsoluteDeviation(float centralPoint, float[] array)
Mean absolute deviation around a central point.
static float meanAbsoluteDeviation(float centralPoint, float[] array, int offset, int length)
Mean absolute deviation around a central point.
static float median(float... array)
Median of the provided data.
static float median(float[] array, int offset, int length)
Median of the provided data.
static float min(float[] array)
Calculates the min value of a given array.
static float min(float[] array, int offset, int length)
Calculates the min value of a given array.
static int minIndex(float... array)
Calculates the index of the min value of a given array.
static int minIndex(float[] array, int offset, int length)
Calculates the index of the min value of a given array.
static void multiply(float[] array, float factor)
Multiplies the given array with the given factor.
static double pDistance(float[] a, float[] b, double p)
p-Distance, that is the norm of the difference of each a,b pair..
static int[] peaks(float[] array, int interval, boolean strict)
Calculates all peaks in the given array.
static float percentageBelowAverage(float[] array)
Percentage of array items below average.
static float[] phases(float[] r, float[] i)
Measures the angle/phase between the real and the imaginary part in radians.
static double pNorm(float[] data, double p)
p-Norm.
static float[] reverse(float[] array)
Reverses the order of the elements in an array in place, that is the original array is modified.
static float rootMeanSquare(float[] floats)
Computes the Root Mean Square (RMS) of one frame of data.
static float skewness(float... array)
Skewness as defined in Wikipedia (third standardized moment).
static float skewness(float[] array, int offset, int length)
Skewness as defined in Wikipedia (third standardized moment).
static float[] square(float[] array)
Changes all values to their square values.
static float standardDeviation(float... data)
Uncorrected sample standard deviation.
static float[] subtract(float[] a, float[] b)
Subtracts the corresponding elements of two arrays.
static float[] sum(float[]... arrays)
Sums the corresponding values of all arrays in the array.
static float sum(float[] array)
Computes the sum over all array elements.
static float[] sum(List<float[]> arrays)
Sums the corresponding values of all arrays in the list.
static void swap(float[] array, int a, int b)
Swaps two elements.
static Float toFloat(float f)
Convert a given float primitive to a Float object.
static float unbiasedVariance(float... array)
Unbiased sample variance of the provided data using Bessel's correction.
static float unbiasedVariance(float[] array, int offset, int length)
Unbiased sample variance of the provided data using Bessel's correction.
static float variance(float... array)
Biased sample variance of the provided data without Bessel's correction.
static float variance(float[] array, int offset, int length)
Biased sample variance of the provided data without Bessel's correction.
static float[] wrap(float[] floats, int length)
Wraps a given array of data into a smaller (or larger) array by adding all values with an index distance of length.
static float zeroCrossingRate(float[] floats)
Computes the zero crossing rate for the given array.
Zero-pads the given array at the end so that it has a length that is a power of two.
static float[] zeroPadAtEnd(int minSize, float[] array)
Zero-pads the given array at the end so that it has a length that is a power of two.
• Method Detail

• log2

public static float log2(float n)
Base 2 logarithm.
Parameters:
n - n
Returns:
base 2 logarithm
• reverse

public static float[] reverse(float[] array)
Reverses the order of the elements in an array in place, that is the original array is modified.
Parameters:
array - array to reverse
Returns:
the original array object in reversed order
• swap

public static void swap(float[] array,
int a,
int b)
Swaps two elements.
Parameters:
array - array
a - index of element a
b - index of element b

float[] array)
Zero-pads the given array at the end so that it has a length that is a power of two. This can be useful e.g. for Transform.transform(float[]).
Parameters:
minSize - minimum size of resulting array
array - array
Returns:
the same array, if its length is already a power of two, or a new array padded at the end, so that it's length is a power of two

Zero-pads the given array at the end so that it has a length that is a power of two. This can be useful e.g. for Transform.transform(float[]).
Parameters:
array - array
Returns:
the same array, if its length is already a power of two, or a new array padded at the end, so that it's length is a power of two
• percentageBelowAverage

public static float percentageBelowAverage(float[] array)
Percentage of array items below average.
Parameters:
array - data
Returns:
value between 0 and 1
• subtract

public static float[] subtract(float[] a,
float[] b)
Subtracts the corresponding elements of two arrays. If one array is longer than the other, the shorter array is padded with zeros so that both arrays have the same length.
Parameters:
a - array
b - array
Returns:
difference

float[] b)
Adds the corresponding elements of two arrays. If one array is longer than the other, the shorter array is padded with zeros so that both arrays have the same length.
Parameters:
a - array
b - array
Returns:
sum
• sum

public static float sum(float[] array)
Computes the sum over all array elements.
Parameters:
array - data
Returns:
sum
• sum

public static float[] sum(List<float[]> arrays)
Sums the corresponding values of all arrays in the list.
Parameters:
arrays - list of float arrays
Returns:
array with the sums
• sum

public static float[] sum(float[]... arrays)
Sums the corresponding values of all arrays in the array.
Parameters:
arrays - array of float arrays
Returns:
array with the sums
• arithmeticMean

public static float arithmeticMean(float[] array,
int offset,
int length)
Computes the arithmetic mean of a portion of an array.
Parameters:
array - array
offset - first index (incl)
length - number of array elements to consider
Returns:
mean
• arithmeticMean

public static float arithmeticMean(float[] array)
Computes the arithmetic mean of the array.
Parameters:
array - array
Returns:
mean
• geometricMean

public static float geometricMean(float[] array,
int offset,
int length)
Computes the geometric mean of a portion of an array.
Parameters:
array - array
offset - offset
length - length
Returns:
geometric mean
• geometricMean

public static float geometricMean(float[] array)
Computes the geometric mean of the array.
Parameters:
array - array
Returns:
geometric mean
• abs

public static float[] abs(float[] array)
Changes all values to their absolute values.
Parameters:
array - array of floats
Returns:
the same array object that was used as parameter, but with Math.abs(float) values
• square

public static float[] square(float[] array)
Changes all values to their square values.
Parameters:
array - array of floats
Returns:
the same array object that was used as parameter, but with square() values
• min

public static float min(float[] array,
int offset,
int length)
Calculates the min value of a given array.
Parameters:
array - float array
offset - offset into the array
length - length
Returns:
minimum
• min

public static float min(float[] array)
Calculates the min value of a given array.
Parameters:
array - float array
Returns:
minimum
• max

public static float max(float[] array,
int offset,
int length)
Calculates the maximum value of a given array.
Parameters:
array - float array
offset - offset into the array
length - length
Returns:
maximum
• max

public static float max(float[] array)
Calculates the maximum value of a given array.
Parameters:
array - float array
Returns:
maximum
• minIndex

public static int minIndex(float[] array,
int offset,
int length)
Calculates the index of the min value of a given array.
Parameters:
array - float array
offset - offset into the array
length - length
Returns:
min
• maxIndex

public static int maxIndex(float[] array,
int offset,
int length)
Calculates the index of the max value of a given array.
Parameters:
array - float array
offset - offset into the array
length - length
Returns:
max
• maxIndices

public static int[] maxIndices(float[] array)
Calculates the indices of the max values of a given array in descending value order.
Parameters:
array - float array
Returns:
max indices
• maxIndices

public static int[] maxIndices(float[] array,
int offset,
int length)
Calculates the indices of the max values of a given array in descending value order.
Parameters:
array - float array
offset - offset into the array
length - length
Returns:
max indices
• toFloat

public static Float toFloat(float f)
Convert a given float primitive to a Float object. Make sure that we use the same object for often used values -1, 0, and 1.
Parameters:
f - float primitive
Returns:
Float object
• multiply

public static void multiply(float[] array,
float factor)
Multiplies the given array with the given factor.
Parameters:
array - array
factor - factor
• dotProduct

public static double dotProduct(float[] a,
float[] b)
Computes the dot product.
Parameters:
a - array a
b - array b
Returns:
dot product
• dotProduct

public static double dotProduct(float[] a,
float[] b,
int offset,
int length)
Computes the dot product.
Parameters:
a - array a
b - array b
offset - offset into both arrays
length - length
Returns:
dot product
• peaks

public static int[] peaks(float[] array,
int interval,
boolean strict)
Calculates all peaks in the given array. A peak is defined as a value with at least interval values to its left that are strictly less than their direct neighbors to the right, and at least interval values to its right that are strictly less than their direct neighbors to the left.
Parameters:
array - array
interval - number of values to the left and the right that the function has to be increasing or decreasing
strict - if true, the shoulders of the peak must be strictly monotonically increasing or decreasing
Returns:
indices of the detected peaks
• minIndex

public static int minIndex(float... array)
Calculates the index of the min value of a given array.
Parameters:
array - float array
Returns:
min
• maxIndex

public static int maxIndex(float... array)
Calculates the index of the max value of a given array.
Parameters:
array - float array
Returns:
max
• variance

public static float variance(float... array)
Biased sample variance of the provided data without Bessel's correction.
Parameters:
array - data
Returns:
(biased sample) variance
• unbiasedVariance

public static float unbiasedVariance(float... array)
Unbiased sample variance of the provided data using Bessel's correction.
Parameters:
array - data
Returns:
unbiased sample variance
• median

public static float median(float... array)
Median of the provided data.
Parameters:
array - data
Returns:
median
• median

public static float median(float[] array,
int offset,
int length)
Median of the provided data.
Parameters:
array - data
offset - offset
length - length
Returns:
median
• meanAbsoluteDeviation

public static float meanAbsoluteDeviation(float centralPoint,
float[] array,
int offset,
int length)
Mean absolute deviation around a central point.
Parameters:
centralPoint - central point
array - data
offset - offset
length - length
Returns:
mean absolute deviation
• meanAbsoluteDeviation

public static float meanAbsoluteDeviation(float centralPoint,
float[] array)
Mean absolute deviation around a central point.
Parameters:
centralPoint - central point
array - data
Returns:
mean absolute deviation
• variance

public static float variance(float[] array,
int offset,
int length)
Biased sample variance of the provided data without Bessel's correction.
Parameters:
array - data
offset - offset
length - length
Returns:
(biased sample) variance
)
• unbiasedVariance

public static float unbiasedVariance(float[] array,
int offset,
int length)
Unbiased sample variance of the provided data using Bessel's correction.
Parameters:
array - data
offset - offset
length - length
Returns:
unbiased sample variance
• standardDeviation

public static float standardDeviation(float... data)
Uncorrected sample standard deviation.
Parameters:
data - data
Returns:
uncorrected sample standard deviation.
correctedStandardDeviation(float...)
• correctedStandardDeviation

public static float correctedStandardDeviation(float... data)
Corrected standard deviation using unbiasedVariance(float...).
Parameters:
data - data
Returns:
corrected sample standard deviation
• skewness

public static float skewness(float[] array,
int offset,
int length)
Skewness as defined in Wikipedia (third standardized moment).
Parameters:
array - data
offset - offset
length - length
Returns:
skewness
• skewness

public static float skewness(float... array)
Skewness as defined in Wikipedia (third standardized moment).
Parameters:
array - data
Returns:
skewness
• deltas

public static float[] deltas(float[] data,
int n)
Compute deltas for a point t that takes ±n points around t into account. This is suitable for computing Delta MFCCs. The array is padded with the first and last value respectively.
Parameters:
data - data
n - size of surrounding
Returns:
deltas
• euclideanNorm

public static double euclideanNorm(float[] data)
Computes the Euclidean norm (root sum of the squared data points, also known as 2-norm) for the given data.
Parameters:
data - data
Returns:
euclidean norm
Wikipedia Euclidean Norm
• euclideanNorm

public static double euclideanNorm(float[] data,
int offset,
int length)
Computes the Euclidean norm (root sum of the squared data points, also known as 2-norm) for the given data taking only a part of the vector into account (defined by offset and length).
Parameters:
data - data
offset - offset into the data vector
length - length of the vector elements to consider
Returns:
euclidean norm
Wikipedia Euclidean Norm
• cityBlockNorm

public static double cityBlockNorm(float[] data)
Computes the city block norm (also known as 1-norm or Manhattan norm) for the given data.
Parameters:
data - data
Returns:
city block norm
Wikipedia City Block Distance
• pNorm

public static double pNorm(float[] data,
double p)
p-Norm.
Parameters:
data - real data
p - p
Returns:
p-norm
Lp space
• pDistance

public static double pDistance(float[] a,
float[] b,
double p)
p-Distance, that is the norm of the difference of each a,b pair..
Parameters:
a - real data
b - real data
p - p
Returns:
p-distance
pNorm(float[], double), Lp space
• euclideanDistance

public static double euclideanDistance(float[] a,
float[] b)
Computes the Euclidean distance between two points, represented as arrays.
Parameters:
a - point a
b - point b
Returns:
distance
Wikipedia Euclidean Distance
• euclideanDistance

public static double euclideanDistance(float[] a,
float[] b,
boolean ignoreNegativeDiffs)
Computes the Euclidean distance between two points, represented as arrays.
Parameters:
a - point a
b - point b
ignoreNegativeDiffs - ignores negative differences, i.e. take only increases into account. If true, the distance is not symmetric.
Returns:
distance
Wikipedia Euclidean Distance
• cityBlockDistance

public static double cityBlockDistance(float[] a,
float[] b)
Computes the city block distance (aka Manhattan distance) between two points, represented as arrays. Mathematically, this is based on the 1-norm.
Parameters:
a - point a
b - point b
Returns:
distance
Wikipedia City Block Distance
• cityBlockDistance

public static double cityBlockDistance(float[] a,
float[] b,
boolean ignoreNegativeDiffs)
Computes the city block distance (a.k.a. Manhattan distance) between two points, represented as arrays.
Parameters:
a - point a
b - point b
ignoreNegativeDiffs - ignores negative differences, i.e. take only increases into account. If true, the distance is not symmetric.
Returns:
distance
Wikipedia City Block Distance
• cosineSimilarity

public static double cosineSimilarity(float[] a,
float[] b,
int offset,
int length)
Computes the cosine similarity between two vectors.
Parameters:
a - vector a
b - vector b
offset - offset into both arrays
length - length of the array elements to compare
Returns:
cosine distance
Wikipedia Cosine Similarity/Distance, cosineDistance(float[], float[])
• cosineSimilarity

public static double cosineSimilarity(float[] a,
float[] b,
int offset,
int length,
double euclideanNormA,
double euclideanNormB)
Computes the cosine similarity between two vectors, taking advantage of already known norms.
Parameters:
a - vector a
b - vector b
offset - offset into both arrays
length - length of the array elements to compare
euclideanNormA - euclidean norm for vector a (taking the offset and length into account)
euclideanNormB - euclidean norm for vector b (taking the offset and length into account)
Returns:
cosine distance
Wikipedia Cosine Similarity/Distance, cosineDistance(float[], float[])
• cosineDistance

public static double cosineDistance(float[] a,
float[] b,
int offset,
int length)
Computes the cosine distance between two vectors.
Parameters:
a - vector a
b - vector b
offset - offset into both arrays
length - length of the array elements to compare
Returns:
cosine similarity
Wikipedia Cosine Similarity/Distance, cosineSimilarity(float[], float[])
• correlation

public static float correlation(float[] a,
float[] b)
Computes the Pearson correlation coefficient between two vectors.
Parameters:
a - vector a
b - vector b
Returns:
correlation coefficient
• rootMeanSquare

public static float rootMeanSquare(float[] floats)
Computes the Root Mean Square (RMS) of one frame of data. RMS is a statistical measure of the magnitude of a varying quantity.
Parameters:
floats - floats
Returns:
rms
RMS on Wikipedia
• zeroCrossingRate

public static float zeroCrossingRate(float[] floats)
Computes the zero crossing rate for the given array.
Parameters:
floats - floats
Returns:
zero crossing rate
Zero Crossing Rate on Wikipedia
• wrap

public static float[] wrap(float[] floats,
int length)
Wraps a given array of data into a smaller (or larger) array by adding all values with an index distance of length.
Parameters:
floats - input data
length - length of the resulting array
Returns:
resulting wrapped array
• autoCorrelationNaive

public static float[] autoCorrelationNaive(float[] samples,
int minDelay,
int maxDelay)
Naive implementation of the auto-correlation function.
Parameters:
samples - samples
minDelay - min delay in samples
maxDelay - max delay in samples
Returns:
autocorrelation function (not normalized)
autoCorrelationFFT(float[],int,int,float), autoCorrelation(float[], int, int)
• convolve

public static float[] convolve(float[] f,
float[] g)
Fully convolves two vectors.

w(k) = sumOverJ( f(j) * g(k+1-j) )

Note that this is a naive implementation, not using FFT.

Parameters:
f - vector
g - vector
Returns:
convolved result
MATLAB conv
• convolveSame

public static float[] convolveSame(float[] f,
float[] g)
Convolves two vectors and returns the central part of the convolution, which is as long as the first vector.

Note that this is a naive implementation, not using FFT.

Parameters:
f - vector
g - vector
Returns:
convolved result
MATLAB conv with shape 'same'
• convolveValid

public static float[] convolveValid(float[] f,
float[] g)
Convolves two vectors and returns only those parts of the convolution that are computed without zero-padding.

Note that this is a naive implementation, not using FFT.

Parameters:
f - vector
g - vector
Returns:
convolved result
MATLAB conv with shape 'valid'
• autoCorrelation

public static float[] autoCorrelation(float[] samples)
Computes the values of the auto-correlation function with a min delay of 0 and a max delay of samples.length/2.
Parameters:
samples - samples
Returns:
values of the auto-correlation function (not normalized, windowed or anything else)
autoCorrelation(float[], int, int, float)
• autoCorrelation

public static float[] autoCorrelation(float[] samples,
int minDelay,
int maxDelay)
Computes the values of the auto-correlation function.
Parameters:
samples - samples
minDelay - min delay in samples
maxDelay - max delay in samples - note that this usually only makes sense up to a value of samples.length/2
Returns:
values of the auto-correlation function (not normalized, windowed or anything else)
autoCorrelation(float[], int, int, float)
• interpolate

public static float[] interpolate(float[] data,
float shift,
int indicesPerOneShift)
Linearly interpolates a given array by shifting its contents by the amount shift.
Parameters:
data - data to interpolate
shift - normalized shift amount, i.e. a value in [-1, - 1]
indicesPerOneShift - indicates how may indices one full shift (+1 or -1) is equal to.
Returns:
interpolated array
• phases

public static float[] phases(float[] r,
float[] i)
Measures the angle/phase between the real and the imaginary part in radians.
Parameters:
r - real values
i - imaginary values
Returns:
angles