Skip to content

testing

Overview

Generate sampled time vectors and synthetic signals for examples and tests.

py_fatigue.testing

Functions:

get_random_data

get_random_data(
    t,
    seed=2,
    random_type="normal",
    min_=-10.0,
    range_=50.0,
    **kwargs
)

Get random data from a given array.

Parameters:

Name Type Description Default
t array_like

Array from which to get data (time).

required
seed int

Seed for the random generator.

2
random_type str

Type of random data to generate. The default is "normal". See numpy.random for more information.

'normal'
min_ float

Minimum value of the random data. The default is -10.0.

-10.0
range_ float

Range of the random data. The default is 50.0.

50.0
**kwargs Any

Keyword arguments for the random generators, see: https://numpy.org/doc/stable/reference/random/generator.html

{}

Returns:

Type Description
array_like

Random data.

Source code in py_fatigue/testing.py
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
def get_random_data(
    t: np.ndarray[Any, np.dtype[np.float64]],
    seed: int = 2,
    random_type: str = "normal",
    min_: float = -10.0,
    range_: float = 50.0,
    **kwargs: Any,
) -> npt.NDArray:
    """
    Get random data from a given array.

    Parameters
    ----------
    t : array_like
        Array from which to get data (time).
    seed : int
        Seed for the random generator.
    random_type : str, optional
        Type of random data to generate. The default is "normal". See
        `numpy.random` for more information.
    min_ : float, optional
        Minimum value of the random data. The default is -10.0.
    range_ : float, optional
        Range of the random data. The default is 50.0.
    **kwargs : Any
        Keyword arguments for the random generators, see:
        https://numpy.org/doc/stable/reference/random/generator.html

    Returns
    -------
    array_like
        Random data.
    """
    rng = np.random.default_rng(seed)
    np.random.seed(seed)
    y = getattr(rng, random_type)(**kwargs, size=len(t))
    y = ((y - y.min()) / (y.max() - y.min())) * range_ + min_
    return y

get_sampled_time

get_sampled_time(fs, duration, start=0.0)

Get the time vector of the signal.

Parameters:

Name Type Description Default
fs float

Sampling frequency in Hz.

required
duration float

Duration of the signal in s.

required
start float

Start time of the signal in s. The default is 0.0.

0.0

Returns:

Type Description
ndarray

Time vector of the signal.

Source code in py_fatigue/testing.py
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
def get_sampled_time(
    fs: float, duration: float, start: float = 0.0
) -> np.ndarray[Any, np.dtype[np.float64]]:
    """
    Get the time vector of the signal.

    Parameters
    ----------
    fs : float
        Sampling frequency in Hz.
    duration : float
        Duration of the signal in s.
    start : float, optional
        Start time of the signal in s. The default is 0.0.

    Returns
    -------
    np.ndarray
        Time vector of the signal.
    """
    if start < 0:
        raise ValueError("Start time must be positive.")
    if duration < 0:
        raise ValueError("Duration must be positive.")
    if fs <= 0:
        raise ValueError("Sampling frequency must be positive.")
    if duration < 1 / fs:
        raise ValueError(
            "The signal duration is too short for the sampling frequency."
        )
    if np.absolute(duration % fs) > 1e-3:
        w_msg = (
            "The signal duration is not a multiple of the sampling frequency."
            f"The signal will last {duration - (duration % (1 / fs))} s."
        )
        warnings.formatwarning = py_fatigue_formatwarning
        warnings.warn(w_msg, UserWarning)

    return cast(
        np.ndarray[Any, np.dtype[np.float64]],
        np.arange(start, start + duration, 1 / fs),
    )