Algorithm of emotional states
Description
The algorithm processes the data by a sliding window of a given length with a given frequency. If artifacts are detected on one of the bipolar channels, the artifacts on the second bipolar channel are checked, and if there are no artifacts, they are switched to that channel; in case of artifacts on both channels, the spectral values and values of mental levels are filled with previous actual values, while the counter of the number of successive artifact windows increases.
Artifacts
When the maximum number of consecutive artifact windows is reached, math.is_artifacted_sequence()
returns true, which allows you to give the user information about the need to check the position of the device. This flag is usually raised 4 sec after receiving continuous artifacts. If there is no need to give notification of momentary artifacts, you can use this function as the primary for artifact notifications. Otherwise, use math.is_both_sides_artifacted()
to check for momentary artifacts, returning true for artifacts on both bipolar channels for the current window.
Emotional states
The estimate of emotional states (mental levels - relaxation and concentration) is available in two variants:
- immediate assessment through alpha and beta wave intensity (and theta in the case of independent assessment).
- relative to the baseline calibration values of alpha and beta wave intensity
In both cases, the current intensity of the waves is defined as the average for the last N windows.
The algorithm starts processing the data after the first N seconds after connecting the device and when the minimum number of points for the spectrum calculation is accumulated.
When reading spectral and mental values an array of appropriate structures (SpectralDataPercents
and MindData
) of length is returned, which is determined by the number of new recorded points, signal frequency and analysis frequency.
In this version the filters are built-in and clearly defined: BandStop_45_55, BandStop_55_65, BandStop_62, HighPass_10, LowPass_30
Calibration
According to the results of calibration, the average base value of alpha and beta waves expression is determined in percent, which are further used to calculate the relative mental levels.
Library mode
The library can operate in two modes - bipolar and multichannel. In bipolar mode only two channels are processed - left and right bipolar. In multichannel mode you can process for any number of channels using the same algorithms.
Parameters
Main parameters description
Structure MathLibSettings
with fields:
- samplingRate - raw signal sampling frequency, Hz, integer value
- processWinFreq - frequency of spectrum analysis and emotional levels, Hz, integer value
- fftWindow - spectrum calculation window length, integer value
- nFirstSecSkipped - skipping the first seconds after connecting to the device, integer value
- bipolarMode - enabled bipolar mode, boolean value
- squaredSpectrum - mode of calculating spectral values of frequencies. If squared = true then values are calculated as the sum of squares of FFT bins of the spectrum of the interval of the corresponding frequency (e.g. alpha), if squared = false then as the sum of FFT bins.
- channelsNumber - count channels for multy-channel library mode, integer value
- channelForAnalysis - in case of multichannel mode: channel by default for computing spectral values and emotional levels, integer value
channelsNumber
and channelForAnalysis
are not used explicitly for bipolar mode, you can leave the default ones.
Separate parameters:
- MentalEstimationMode - type of evaluation of instant mental levels - disabled by default, boolean value
- SpectNormalizationByBandsWidth - spectrum normalization by bandwidth - disabled by default, boolean value
Artifact detection parameters description
Structure ArtifactDetectSetting
with fields:
- artBord - boundary for the long amplitude artifact, mcV, integer value
- allowedPercentArtpoints - percent of allowed artifact points in the window, integer value
- rawBetapLimit - boundary for spectral artifact (beta power), detection of artifacts on the spectrum occurs by checking the excess of the absolute value of the raw beta wave power, integer value
- totalPowBorder - boundary for spectral artifact (in case of assessment by total power) and for channels signal quality estimation, integer value
- globalArtwinSec - number of seconds for an artifact sequence, the maximum number of consecutive artifact windows (on both channels) before issuing a prolonged artifact notification / device position check, integer value
- spectArtByTotal - assessment of spectral artifacts by total power, boolean value
- hanningWinSpectrum - setting the smoothing of the spectrum calculation by Hamming, boolean value
- hammingWinSpectrum - setting the smoothing of the spectrum calculation by Henning, boolean value
- numWinsForQualityAvg - number of windows for estimation of signals quality, by default = 100, which, for example, with process_win_freq=25Hz, will be equal to 4 seconds, integer value
Structure ShortArtifactDetectSetting
with fields:
- amplArtDetectWinSize - the length of the sliding window segments for the detection of short-term amplitude artifacts, ms, integer value
- amplArtZerodArea - signal replacement area of the previous non-artefact to the left and right of the extremum point, ms, integer value
- amplArtExtremumBorder - boundary for the extremum considered to be artifactual, mcV, integer value
Structure MentalAndSpectralSetting
with fields:
- nSecForInstantEstimation - the number of seconds to calculate the values of mental levels, integer value
- nSecForAveraging - spectrum averaging, integer value
Separate setting is the number of windows after the artifact with the previous actual value - to smooth the switching process after artifacts (SkipWinsAfterArtifact
).
Initialization
Main parameters
final mls = MathLibSetting(samplingRate: 250,
processWinFreq: 25,
nSirstSecSkipped: 4,
fftWindow=1000,
bipolarMode: true,
squaredSpectrum: true,
channelsNumber: 4,
channelForAnalysis: 0);
final ads = ArtifactDetectSetting(artBord: 110,
allowedPercentArtpoints: 70,
rawBetapLimit: 800000,
globalArtwinSec: 4,
numWinsForQualityAvg: 125,
hammingWinSpectrum: true,
hanningWinSpectrum: false,
totalPowBorder: 400000000,
spectArtByTotalp: true);
final sads = ShortArtifactDetectSetting(amplArtDetectWinSize: 200,
amplArtZerodArea: 200,
amplArtExtremumBorder: 25);
final mss = MentalAndSpectralSetting(nSecForAveraging: 2,
nSecForInstantEstimation: 4);
EmotionalMath math = EmotionalMath(mls, ads, sads, mss);
Optional parameters
// setting calibration length
int calibrationLength = 6;
math.setCalibrationLength(calibrationLength);
// type of evaluation of instant mental levels
bool independentMentaLevels = false;
math.setMentalEstimationMode(independentMentaLevels);
// number of windows after the artifact with the previous actual value
int nwinsSkipAfterArtifact = 10;
math.setSkipWinsAfterArtifact(nwinsSkipAfterArtifact);
// calculation of mental levels relative to calibration values
math.setZeroSpectWaves(true, 0, 1, 1, 1, 0);
// spectrum normalization by bandwidth
math.setSpectNormalizationByBandsWidth(True);
Types
RawChannels
Structure contains left and right bipolar values to bipolar library mode with fields:
- leftBipolar - left bipolar value, double value
- rightBipolar - right bipolar value, double value
MindData
Mental levels. Struct with fields:
- relAttention - relative attention value
- reRelaxation - relative relaxation value
- instAttention - instantiate attention value
- instRelaxation - instantiate relaxation value
SpectralDataPercents
Relative spectral values. Struct with double fields:
- delta
- theta
- alpha
- beta
- gamma
SideType
Side of current artufact. Enum with values:
- LEFT
- RIGHT
- NONE
Usage
- If you need calibration start calibration right after library init:
math.startCalibration();
- Adding and process data In bipolar mode:
StreamSubscription<List<BrainBitSignalData>>? signalSubscr =
sensor.signalDataStream
.listen((event) {
final samples = event.map((data) => RawChanel(leftBipolar: data.t3 - data.o1, rightBipolar: data.t4 - data.o2)).toList();
_math.pushBipolars(samples);
_math.processData();
}));
In multy-channel mode:
final samples = List<List<double>>.generate(size, (i) => [0, 0, 0, 0]);
math.pushMonopolars(samples);
math.processData();
- Then check calibration status if you need to calibrate values:
bool calibrationFinished = math.isCalibrationFinished();
// and calibration progress
int calibrationProgress = math.getCalibrationPercents();
- If calibration finished (or you don't need to calibrate) read output values:
// Reading mental levels in percent
List<MindData> mentalData = math.readMentalData();
// Reading relative spectral values in percent
List<SpectralDataPercents> spData = math.readSpectralDataPercents();
- Check artifacts 4.1. During calibration
if(math.isBothSidesArtefacted())
// signal corruption
4.2. After (without) calibration
if(math.isArtifactedSequence())
// signal corruption
Finishing work with the library
math.dispose();
Libraries
- bindings/artifacts_bindings_generated
- core/artifacts_detect_setting
- core/artifacts_exception
- core/artifacts_library
- core/bindings_factory
- core/math_lib_settings
- core/mental_and_spectral_setting
- core/mind_data
- core/raw_chanel
- core/raw_spectral_values
- core/short_artifacts_detect_setting
- core/spectral_data_percents
- em_st_artifacts
- extensions/native_artifact_detect_setting_with_managed_extension
- extensions/native_math_lib_setting_with_managed_extension
- extensions/native_mental_and_spectral_setting
- extensions/native_mind_data_with_managed
- extensions/native_raw_chanels_with_managed_extension
- extensions/native_raw_spectral_values_with_managed_extension
- extensions/native_short_artifact_detect_setting_with_managed_extension
- extensions/native_spectral_data_percents_with_managed_extension
- extensions/op_status_exceptions