Skip to content

Commit 5fcbb32

Browse files
author
AJ Keller
committed
Add impedance calculation functions from openBCI_NodeJS
1 parent 406fadc commit 5fcbb32

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

Diff for: changelog.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# 0.0.9
2+
3+
### New Features
4+
5+
* Add impedance calculation functions from cyton
6+
17
# 0.0.8
28

39
### New Features

Diff for: openBCIUtilities.js

+48
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,54 @@ var utilitiesModule = {
476476
};
477477
},
478478
scaleFactorAux: SCALE_FACTOR_ACCEL,
479+
/**
480+
* Calculate the impedance
481+
* @param sample {Object} - Standard sample
482+
* @param impedanceTest {Object} - Impedance Object from openBCIBoard.js
483+
* @return {null | Object} - Null if not enough samples have passed to calculate an accurate
484+
*/
485+
impedanceCalculateArray: (sample, impedanceTest) => {
486+
impedanceTest.buffer.push(sample.channelData);
487+
impedanceTest.count++;
488+
489+
if (impedanceTest.count >= impedanceTest.window) {
490+
let output = [];
491+
for (let i = 0; i < sample.channelData.length; i++) {
492+
let max = 0.0; // sumSquared
493+
for (let j = 0; j < impedanceTest.window; j++) {
494+
if (impedanceTest.buffer[i][j] > max) {
495+
max = impedanceTest.buffer[i][j];
496+
}
497+
}
498+
let min = 0.0;
499+
for (let j = 0; j < impedanceTest.window; j++) {
500+
if (impedanceTest.buffer[i][j] < min) {
501+
min = impedanceTest.buffer[i][j];
502+
}
503+
}
504+
const vP2P = max - min; // peak to peak
505+
506+
output.push(vP2P / 2 / k.OBCILeadOffDriveInAmps);
507+
}
508+
impedanceTest.count = 0;
509+
return output;
510+
}
511+
return null;
512+
},
513+
impedanceTestObjDefault: (impedanceTestObj) => {
514+
let newObj = impedanceTestObj || {};
515+
newObj['active'] = false;
516+
newObj['buffer'] = [];
517+
newObj['count'] = 0;
518+
newObj['isTestingPInput'] = false;
519+
newObj['isTestingNInput'] = false;
520+
newObj['onChannel'] = 0;
521+
newObj['sampleNumber'] = 0;
522+
newObj['continuousMode'] = false;
523+
newObj['impedanceForChannel'] = 0;
524+
newObj['window'] = 40;
525+
return newObj;
526+
},
479527
samplePacket: sampleNumber => {
480528
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)]);
481529
},

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "openbci-utilities",
3-
"version": "0.0.8",
3+
"version": "0.0.9",
44
"description": "The official utility package of Node.js SDK for the OpenBCI Biosensor Boards.",
55
"main": "index.js",
66
"scripts": {

Diff for: test/openBCIUtilities-test.js

+39
Original file line numberDiff line numberDiff line change
@@ -1625,6 +1625,45 @@ $$$`);
16251625
expect(openBCIUtilities.stripToEOTBuffer(totalBuf)).to.equal(null);
16261626
});
16271627
});
1628+
describe('#impedanceTestObjDefault', function () {
1629+
it('should give a new impedance object', function () {
1630+
const expectedImpedanceObj = {
1631+
active: false,
1632+
buffer: [],
1633+
count: 0,
1634+
isTestingPInput: false,
1635+
isTestingNInput: false,
1636+
onChannel: 0,
1637+
sampleNumber: 0,
1638+
continuousMode: false,
1639+
impedanceForChannel: 0,
1640+
window: 40
1641+
};
1642+
expect(openBCIUtilities.impedanceTestObjDefault()).to.deep.equal(expectedImpedanceObj);
1643+
});
1644+
});
1645+
describe('#impedanceCalculateArray', function () {
1646+
const numberOfChannels = k.OBCINumberOfChannelsDefault;
1647+
const newRandomSample = openBCIUtilities.randomSample(numberOfChannels, k.OBCISampleRate250, false, 'none');
1648+
1649+
afterEach(() => bluebirdChecks.noPendingPromises());
1650+
1651+
it('should not produce an array of impedances till window', function () {
1652+
const impTestObj = openBCIUtilities.impedanceTestObjDefault();
1653+
for (let i = 0; i < impTestObj.window - 1; i++) {
1654+
expect(openBCIUtilities.impedanceCalculateArray(newRandomSample(i), impTestObj)).to.equal(null);
1655+
}
1656+
expect(impTestObj.buffer.length).to.equal(impTestObj.window - 1);
1657+
});
1658+
it('should produce and array of impedances at window', function () {
1659+
const impTestObj = openBCIUtilities.impedanceTestObjDefault();
1660+
let impedanceArray = null;
1661+
for (let i = 0; i < impTestObj.window; i++) {
1662+
impedanceArray = openBCIUtilities.impedanceCalculateArray(newRandomSample(i), impTestObj);
1663+
}
1664+
expect(impedanceArray.length).to.equal(numberOfChannels);
1665+
});
1666+
});
16281667
});
16291668

16301669
describe('openBCIGanglionUtils', function () {

0 commit comments

Comments
 (0)