@@ -50,123 +50,72 @@ For a complete list of methods, see the [docs](https://bci.js.org/docs/).
5050
5151More examples can be found in the [ examples] ( https://github.com/pwstegman/bci.js/tree/master/examples ) directory
5252
53- ### Signal Processing
53+ ### Bandpower
5454
5555``` javascript
5656const bci = require (' bcijs' );
5757
58- // Generate 1 second of sample data
59- let sampleRate = 512 ;
60- let duration = 1 ;
61- let amplitudes = [8 , 4 , 2 , 1 ];
62- let frequencies = [
63- 1 , // 1 Hz, delta range
64- 5 , // 5 Hz, theta range
65- 8 , // 8 Hz, alpha range
66- 17 // 17 Hz, beta range
67- ];
68-
69- let signal = bci .generateSignal (amplitudes, frequencies, sampleRate, duration);
58+ // Generate 1 second of sample data at 512 Hz
59+ // Contains 8 μV / 8 Hz and 4 μV / 17 Hz
60+ let samplerate = 512 ;
61+ let signal = bci .generateSignal ([8 , 4 ], [8 , 17 ], samplerate, 1 );
7062
7163// Compute relative power in each frequency band
72- let bandpowers = bci .bandpower (
73- signal,
74- sampleRate,
75- [' delta' , ' theta' , ' alpha' , ' beta' ],
76- {relative: true }
77- );
64+ let bandpowers = bci .bandpower (signal, samplerate, [' alpha' , ' beta' ], {relative: true });
7865
79- console .log (bandpowers);
80- /*
81- [
82- 0.7171876695851037,
83- 0.22444067394892755,
84- 0.04489131763080717,
85- 0.013469490282877555
86- ]
87- */
66+ console .log (bandpowers); // [ 0.6661457715567836, 0.199999684787573 ]
8867```
8968
90- ### Machine Learning
69+ ### Epoch data
9170
92- Check out [ https://bci.js.org/examples/lda ] ( https://bci.js.org/examples/lda ) for a visual demo of how LDA works
71+ ``` javascript
72+ let samples = [[1 ,2 ], [3 ,4 ], ... ] // 2D array where rows are samples and columns are channels
73+ let samplerate = 256 ; // 256 Hz
74+
75+ // Epoch data into epochs of 256 samples with a step of 64 (75% overlap)
76+ // Then find the average alpha and beta powers in each epoch.
77+ let powers = bci .windowApply (
78+ samples,
79+ epoch => bci .bandpower (epoch, samplerate, [' alpha' , ' beta' ], {average: true }),
80+ 256 ,
81+ 64
82+ );
83+ ```
84+
85+ ### Subscript
86+
87+ ``` javascript
88+ const bci = require (' bcijs' );
89+
90+ // 5 samples of data from 3 channels
91+ let signal = [[1 ,2 ,3 ], [5 ,3 ,4 ], [4 ,5 ,6 ], [7 ,5 ,8 ], [4 ,4 ,2 ]];
92+
93+ // Select the first 3 samples from channels 1 and 3
94+ let subset = bci .subscript (signal, ' 1:3' , ' 1 3' ); // [ [ 1, 3 ], [ 5, 4 ], [ 4, 6 ] ]
95+ ```
96+
97+ ### Linear discriminant analysis
9398
9499``` javascript
95100const bci = require (' bcijs' );
96101
97102// Training set
98- let class1 = [
99- [0 , 0 ],
100- [1 , 2 ],
101- [2 , 2 ],
102- [1.5 , 0.5 ]
103- ];
104- let class2 = [
105- [8 , 8 ],
106- [9 , 10 ],
107- [7 , 8 ],
108- [9 , 9 ]
109- ];
103+ let class1 = [[0 , 0 ], [1 , 2 ], [2 , 2 ], [1.5 , 0.5 ]];
104+ let class2 = [[8 , 8 ], [9 , 10 ], [7 , 8 ], [9 , 9 ]];
110105
111106// Testing set
112- let unknownPoints = [
113- [- 1 , 0 ],
114- [1.5 , 2 ],
115- [3 , 3 ],
116- [5 , 5 ],
117- [7 , 9 ],
118- [10 , 12 ]
119- ];
107+ let unknownPoints = [[- 1 , 0 ], [1.5 , 2 ], [7 , 9 ], [10 , 12 ]];
120108
121109// Learn an LDA classifier
122110let ldaParams = bci .ldaLearn (class1, class2);
123111
124112// Test classifier
125113let predictions = bci .ldaClassify (ldaParams, unknownPoints);
126114
127- console .log (predictions); // [ 0, 0, 0, 1, 1, 1 ]
115+ console .log (predictions); // [ 0, 0, 1, 1 ]
128116```
129117
130- ### Data Manipulation and Feature Extraction
131-
132- ``` javascript
133- const bci = require (' bcijs' );
134-
135- // Some random numbers
136- let data = [3 , 2 , 3 , 0 , 4 , 0 , 0 , 5 , 4 , 0 ];
137-
138- // Partition into training and testing sets
139- let [training, testing] = bci .partition (data, 0.6 , 0.4 );
140-
141- console .log (training); // [3, 2, 3, 0, 4, 0]
142- console .log (testing); // [0, 5, 4, 0]
143-
144- // Traverse the data array with windows of size 3 and a step of 2 (overlap of 1 item per window)
145- bci .windowApply (data, window => console .log (window ), 3 , 2 );
146- /*
147- [ 3, 2, 3 ]
148- [ 3, 0, 4 ]
149- [ 4, 0, 0 ]
150- [ 0, 5, 4 ]
151- */
152-
153- // Find the log of the variance of these windows (feature extraction)
154- let features = bci .windowApply (data, bci .features .logvar , 3 , 2 );
155- console .log (features); // [-1.099, 1.466, 1.674, 1.946]
156-
157- // Colon notation for array subscripting
158- let arr = [
159- [1 , 2 , 3 , 4 ],
160- [5 , 6 , 7 , 8 ],
161- [9 , 10 , 11 , 12 ]
162- ];
163- let subarr = bci .subscript (arr, ' 1 3' , ' 2:4' ); // rows 1 and 3, columns 2 through 4
164- console .log (subarr);
165- /*
166- [[2, 3, 4],
167- [10, 11, 12]]
168- */
169- ```
118+ Check out [ https://bci.js.org/examples/lda ] ( https://bci.js.org/examples/lda ) for a visual demo of how LDA works
170119
171120## Usage in the web
172121
0 commit comments