webbci

Classes

oscStream

Namespaces

features

Methods

(static) cspLearn(class1, class2) → {Object}

Source:

Learn common spatial pattern for two datasets

Parameters:
Name Type Description
class1 Array.<Array.<number>>

Data samples for class 1. Rows should be samples, columns should be signals.

class2 Array.<Array.<number>>

Data samples for class 2. Rows should be samples, columns should be signals.

Returns:

Learned CSP parameters

Type
Object

(static) cspProject(cspParams, data, dimensionsopt) → {Array.<Array.<number>>}

Source:

Projects data and reduces to given number of dimensions

Parameters:
Name Type Attributes Description
cspParams object

CSP parameters computed using the cspLearn function

data Array.<Array.<number>>

Data points to be projected. Rows should be samples, columns should be signals.

dimensions number <optional>

Number of dimensions to be returned. Can range from 1 to number of signals. Defaults to number of signals.

Returns:

Projected data. Rows are samples, columns are dimensions sorted by descending importance.

Type
Array.<Array.<number>>

(static) generateSignal(amplitudes, frequencies, sampleRate, duration) → {Array.<number>}

Source:

Generate a signal with the given frequencies and their amplitudes.

Parameters:
Name Type Description
amplitudes Array.<number>

The amplitudes of each frequency.

frequencies Array.<number>

The frequencies.

sampleRate number

Sample rate of the signal in Hz.

duration number

Duration of the signal in seconds.

Returns:

The generated signal.

Type
Array.<number>

(static) ldaLearn(class1, class2) → {Object}

Source:

Perform linear discriminant analysis between two datasets

Parameters:
Name Type Description
class1 Array.<Array.<number>>

Data set for class 1, rows are samples, columns are variables

class2 Array.<Array.<number>>

Data set for class 2, rows are samples, columns are variables

Returns:

Computed LDA parameters

Type
Object

(static) ldaProject(ldaParams, point) → {number}

Source:

Predict the class of an unknown data point.

Parameters:
Name Type Description
ldaParams object

The parameters for the LDA, computed with the function ldaLearn

point Array.<number>

The data point to be classified.

Returns:

value less than 0 if predicted to be in class 1, 0 if exactly inbetween, greater than 0 if class 2

Type
number

(static) loadCSV(filePath) → {Promise}

Source:

Loads a CSV file into an array

Parameters:
Name Type Description
filePath string

The path to the CSV file

Returns:

A promise object which resolves to the CSV data array

Type
Promise

(static) nextpow2(num) → {number}

Source:

Returns the ceil of the log2 of the absolute value of the passed number

Example
nextpow2(8); // 3
nextpow2(9); // 4
nextpow2(16); // 4
nextpow2(30); // 5
nextpow2(0); // -Infinity
Parameters:
Name Type Description
num number
Returns:

The ceil of the log2 of the absolute value of the passed number

Type
number

(static) oscCollect(address, port, header, samples) → {Promise}

Source:

Collect a set number of samples over OSC

Parameters:
Name Type Description
address string

OSC address

port number

OSC port

header string

OSC header, can be found by scanning with oscHeaderScan if unknown

samples number

The number of samples to collect

Returns:

Resolves with collected data

Type
Promise

(static) oscHeaderScan(address, port, duration) → {Promise}

Source:

Scan for OSC headers on a port and address

Parameters:
Name Type Description
address any

OSC address

port any

OSC port

duration any

Duration of scan in milliseconds

Returns:

Resolves with an array of found headers

Type
Promise

(static) partition(array, …divisions) → {Array.<Array>}

Source:

Partitions an array into multiple arrays Can be used to split data into training and testing sets

Example
partition([1, 2, 3, 4], 0.25, 0.75); // returns [[1], [2, 3, 4]]
Parameters:
Name Type Attributes Description
array Array

The array to be partitioned

divisions Array.<number> <repeatable>

The size of each partition, each value should range from 0 to 1

Returns:

Array of subarrays which are the partitons

Type
Array.<Array>

(static) prompt(question) → {Promise}

Source:

Prompts the user for input via stdin

Parameters:
Name Type Description
question string

Question shown to user

Returns:

A promise object that resolves with the response

Type
Promise

(static) psd(signal, optionsopt) → {Array.<number>}

Source:

Compute the power spectral density of a given signal.

Parameters:
Name Type Attributes Description
signal Array.<number>

The signal.

options Object <optional>
Properties
Name Type Attributes Default Description
fftSize number <optional>
Math.pow(2, bci.nextpow2(signal.length))

Size of the fft to be used. Should be a power of 2.

truncate boolean <optional>
false

If true, only the first half of the PSD array is returned

Returns:

The PSD.

Type
Array.<number>

(static) psdBandPower(psd, sampleRate, band, fftSizeopt) → {number}

Source:

Compute the average power across a given frequency band given the PSD.

Parameters:
Name Type Attributes Default Description
psd Array.<number>

Power spectral density of the signal.

sampleRate number

The sample rate of the signal.

band Array.<number> | string

The frequency band provided as an array [frequencyStart, frequencyStop] or a string delta (1-3 Hz), theta (4-7 Hz), alpha (8-12 Hz), beta (13-30 Hz), or gamma (31-50 Hz). While string representations allow for easier prototyping, the use of a specific band passed as an array is recommended, as band string representations may change in future updates.

fftSize number <optional>
Math.pow(2, bci.nextpow2(psd.length))

Size of the fourier transform used to compute the PSD.

Returns:

The average power in the frequency band.

Type
number

(static) round(array, places) → {Array.<number>}

Source:

Rounds every value in an array to a set number of decimal places

Parameters:
Name Type Description
array Array.<number>
places number
Returns:

The rounded array

Type
Array.<number>

(static) saveCSV(array, filename) → {Promise}

Source:

Saves an array to a CSV file

Parameters:
Name Type Description
array Array
filename string
Returns:

A promise object that resolves when the file has been saved. Does not currently reject on write error.

Type
Promise

(static) signalBandPower(signal, sampleRate, band, fftSizeopt) → {number}

Source:

Compute the average power across a given frequency band in a signal.

Parameters:
Name Type Attributes Default Description
signal Array.<number>

The signal.

sampleRate number

The sample rate of the signal.

band Array.<number> | string

The frequency band provided as an array [frequencyStart, frequencyStop] or a string delta (1-3 Hz), theta (4-7 Hz), alpha (8-12 Hz), beta (13-30 Hz), or gamma (31-50 Hz). While string representations allow for easier prototyping, the use of a specific band passed as an array is recommended, as band string representations may change in future updates.

fftSize number <optional>
Math.pow(2, bci.nextpow2(signal.length))

Size of the fourier transform used to compute the PSD.

Returns:

The average power in the frequency band.

Type
number

(static) subscript(array, …params) → {Array}

Source:

Subscript an array with MATLAB-like syntax

Examples
var bci = require('webbci');
var arr = [3, 2, 4, 1, 5];
var subarr = bci.subscript(arr, '1:3');
console.log(subarr); // [3, 2, 4]
var bci = require('webbci');
var arr = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
];
var subarr = bci.subscript(arr, '1 3', '2:3'); // rows 1 and 3, columns 2 through 3
console.log(subarr); // [[2, 3], [8, 9]]
Parameters:
Name Type Attributes Description
array Array

The array to be subscripted

params string <repeatable>

Colon notation for which elements to include in each dimension

Returns:

The subscripted array

Type
Array

(static) toFixed(array, places) → {Array.<string>}

Source:

Returns an array of numbers as strings rounded to the proper number of decimal places and padded with zeros as needed.

Parameters:
Name Type Description
array Array
places number
Returns:

Array of string representations of numbers

Type
Array.<string>

(static) toTable(array) → {string}

Source:

Returns an ASCII table representation of an array

Parameters:
Name Type Description
array Array
Returns:

ASCII table

Type
string

(static) wait(ms) → {Promise}

Source:
Parameters:
Name Type Description
ms number

Number of milliseconds to wait

Returns:

A promise which resolves when the timeout occurs

Type
Promise

(static) windowApply(array, func, length, step, tail) → {Array}

Source:

Similar to JavaScript's map, but it applies a function to sub arrays instead of each element. Each sub array, or window, starts at index 0 and has length 'length' Each next window will be shifted 'step' elements from the first. The result of each function is stored in a returned array.

Examples
var bci = require('webbci');
bci.windowApply([1, 2, 3, 4, 5], window => console.log(window), 3, 1);
// [1, 2, 3]
// [2, 3, 4]
// [3, 4, 5] 
var bci = require('webbci');
var sums = bci.windowApply([1, 2, 3, 4, 5], window => {
  var sum = 0;
  window.forEach(x => sum += x);
  return sum;
}, 3, 1);
console.log(sums);
// [6, 9, 12]
Parameters:
Name Type Default Description
array Array

The array of elements which will be windowed

func function

The function to call on each window, the returned result is stored in a returned array

length number

The number of elements to include in each window

step number

The start of the window is incremented by this amount every iteration

tail boolean false

If false, windows which begin near the end of the array which cannot reach length 'length' will be ignored

Returns:

An array containing the function result for each window

Type
Array