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
- static
.interpolate_data(input)
.permute_data(array, dims, axes)
β*
.softmax(arr)
βT
.log_softmax(arr)
βT
.dot(arr1, arr2)
βnumber
.cos_sim(arr1, arr2)
βnumber
.magnitude(arr)
βnumber
.min(arr)
β*
.max(arr)
β*
.medianFilter(data, windowSize)
.round(num, decimals)
βnumber
.bankers_round(x)
βnumber
.dynamic_time_warping(matrix)
βArray.<Array<number>>
- inner
- ~P2FFT
new P2FFT(size)
.createComplexArray()
βFloat64Array
.fromComplexArray(complex, [storage])
βArray.<number>
.toComplexArray(input, [storage])
βFloat64Array
.transform(out, data)
βvoid
.realTransform(out, data)
.inverseTransform(out, data)
βvoid
._transform4(out, data, inv)
βvoid
._singleTransform2(data, out, outOff, off, step)
βvoid
._singleTransform4(data, out, outOff, off, step, inv)
βvoid
._realTransform4(out, data, inv)
._singleRealTransform2(data, out, outOff, off, step)
βvoid
._singleRealTransform4(data, out, outOff, off, step, inv)
- ~NP2FFT
~AnyTypedArray
:Int8Array
|Uint8Array
|Uint8ClampedArray
|Int16Array
|Uint16Array
|Int32Array
|Uint32Array
|Float32Array
|Float64Array
- ~P2FFT
- static
utils/maths.interpolate_data(input)
Kind: static method of utils/maths
Param | Type |
---|---|
input | TypedArray |
utils/maths.permute_data(array, dims, axes) β <code> * </code>
Helper method to permute a AnyTypedArray
directly
Kind: static method of utils/maths
Returns: *
- The permuted array and the new shape.
Param | Type |
---|---|
array | T |
dims | Array.<number> |
axes | Array.<number> |
utils/maths.softmax(arr) β <code> T </code>
Compute the softmax of an array of numbers.
Kind: static method of utils/maths
Returns: T
- The softmax array.
Param | Type | Description |
---|---|---|
arr | T | The array of numbers to compute the softmax of. |
utils/maths.log_softmax(arr) β <code> T </code>
Calculates the logarithm of the softmax function for the input array.
Kind: static method of utils/maths
Returns: T
- The resulting log_softmax array.
Param | Type | Description |
---|---|---|
arr | T | 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.
Param | Type | Description |
---|---|---|
arr1 | Array.<number> | The first array. |
arr2 | Array.<number> | The second array. |
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.
Param | Type | Description |
---|---|---|
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.
Param | Type | Description |
---|---|---|
arr | Array.<number> | The array to calculate the magnitude of. |
utils/maths.min(arr) β <code> * </code>
Returns the value and index of the minimum element in an array.
Kind: static method of utils/maths
Returns: *
- the value and index of the minimum element, of the form: [valueOfMin, indexOfMin]
Throws:
Error
If array is empty.
Param | Type | Description |
---|---|---|
arr | Array<number> | TypedArray | array of numbers. |
utils/maths.max(arr) β <code> * </code>
Returns the value and index of the maximum element in an array.
Kind: static method of utils/maths
Returns: *
- the value and index of the maximum element, of the form: [valueOfMax, indexOfMax]
Throws:
Error
If array is empty.
Param | Type | Description |
---|---|---|
arr | Array<number> | AnyTypedArray | array of numbers. |
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
Param | Type | Description |
---|---|---|
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
Param | Type | Description |
---|---|---|
num | number | The number to round |
decimals | number | The number of decimals |
utils/maths.bankers_round(x) β <code> number </code>
Helper function to round a number to the nearest integer, with ties rounded to the nearest even number. Also known as βbankersβ roundingβ. This is the default rounding mode in python. For example: 1.5 rounds to 2 and 2.5 rounds to 2.
Kind: static method of utils/maths
Returns: number
- The rounded number
Param | Type | Description |
---|---|---|
x | number | The number to round |
utils/maths.dynamic_time_warping(matrix) β <code> Array. < Array < number > > </code>
Measures similarity between two temporal sequences (e.g., input audio and output tokens to generate token-level timestamps).
Kind: static method of utils/maths
Param | Type |
---|---|
matrix | Array.<Array<number>> |
utils/maths~P2FFT
Implementation of Radix-4 FFT.
P2FFT class provides functionality for performing Fast Fourier Transform on arrays which are a power of two in length. Code adapted from https://www.npmjs.com/package/fft.js
Kind: inner class of utils/maths
- ~P2FFT
new P2FFT(size)
.createComplexArray()
βFloat64Array
.fromComplexArray(complex, [storage])
βArray.<number>
.toComplexArray(input, [storage])
βFloat64Array
.transform(out, data)
βvoid
.realTransform(out, data)
.inverseTransform(out, data)
βvoid
._transform4(out, data, inv)
βvoid
._singleTransform2(data, out, outOff, off, step)
βvoid
._singleTransform4(data, out, outOff, off, step, inv)
βvoid
._realTransform4(out, data, inv)
._singleRealTransform2(data, out, outOff, off, step)
βvoid
._singleRealTransform4(data, out, outOff, off, step, inv)
new P2FFT(size)
Throws:
Error
FFT size must be a power of two larger than 1.
Param | Type | Description |
---|---|---|
size | number | The size of the input array. Must be a power of two larger than 1. |
p2FFT.createComplexArray() β <code> Float64Array </code>
Create a complex number array with size 2 * size
Kind: instance method of P2FFT
Returns: Float64Array
- A complex number array with size 2 * size
p2FFT.fromComplexArray(complex, [storage]) β <code> Array. < number > </code>
Converts a complex number representation stored in a Float64Array to an array of real numbers.
Kind: instance method of P2FFT
Returns: Array.<number>
- An array of real numbers representing the input complex number representation.
Param | Type | Description |
---|---|---|
complex | Float64Array | The complex number representation to be converted. |
[storage] | Array.<number> | An optional array to store the result in. |
p2FFT.toComplexArray(input, [storage]) β <code> Float64Array </code>
Convert a real-valued input array to a complex-valued output array.
Kind: instance method of P2FFT
Returns: Float64Array
- The complex-valued output array.
Param | Type | Description |
---|---|---|
input | Float64Array | The real-valued input array. |
[storage] | Float64Array | Optional buffer to store the output array. |
p2FFT.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 P2FFT
Throws:
Error
Input and output buffers must be different.
Param | Type | Description |
---|---|---|
out | Float64Array | The output buffer to store the result. |
data | Float64Array | The input data to transform. |
p2FFT.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 P2FFT
Throws:
Error
If the input and output buffers are the same.
Param | Type | Description |
---|---|---|
out | Float64Array | The output buffer. |
data | Float64Array | The input buffer containing real values. |
p2FFT.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 P2FFT
Throws:
Error
If `out` and `data` refer to the same buffer.
Param | Type | Description |
---|---|---|
out | Float64Array | The output buffer for the transformed data. |
data | Float64Array | The input data to transform. |
p2FFT._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 P2FFT
Param | Type | Description |
---|---|---|
out | Float64Array | The output buffer for the transformed data. |
data | Float64Array | The input buffer of data to be transformed. |
inv | number | A scaling factor to apply to the transform. |
p2FFT._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 P2FFT
Param | Type | Description |
---|---|---|
data | Float64Array | The input buffer of data to be transformed. |
out | Float64Array | 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. |
p2FFT._singleTransform4(data, out, outOff, off, step, inv) β <code> void </code>
Performs radix-4 transformation on input data of length 8
Kind: instance method of P2FFT
Param | Type | Description |
---|---|---|
data | Float64Array | Input data array of length 8 |
out | Float64Array | 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 |
p2FFT._realTransform4(out, data, inv)
Real input radix-4 implementation
Kind: instance method of P2FFT
Param | Type | Description |
---|---|---|
out | Float64Array | Output array for the transformed data |
data | Float64Array | Input array of real data to be transformed |
inv | number | The scale factor used to normalize the inverse transform |
p2FFT._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 P2FFT
Param | Type | Description |
---|---|---|
data | Float64Array | The input data array |
out | Float64Array | The output data array |
outOff | number | The output offset |
off | number | The input offset |
step | number | The step |
p2FFT._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 P2FFT
Param | Type | Description |
---|---|---|
data | Float64Array | The input data array. |
out | Float64Array | 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~NP2FFT
NP2FFT class provides functionality for performing Fast Fourier Transform on arrays which are not a power of two in length. In such cases, the chirp-z transform is used.
For more information, see: https://math.stackexchange.com/questions/77118/non-power-of-2-ffts/77156#77156
Kind: inner class of utils/maths
new NP2FFT(fft_length)
Constructs a new NP2FFT object.
Param | Type | Description |
---|---|---|
fft_length | number | The length of the FFT |
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
< > Update on GitHub