bcijs

EEG signal processing and machine learning

Usage no npm install needed!

<script type="module">
  import bcijs from 'https://cdn.skypack.dev/bcijs';
</script>

README

Version Downloads CDN License


BCI.js is a library for EEG-based brain computer interface (BCI) design with JavaScript and Node.js. It allows for the creation of BCI enabled web apps or Node.js applications, with features such as:

  • Signal processing and machine learning (Bandpower, PSD, LDA, CSP, ICA, etc.)
  • Data manipulation (MATLAB style array subscripting, data windowing, CSV file support, etc.)
  • Networking (data collection, streaming via OSC, etc.)

You can view all available methods in the docs

Latest release is v1.7.1. You can view the release notes at releases

Getting Started

Node.js

npm install bcijs

Browser

<script src="https://cdn.jsdelivr.net/npm/bcijs@1.7.1/dist/bci.min.js"></script>

Tutorials

Examples

More examples can be found in the examples directory

Signal Processing

const bci = require('bcijs');

// Generate 1 second of sample data
let sampleRate = 512;
let duration = 1;
let amplitudes = [8, 4, 2, 1];
let frequencies = [
    1, // 1 Hz, delta range
    5, // 5 Hz, theta range
    8, // 8 Hz, alpha range
    17 // 17 Hz, beta range
];

let signal = bci.generateSignal(amplitudes, frequencies, sampleRate, duration);

// Compute relative power in each frequency band
let bandpowers = bci.bandpower(
    signal,
    sampleRate,
    ['delta', 'theta', 'alpha', 'beta'],
    {relative: true}
);

console.log(bandpowers);
/*
[
  0.7171876695851037, 
  0.22444067394892755,
  0.04489131763080717,
  0.013469490282877555
]
*/

Machine Learning

Check out https://bci.js.org/examples/lda for a visual demo of how LDA works

const bci = require('bcijs');

// Training set
let class1 = [
    [0, 0],
    [1, 2],
    [2, 2],
    [1.5, 0.5]
];
let class2 = [
    [8, 8],
    [9, 10],
    [7, 8],
    [9, 9]
];

// Testing set
let unknownPoints = [
    [-1, 0],
    [1.5, 2],
    [3, 3],
    [5, 5],
    [7, 9],
    [10, 12]
];

// Learn an LDA classifier
let ldaParams = bci.ldaLearn(class1, class2);

// Test classifier
let predictions = bci.ldaClassify(ldaParams, unknownPoints);

console.log(predictions); // [ 0, 0, 0, 1, 1, 1 ]

Data Manipulation and Feature Extraction

const bci = require('bcijs');

// Some random numbers
let data = [3, 2, 3, 0, 4, 0, 0, 5, 4, 0];

// Partition into training and testing sets
let [training, testing] = bci.partition(data, 0.6, 0.4);

console.log(training); // [3, 2, 3, 0, 4, 0]
console.log(testing); // [0, 5, 4, 0]

// Traverse the data array with windows of size 3 and a step of 2 (overlap of 1 item per window)
bci.windowApply(data, window => console.log(window), 3, 2);
/*
[ 3, 2, 3 ]
[ 3, 0, 4 ]
[ 4, 0, 0 ]
[ 0, 5, 4 ]
*/

// Find the log of the variance of these windows (feature extraction)
let features = bci.windowApply(data, bci.features.logvar, 3, 2);
console.log(features); // [-1.099, 1.466, 1.674, 1.946]

// Colon notation for array subscripting
let arr = [
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9, 10, 11, 12]
];
let subarr = bci.subscript(arr, '1 3', '2:4'); // rows 1 and 3, columns 2 through 4
console.log(subarr);
/*
[[2, 3, 4],
 [10, 11, 12]]
*/

Usage in the web

BCI.js can be loaded from the jsDelivr CDN with

<script src="https://cdn.jsdelivr.net/npm/bcijs@1.7.1/dist/bci.min.js"></script>

You can also find bci.js and bci.min.js in the /dist directory.

BCI.js methods are accessible via the global object bci.

If building a web distributable using a tool such as browserify or webpack, require bcijs/browser.js to load only methods that are browser compatible. Node.js specific methods such as networking and file system methods will not be included.

const bci = require('bcijs/browser.js');

Requiring specific methods

You can require specific methods as well. For example, if you only need fastICA, you can use

const fastICA = require('bcijs/lib/math/fastICA.js');

BCI.js methods can be found in the lib/ directory.

Documentation

Documentation can be found at https://bci.js.org/docs or by viewing api.md

Deprecated methods can be found at deprecated.md

Building

See dev.md for info on how to modify and build BCI.js

Reference

BCI.js began as WebBCI, a library developed to aid in my research at the Human Technology Interaction Lab at the University of Alabama Department of Computer Science. If you use BCI.js in a published work, please reference this paper

P. Stegman, C. Crawford, and J. Gray, "WebBCI: An Electroencephalography Toolkit Built on Modern Web Technologies," in Augmented Cognition: Intelligent Technologies, 2018, pp. 212–221.

Logo uses icon from Font Awesome.

Contact

If you have a commercial use case for BCI.js and would like to discuss working together, contact me at pwstegman@gmail.com