aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--pyecsca/sca/trace/trace.py10
-rw-r--r--test/sca/test_trace.py6
2 files changed, 16 insertions, 0 deletions
diff --git a/pyecsca/sca/trace/trace.py b/pyecsca/sca/trace/trace.py
index 5c0fd2c..dbab02d 100644
--- a/pyecsca/sca/trace/trace.py
+++ b/pyecsca/sca/trace/trace.py
@@ -5,6 +5,7 @@ from copy import copy, deepcopy
from numpy import ndarray
import numpy as np
+from numpy.typing import DTypeLike
from public import public
@@ -89,6 +90,15 @@ class Trace:
"""
return Trace(samples, deepcopy(self.meta))
+ def astype(self, dtype: DTypeLike) -> "Trace":
+ """
+ Construct a copy of this trace, with the same samples retyped using `dtype`.
+
+ :param dtype: The numpy dtype.
+ :return: The new trace
+ """
+ return self.with_samples(np.array(self.samples.astype(dtype)))
+
def __copy__(self):
return Trace(copy(self.samples), copy(self.meta), copy(self.trace_set))
diff --git a/test/sca/test_trace.py b/test/sca/test_trace.py
index 98818d3..bea68f2 100644
--- a/test/sca/test_trace.py
+++ b/test/sca/test_trace.py
@@ -7,3 +7,9 @@ def test_basic():
assert trace is not None
assert "Trace" in str(trace)
assert trace.trace_set is None
+
+
+def test_astype():
+ trace = Trace(np.array([10, 15, 24], dtype=np.dtype("i1")))
+ ta = trace.astype(np.float32)
+ assert ta.samples.dtype == np.float32