Array manipulation functions
This module contains helper functions to work with arrays (usually typed arrays, but not required).
This module accepts the premise that explicit is better than implicit. For this reason:
- The first parameter of all the functions is the number of samples to process.
- The last parameter of all modifyng functions is the array to use as output allowing explicit in-place modification
This is part of dsp-kit
- Source:
Examples
var array = require('dsp-array')
const sine = array.fill(1024, (x) => Math.sin(0.5 * x))
// included in dsp-kit package
var dsp = require('dsp-kit')
dsp.fill(...)
Methods
(static) add(numberOfSamples, a, b, output) → {Array}
Add elements from two arrays. Can work in-place
Parameters:
Name | Type | Description |
---|---|---|
numberOfSamples |
Integer | the number of samples to add |
a |
Array | one buffer to add |
b |
Array | the other buffer |
output |
Array | (Optional) the output buffer (or a new one if not provided) |
- Source:
Returns:
the output buffer
- Type
- Array
Example
add(10, signalA, signalB)
// in-place (store the result in signalA)
add(10, signalA, signalB, signalA)
(static) concat(arrayA, arrayB, destination) → {Array}
Concatenate two arrays
Parameters:
Name | Type | Description |
---|---|---|
arrayA |
Array | |
arrayB |
Array | |
destination |
Array | (Optional) If provided, the length must be at least the sum of the arrayA and arrayB length plus the destOffset |
- Source:
Returns:
destination
- Type
- Array
Example
// concat into a new array
const arrayC = array.concat(arrayA, arrayB)
(static) fill(array, fn)
Fill an array using a function
Parameters:
Name | Type | Description |
---|---|---|
array |
Number | Array | The array (to reuse) or an array length to create one |
fn |
function | the generator function. It receives the following parameters:
|
- Source:
Example
const sine = array.fill(10, (x) => Math.sin(x))
(static) mult(numberOfSamples, a, b, output) → {Array}
Multiply elements from two arrays. Can work in-place
Parameters:
Name | Type | Description |
---|---|---|
numberOfSamples |
Integer | the number of samples to add |
a |
Array | one buffer to add |
b |
Array | the other buffer |
output |
Array | (Optional) the output buffer (or a new one if not provided) |
- Source:
Returns:
the output buffer
- Type
- Array
Example
mult(10, signalA, signalB)
// in-place (store the result in signalA)
mult(10, signalA, signalB, signalA)
(static) round(array, decimals)
Round the values of an array to a number of decimals.
There are small differences of precission between algorithms. This helper function allows to compare them discarding the precission errors.
Parameters:
Name | Type | Description |
---|---|---|
array |
Array | |
decimals |
Integer | (Optional) the number of decimals (8 by default) |
- Source:
(static) roundTo(decimals) → {function}
Create a function that rounds to the given decimals
Parameters:
Name | Type | Description |
---|---|---|
decimals |
Integer | The number of decimals |
- Source:
- See:
-
- round
Returns:
a function
- Type
- function
(static) substr(numberOfSamples, minuend, subtrahend, output) → {Array}
Substract elements from two arrays. Can work in-place
Parameters:
Name | Type | Description |
---|---|---|
numberOfSamples |
Integer | the number of samples to add |
minuend |
Array | the buffer to substract from |
subtrahend |
Array | the buffer to get the numbers to being subtracted |
output |
Array | (Optional) the output buffer (or a new one if not provided) |
- Source:
Returns:
the output buffer
- Type
- Array
Example
var signalA = [3, 3, 3, 3]
var signalB = [0, 1, 2, 3]
substr(10, signalA, signalB) // => [3, 2, 1, 0]
// in-place (store the result in signalA)
substr(10, signalA, signalB, signalA) // => signalA contains the result
(static) testAll(N, predicate, array) → {Boolean}
Test if the N first elements of an array is true for a given predicate
Parameters:
Name | Type | Description |
---|---|---|
N |
Integer | the number of elements to test |
predicate |
function | a function that receive an element of the array and should return true or false |
array |
Array | the array |
- Source:
Returns:
- Type
- Boolean
Example
var signal = [1, 1, 1, 2, 2, 2]
testAll(3, signal, (x) => x === 1) // => true
testAll(4, signal, (x) => x === 1) // => false
(static) zeros(size) → {Array}
Create a typed array (a Float64Array) filled with zeros
Parameters:
Name | Type | Description |
---|---|---|
size |
Integer |
- Source:
Returns:
the array
- Type
- Array