|
17 | 17 | """ |
18 | 18 |
|
19 | 19 | import numpy as np |
| 20 | +import warnings |
20 | 21 | from collections import deque |
21 | | -from scipy.signal import hilbert |
22 | 22 | from sklearn.cross_decomposition import CCA |
23 | 23 |
|
| 24 | +from pyeyesweb.utils.signal_processing import compute_hilbert_phases |
| 25 | +from pyeyesweb.utils.math_utils import compute_phase_locking_value |
| 26 | + |
24 | 27 |
|
25 | 28 | class BilateralSymmetryAnalyzer: |
26 | 29 | """ |
@@ -102,25 +105,20 @@ def _compute_phase_synchronization(self, left_signal, right_signal): |
102 | 105 | Returns: |
103 | 106 | float: Phase locking value (0-1, where 1 is perfect synchronization) |
104 | 107 | """ |
105 | | - if len(left_signal) < 10: # Need minimum data for Hilbert |
106 | | - return np.nan # Not enough data for analysis |
| 108 | + if len(left_signal) < 10: # Need minimum samples for Hilbert Transform |
| 109 | + return np.nan |
107 | 110 |
|
108 | 111 | try: |
109 | | - # Extract phases using Hilbert transform |
110 | | - left_analytic = hilbert(left_signal - np.mean(left_signal)) |
111 | | - right_analytic = hilbert(right_signal - np.mean(right_signal)) |
112 | | - |
113 | | - left_phase = np.angle(left_analytic) |
114 | | - right_phase = np.angle(right_analytic) |
115 | | - |
116 | | - # Compute Phase Locking Value (PLV) |
117 | | - phase_diff = left_phase - right_phase |
118 | | - plv = np.abs(np.mean(np.exp(1j * phase_diff))) |
119 | | - |
| 112 | + left_centered = left_signal - np.mean(left_signal) |
| 113 | + right_centered = right_signal - np.mean(right_signal) |
| 114 | + sig = np.column_stack([left_centered, right_centered]) |
| 115 | + |
| 116 | + phase1, phase2 = compute_hilbert_phases(sig) |
| 117 | + plv = compute_phase_locking_value(phase1, phase2) |
| 118 | + |
120 | 119 | return plv |
121 | 120 |
|
122 | 121 | except Exception as e: |
123 | | - import warnings |
124 | 122 | warnings.warn(f"Phase symmetry computation failed: {e}", RuntimeWarning) |
125 | 123 | return np.nan |
126 | 124 |
|
|
0 commit comments