HP3562A/trace_decoder.py

Tue, 18 Dec 2007 00:38:33 +0100

author
David Douard <david.douard@logilab.fr>
date
Tue, 18 Dec 2007 00:38:33 +0100
changeset 15
b930440af354
parent 14
07e2cbf140df
child 16
de9122b5680a
permissions
-rw-r--r--

make header_formatter a little bit smarter (interpret bool values when it can)

0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
1 # -*- coding: utf-8 -*-
14
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
2 """
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
3 trace_decoder
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
4 =============
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
5
14
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
6 Module for decoding a trace generated by the HP3562A DSA, using the
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
7 GPIB command "DDBN" (Dump Data BiNary).
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
8
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
9 This file can be exectued as a python script. Use '-h' for more informations.
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
10 """
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
11 import struct
10
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
12 import numpy
14
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
13 from HP3562A import format_header, decode_float, decode_string, decode_header
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
14 from HP3562A.enum_types import *
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
15
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
16
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
17 HEADER = [ ("Display function", EDSP, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
18 ('Number of elements', int, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
19 ('Displayed elements', int, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
20 ('Number of averages', int, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
21 ('Channel selection', ECH, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
22 ('Overflow status', EOVR, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
23 ('Overlap percentage', int, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
24 ('Domain', EDOM, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
25 ('Volts peak/rms', EVLT, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
26 ('Amplitude units', EAMP, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
27 ('X axis units', EXAXIS, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
28 ('Auto math label', str, 's', 14),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
29 ('Trace label', str, 's', 22),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
30 ('EU label 1', str, 's', 6),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
31 ('EU label 2', str, 's', 6),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
32 ('Float/Interger', bool, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
33 ('Complex/Real', bool, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
34 ('Live/Recalled', bool, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
35 ('Math result', bool, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
36 ('Real/Complex input', bool, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
37 ('Log/Linear data', bool, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
38 ('Auto math', bool, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
39 ('Real time status', bool, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
40 ('Measurement mode', EMEAS, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
41 ('Window', EWIN, 'h', 2),
14
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
42 ('Demod type channel 1', EDEMOD, 'h', 2),
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
43 ('Demod type channel 2', EDEMOD, 'h', 2),
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
44 ('Demod active channel 1', bool, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
45 ('Demod active channel 2', bool, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
46 ('Average status', EAVG, 'h', 2),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
47 ('Not used', int, 'hh', 4),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
48 ('Samp freq/2 (real)', decode_float, None, 4),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
49 ('Samp freq/2 (imag)', decode_float, None, 4),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
50 ('Not used', decode_float, None, 4),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
51 ('Delta X-axis', decode_float, None, 4),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
52 ('Max range', decode_float, None, 4),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
53 ('Start time value', decode_float, None, 4),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
54 ('Expon wind const 1', decode_float, None, 4),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
55 ('Expon wind const 2', decode_float, None, 4),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
56 ('EU value chan 1', decode_float, None, 4),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
57 ('EU value chan 2', decode_float, None, 4),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
58 ('Trig delay chan 1', decode_float, None, 4),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
59 ('Trig delay chan 2', decode_float, None, 4),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
60 ('Start freq value', decode_float, None, 8),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
61 ('Start data value', decode_float, None, 8),
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
62 ]
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
63
14
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
64 def decode_trace(data):
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
65 """
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
66 Decode the data (as generated by the HP3562A DSA in response to a
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
67 "DDBN" command), and returns a couple (header, values).
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
68
14
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
69 header is the dictionnary of the header of the dumped data block,
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
70 value is a numpy array holding the trace (vector of float or
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
71 complex values).
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
72 """
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
73 header, idx = decode_header(data, HEADER)
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
74 resu = []
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
75 for i in range(header["Number of elements"]):
14
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
76 resu.append(decode_float(data[idx: idx+4]))
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
77 idx += 4
10
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
78 return header, numpy.array(resu, dtype=float)
14
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
79
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
80
14
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
81
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
82 def main():
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
83 import sys
10
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
84 import optparse
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
85 opt = optparse.OptionParser("A simple tool for tracing a dumped trace")
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
86 opt.add_option('-f', '--filename', default=None,
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
87 dest='filename',
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
88 help='Output filename. If not set, read from stdin')
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
89 opt.add_option('-m', '--mode', default='binary',
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
90 dest='mode',
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
91 help='Dumping mode (may be "binary" [default], "ascii" or "ansi")',
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
92 )
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
93 opt.add_option('-d', '--display-header', default=False,
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
94 action="store_true",
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
95 dest="displayheader",
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
96 help="Display the trace header")
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
97 opt.add_option('-P', '--noplot-trace', default=True,
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
98 action="store_false",
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
99 dest="plot",
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
100 help="Do not display the plot of the trace")
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
101 opt.add_option('-x', '--xmode', default='lin',
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
102 dest='xmode',
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
103 help='X coordinate mode (may be "lin" [default] or "log")')
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
104 opt.add_option('-y', '--ymode', default='lin',
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
105 dest='ymode',
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
106 help='Y coordinate mode (may be "lin" [default], "log" or "db")')
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
107
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
108 options, argv = opt.parse_args(sys.argv)
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
109
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
110
10
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
111 if options.filename is None:
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
112 print "Can't deal stdin for now..."
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
113 sys.exit(1)
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
114 try:
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
115 header, data = decode_trace(open(options.filename, 'rb').read())
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
116 except Exception, e:
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
117 print "ERROR: can't read %s an interpret it as a HP3562 trace"%options.filename
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
118 print e
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
119 sys.exit(1)
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
120
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
121 if options.displayheader:
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
122 print format_header(header, HEADER, 100)
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
123 if options.plot:
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
124 f0 = header['Start freq value']
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
125 dx = header['Delta X-axis']
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
126 n = header['Number of elements']
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
127 x = numpy.linspace(f0, f0+dx*n, len(data))
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
128 y = data.copy()
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
129
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
130 import pylab
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
131 if options.ymode != "lin":
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
132 minv = min(y[y>0])
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
133 y[y==0] = minv
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
134 y = numpy.log10(y)
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
135 if options.ymode == "db":
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
136 y = y*10
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
137 pylab.ylabel('db')
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
138 pylab.grid()
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
139 pylab.plot(x, y)
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
140 pylab.xlabel('frequency')
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
141 pylab.show()
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
142
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
143
14
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
144 if __name__ == "__main__":
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
145 main()

mercurial