diff options
Diffstat (limited to '')
| -rw-r--r-- | pyecsca/sca/trace/process.py | 17 | ||||
| -rw-r--r-- | test/sca/test_process.py | 2 |
2 files changed, 3 insertions, 16 deletions
diff --git a/pyecsca/sca/trace/process.py b/pyecsca/sca/trace/process.py index eee487c..bc5b8dd 100644 --- a/pyecsca/sca/trace/process.py +++ b/pyecsca/sca/trace/process.py @@ -1,7 +1,7 @@ """Provides functions for sample-wise processing of single traces.""" -from typing import cast import numpy as np +from scipy.signal import convolve from public import public from .trace import Trace @@ -44,12 +44,6 @@ def threshold(trace: Trace, value) -> Trace: return trace.with_samples(result_samples) -def _rolling_window(samples: np.ndarray, window: int) -> np.ndarray: - shape = samples.shape[:-1] + (samples.shape[-1] - window + 1, window) - strides = samples.strides + (samples.strides[-1],) - return np.lib.stride_tricks.as_strided(samples, shape=shape, strides=strides) # type: ignore[attr-defined] - - @public def rolling_mean(trace: Trace, window: int) -> Trace: """ @@ -61,14 +55,7 @@ def rolling_mean(trace: Trace, window: int) -> Trace: :param window: :return: """ - return trace.with_samples( - cast( - np.ndarray, - np.mean(_rolling_window(trace.samples, window), -1).astype( - dtype=trace.samples.dtype, copy=False - ), - ) - ) + return trace.with_samples(convolve(trace.samples, np.ones(window, dtype=trace.samples.dtype), "valid") / window) @public diff --git a/test/sca/test_process.py b/test/sca/test_process.py index cf9fbbd..9ab8bcf 100644 --- a/test/sca/test_process.py +++ b/test/sca/test_process.py @@ -43,7 +43,7 @@ def test_rolling_mean(trace): assert result is not None assert len(result.samples) == 3 assert result.samples[0] == -15 - assert result.samples[1] == 42 + assert result.samples[1] == 42.5 assert result.samples[2] == 196 |
