Debian Codegraph @

examples/working with Signal class.py

raw
1 """
2 *asammdf* Signal usage example
3 """
4
5 import numpy as np
6
7 from asammdf import Signal
8
9 # create 3 Signal objects with different time stamps
10
11 # unit8 with 100ms time raster
12 timestamps = np.array([0.1 * t for t in range(5)], dtype=np.float32)
13 s_uint8 = Signal(
14 samples=np.array(list(range(5)), dtype=np.uint8),
15 timestamps=timestamps,
16 name="Uint8_Signal",
17 unit="u1",
18 )
19
20 # int32 with 50ms time raster
21 timestamps = np.array([0.05 * t for t in range(10)], dtype=np.float32)
22 s_int32 = Signal(
23 samples=np.array(list(range(-500, 500, 100)), dtype=np.int32),
24 timestamps=timestamps,
25 name="Int32_Signal",
26 unit="i4",
27 )
28
29 # float64 with 300ms time raster
30 timestamps = np.array([0.3 * t for t in range(3)], dtype=np.float32)
31 s_float64 = Signal(
32 samples=np.array(list(range(2000, -1000, -1000)), dtype=np.int32),
33 timestamps=timestamps,
34 name="Float64_Signal",
35 unit="f8",
36 )
37
38 # map signals
39 xs = np.linspace(-1, 1, 50)
40 ys = np.linspace(-1, 1, 50)
41 X, Y = np.meshgrid(xs, ys)
42 vals = np.linspace(0, 180.0 / np.pi, 100)
43 phi = np.ones((len(vals), 50, 50), dtype=np.float64)
44 for i, val in enumerate(vals):
45 phi[i] *= val
46 R = 1 - np.sqrt(X**2 + Y**2)
47 samples = np.cos(2 * np.pi * X + phi) * R
48
49 timestamps = np.arange(0, 2, 0.02)
50
51 s_map = Signal(samples=samples, timestamps=timestamps, name="Variable Map Signal", unit="dB")
52 s_map.plot()
53
54
55 prod = s_float64 * s_uint8
56 prod.name = "Uint8_Signal * Float64_Signal"
57 prod.unit = "*"
58 prod.plot()
59
60 pow2 = s_uint8**2
61 pow2.name = "Uint8_Signal ^ 2"
62 pow2.unit = "u1^2"
63 pow2.plot()
64
65 allsum = s_uint8 + s_int32 + s_float64
66 allsum.name = "Uint8_Signal + Int32_Signal + Float64_Signal"
67 allsum.unit = "+"
68 allsum.plot()
69
70 # inplace operations
71 pow2 *= -1
72 pow2.name = "- Uint8_Signal ^ 2"
73 pow2.plot()
74
75 # cut signal
76 s_int32.plot()
77 cut_signal = s_int32.cut(start=0.2, stop=0.35)
78 cut_signal.plot()