aboutsummaryrefslogtreecommitdiff
path: root/pyecsca/sca/trace/sampling.py
blob: 71dbdace7d61ccc7f1db63c4c955b79a6aa59a70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"""
This module provides downsampling functions for traces.
"""
from typing import cast

import numpy as np
from public import public
from scipy.signal import decimate

from .trace import Trace


@public
def downsample_average(trace: Trace, factor: int = 2) -> Trace:
    """
    Downsample samples of `trace` by `factor` by averaging `factor` consecutive samples in
    non-intersecting windows.

    :param trace:
    :param factor:
    :return:
    """
    resized = np.resize(
        trace.samples, len(trace.samples) - (len(trace.samples) % factor)
    )
    result_samples = cast(
        np.ndarray,
        resized.reshape(-1, factor)
        .mean(axis=1)
        .astype(trace.samples.dtype, copy=False),
    )
    return trace.with_samples(result_samples)


@public
def downsample_pick(trace: Trace, factor: int = 2, offset: int = 0) -> Trace:
    """
    Downsample samples of `trace` by `factor` by picking each `factor`-th sample, starting at `offset`.

    :param trace:
    :param factor:
    :param offset:
    :return:
    """
    result_samples = trace.samples[offset::factor].copy()
    return trace.with_samples(result_samples)


@public
def downsample_max(trace: Trace, factor: int = 2) -> Trace:
    """
    Downsample samples of `trace` by `factor` by taking the maximum out of `factor` consecutive samples in
    non-intersecting windows.

    :param trace:
    :param factor:
    :return:
    """
    resized = np.resize(
        trace.samples, len(trace.samples) - (len(trace.samples) % factor)
    )
    result_samples = cast(
        np.ndarray,
        resized.reshape(-1, factor).max(axis=1).astype(trace.samples.dtype, copy=False),
    )
    return trace.with_samples(result_samples)


@public
def downsample_min(trace: Trace, factor: int = 2) -> Trace:
    """
    Downsample samples of `trace` by `factor` by taking the minimum out of `factor` consecutive samples in
    non-intersecting windows.

    :param trace:
    :param factor:
    :return:
    """
    resized = np.resize(
        trace.samples, len(trace.samples) - (len(trace.samples) % factor)
    )
    result_samples = cast(
        np.ndarray,
        resized.reshape(-1, factor).min(axis=1).astype(trace.samples.dtype, copy=False),
    )
    return trace.with_samples(result_samples)


@public
def downsample_decimate(trace: Trace, factor: int = 2) -> Trace:
    """
    Downsample samples of `trace` by `factor` by decimating.

    :param trace:
    :param factor:
    :return:
    """
    result_samples = decimate(trace.samples, factor)
    return trace.with_samples(result_samples)