utils/maths

Helper module for mathematical processing.

These functions and classes are only used internally, meaning an end-user shouldn’t need to access anything here.


utils/maths.FFT

FFT class provides functionality for performing Fast Fourier Transform on arrays Code adapted from https://www.npmjs.com/package/fft.js

Kind: static class of utils/maths


new FFT(size)

Throws:

ParamTypeDescription
sizenumber

The size of the input array. Must be a power of two and bigger than 1.


ffT.createComplexArray()Float32Array

Create a complex number array with size 2 * size

Kind: instance method of FFT
Returns: Float32Array - A complex number array with size 2 * size


ffT.fromComplexArray(complex, [storage])Array.<number>

Converts a complex number representation stored in a Float32Array to an array of real numbers.

Kind: instance method of FFT
Returns: Array.<number> - An array of real numbers representing the input complex number representation.

ParamTypeDescription
complexFloat32Array

The complex number representation to be converted.

[storage]Array.<number>

An optional array to store the result in.


ffT.toComplexArray(input, [storage])Float32Array

Convert a real-valued input array to a complex-valued output array.

Kind: instance method of FFT
Returns: Float32Array - The complex-valued output array.

ParamTypeDescription
inputFloat32Array

The real-valued input array.

[storage]Float32Array

Optional buffer to store the output array.


ffT.completeSpectrum(spectrum)void

Completes the spectrum by adding its mirrored negative frequency components.

Kind: instance method of FFT

ParamTypeDescription
spectrumFloat32Array

The input spectrum.


ffT.transform(out, data)void

Performs a Fast Fourier Transform (FFT) on the given input data and stores the result in the output buffer.

Kind: instance method of FFT
Throws:

ParamTypeDescription
outFloat32Array

The output buffer to store the result.

dataFloat32Array

The input data to transform.


ffT.realTransform(out, data)

Performs a real-valued forward FFT on the given input buffer and stores the result in the given output buffer. The input buffer must contain real values only, while the output buffer will contain complex values. The input and output buffers must be different.

Kind: instance method of FFT
Throws:

ParamTypeDescription
outFloat32Array

The output buffer.

dataFloat32Array

The input buffer containing real values.


ffT.inverseTransform(out, data)void

Performs an inverse FFT transformation on the given data array, and stores the result in out. The out array must be a different buffer than the data array. The out array will contain the result of the transformation. The data array will not be modified.

Kind: instance method of FFT
Throws:

ParamTypeDescription
outFloat32Array

The output buffer for the transformed data.

dataFloat32Array

The input data to transform.


ffT._transform4(out, data, inv)void

Performs a radix-4 implementation of a discrete Fourier transform on a given set of data.

Kind: instance method of FFT

ParamTypeDescription
outFloat32Array

The output buffer for the transformed data.

dataFloat32Array

The input buffer of data to be transformed.

invnumber

A scaling factor to apply to the transform.


ffT._singleTransform2(data, out, outOff, off, step)void

Performs a radix-2 implementation of a discrete Fourier transform on a given set of data.

Kind: instance method of FFT

ParamTypeDescription
dataFloat32Array

The input buffer of data to be transformed.

outFloat32Array

The output buffer for the transformed data.

outOffnumber

The offset at which to write the output data.

offnumber

The offset at which to begin reading the input data.

stepnumber

The step size for indexing the input data.


ffT._singleTransform4(data, out, outOff, off, step, inv)void

Performs radix-4 transformation on input data of length 8

Kind: instance method of FFT

ParamTypeDescription
dataFloat32Array

Input data array of length 8

outFloat32Array

Output data array of length 8

outOffnumber

Index of output array to start writing from

offnumber

Index of input array to start reading from

stepnumber

Step size between elements in input array

invnumber

Scaling factor for inverse transform


ffT._realTransform4(out, data, inv)

Real input radix-4 implementation

Kind: instance method of FFT

ParamTypeDescription
outFloat32Array

Output array for the transformed data

dataFloat32Array

Input array of real data to be transformed

invnumber

The scale factor used to normalize the inverse transform


ffT._singleRealTransform2(data, out, outOff, off, step)void

Performs a single real input radix-2 transformation on the provided data

Kind: instance method of FFT

ParamTypeDescription
dataFloat32Array

The input data array

outFloat32Array

The output data array

outOffnumber

The output offset

offnumber

The input offset

stepnumber

The step


ffT._singleRealTransform4(data, out, outOff, off, step, inv)

Computes a single real-valued transform using radix-4 algorithm. This method is only called for len=8.

Kind: instance method of FFT

ParamTypeDescription
dataFloat32Array

The input data array.

outFloat32Array

The output data array.

outOffnumber

The offset into the output array.

offnumber

The offset into the input array.

stepnumber

The step size for the input array.

invnumber

The value of inverse.


utils/maths.interpolate_data(input)

Kind: static method of utils/maths

ParamType
inputTypedArray

utils/maths.transpose_data(array, dims, axes)*

Helper method to transpose a AnyTypedArray directly

Kind: static method of utils/maths
Returns: * - The transposed array and the new shape.

ParamType
arrayT
dimsArray.<number>
axesArray.<number>

utils/maths.softmax(arr)Array.<number>

Compute the softmax of an array of numbers.

Kind: static method of utils/maths
Returns: Array.<number> - The softmax array.

ParamTypeDescription
arrArray.<number>

The array of numbers to compute the softmax of.


utils/maths.log_softmax(arr)any

Calculates the logarithm of the softmax function for the input array.

Kind: static method of utils/maths
Returns: any - The resulting log_softmax array.

ParamTypeDescription
arrArray.<number>

The input array to calculate the log_softmax function for.


utils/maths.dot(arr1, arr2)number

Calculates the dot product of two arrays.

Kind: static method of utils/maths
Returns: number - The dot product of arr1 and arr2.

ParamTypeDescription
arr1Array.<number>

The first array.

arr2Array.<number>

The second array.


utils/maths.getTopItems(items, [top_k])Array

Get the top k items from an iterable, sorted by descending order

Kind: static method of utils/maths
Returns: Array - The top k items, sorted by descending order

ParamTypeDefaultDescription
itemsArray

The items to be sorted

[top_k]number0

The number of top items to return (default: 0 = return all)


utils/maths.cos_sim(arr1, arr2)number

Computes the cosine similarity between two arrays.

Kind: static method of utils/maths
Returns: number - The cosine similarity between the two arrays.

ParamTypeDescription
arr1Array.<number>

The first array.

arr2Array.<number>

The second array.


utils/maths.magnitude(arr)number

Calculates the magnitude of a given array.

Kind: static method of utils/maths
Returns: number - The magnitude of the array.

ParamTypeDescription
arrArray.<number>

The array to calculate the magnitude of.


utils/maths.min(arr)Array.<number>

Returns the value and index of the minimum element in an array.

Kind: static method of utils/maths
Returns: Array.<number> - the value and index of the minimum element, of the form: [valueOfMin, indexOfMin]
Throws:

ParamTypeDescription
arrArray.<number>

array of numbers.


utils/maths.max(arr)Array.<number>

Returns the value and index of the maximum element in an array.

Kind: static method of utils/maths
Returns: Array.<number> - the value and index of the maximum element, of the form: [valueOfMax, indexOfMax]
Throws:

ParamTypeDescription
arrArray.<number>

array of numbers.


utils/maths.rfftfreq(n, [d])Array.<number>

Return the Discrete Fourier Transform sample frequencies.

Code adapted from https://github.com/numpy/numpy/blob/25908cacd19915bf3ddd659c28be28a41bd97a54/numpy/fft/helper.py#L173-L221 Original Python doc: https://numpy.org/doc/stable/reference/generated/numpy.fft.rfftfreq.html

Kind: static method of utils/maths
Returns: Array.<number> - Array of length Math.floor(n / 2) + 1; containing the sample frequencies.
Throws:

ParamTypeDefaultDescription
nnumber

Window length

[d]number1.0

Sample spacing (inverse of the sampling rate). Defaults to 1.

Example

rfftfreq(400, 1 / 16000) // (201) [0, 40, 80, 120, 160, 200, ..., 8000]

utils/maths.medianFilter(data, windowSize)

Performs median filter on the provided data. Padding is done by mirroring the data.

Kind: static method of utils/maths

ParamTypeDescription
dataAnyTypedArray

The input array

windowSizenumber

The window size


utils/maths.round(num, decimals)number

Helper function to round a number to a given number of decimals

Kind: static method of utils/maths
Returns: number - The rounded number

ParamTypeDescription
numnumber

The number to round

decimalsnumber

The number of decimals


utils/maths~AnyTypedArray : Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array

Kind: inner typedef of utils/maths