浏览代码

scp_info: Add an option to create a scatter plot

Keir Fraser 4 年之前
父节点
当前提交
e9b1dde861
共有 1 个文件被更改,包括 36 次插入9 次删除
  1. 36 9
      scripts/misc/scp_info.py

+ 36 - 9
scripts/misc/scp_info.py

@@ -1,4 +1,9 @@
 import struct, sys
+import matplotlib.pyplot as plt
+
+NO_DAT    = 0
+PRINT_DAT = 1
+PLOT_DAT  = 2
 
 def dump_track(dat, trk_offs, trknr, show_dat):
     print("Track %u:" % trknr)
@@ -13,9 +18,12 @@ def dump_track(dat, trk_offs, trknr, show_dat):
     sig, tnr, _, _, s_off = struct.unpack("<3sB3I", thdr[:16])
     assert sig == b"TRK"
     assert tnr == trknr
+    p_idx, tot = [], 0.0
     for i in range(nr_revs):
         t,n,_ = struct.unpack("<3I", thdr[4+i*12:4+(i+1)*12])
         print("Rev %u: time=%uus flux=%u" % (i, t//40, n))
+        tot += t/40
+        p_idx.append(tot)
     if not show_dat:
         return
     
@@ -28,18 +36,37 @@ def dump_track(dat, trk_offs, trknr, show_dat):
         fluxl.append(flux / 40)
     tot = 0.0
     i = 0
+    px, py = [], []
     for x in fluxl:
-        bad = ""
-        if (x < 3.6) or ((x > 4.4) and (x < 5.4)) \
-           or ((x > 6.6) and (x < 7.2)) or (x > 8.8):
-            bad = "BAD"
-        print("%d: %f %s" % (i, x, bad))
+        if show_dat == PRINT_DAT:
+            bad = ""
+            if (x < 3.6) or ((x > 4.4) and (x < 5.4)) \
+               or ((x > 6.6) and (x < 7.2)) or (x > 8.8):
+                bad = "BAD"
+            print("%d: %f %s" % (i, x, bad))
+        else:
+            px.append(tot/1000)
+            py.append(x)
         i += 1
         tot += x
     print("Total: %uus (%uus per rev)" % (int(tot), tot//nr_revs))
+    if show_dat == PLOT_DAT:
+        plt.xlabel("Time (ms)")
+        plt.ylabel("Flux (us)")
+        plt.gcf().set_size_inches(12, 8)
+        plt.axvline(x=0, ymin=0.95, color='r')
+        for t in p_idx:
+            plt.axvline(x=t/1000, ymin=0.95, color='r')
+        plt.scatter(px, py, s=1)
+        plt.show()
+
+argv = sys.argv
 
+plot = (argv[1] == '--plot')
+if plot:
+    argv = argv[1:]
 
-with open(sys.argv[1], "rb") as f:
+with open(argv[1], "rb") as f:
     dat = f.read()
 
 header = struct.unpack("<3s9BI", dat[0:16])
@@ -51,9 +78,9 @@ trk_offs = struct.unpack("<168I", dat[16:0x2b0])
 
 print("Revolutions: %u" % nr_revs)
 
-if len(sys.argv) == 3:
-    dump_track(dat, trk_offs, int(sys.argv[2]), True)
+if len(argv) == 3:
+    dump_track(dat, trk_offs, int(argv[2]), PLOT_DAT if plot else PRINT_DAT)
 else:
     for i in range(s_trk, e_trk+1):
-        dump_track(dat, trk_offs, i, False)
+        dump_track(dat, trk_offs, i, NO_DAT)