-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_sample.py
More file actions
121 lines (97 loc) · 3.52 KB
/
Copy pathmake_sample.py
File metadata and controls
121 lines (97 loc) · 3.52 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""生成一个用于演示的 .dlog 示例文件。
模拟一块电池供电的物联网设备:上电冲击 + 周期性唤醒发射 + 休眠底电流,
同时记录电压和电流,并带 sense_minmax(每个记录间隔的 平均/最小/最大)。
.dlog 文件结构:
XML 文本头,以 "</dlog>\\n" 结束
8 字节二进制块
大端 float32 数据,按采样点交错排列
每个采样点的列顺序 = 每个通道 [平均, 最小, 最大],通道顺序为先电压后电流
用法:
python tools/make_sample.py sample/demo.dlog
"""
import os
import sys
import numpy as np
HEADER = """<!-- N6700X dlog settings -->
<dlog>
\t<channel id="1">
\t\t<curr_trig_lev>0</curr_trig_lev>
\t\t<volt_trig_lev>0</volt_trig_lev>
\t\t<sense_volt>1</sense_volt>
\t\t<sense_curr>1</sense_curr>
\t\t<volt_range>0</volt_range>
\t\t<curr_range>0</curr_range>
\t\t<curr_auto_range>1</curr_auto_range>
\t\t<volt_auto_range>1</volt_auto_range>
\t\t<ident>
\t\t\t<model>N6781A</model>
\t\t\t<option>
\t\t\t\t<1ua>0</1ua>
\t\t\t\t<2ua>0</2ua>
\t\t\t\t<lga>0</lga>
\t\t\t</option>
\t\t</ident>
\t</channel>
\t<frame>
\t\t<sense_minmax>1</sense_minmax>
\t\t<trig_source>1</trig_source>
\t\t<time>60</time>
\t\t<offset>0</offset>
\t\t<tint>0.01</tint>
\t\t<date>"Mon May 11 09:15:20 2026"</date>
\t</frame>
</dlog>
"""
INTERVAL = 0.01 # 10 ms 记录间隔
SUB_SAMPLES = 64 # 每个记录间隔内部的高速采样数,用来算出 min/max
def device_current(t):
"""设备电流波形。t 可以是任意形状的数组,单位秒。"""
current = np.full(t.shape, 0.0045) # 休眠底电流 4.5 mA
# 上电冲击:0.4 s 处一段快速衰减的浪涌
surge = (t >= 0.40) & (t < 0.62)
current = np.where(surge, 0.05 + 1.9 * np.exp(-(t - 0.40) / 0.03), current)
# 每 2 s 唤醒一次,工作 300 ms
phase = np.mod(t - 1.0, 2.0)
active = (t > 1.0) & (phase >= 0) & (phase < 0.30)
# 工作段:MCU 打底 + 1 kHz 的射频发射脉冲。脉冲比 10 ms 记录间隔快得多,
# 均值线上看不出来,只能靠 min/max 才看得到真实峰值
mcu = 0.15 + 0.02 * np.sin(2 * np.pi * phase / 0.30)
tx = (np.mod(phase, 0.001) < 0.00025) * 0.62
current = np.where(active, mcu + tx, current)
return current
def device_voltage(current):
"""电源内阻造成的压降,电流越大电压越低。"""
return 3.30 - 0.25 * current
def main():
out = sys.argv[1] if len(sys.argv) > 1 else "sample/demo.dlog"
os.makedirs(os.path.dirname(out) or ".", exist_ok=True)
duration = 60.0
count = int(duration / INTERVAL)
rng = np.random.default_rng(20260511)
# 先按高速率算出真实波形,再压成每个记录间隔的 平均/最小/最大
fine_t = (np.arange(count * SUB_SAMPLES) * (INTERVAL / SUB_SAMPLES)).reshape(
count, SUB_SAMPLES
)
fine_i = device_current(fine_t)
fine_i = fine_i + rng.normal(0, 0.0016, fine_i.shape)
fine_u = device_voltage(fine_i) + rng.normal(0, 0.0008, fine_i.shape)
columns = np.stack(
[
fine_u.mean(axis=1),
fine_u.min(axis=1),
fine_u.max(axis=1),
fine_i.mean(axis=1),
fine_i.min(axis=1),
fine_i.max(axis=1),
],
axis=1,
)
with open(out, "wb") as f:
f.write(HEADER.encode())
f.write(b"\x00" * 8)
f.write(columns.astype(">f4").tobytes())
print(f"{out}: {count} 个采样点, {os.path.getsize(out)} 字节")
if __name__ == "__main__":
main()