From e4857e2de52b83363dc4f7deeae9f5992a8a4671 Mon Sep 17 00:00:00 2001 From: Yannick Date: Fri, 30 Oct 2020 11:53:06 +0100 Subject: [PATCH 1/2] AbsolutePower + mapper for powerByBand --- src/pipes/frequency/absolutePower.ts | 21 +++++++++++++++++++++ src/pipes/frequency/averagePower.ts | 2 +- src/pipes/frequency/index.ts | 1 + src/pipes/frequency/powerByBand.ts | 5 +++-- 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 src/pipes/frequency/absolutePower.ts diff --git a/src/pipes/frequency/absolutePower.ts b/src/pipes/frequency/absolutePower.ts new file mode 100644 index 0000000..ccbf235 --- /dev/null +++ b/src/pipes/frequency/absolutePower.ts @@ -0,0 +1,21 @@ +import { pipe } from "rxjs"; +import { map } from "rxjs/operators"; + +import { sum } from "../../utils/stats"; +import { IPSD } from "../../types/psd"; + +/** + * Takes a stream of PSDs and returns a stream of arrays, containing the absolute power in each channel + * @method absolutePower + * @example eeg$.pipe(epoch({ duration: 256, interval: 100, samplingRate: 256 }), fft({ bins: 256 }), sliceFFT(10, 20), absolutePower()) + * @returns {Observable} band powers array + */ +export const absolutePower = () => + pipe( + map((inputPSD: IPSD) => + inputPSD.psd.reduce( + (acc, channel) => [...acc, sum(channel)], + [] + ) + ) + ); diff --git a/src/pipes/frequency/averagePower.ts b/src/pipes/frequency/averagePower.ts index 505a6db..8f3713f 100644 --- a/src/pipes/frequency/averagePower.ts +++ b/src/pipes/frequency/averagePower.ts @@ -5,7 +5,7 @@ import { average } from "../../utils/stats"; import { IPSD } from "../../types/psd"; /** - * Takes a stream of PSDs and returns a sream of arrays, containing the average power in each channel + * Takes a stream of PSDs and returns a stream of arrays, containing the average power in each channel * @method averagePower * @example eeg$.pipe(epoch({ duration: 256, interval: 100, samplingRate: 256 }), fft({ bins: 256 }), sliceFFT(10, 20), averagePower()) * @returns {Observable} band powers array diff --git a/src/pipes/frequency/index.ts b/src/pipes/frequency/index.ts index 14912cb..e7470d3 100644 --- a/src/pipes/frequency/index.ts +++ b/src/pipes/frequency/index.ts @@ -1,3 +1,4 @@ +export * from "./absolutePower"; export * from "./alphaPower"; export * from "./averagePower"; export * from "./betaPower"; diff --git a/src/pipes/frequency/powerByBand.ts b/src/pipes/frequency/powerByBand.ts index 7948ba5..3183108 100644 --- a/src/pipes/frequency/powerByBand.ts +++ b/src/pipes/frequency/powerByBand.ts @@ -11,16 +11,17 @@ import { FREQUENCY_BANDS as defaultBands } from "../../constants"; * @method powerByBand * @example eeg$.pipe(epoch({ duration: 256, interval: 100, samplingRate: 256 }), fft({ bins: 256 }), powerByBand()) * @param {Object} [bands] Custom bands object containing corresponding names and frequency ranges + * @param {Function} [powerMapper] Custom mapping function to compute band power (default = averagePower) * @returns {Observable>} */ -export const powerByBand = (bands = defaultBands) => +export const powerByBand = (bands = defaultBands, powerMapper = averagePower) => pipe( flatMap(inputPSD => { const entries = Object.entries(bands); const bandPowers = entries.map(([_, range]) => of(inputPSD).pipe( sliceFFT(range), - averagePower() + powerMapper() ) ); const zipPowers = (...powers) => From 1e156125e7adc89d236c82b5eede2ed08d98569a Mon Sep 17 00:00:00 2001 From: Yannick Date: Wed, 28 Oct 2020 16:17:03 +0100 Subject: [PATCH 2/2] Moved signalQuality under info --- package-lock.json | 12 ++++++------ package.json | 2 +- src/pipes/utility/addSignalQuality.ts | 17 ++++++++++------- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/package-lock.json b/package-lock.json index a62aa79..597d43d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10227,9 +10227,9 @@ "dev": true }, "rxjs": { - "version": "6.5.3", - "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.5.3.tgz", - "integrity": "sha512-wuYsAYYFdWTAnAaPoKGNhfpWwKZbJW+HgAJ+mImp+Epl7BG8oNWBCTyRM8gba9k4lk8BgWdoYm21Mo/RYhhbgA==", + "version": "6.6.3", + "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.6.3.tgz", + "integrity": "sha512-trsQc+xYYXZ3urjOiJOuCOa5N3jAZ3eiSpQB5hIT8zGlL2QfnHLJ2r7GMkBGuIausdJN1OneaI6gQlsqNHHmZQ==", "requires": { "tslib": "^1.9.0" } @@ -11690,9 +11690,9 @@ } }, "tslib": { - "version": "1.10.0", - "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.10.0.tgz", - "integrity": "sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ==" + "version": "1.14.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz", + "integrity": "sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==" }, "tty-browserify": { "version": "0.0.0", diff --git a/package.json b/package.json index 8569dc9..6e649d9 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "dependencies": { "dsp.js": "^1.0.1", "fili": "^2.0.1", - "rxjs": "^6.3.1" + "rxjs": "^6.6.3" }, "devDependencies": { "@types/node": "^10.12.10", diff --git a/src/pipes/utility/addSignalQuality.ts b/src/pipes/utility/addSignalQuality.ts index 81401e2..af60adb 100644 --- a/src/pipes/utility/addSignalQuality.ts +++ b/src/pipes/utility/addSignalQuality.ts @@ -21,13 +21,16 @@ export const addSignalQuality = ({ dataProp = defaultDataProp } = {}) => : epoch[dataProp].map((_, i) => i); return { ...epoch, - signalQuality: epoch[dataProp].reduce( - (acc, curr, index) => ({ - ...acc, - [names[index]]: standardDeviation(curr) - }), - {} - ) + info: { + ...epoch.info, + signalQuality: epoch[dataProp].reduce( + (acc, curr, index) => ({ + ...acc, + [names[index]]: standardDeviation(curr) + }), + {} + ) + } }; }) );