forked from openbci-archive/OpenBCI_NodeJS
-
Notifications
You must be signed in to change notification settings - Fork 6
/
openBCISample.js
1256 lines (1109 loc) · 53.8 KB
/
openBCISample.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
var gaussian = require('gaussian'),
k = require('./openBCIConstants'),
StreamSearch = require('streamsearch');
/** Constants for interpreting the EEG data */
// Reference voltage for ADC in ADS1299.
// Set by its hardware.
const ADS1299_VREF = 4.5;
// Scale factor for aux data
const SCALE_FACTOR_ACCEL = 0.002 / Math.pow(2,4);
// X, Y, Z
const ACCEL_NUMBER_AXIS = 3;
// Default ADS1299 gains array
// For computing Goertzel Algorithm
// See: http://www.embedded.com/design/configurable-systems/4024443/The-Goertzel-Algorithm
// In the tutorial cited above, GOERTZEL_BLOCK_SIZE is referred to as N
const GOERTZEL_BLOCK_SIZE = 62;
const GOERTZEL_K_250 = Math.floor(0.5 + ((GOERTZEL_BLOCK_SIZE * k.OBCILeadOffFrequencyHz) / k.OBCISampleRate250));
const GOERTZEL_W_250 = ((2 * Math.PI) / GOERTZEL_BLOCK_SIZE) * GOERTZEL_K_250;
const GOERTZEL_COEFF_250 = 2 * Math.cos(GOERTZEL_W_250);
// TODO: Add support for 16 channel Daisy board
var oddSample = {};
var sampleModule = {
/**
* @description This takes a 33 byte packet and converts it based on the last four bits.
* 0000 - Standard OpenBCI V3 Sample Packet
* @param dataBuf {Buffer} - A 33 byte buffer
* @param channelSettingsArray - An array of channel settings that is an Array that has shape similar to the one
* calling OpenBCIConstans.channelSettingsArrayInit(). The most important rule here is that it is
* Array of objects that have key-value pair {gain:NUMBER}
* @param convertAuxToAccel (optional) {Boolean} - Do you want to convert to g's? (Defaults to true)
* @returns {Promise} - A sample object
*/
parseRawPacketStandard: (dataBuf,channelSettingsArray,convertAuxToAccel) => {
const defaultChannelSettingsArray = k.channelSettingsArrayInit(k.OBCINumberOfChannelsDefault);
return new Promise((resolve, reject) => {
if (dataBuf === undefined || dataBuf === null) reject('Error [parseRawPacket]: dataBuf must be defined.');
// Verify proper start byte
if (dataBuf[0] != k.OBCIByteStart) reject('Error [parseRawPacket]: Invalid start byte of ' + dataBuf[0].toString(16) + ' expected ' + k.OBCIByteStart.toString(16));
// channelSettingsArray is optional, defaults to CHANNEL_SETTINGS_ARRAY_DEFAULT
channelSettingsArray = channelSettingsArray || defaultChannelSettingsArray;
// By default convert to g's
if (convertAuxToAccel === undefined || convertAuxToAccel === null) convertAuxToAccel = true;
if (convertAuxToAccel) {
parsePacketStandardAccel(dataBuf,channelSettingsArray).then(sampleObject => {
resolve(sampleObject);
}).catch(err => reject(err));
} else {
parsePacketStandardRawAux(dataBuf,channelSettingsArray).then(sampleObject => {
resolve(sampleObject);
}).catch(err => reject(err));
}
});
},
getRawPacketType,
getFromTimePacketAccel,
getFromTimePacketTime,
getFromTimePacketRawAux,
parsePacketTimeSyncedAccel,
parsePacketTimeSyncedRawAux,
/**
* @description Mainly used by the simulator to convert a randomly generated sample into a std OpenBCI V3 Packet
* @param sample - A sample object
* @returns {Buffer}
*/
convertSampleToPacketStandard: (sample) => {
var packetBuffer = new Buffer(k.OBCIPacketSize);
packetBuffer.fill(0);
// start byte
packetBuffer[0] = k.OBCIByteStart;
// sample number
packetBuffer[1] = sample.sampleNumber;
// channel data
for (var i = 0; i < k.OBCINumberOfChannelsDefault; i++) {
var threeByteBuffer = floatTo3ByteBuffer(sample.channelData[i]);
threeByteBuffer.copy(packetBuffer, 2 + (i * 3));
}
for (var j = 0; j < 3; j++) {
var twoByteBuffer = floatTo2ByteBuffer(sample.auxData[j]);
twoByteBuffer.copy(packetBuffer, (k.OBCIPacketSize - 1 - 6) + (i * 2));
}
// stop byte
packetBuffer[k.OBCIPacketSize - 1] = k.OBCIByteStop;
return packetBuffer;
},
/**
* @description Mainly used by the simulator to convert a randomly generated sample into a std OpenBCI V3 Packet
* @param sample - A sample object
* @param rawAux {Buffer} - A 6 byte long buffer to insert into raw buffer
* @returns {Buffer} - A 33 byte long buffer
*/
convertSampleToPacketRawAux: (sample, rawAux) => {
var packetBuffer = new Buffer(k.OBCIPacketSize);
packetBuffer.fill(0);
// start byte
packetBuffer[0] = k.OBCIByteStart;
// sample number
packetBuffer[1] = sample.sampleNumber;
// channel data
for (var i = 0; i < k.OBCINumberOfChannelsDefault; i++) {
var threeByteBuffer = floatTo3ByteBuffer(sample.channelData[i]);
threeByteBuffer.copy(packetBuffer, 2 + (i * 3));
}
// Write the raw aux bytes
rawAux.copy(packetBuffer,26);
// stop byte
packetBuffer[k.OBCIPacketSize - 1] = makeTailByteFromPacketType(k.OBCIStreamPacketStandardRawAux);
return packetBuffer;
},
/**
* @description Mainly used by the simulator to convert a randomly generated sample into an accel time sync set buffer
* @param sample {Buffer} - A sample object
* @param time {Number} - The time to inject into the sample.
* @returns {Buffer} - A time sync accel packet
*/
convertSampleToPacketAccelTimeSyncSet: (sample, time) => {
var buf = convertSampleToPacketAccelTimeSynced(sample, time);
buf[k.OBCIPacketPositionStopByte] = makeTailByteFromPacketType(k.OBCIStreamPacketAccelTimeSyncSet);
return buf;
},
/**
* @description Mainly used by the simulator to convert a randomly generated sample into an accel time synced buffer
* @param sample {Buffer} - A sample object
* @param time {Number} - The time to inject into the sample.
* @returns {Buffer} - A time sync accel packet
*/
convertSampleToPacketAccelTimeSynced,
/**
* @description Mainly used by the simulator to convert a randomly generated sample into a raw aux time sync set packet
* @param sample {Buffer} - A sample object
* @param time {Number} - The time to inject into the sample.
* @param rawAux {Buffer} - 2 byte buffer to inject into sample
* @returns {Buffer} - A time sync raw aux packet
*/
convertSampleToPacketRawAuxTimeSyncSet: (sample, time, rawAux) => {
var buf = convertSampleToPacketRawAuxTimeSynced(sample, time, rawAux);
buf[k.OBCIPacketPositionStopByte] = makeTailByteFromPacketType(k.OBCIStreamPacketRawAuxTimeSyncSet);
return buf;
},
convertSampleToPacketRawAuxTimeSynced,
debugPrettyPrint: (sample) => {
if(sample === null || sample === undefined) {
console.log('== Sample is undefined ==');
} else {
console.log('-- Sample --');
console.log('---- Start Byte: ' + sample.startByte);
console.log('---- Sample Number: ' + sample.sampleNumber);
for(var i = 0; i < 8; i++) {
console.log('---- Channel Data ' + (i + 1) + ': ' + sample.channelData[i]);
}
if(!!sample.accelData) {
for(var j = 0; j < 3; j++) {
console.log('---- Accel Data ' + j + ': ' + sample.accelData[j]);
}
}
if (!!sample.auxData) {
console.log('---- Aux Data ' + sample.auxData);
}
console.log('---- Stop Byte: ' + sample.stopByte);
}
},
samplePrintHeader: () => {
return (
'All voltages in Volts!' +
'sampleNumber, channel1, channel2, channel3, channel4, channel5, channel6, channel7, channel8, aux1, aux2, aux3\n');
},
samplePrintLine: sample => {
return new Promise((resolve, reject) => {
if (sample === null || sample === undefined) reject('undefined sample');
resolve(
sample.sampleNumber + ',' +
sample.channelData[0].toFixed(8) + ',' +
sample.channelData[1].toFixed(8) + ',' +
sample.channelData[2].toFixed(8) + ',' +
sample.channelData[3].toFixed(8) + ',' +
sample.channelData[4].toFixed(8) + ',' +
sample.channelData[5].toFixed(8) + ',' +
sample.channelData[6].toFixed(8) + ',' +
sample.channelData[7].toFixed(8) + ',' +
sample.auxData[0].toFixed(8) + ',' +
sample.auxData[1].toFixed(8) + ',' +
sample.auxData[2].toFixed(8) + '\n'
);
});
},
floatTo3ByteBuffer,
floatTo2ByteBuffer,
/**
* @description Calculate the impedance for one channel only.
* @param sampleObject - Standard OpenBCI sample object
* @param channelNumber - Number, the channel you want to calculate impedance for.
* @returns {Promise} - Fulfilled with impedance value for the specified channel.
* @author AJ Keller
*/
impedanceCalculationForChannel: (sampleObject,channelNumber) => {
const sqrt2 = Math.sqrt(2);
return new Promise((resolve,reject) => {
if(sampleObject === undefined || sampleObject === null) reject('Sample Object cannot be null or undefined');
if(sampleObject.channelData === undefined || sampleObject.channelData === null) reject('Channel cannot be null or undefined');
if(channelNumber < 1 || channelNumber > k.OBCINumberOfChannelsDefault) reject('Channel number invalid.');
var index = channelNumber - 1;
if (sampleObject.channelData[index] < 0) {
sampleObject.channelData[index] *= -1;
}
var impedance = (sqrt2 * sampleObject.channelData[index]) / k.OBCILeadOffDriveInAmps;
//if (index === 0) console.log("Voltage: " + (sqrt2*sampleObject.channelData[index]) + " leadoff amps: " + k.OBCILeadOffDriveInAmps + " impedance: " + impedance);
resolve(impedance);
});
},
/**
* @description Calculate the impedance for all channels.
* @param sampleObject - Standard OpenBCI sample object
* @returns {Promise} - Fulfilled with impedances for the sample
* @author AJ Keller
*/
impedanceCalculationForAllChannels: sampleObject => {
const sqrt2 = Math.sqrt(2);
return new Promise((resolve,reject) => {
if(sampleObject === undefined || sampleObject === null) reject('Sample Object cannot be null or undefined');
if(sampleObject.channelData === undefined || sampleObject.channelData === null) reject('Channel cannot be null or undefined');
var sampleImpedances = [];
var numChannels = sampleObject.channelData.length;
for (var index = 0;index < numChannels; index++) {
if (sampleObject.channelData[index] < 0) {
sampleObject.channelData[index] *= -1;
}
var impedance = (sqrt2 * sampleObject.channelData[index]) / k.OBCILeadOffDriveInAmps;
sampleImpedances.push(impedance);
//if (index === 0) console.log("Voltage: " + (sqrt2*sampleObject.channelData[index]) + " leadoff amps: " + k.OBCILeadOffDriveInAmps + " impedance: " + impedance);
}
sampleObject.impedances = sampleImpedances;
resolve(sampleObject);
});
},
interpret16bitAsInt32: twoByteBuffer => {
var prefix = 0;
if(twoByteBuffer[0] > 127) {
//console.log('\t\tNegative number');
prefix = 65535; // 0xFFFF
}
return (prefix << 16) | (twoByteBuffer[0] << 8) | twoByteBuffer[1];
},
interpret24bitAsInt32: threeByteBuffer => {
var prefix = 0;
if(threeByteBuffer[0] > 127) {
//console.log('\t\tNegative number');
prefix = 255;
}
return (prefix << 24 ) | (threeByteBuffer[0] << 16) | (threeByteBuffer[1] << 8) | threeByteBuffer[2];
},
impedanceArray: numberOfChannels => {
var impedanceArray = [];
for (var i = 0; i < numberOfChannels; i++) {
impedanceArray.push(newImpedanceObject(i+1));
}
return impedanceArray;
},
impedanceObject: newImpedanceObject,
impedanceSummarize: singleInputObject => {
if (singleInputObject.raw > k.OBCIImpedanceThresholdBadMax) { // The case for no load (super high impedance)
singleInputObject.text = k.OBCIImpedanceTextNone;
} else {
singleInputObject.text = k.getTextForRawImpedance(singleInputObject.raw); // Get textual impedance
}
},
newSample,
/**
* @description Create a configurable function to return samples for a simulator. This implements 1/f filtering injection to create more brain like data.
* @param numberOfChannels {Number} - The number of channels in the sample... either 8 or 16
* @param sampleRateHz {Number} - The sample rate
* @param injectAlpha {Boolean} - True if you want to inject noise
* @param lineNoise {String} - A string that can be either:
* `60Hz` - 60Hz line noise (Default) (ex. __United States__)
* `50Hz` - 50Hz line noise (ex. __Europe__)
* `None` - Do not inject line noise.
*
* @returns {Function}
*/
randomSample: (numberOfChannels,sampleRateHz, injectAlpha,lineNoise) => {
var self = this;
const distribution = gaussian(0,1);
const sineWaveFreqHz10 = 10;
const sineWaveFreqHz50 = 50;
const sineWaveFreqHz60 = 60;
const uVolts = 1000000;
var sinePhaseRad = new Array(numberOfChannels+1); //prevent index error with '+1'
sinePhaseRad.fill(0);
var auxData = [0,0,0];
var accelCounter = 0;
// With 250Hz, every 10 samples, with 125Hz, every 5...
var samplesPerAccelRate = Math.floor(sampleRateHz / 25); // best to make this an integer
if (samplesPerAccelRate < 1) samplesPerAccelRate = 1;
// Init arrays to hold coefficients for each channel and init to 0
// This gives the 1/f filter memory on each iteration
var b0 = new Array(numberOfChannels).fill(0);
var b1 = new Array(numberOfChannels).fill(0);
var b2 = new Array(numberOfChannels).fill(0);
/**
* @description Use a 1/f filter
* @param previousSampleNumber {Number} - The previous sample number
*/
return previousSampleNumber => {
var sample = newSample();
var whiteNoise;
for(var i = 0; i < numberOfChannels; i++) { //channels are 0 indexed
// This produces white noise
whiteNoise = distribution.ppf(Math.random()) * Math.sqrt(sampleRateHz/2)/uVolts;
switch (i) {
case 0: // Add 10Hz signal to channel 1... brainy
case 1:
if (injectAlpha) {
sinePhaseRad[i] += 2 * Math.PI * sineWaveFreqHz10 / sampleRateHz;
if (sinePhaseRad[i] > 2 * Math.PI) {
sinePhaseRad[i] -= 2 * Math.PI;
}
whiteNoise += (5 * Math.SQRT2 * Math.sin(sinePhaseRad[i]))/uVolts;
}
break;
default:
if (lineNoise === k.OBCISimulatorLineNoiseHz60) {
// If we're in murica we want to add 60Hz line noise
sinePhaseRad[i] += 2 * Math.PI * sineWaveFreqHz60 / sampleRateHz;
if (sinePhaseRad[i] > 2 * Math.PI) {
sinePhaseRad[i] -= 2 * Math.PI;
}
whiteNoise += (8 * Math.SQRT2 * Math.sin(sinePhaseRad[i])) / uVolts;
} else if (lineNoise === k.OBCISimulatorLineNoiseHz50){
// add 50Hz line noise if we are not in america
sinePhaseRad[i] += 2 * Math.PI * sineWaveFreqHz50 / sampleRateHz;
if (sinePhaseRad[i] > 2 * Math.PI) {
sinePhaseRad[i] -= 2 * Math.PI;
}
whiteNoise += (8 * Math.SQRT2 * Math.sin(sinePhaseRad[i])) / uVolts;
}
}
/**
* See http://www.firstpr.com.au/dsp/pink-noise/ section "Filtering white noise to make it pink"
*/
b0[i] = 0.99765 * b0[i] + whiteNoise * 0.0990460;
b1[i] = 0.96300 * b1[i] + whiteNoise * 0.2965164;
b2[i] = 0.57000 * b2[i] + whiteNoise * 1.0526913;
sample.channelData[i] = b0[i] + b1[i] + b2[i] + whiteNoise * 0.1848;
}
if (previousSampleNumber == 255) {
sample.sampleNumber = 0;
} else {
sample.sampleNumber = previousSampleNumber + 1;
}
/**
* Sample rate of accelerometer is 25Hz... when the accelCounter hits the relative sample rate of the accel
* we will output a new accel value. The approach will be to consider that Z should be about 1 and X and Y
* should be somewhere around 0.
*/
if (accelCounter == samplesPerAccelRate) {
// Initialize a new array
var accelArray = [0,0,0];
// Calculate X
accelArray[0] = (Math.random() * 0.1 * (Math.random() > 0.5 ? -1 : 1));
// Calculate Y
accelArray[1] = (Math.random() * 0.1 * (Math.random() > 0.5 ? -1 : 1));
// Calculate Z, this is around 1
accelArray[2] = 1 - ((Math.random() * 0.4) * (Math.random() > 0.5 ? -1 : 1));
// Store the newly calculated value
sample.auxData = accelArray;
// Reset the counter
accelCounter = 0;
} else {
// Increment counter
accelCounter++;
// Store the default value
sample.auxData = auxData;
}
return sample;
};
},
scaleFactorAux: SCALE_FACTOR_ACCEL,
k,
/**
* @description Use the Goertzel algorithm to calculate impedances
* @param sample - a sample with channelData Array
* @param goertzelObj - An object that was created by a call to this.goertzelNewObject()
* @returns {Array} - Returns an array if finished computing
*/
goertzelProcessSample: (sample,goertzelObj) => {
// calculate the goertzel values for all channels
for (var i = 0; i < goertzelObj.numberOfChannels; i++) {
var q0 = GOERTZEL_COEFF_250 * goertzelObj.q1[i] - goertzelObj.q2[i] + sample.channelData[i];
goertzelObj.q2[i] = goertzelObj.q1[i];
goertzelObj.q1[i] = q0;
//console.log('Q1: ' + goertzelObj.q1[i] + ' Q2: ' + goertzelObj.q2[i]);
}
// Increment the index counter
goertzelObj.index++;
// Have we iterated more times then block size?
if (goertzelObj.index > GOERTZEL_BLOCK_SIZE) {
var impedanceArray = [];
for (var j = 0; j < goertzelObj.numberOfChannels; j++) {
// Calculate the magnitude of the voltage
var q1SQRD = goertzelObj.q1[j] * goertzelObj.q1[j];
var q2SQRD = goertzelObj.q2[j] * goertzelObj.q2[j];
var lastPart = goertzelObj.q1[j] * goertzelObj.q2[j] * GOERTZEL_COEFF_250;
//console.log('Chan ' + j + ', Q1^2: ' + q1SQRD + ', Q2^2: ' + q2SQRD + ', Last Part: ' + lastPart);
var voltage = Math.sqrt((goertzelObj.q1[j] * goertzelObj.q1[j]) + (goertzelObj.q2[j] * goertzelObj.q2[j]) - goertzelObj.q1[j] * goertzelObj.q2[j] * GOERTZEL_COEFF_250);
// Calculate the impedance r = v/i
var impedance = voltage / k.OBCILeadOffDriveInAmps;
// Push the impedance into the final array
impedanceArray.push(impedance);
// Reset the goertzel variables to get ready for the next iteration
goertzelObj.q1[j] = 0;
goertzelObj.q2[j] = 0;
}
// Reset the goertzel index counter
goertzelObj.index = 0;
// Pass out the impedance array
return impedanceArray;
} else {
// This reject is really just for debugging
return;
}
},
goertzelNewObject: numberOfChannels => {
// Object to help calculate the goertzel
var q1 = [];
var q2 = [];
for (var i = 0; i < numberOfChannels; i++) {
q1.push(0);
q2.push(0);
}
return {
q1: q1,
q2: q2,
index: 0,
numberOfChannels: numberOfChannels
}
},
GOERTZEL_BLOCK_SIZE,
samplePacket: sampleNumber => {
return new Buffer([0xA0, sampleNumberNormalize(sampleNumber), 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 0, 1, 0, 2, makeTailByteFromPacketType(k.OBCIStreamPacketStandardAccel)]);
},
samplePacketReal: sampleNumber => {
return new Buffer([0xA0, sampleNumberNormalize(sampleNumber), 0x8F, 0xF2, 0x40, 0x8F, 0xDF, 0xF4, 0x90, 0x2B, 0xB6, 0x8F, 0xBF, 0xBF, 0x7F, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0x94, 0x25, 0x34, 0x20, 0xB6, 0x7D, 0, 0xE0, 0, 0xE0, 0x0F, 0x70, makeTailByteFromPacketType(k.OBCIStreamPacketStandardAccel)]);
},
samplePacketStandardRawAux: sampleNumber => {
return new Buffer([0xA0, sampleNumberNormalize(sampleNumber), 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 1, 2, 3, 4, 5, makeTailByteFromPacketType(k.OBCIStreamPacketStandardRawAux)]);
},
samplePacketAccelTimeSyncSet: sampleNumber => {
return new Buffer([0xA0, sampleNumberNormalize(sampleNumber), 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 1, 0, 0, 0, 1, makeTailByteFromPacketType(k.OBCIStreamPacketAccelTimeSyncSet)]);
},
samplePacketAccelTimeSynced: sampleNumber => {
return new Buffer([0xA0, sampleNumberNormalize(sampleNumber), 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 1, 0, 0, 0, 1, makeTailByteFromPacketType(k.OBCIStreamPacketAccelTimeSynced)]);
},
samplePacketRawAuxTimeSyncSet: sampleNumber => {
return new Buffer([0xA0, sampleNumberNormalize(sampleNumber), 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0x00, 0x01, 0, 0, 0, 1, makeTailByteFromPacketType(k.OBCIStreamPacketRawAuxTimeSyncSet)]);
},
samplePacketRawAuxTimeSynced: sampleNumber => {
return new Buffer([0xA0, sampleNumberNormalize(sampleNumber), 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0x00, 0x01, 0, 0, 0, 1, makeTailByteFromPacketType(k.OBCIStreamPacketRawAuxTimeSynced)]);
},
samplePacketUserDefined: () => {
return new Buffer([0xA0, 0x00, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, makeTailByteFromPacketType(k.OBCIStreamPacketUserDefinedType)]);
},
makeDaisySampleObject,
getChannelDataArray,
isEven,
isOdd,
countADSPresent,
doesBufferHaveEOT,
findV2Firmware,
isFailureInBuffer,
isSuccessInBuffer,
isTimeSyncSetConfirmationInBuffer,
makeTailByteFromPacketType,
isStopByte,
newSyncObject
};
module.exports = sampleModule;
function newImpedanceObject(channelNumber) {
return {
channel: channelNumber,
P: {
raw: -1,
text: k.OBCIImpedanceTextInit
},
N: {
raw: -1,
text: k.OBCIImpedanceTextInit
}
}
}
function newSyncObject() {
return {
boardTime: 0,
correctedTransmissionTime: false,
timeSyncSent: 0,
timeSyncSentConfirmation: 0,
timeSyncSetPacket: 0,
timeRoundTrip: 0,
timeTransmission: 0,
timeOffset: 0,
valid: false
}
}
/**
* @description This method parses a 33 byte OpenBCI V3 packet and converts to a sample object
* @param dataBuf - 33 byte packet that has bytes:
* 0:[startByte] | 1:[sampleNumber] | 2:[Channel-1.1] | 3:[Channel-1.2] | 4:[Channel-1.3] | 5:[Channel-2.1] | 6:[Channel-2.2] | 7:[Channel-2.3] | 8:[Channel-3.1] | 9:[Channel-3.2] | 10:[Channel-3.3] | 11:[Channel-4.1] | 12:[Channel-4.2] | 13:[Channel-4.3] | 14:[Channel-5.1] | 15:[Channel-5.2] | 16:[Channel-5.3] | 17:[Channel-6.1] | 18:[Channel-6.2] | 19:[Channel-6.3] | 20:[Channel-7.1] | 21:[Channel-7.2] | 22:[Channel-7.3] | 23:[Channel-8.1] | 24:[Channel-8.2] | 25:[Channel-8.3] | 26:[Aux-1.1] | 27:[Aux-1.2] | 28:[Aux-2.1] | 29:[Aux-2.2] | 30:[Aux-3.1] | 31:[Aux-3.2] | 32:StopByte
* @param channelSettingsArray {Array} - An array of channel settings that is an Array that has shape similar to the one
* calling OpenBCIConstans.channelSettingsArrayInit(). The most important rule here is that it is
* Array of objects that have key-value pair {gain:NUMBER}
* @returns {Promise} - Fulfilled with a sample object that has form:
* {
* channelData: Array of floats
* accelData: Array of floats of accel data
* sampleNumber: a Number that is the sample
* }
*/
function parsePacketStandardAccel(dataBuf, channelSettingsArray) {
return new Promise((resolve, reject) => {
if (dataBuf.byteLength != k.OBCIPacketSize) reject(`Error [parsePacketStandardAccel]: input buffer must be ${k.OBCIPacketSize} bytes!`);
var sampleObject = {};
// Need build the standard sample object
getDataArrayAccel(dataBuf.slice(k.OBCIPacketPositionStartAux,k.OBCIPacketPositionStopAux+1))
.then(accelData => {
sampleObject.accelData = accelData;
return getChannelDataArray(dataBuf, channelSettingsArray);
})
.then(channelSettingArray => {
sampleObject.channelData = channelSettingArray;
// Get the raw aux values
if (k.getVersionNumber(process.version) >= 6) {
// From introduced in node version 6.x.x
sampleObject.auxData = Buffer.from(dataBuf.slice(k.OBCIPacketPositionStartAux,k.OBCIPacketPositionStopAux+1));
} else {
sampleObject.auxData = new Buffer(dataBuf.slice(k.OBCIPacketPositionStartAux,k.OBCIPacketPositionStopAux+1));
}
// Get the sample number
sampleObject.sampleNumber = dataBuf[k.OBCIPacketPositionSampleNumber];
// Get the start byte
sampleObject.startByte = dataBuf[0];
// Get the stop byte
sampleObject.stopByte = dataBuf[k.OBCIPacketPositionStopByte];
resolve(sampleObject);
})
.catch(err => {
console.log(err);
reject(err);
});
});
}
/**
* @description This method parses a 33 byte OpenBCI V3 packet and converts to a sample object
* @param dataBuf - 33 byte packet that has bytes:
* 0:[startByte] | 1:[sampleNumber] | 2:[Channel-1.1] | 3:[Channel-1.2] | 4:[Channel-1.3] | 5:[Channel-2.1] | 6:[Channel-2.2] | 7:[Channel-2.3] | 8:[Channel-3.1] | 9:[Channel-3.2] | 10:[Channel-3.3] | 11:[Channel-4.1] | 12:[Channel-4.2] | 13:[Channel-4.3] | 14:[Channel-5.1] | 15:[Channel-5.2] | 16:[Channel-5.3] | 17:[Channel-6.1] | 18:[Channel-6.2] | 19:[Channel-6.3] | 20:[Channel-7.1] | 21:[Channel-7.2] | 22:[Channel-7.3] | 23:[Channel-8.1] | 24:[Channel-8.2] | 25:[Channel-8.3] | 26:[Aux-1.1] | 27:[Aux-1.2] | 28:[Aux-2.1] | 29:[Aux-2.2] | 30:[Aux-3.1] | 31:[Aux-3.2] | 32:StopByte
* @param channelSettingsArray - An array of channel settings that is an Array that has shape similar to the one
* calling OpenBCIConstans.channelSettingsArrayInit(). The most important rule here is that it is
* Array of objects that have key-value pair {gain:NUMBER}
* @returns {Promise} - Fulfilled with a sample object that has form:
* {
* channelData: Array of floats
* auxData: 6 byte long buffer of raw aux data
* sampleNumber: a Number that is the sample
* }
*/
function parsePacketStandardRawAux(dataBuf, channelSettingsArray) {
return new Promise((resolve, reject) => {
if (dataBuf.byteLength != k.OBCIPacketSize) reject("Error [parsePacketStandardAccel]: input buffer must be " + k.OBCIPacketSize + " bytes!");
var sampleObject = {};
// Need build the standard sample object
getChannelDataArray(dataBuf, channelSettingsArray)
.then(channelSettingArray => {
// Slice the buffer for the aux data
if (k.getVersionNumber(process.version) >= 6) {
// From introduced in node version 6.x.x
sampleObject.auxData = Buffer.from(dataBuf.slice(k.OBCIPacketPositionStartAux,k.OBCIPacketPositionStopAux+1));
} else {
sampleObject.auxData = new Buffer(dataBuf.slice(k.OBCIPacketPositionStartAux,k.OBCIPacketPositionStopAux+1));
}
// Store the channel data
sampleObject.channelData = channelSettingArray;
// Get the sample number
sampleObject.sampleNumber = dataBuf[k.OBCIPacketPositionSampleNumber];
// Get the start byte
sampleObject.startByte = dataBuf[0];
// Get the stop byte
sampleObject.stopByte = dataBuf[k.OBCIPacketPositionStopByte];
resolve(sampleObject);
})
.catch(err => {
//console.log(err);
reject(err);
});
});
}
/**
* @description Grabs an accel value from a raw but time synced packet. Important that this utilizes the fact that:
* X axis data is sent with every sampleNumber % 10 === 0
* Y axis data is sent with every sampleNumber % 10 === 1
* Z axis data is sent with every sampleNumber % 10 === 2
* @param dataBuf {Buffer} - The 33byte raw time synced accel packet
* @param channelSettingsArray {Array} - An array of channel settings that is an Array that has shape similar to the one
* calling OpenBCIConstans.channelSettingsArrayInit(). The most important rule here is that it is
* Array of objects that have key-value pair {gain:NUMBER}
* @param boardOffsetTime {Number} - The difference between board time and current time calculated with sync methods.
* @param accelArray {Array} - A 3 element array that allows us to have inter packet memory of x and y axis data and emit only on the z axis packets.
* @returns {Promise} - Fulfills with a sample object. NOTE: Only has accelData if this is a Z axis packet.
*/
function parsePacketTimeSyncedAccel(dataBuf,channelSettingsArray,boardOffsetTime,accelArray) {
// Ths packet has 'A0','00'....,'AA','AA','FF','FF','FF','FF','C4'
// where the 'AA's form an accel 16bit num and 'FF's form a 32 bit time in ms
return new Promise((resolve, reject) => {
// The sample object we are going to build
var sampleObject = {};
if (dataBuf.byteLength != k.OBCIPacketSize) reject("Error [parsePacketTimeSyncedAccel]: input buffer must be " + k.OBCIPacketSize + " bytes!");
// Get the sample number
sampleObject.sampleNumber = dataBuf[k.OBCIPacketPositionSampleNumber];
// Get the start byte
sampleObject.startByte = dataBuf[0];
// Get the stop byte
sampleObject.stopByte = dataBuf[k.OBCIPacketPositionStopByte];
getFromTimePacketTime(dataBuf)
.then(boardTime => {
sampleObject.boardTime = boardTime;
sampleObject.timeStamp = boardTime + boardOffsetTime;
return getFromTimePacketRawAux(dataBuf);
})
.then(auxDataBuffer => {
sampleObject.auxData = auxDataBuffer;
return getFromTimePacketAccel(dataBuf, accelArray);
})
.then(accelArrayFilled => {
if (accelArrayFilled) {
sampleObject.accelData = accelArray;
}
return getChannelDataArray(dataBuf, channelSettingsArray);
})
.then(channelDataArray => {
sampleObject.channelData = channelDataArray;
resolve(sampleObject);
})
.catch(err => {
reject(err);
});
});
}
/**
* @description Grabs an accel value from a raw but time synced packet. Important that this utilizes the fact that:
* X axis data is sent with every sampleNumber % 10 === 0
* Y axis data is sent with every sampleNumber % 10 === 1
* Z axis data is sent with every sampleNumber % 10 === 2
* @param dataBuf {Buffer} - The 33byte raw time synced accel packet
* @param channelSettingsArray {Array} - An array of channel settings that is an Array that has shape similar to the one
* calling OpenBCIConstans.channelSettingsArrayInit(). The most important rule here is that it is
* Array of objects that have key-value pair {gain:NUMBER}
* @param boardOffsetTime {Number} - The difference between board time and current time calculated with sync methods.
* @returns {Promise} - Fulfills with a sample object. NOTE: The aux data is placed in a 2 byte buffer
*/
function parsePacketTimeSyncedRawAux(dataBuf,channelSettingsArray,boardOffsetTime) {
// Ths packet has 'A0','00'....,'AA','AA','FF','FF','FF','FF','C4'
// where the 'AA's form an accel 16bit num and 'FF's form a 32 bit time in ms
return new Promise((resolve, reject) => {
// The sample object we are going to build
var sampleObject = {};
if (dataBuf.byteLength != k.OBCIPacketSize) reject("Error [parsePacketTimeSyncedRawAux]: input buffer must be " + k.OBCIPacketSize + " bytes!");
// Get the sample number
sampleObject.sampleNumber = dataBuf[k.OBCIPacketPositionSampleNumber];
// Get the start byte
sampleObject.startByte = dataBuf[0];
// Get the stop byte
sampleObject.stopByte = dataBuf[k.OBCIPacketPositionStopByte];
getFromTimePacketTime(dataBuf)
.then(boardTime => {
sampleObject.boardTime = boardTime;
sampleObject.timeStamp = boardTime + boardOffsetTime;
return getFromTimePacketRawAux(dataBuf);
})
.then(auxDataBuffer => {
sampleObject.auxData = auxDataBuffer;
return getChannelDataArray(dataBuf, channelSettingsArray);
})
.then(channelDataArray => {
sampleObject.channelData = channelDataArray;
resolve(sampleObject);
})
.catch(err => {
reject(err);
});
});
}
/**
* @description Extract a time from a time packet in ms.
* @param dataBuf - A raw packet with 33 bytes of data
* @returns {Promise} - Fulfills with time in milli seconds
* @author AJ Keller (@pushtheworldllc)
*/
function getFromTimePacketTime(dataBuf) {
// Ths packet has 'A0','00'....,'00','00','FF','FF','FF','FF','C3' where the 'FF's are times
const lastBytePosition = k.OBCIPacketSize - 1; // This is 33, but 0 indexed would be 32 minus 1 for the stop byte and another two for the aux channel or the
return new Promise((resolve, reject) => {
if (dataBuf.byteLength != k.OBCIPacketSize) reject("Error [getFromTimePacketTime]: input buffer must be " + k.OBCIPacketSize + " bytes!");
// Grab the time from the packet
resolve(dataBuf.readUInt32BE(lastBytePosition - k.OBCIStreamPacketTimeByteSize));
});
}
/**
* @description Grabs an accel value from a raw but time synced packet. Important that this utilizes the fact that:
* X axis data is sent with every sampleNumber % 10 === 7
* Y axis data is sent with every sampleNumber % 10 === 8
* Z axis data is sent with every sampleNumber % 10 === 9
* @param dataBuf {Buffer} - The 33byte raw time synced accel packet
* @param accelArray {Array} - A 3 element array that allows us to have inter packet memory of x and y axis data and emit only on the z axis packets.
* @returns {Promise} - Fulfills with a boolean that is true only when the accel array is ready to be emitted... i.e. when this is a Z axis packet
*/
function getFromTimePacketAccel(dataBuf, accelArray) {
const accelNumBytes = 2;
const lastBytePosition = k.OBCIPacketSize - 1 - k.OBCIStreamPacketTimeByteSize - accelNumBytes; // This is 33, but 0 indexed would be 32 minus
return new Promise((resolve, reject) => {
if (dataBuf.byteLength != k.OBCIPacketSize) reject("Error [getFromTimePacketAccel]: input buffer must be " + k.OBCIPacketSize + " bytes!");
var sampleNumber = dataBuf[k.OBCIPacketPositionSampleNumber];
switch (sampleNumber % 10) { // The accelerometer is on a 25Hz sample rate, so every ten channel samples, we can get new data
case k.OBCIAccelAxisX:
accelArray[0] = sampleModule.interpret16bitAsInt32(dataBuf.slice(lastBytePosition, lastBytePosition + 2)) * SCALE_FACTOR_ACCEL; // slice is not inclusive on the right
resolve(false);
break;
case k.OBCIAccelAxisY:
accelArray[1] = sampleModule.interpret16bitAsInt32(dataBuf.slice(lastBytePosition, lastBytePosition + 2)) * SCALE_FACTOR_ACCEL; // slice is not inclusive on the right
resolve(false);
break;
case k.OBCIAccelAxisZ:
accelArray[2] = sampleModule.interpret16bitAsInt32(dataBuf.slice(lastBytePosition, lastBytePosition + 2)) * SCALE_FACTOR_ACCEL; // slice is not inclusive on the right
resolve(true);
break;
default:
resolve(false);
break;
}
});
}
/**
* @description Grabs a raw aux value from a raw but time synced packet.
* @param dataBuf {Buffer} - The 33byte raw time synced raw aux packet
* @returns {Promise} - Fulfills a 2 byte buffer
*/
function getFromTimePacketRawAux(dataBuf) {
return new Promise((resolve, reject) => {
if (dataBuf.byteLength != k.OBCIPacketSize) reject("Error [getFromTimePacketRawAux]: input buffer must be " + k.OBCIPacketSize + " bytes!");
if (k.getVersionNumber(process.version) >= 6) {
resolve(Buffer.from(dataBuf.slice(k.OBCIPacketPositionTimeSyncAuxStart, k.OBCIPacketPositionTimeSyncAuxStop)));
} else {
resolve(new Buffer(dataBuf.slice(k.OBCIPacketPositionTimeSyncAuxStart, k.OBCIPacketPositionTimeSyncAuxStop)));
}
});
}
/**
* @description Takes a buffer filled with 3 16 bit integers from an OpenBCI device and converts based on settings
* of the MPU, values are in ?
* @param dataBuf - Buffer that is 6 bytes long
* @returns {Promise} - Fulfilled with Array of floats 3 elements long
* @author AJ Keller (@pushtheworldllc)
*/
function getDataArrayAccel(dataBuf) {
return new Promise(resolve => {
var accelData = [];
for (var i = 0; i < ACCEL_NUMBER_AXIS; i++) {
var index = i * 2;
accelData.push(sampleModule.interpret16bitAsInt32(dataBuf.slice(index, index + 2)) * SCALE_FACTOR_ACCEL);
}
resolve(accelData);
});
}
/**
* @description Takes a buffer filled with 24 bit signed integers from an OpenBCI device with gain settings in
* channelSettingsArray[index].gain and converts based on settings of ADS1299... spits out an
* array of floats in VOLTS
* @param dataBuf {Buffer} - Buffer with 33 bit signed integers, number of elements is same as channelSettingsArray.length * 3
* @param channelSettingsArray {Array} - The channel settings array, see OpenBCIConstants.channelSettingsArrayInit() for specs
* @returns {Promise} - Fulfilled with Array filled with floats for each channel's voltage in VOLTS
* @author AJ Keller (@pushtheworldllc)
*/
function getChannelDataArray(dataBuf, channelSettingsArray) {
return new Promise((resolve, reject) => {
if (!Array.isArray(channelSettingsArray)) reject('Error [getChannelDataArray]: Channel Settings must be an array!');
var channelData = [];
// Grab the sample number from the buffer
var sampleNumber = dataBuf[k.OBCIPacketPositionSampleNumber];
var daisy = channelSettingsArray.length > k.OBCINumberOfChannelsDefault;
// Channel data arrays are always 8 long
for (var i = 0; i < k.OBCINumberOfChannelsDefault; i++) {
if (!channelSettingsArray[i].hasOwnProperty("gain")) reject(`Error [getChannelDataArray]: Invalid channel settings object at index ${i}`);
if (!k.isNumber(channelSettingsArray[i].gain)) reject('Error [getChannelDataArray]: Property gain of channelSettingsObject not or type Number');
var scaleFactor = 0;
if(isEven(sampleNumber) && daisy) {
scaleFactor = ADS1299_VREF / channelSettingsArray[i + k.OBCINumberOfChannelsDefault].gain / (Math.pow(2,23) - 1);
} else {
scaleFactor = ADS1299_VREF / channelSettingsArray[i].gain / (Math.pow(2,23) - 1);
}
// Convert the three byte signed integer and convert it
channelData.push(scaleFactor * sampleModule.interpret24bitAsInt32(dataBuf.slice((i * 3) + k.OBCIPacketPositionChannelDataStart, (i * 3) + k.OBCIPacketPositionChannelDataStart + 3)));
}
resolve(channelData);
});
}
function getRawPacketType(stopByte) {
return stopByte & 0xF;
}
/**
* @description This method is useful for normalizing sample numbers for fake sample packets. This is intended to be
* useful for the simulator and automated testing.
* @param sampleNumber {Number} - The sample number you want to assign to the packet
* @returns {Number} - The normalized input `sampleNumber` between 0-255
*/
function sampleNumberNormalize(sampleNumber) {
if (sampleNumber || sampleNumber === 0) {
if (sampleNumber > 255) {
sampleNumber = 255;
}
} else {
sampleNumber = 0x45;
}
return sampleNumber;
}
function newSample(sampleNumber) {
if (sampleNumber || sampleNumber === 0) {
if (sampleNumber > 255) {
sampleNumber = 255;
}
} else {
sampleNumber = 0;
}
return {
startByte: k.OBCIByteStart,
sampleNumber:sampleNumber,
channelData: [],
accelData: [],
auxData: null,
stopByte: k.OBCIByteStop,
boardTime: 0,
timeStamp: 0
}
}
/**
* @description Convert float number into three byte buffer. This is the opposite of .interpret24bitAsInt32()
* @param float - The number you want to convert
* @returns {Buffer} - 3-byte buffer containing the float
*/
function floatTo3ByteBuffer(float) {
var intBuf = new Buffer(3); // 3 bytes for 24 bits
intBuf.fill(0); // Fill the buffer with 0s
var temp = float / ( ADS1299_VREF / 24 / (Math.pow(2,23) - 1)); // Convert to counts
temp = Math.floor(temp); // Truncate counts number
// Move into buffer
intBuf[2] = temp & 255;
intBuf[1] = (temp & (255 << 8)) >> 8;
intBuf[0] = (temp & (255 << 16)) >> 16;
return intBuf;
}
/**
* @description Convert float number into three byte buffer. This is the opposite of .interpret24bitAsInt32()
* @param float - The number you want to convert
* @returns {Buffer} - 3-byte buffer containing the float
*/
function floatTo2ByteBuffer(float) {
var intBuf = new Buffer(2); // 2 bytes for 16 bits
intBuf.fill(0); // Fill the buffer with 0s
var temp = float / SCALE_FACTOR_ACCEL; // Convert to counts
temp = Math.floor(temp); // Truncate counts number
//console.log('Num: ' + temp);
// Move into buffer
intBuf[1] = temp & 255;
intBuf[0] = (temp & (255 << 8)) >> 8;
return intBuf;
}
/**
* @description Used to make one sample object from two sample objects. The sample number of the new daisy sample will
* be the upperSampleObject's sample number divded by 2. This allows us to preserve consecutive sample numbers that
* flip over at 127 instead of 255 for an 8 channel. The daisySampleObject will also have one `channelData` array
* with 16 elements inside it, with the lowerSampleObject in the lower indices and the upperSampleObject in the
* upper set of indices. The auxData from both channels shall be captured in an object called `auxData` which
* contains two arrays referenced by keys `lower` and `upper` for the `lowerSampleObject` and `upperSampleObject`,
* respectively. The timestamps shall be averaged and moved into an object called `timestamp`. Further, the
* un-averaged timestamps from the `lowerSampleObject` and `upperSampleObject` shall be placed into an object called
* `_timestamps` which shall contain two keys `lower` and `upper` which contain the original timestamps for their
* respective sampleObjects.
* @param lowerSampleObject {Object} - Lower 8 channels with odd sample number
* @param upperSampleObject {Object} - Upper 8 channels with even sample number
* @returns {Object} - The new merged daisy sample object
*/
function makeDaisySampleObject(lowerSampleObject,upperSampleObject) {
var daisySampleObject= {};
daisySampleObject["channelData"] = lowerSampleObject.channelData.concat(upperSampleObject.channelData);
daisySampleObject["sampleNumber"] = Math.floor(upperSampleObject.sampleNumber / 2);
daisySampleObject["auxData"] = {
"lower" : lowerSampleObject.auxData,
"upper" : upperSampleObject.auxData
};
daisySampleObject["timestamp"] = (lowerSampleObject.timestamp + upperSampleObject.timestamp) / 2;
daisySampleObject["_timestamps"] = {
"lower" : lowerSampleObject.timestamp,
"upper" : upperSampleObject.timestamp
};