Maths
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.
static
.createComplexArray()
⇒Float32Array
.fromComplexArray(complex, [storage])
⇒Array.<number>
.toComplexArray(input, [storage])
⇒Float32Array
.completeSpectrum(spectrum)
⇒void
.transform(out, data)
⇒void
.inverseTransform(out, data)
⇒void
._transform4(out, data, inv)
⇒void
.softmax(arr)
⇒Array.<number>
.log_softmax(arr)
⇒any
.dot(arr1, arr2)
⇒number
.getTopItems(items, [top_k])
⇒Array
.cos_sim(arr1, arr2)
⇒number
.magnitude(arr)
⇒number
.min(arr)
⇒Array.<number>
.max(arr)
⇒Array.<number>
.rfftfreq(n, [d])
⇒Array.<number>
.round(num, decimals)
⇒number
inner
~AnyTypedArray
:Int8Array
|Uint8Array
|Uint8ClampedArray
|Int16Array
|Uint16Array
|Int32Array
|Uint32Array
|Float32Array
|Float64Array
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
.createComplexArray()
⇒Float32Array
.fromComplexArray(complex, [storage])
⇒Array.<number>
.toComplexArray(input, [storage])
⇒Float32Array
.completeSpectrum(spectrum)
⇒void
.transform(out, data)
⇒void
.inverseTransform(out, data)
⇒void
._transform4(out, data, inv)
⇒void
new FFT(size)
Throws:
Error
FFT size must be a power of two and bigger than 1.
size
number
The size of the input array. Must be a power of two and bigger than 1.
ffT.createComplexArray() ⇒ <code> Float32Array </code>
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]) ⇒ <code> Array. < number > </code>
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.
complex
Float32Array
The complex number representation to be converted.
[storage]
Array.<number>
An optional array to store the result in.
ffT.toComplexArray(input, [storage]) ⇒ <code> Float32Array </code>
Convert a real-valued input array to a complex-valued output array.
Kind: instance method of FFT
Returns: Float32Array
- The complex-valued output array.
input
Float32Array
The real-valued input array.
[storage]
Float32Array
Optional buffer to store the output array.
ffT.completeSpectrum(spectrum) ⇒ <code> void </code>
Completes the spectrum by adding its mirrored negative frequency components.
Kind: instance method of FFT
spectrum
Float32Array
The input spectrum.
ffT.transform(out, data) ⇒ <code> void </code>
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:
Error
Input and output buffers must be different.
out
Float32Array
The output buffer to store the result.
data
Float32Array
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:
Error
If the input and output buffers are the same.
out
Float32Array
The output buffer.
data
Float32Array
The input buffer containing real values.
ffT.inverseTransform(out, data) ⇒ <code> void </code>
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:
Error
If `out` and `data` refer to the same buffer.
out
Float32Array
The output buffer for the transformed data.
data
Float32Array
The input data to transform.
ffT._transform4(out, data, inv) ⇒ <code> void </code>
Performs a radix-4 implementation of a discrete Fourier transform on a given set of data.
Kind: instance method of FFT
out
Float32Array
The output buffer for the transformed data.
data
Float32Array
The input buffer of data to be transformed.
inv
number
A scaling factor to apply to the transform.
ffT._singleTransform2(data, out, outOff, off, step) ⇒ <code> void </code>
Performs a radix-2 implementation of a discrete Fourier transform on a given set of data.
Kind: instance method of FFT
data
Float32Array
The input buffer of data to be transformed.
out
Float32Array
The output buffer for the transformed data.
outOff
number
The offset at which to write the output data.
off
number
The offset at which to begin reading the input data.
step
number
The step size for indexing the input data.
ffT._singleTransform4(data, out, outOff, off, step, inv) ⇒ <code> void </code>
Performs radix-4 transformation on input data of length 8
Kind: instance method of FFT
data
Float32Array
Input data array of length 8
out
Float32Array
Output data array of length 8
outOff
number
Index of output array to start writing from
off
number
Index of input array to start reading from
step
number
Step size between elements in input array
inv
number
Scaling factor for inverse transform
ffT._realTransform4(out, data, inv)
Real input radix-4 implementation
Kind: instance method of FFT
out
Float32Array
Output array for the transformed data
data
Float32Array
Input array of real data to be transformed
inv
number
The scale factor used to normalize the inverse transform
ffT._singleRealTransform2(data, out, outOff, off, step) ⇒ <code> void </code>
Performs a single real input radix-2 transformation on the provided data
Kind: instance method of FFT
data
Float32Array
The input data array
out
Float32Array
The output data array
outOff
number
The output offset
off
number
The input offset
step
number
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
data
Float32Array
The input data array.
out
Float32Array
The output data array.
outOff
number
The offset into the output array.
off
number
The offset into the input array.
step
number
The step size for the input array.
inv
number
The value of inverse.
utils/maths.interpolate_data(input)
Kind: static method of utils/maths
input
TypedArray
utils/maths.transpose_data(array, dims, axes) ⇒ <code> * </code>
Helper method to transpose a AnyTypedArray
directly
Kind: static method of utils/maths
Returns: *
- The transposed array and the new shape.
array
T
dims
Array.<number>
axes
Array.<number>
utils/maths.softmax(arr) ⇒ <code> Array. < number > </code>
Compute the softmax of an array of numbers.
Kind: static method of utils/maths
Returns: Array.<number>
- The softmax array.
arr
Array.<number>
The array of numbers to compute the softmax of.
utils/maths.log_softmax(arr) ⇒ <code> any </code>
Calculates the logarithm of the softmax function for the input array.
Kind: static method of utils/maths
Returns: any
- The resulting log_softmax array.
arr
Array.<number>
The input array to calculate the log_softmax function for.
utils/maths.dot(arr1, arr2) ⇒ <code> number </code>
Calculates the dot product of two arrays.
Kind: static method of utils/maths
Returns: number
- The dot product of arr1 and arr2.
arr1
Array.<number>
The first array.
arr2
Array.<number>
The second array.
utils/maths.getTopItems(items, [top_k]) ⇒ <code> Array </code>
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
items
Array
The items to be sorted
[top_k]
number
0
The number of top items to return (default: 0 = return all)
utils/maths.cos_sim(arr1, arr2) ⇒ <code> number </code>
Computes the cosine similarity between two arrays.
Kind: static method of utils/maths
Returns: number
- The cosine similarity between the two arrays.
arr1
Array.<number>
The first array.
arr2
Array.<number>
The second array.
utils/maths.magnitude(arr) ⇒ <code> number </code>
Calculates the magnitude of a given array.
Kind: static method of utils/maths
Returns: number
- The magnitude of the array.
arr
Array.<number>
The array to calculate the magnitude of.
utils/maths.min(arr) ⇒ <code> Array. < number > </code>
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:
Error
If array is empty.
arr
Array.<number>
array of numbers.
utils/maths.max(arr) ⇒ <code> Array. < number > </code>
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:
Error
If array is empty.
arr
Array.<number>
array of numbers.
utils/maths.rfftfreq(n, [d]) ⇒ <code> Array. < number > </code>
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:
TypeError
If n is not an integer.
n
number
Window length
[d]
number
1.0
Sample spacing (inverse of the sampling rate). Defaults to 1.
Example
Copied
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
data
AnyTypedArray
The input array
windowSize
number
The window size
utils/maths.round(num, decimals) ⇒ <code> number </code>
Helper function to round a number to a given number of decimals
Kind: static method of utils/maths
Returns: number
- The rounded number
num
number
The number to round
decimals
number
The number of decimals
utils/maths~AnyTypedArray : <code> Int8Array </code> | <code> Uint8Array </code> | <code> Uint8ClampedArray </code> | <code> Int16Array </code> | <code> Uint16Array </code> | <code> Int32Array </code> | <code> Uint32Array </code> | <code> Float32Array </code> | <code> Float64Array </code>
Kind: inner typedef of utils/maths
Last updated