Package: pyem-st-artifacts
Algorithm of emotional statesDescriptionThe 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.ArtifactsWhen the maximum number of consecutive artifact windows is reached,MathLibIsArtifactedSequence()returns true, which allows you to give the user information about the need to check the position of the device. If there is no need to give notification of momentary artifacts, you can use this function as the primary for artifact notifications. Otherwise, useMathLibIsBothSidesArtifacted()to check for momentary artifacts, returning true for artifacts on both bipolar channels for the current window.Emotional statesThe 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 intensityIn 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 (SpectralDataPercentsandMindData) 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_30CalibrationAccording 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 modeThe 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.ParametersMain parameters descriptionStructureMathLibSettingswith fields:sampling_rate - raw signal sampling frequency, Hz, integer valueprocess_win_freq - frequency of spectrum analysis and emotional levels, Hz, integer valuefft_window - spectrum calculation window length, integer valuen_first_sec_skipped - skipping the first seconds after connecting to the device, integer valuebipolar_mode - enabled bipolar mode, boolean valuesquared_spectrumchannels_number - count channels for multy-channel library mode, integer valuechannel_for_analysis - in case of multichannel mode: channel by default for computing spectral values and emotional levels, integer valuechannels_numberandchannel_for_analysisare 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 valueSpectNormalizationByBandsWidth - spectrum normalization by bandwidth - disabled by default, boolean valueArtifact detection parameters descriptionStructureArtifactDetectSettingwith fields:art_bord - boundary for the long amplitude artifact, mcV, integer valueallowed_percent_artpoints - percent of allowed artifact points in the window, integer valueraw_betap_limit - 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 valuetotal_pow_border - boundary for spectral artifact (in case of assessment by total power) and for channels signal quality estimation, integer valueglobal_artwin_sec - 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 valuespect_art_by_totalp - assessment of spectral artifacts by total power, boolean valuehanning_win_spectrum - setting the smoothing of the spectrum calculation by Hamming, boolean valuehamming_win_spectrum - setting the smoothing of the spectrum calculation by Henning, boolean valuenum_wins_for_quality_avg - 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 valueStructureShortArtifactDetectSettingwith fields:ampl_art_detect_win_size - the length of the sliding window segments for the detection of short-term amplitude artifacts, ms, integer valueampl_art_zerod_area - signal replacement area of the previous non-artefact to the left and right of the extremum point, ms, integer valueampl_art_extremum_border - boundary for the extremum considered to be artifactual, mcV, integer valueStructureMentalAndSpectralSettingwith fields:n_sec_for_instant_estimation - the number of seconds to calculate the values of mental levels, integer valuen_sec_for_averaging - spectrum averaging, integer valueSeparate setting is the number of windows after the artifact with the previous actual value - to smooth the switching process after artifacts (SkipWinsAfterArtifact).InitializationMain parametersPythonmls=MathLibSetting(sampling_rate=250,process_win_freq=25,fft_window=1000,n_first_sec_skipped=4,bipolar_mode=True,squared_spectrum=True,channels_number=4,channel_for_analysis=0)ads=ArtifactDetectSetting(hanning_win_spectrum=True,num_wins_for_quality_avg=125)sads=ShortArtifactDetectSetting(ampl_art_extremum_border=25)mss=MentalAndSpectralSetting()emotions=EmotionalMath(mls,ads,sads,mss)Optional parametersPython# setting calibration lengthcalibration_length=8math.set_calibration_length(calibration_length)# type of evaluation of instant mental levelsindependent_mental_levels=Falsemath.set_mental_estimation_mode(independent_mental_levels)# number of windows after the artifact with the previous actual valuenwins_skip_after_artifact=10math.set_skip_wins_after_artifact(nwins_skip_after_artifact)# calculation of mental levels relative to calibration valuesmath.set_zero_spect_waves(True,0,1,1,1,0)# spectrum normalization by bandwidthmath.set_spect_normalization_by_bands_width(True)TypesRawChannelsStructure contains left and right bipolar values to bipolar library mode with fields:LeftBipolar - left bipolar value, double valueRightBipolar - right bipolar value, double valueRawChannelsArrayStructure contains array of values of channels with field:channels - double arrayMindDataMental levels. Struct with fields:Rel_Attention - relative attention valueRel_Relaxation - relative relaxation valueInst_Attention - instantiate attention valueInst_Relaxation - instantiate relaxation valueSpectralDataPercentsRelative spectral values. Struct with double fields:DeltaThetaAlphaBetaGammaSideTypeSide of current artufact. Enum with values:LEFTRIGHTNONEUsageIf you need calibration start calibration right after library init:Pythonmath.start_calibration()Adding and process data
In bipolar mode:Pythonsamples=[]math.push_data(samples)math.process_data_arr()In multy-channel mode:Pythonsamples=[]math.push_data_arr(samples)math.process_data_arr()Then check calibration status if you need to calibrate values:Pythoncalibration_finished=math.calibration_finished()# and calibration progresscalibration_progress=math.get_calibration_percents()If calibration finished (or you don't need to calibrate) read output values:Python# Reading mental levels in percentmental_data=math.read_mental_data_arr()# Reading relative spectral values in percentsp_data=math.read_spectral_data_percents_arr()Check artifacts
4.1. During calibrationPythonifmath.is_both_sides_artifacted():# signal corruption4.2. After (without) calibrationPythonifmath.is_artifacted_sequence():# signal corruptionFinishing work with the libraryPythondelmath