Debian Codegraph @

examples/working with MDF class.py

raw
1 """
2 *asammdf* MDF usage example
3 """
4
5 import numpy as np
6
7 from asammdf import MDF, Signal
8
9 # create 3 Signal objects
10
11 timestamps = np.array([0.1, 0.2, 0.3, 0.4, 0.5], dtype=np.float32)
12
13 # unit8
14 s_uint8 = Signal(
15 samples=np.array([0, 1, 2, 3, 4], dtype=np.uint8),
16 timestamps=timestamps,
17 name="Uint8_Signal",
18 unit="u1",
19 )
20 # int32
21 s_int32 = Signal(
22 samples=np.array([-20, -10, 0, 10, 20], dtype=np.int32),
23 timestamps=timestamps,
24 name="Int32_Signal",
25 unit="i4",
26 )
27
28 # float64
29 s_float64 = Signal(
30 samples=np.array([-20, -10, 0, 10, 20], dtype=np.float64),
31 timestamps=timestamps,
32 name="Float64_Signal",
33 unit="f8",
34 )
35
36 # create empty MDf version 4.00 file
37 with MDF(version="4.10") as mdf4:
38 # append the 3 signals to the new file
39 signals = [s_uint8, s_int32, s_float64]
40 mdf4.append(signals, comment="Created by Python")
41
42 # save new file
43 mdf4.save("my_new_file.mf4", overwrite=True)
44
45 # convert new file to mdf version 3.10
46 mdf3 = mdf4.convert(version="3.10")
47 print(mdf3.version)
48
49 # get the float signal
50 sig = mdf3.get("Float64_Signal")
51 print(sig)
52
53 # cut measurement from 0.3s to end of measurement
54 mdf4_cut = mdf4.cut(start=0.3)
55 mdf4_cut.get("Float64_Signal").plot()
56
57 # cut measurement from start of measurement to 0.4s
58 mdf4_cut = mdf4.cut(stop=0.45)
59 mdf4_cut.get("Float64_Signal").plot()
60
61 # filter some signals from the file
62 mdf4 = mdf4.filter(["Int32_Signal", "Uint8_Signal"])
63
64 # save using zipped transpose deflate blocks
65 mdf4.save("out.mf4", compression=2, overwrite=True)