Math Utils
Mathematical utility functions for signal analysis.
This module provides mathematical functions used throughout the PyEyesWeb library for signal processing, phase analysis, and movement metrics.
center_signals(sig)
Remove the mean from each signal to center the data.
Centers signals by subtracting the mean, removing DC bias.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sig
|
ndarray
|
Signal array of shape (n_samples, n_channels). |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Centered signal with same shape as input. |
Source code in pyeyesweb/utils/math_utils.py
compute_jerk_rms(signal, rate_hz=50.0, signal_type='velocity')
Compute RMS of jerk (rate of change of acceleration) from a signal.
Jerk is the third derivative of position or the first derivative of acceleration. Lower RMS jerk values indicate smoother movement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal
|
ndarray
|
1D movement signal. |
required |
rate_hz
|
float
|
Sampling rate in Hz (default: 50.0). |
50.0
|
signal_type
|
str
|
Type of input signal: 'position' or 'velocity' (default: 'velocity'). - 'position': Computes third derivative to get jerk - 'velocity': Computes second derivative to get jerk |
'velocity'
|
Returns:
| Type | Description |
|---|---|
float
|
Root mean square of jerk. Returns NaN if signal has insufficient samples for the required derivatives. |
Notes
Uses numpy.gradient for smooth derivative approximation with central differences where possible, providing better accuracy than forward differences.
Source code in pyeyesweb/utils/math_utils.py
compute_phase_locking_value(phase1, phase2)
Compute the Phase Locking Value (PLV) from two phase arrays.
PLV measures the inter-trial variability of the phase difference between two signals. A value of 1 indicates perfect phase locking, while 0 indicates no phase relationship.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phase1
|
ndarray
|
Phase values of first signal in radians. |
required |
phase2
|
ndarray
|
Phase values of second signal in radians. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Phase Locking Value between 0 and 1. |
References
Lachaux et al. (1999). Measuring phase synchrony in brain signals.
Source code in pyeyesweb/utils/math_utils.py
compute_sparc(signal, rate_hz=50.0)
Compute SPARC (Spectral Arc Length) from a signal.
SPARC is a dimensionless smoothness metric that quantifies movement smoothness independent of movement amplitude and duration. More negative values indicate smoother movement.
This implementation is based on the original algorithm by Balasubramanian et al. (2015). SPARC values are typically negative, with values closer to 0 indicating less smooth (more complex) movements. For healthy reaching movements, values around -1.4 to -1.6 are common. Pathological or very unsmooth movements may have values ranging from -3 to -10 or lower, depending on the degree of movement fragmentation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal
|
ndarray
|
1D movement signal. |
required |
rate_hz
|
float
|
Sampling rate in Hz (default: 50.0). |
50.0
|
Returns:
| Type | Description |
|---|---|
float
|
SPARC value (negative, more negative = smoother). Returns NaN if signal has less than 2 samples. |
References
Balasubramanian, S., Melendez-Calderon, A., Roby-Brami, A., & Burdet, E. (2015). On the analysis of movement smoothness. Journal of NeuroEngineering and Rehabilitation, 12(1), 1-11.
Source code in pyeyesweb/utils/math_utils.py
extract_velocity_from_position(position, rate_hz=50.0)
Extract velocity from position data.
Computes velocity magnitude from position data of any dimensionality. For 1D position, returns absolute velocity. For multi-dimensional position, returns the Euclidean norm of the velocity vector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
position
|
ndarray
|
Position data. Can be: - 1D array: single position coordinate - 2D array with shape (n_samples, n_dims): multi-dimensional positions |
required |
rate_hz
|
float
|
Sampling rate in Hz (default: 50.0). |
50.0
|
Returns:
| Type | Description |
|---|---|
ndarray
|
1D array of velocity magnitudes. |
Examples:
>>> # 1D position data
>>> position_1d = np.array([0, 1, 2, 3, 4])
>>> velocity = extract_velocity_from_position(position_1d, rate_hz=100)
>>> # 2D position data (x, y coordinates)
>>> position_2d = np.array([[0, 0], [1, 0], [1, 1], [2, 1]])
>>> velocity = extract_velocity_from_position(position_2d, rate_hz=100)
Source code in pyeyesweb/utils/math_utils.py
normalize_signal(signal)
Normalize signal by its maximum absolute value.
Scales the signal to the range [-1, 1] by dividing by the maximum absolute value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
signal
|
ndarray
|
Input signal to normalize. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Normalized signal with same shape as input. Returns original signal if max absolute value is 0. |