|
5 | 5 | - :func:`estimate_snr` — an SNR estimator using a derivative-based noise |
6 | 6 | estimate and peak-based signal estimate. |
7 | 7 | - :func:`estimate_kurtosis` — excess kurtosis of the trace distribution. |
| 8 | +- :func:`estimate_skewness` — skewness of the trace distribution. |
8 | 9 |
|
9 | 10 | Notes |
10 | 11 | ----- |
|
27 | 28 | True |
28 | 29 | >>> isinstance(estimate_kurtosis(y), float) |
29 | 30 | True |
| 31 | +>>> isinstance(estimate_skewness(y), float) |
| 32 | +True |
30 | 33 | """ |
31 | 34 |
|
32 | 35 | from __future__ import annotations |
|
37 | 40 | import numpy as np |
38 | 41 | from numpy.typing import NDArray |
39 | 42 | from scipy.signal import find_peaks |
40 | | -from scipy.stats import kurtosis |
| 43 | +from scipy.stats import kurtosis, skew |
41 | 44 |
|
42 | | -__all__ = ["estimate_snr", "estimate_kurtosis"] |
| 45 | +__all__ = ["estimate_snr", "estimate_kurtosis", "estimate_skewness"] |
43 | 46 |
|
44 | 47 |
|
45 | 48 | def estimate_snr( |
@@ -132,3 +135,27 @@ def estimate_kurtosis(trace: NDArray[np.floating]) -> float: |
132 | 135 |
|
133 | 136 | # Excess kurtosis (normal distribution = 0) |
134 | 137 | return float(kurtosis(trace, fisher=True, bias=False)) |
| 138 | + |
| 139 | + |
| 140 | +def estimate_skewness(trace: NDArray[np.floating]) -> float: |
| 141 | + """ |
| 142 | + Compute the **skewness** of a 1D trace distribution. |
| 143 | +
|
| 144 | + Parameters |
| 145 | + ---------- |
| 146 | + trace : numpy.ndarray |
| 147 | + 1D input trace. NaNs will be replaced with the median of ``trace``. |
| 148 | +
|
| 149 | + Returns |
| 150 | + ------- |
| 151 | + float |
| 152 | + Skewness of the distribution: |
| 153 | + - Symmetric distribution → 0.0 |
| 154 | + - Right-skewed (positive skew) → positive |
| 155 | + - Left-skewed (negative skew) → negative |
| 156 | + """ |
| 157 | + # Replace NaNs with the median of the trace |
| 158 | + trace = np.nan_to_num(trace, nan=np.nanmedian(trace)) |
| 159 | + |
| 160 | + # Skewness |
| 161 | + return float(skew(trace, bias=False)) |
0 commit comments