HP3562A/trace_decoder.py

Fri, 21 Dec 2007 23:51:36 +0100

author
David Douard <david.douard@logilab.fr>
date
Fri, 21 Dec 2007 23:51:36 +0100
changeset 18
c691b040e8c9
parent 16
de9122b5680a
permissions
-rw-r--r--

added a (very) beginning HPGL parser (with a simplist matplotlib backend)

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
16
de9122b5680a Several improvements (code refactoring) + add stuff to decode binary coordinate data block ("DCBN" command)
David Douard <david.douard@logilab.fr>
parents: 14
diff changeset
13 from HP3562A import format_header, decode_float, decode_string, decode_header, read_trace
14
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
16
de9122b5680a Several improvements (code refactoring) + add stuff to decode binary coordinate data block ("DCBN" command)
David Douard <david.douard@logilab.fr>
parents: 14
diff changeset
64 def decode_trace(data, idx=0):
14
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 """
16
de9122b5680a Several improvements (code refactoring) + add stuff to decode binary coordinate data block ("DCBN" command)
David Douard <david.douard@logilab.fr>
parents: 14
diff changeset
73 header, idx = decode_header(data, HEADER, idx)
de9122b5680a Several improvements (code refactoring) + add stuff to decode binary coordinate data block ("DCBN" command)
David Douard <david.douard@logilab.fr>
parents: 14
diff changeset
74 return header, read_trace(data, idx, header["Number of elements"])
14
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
75
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
76 def main():
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
77 import sys
10
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
78 import optparse
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
79 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
80 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
81 dest='filename',
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
82 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
83 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
84 dest='mode',
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
85 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
86 )
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
87 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
88 action="store_true",
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
89 dest="displayheader",
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
90 help="Display the trace header")
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
91 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
92 action="store_false",
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
93 dest="plot",
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
94 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
95 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
96 dest='xmode',
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
97 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
98 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
99 dest='ymode',
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
100 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
101
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
102 options, argv = opt.parse_args(sys.argv)
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
103
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
104
10
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
105 if options.filename is None:
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
106 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
107 sys.exit(1)
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
108 try:
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
109 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
110 except Exception, e:
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
111 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
112 print e
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
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
115 if options.displayheader:
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
116 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
117 if options.plot:
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
118 f0 = header['Start freq value']
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
119 dx = header['Delta X-axis']
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
120 n = header['Number of elements']
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
121 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
122 y = data.copy()
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
123
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
124 import pylab
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
125 if options.ymode != "lin":
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
126 minv = min(y[y>0])
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
127 y[y==0] = minv
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
128 y = numpy.log10(y)
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
129 if options.ymode == "db":
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
130 y = y*10
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
131 pylab.ylabel('db')
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
132 pylab.grid()
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
133 pylab.plot(x, y)
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
134 pylab.xlabel('frequency')
2999318b49a2 many improvements in the read/dump trace tools.
David Douard <david.douard@logilab.fr>
parents: 6
diff changeset
135 pylab.show()
0
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
136
9af1509775b6 initial import
David Douard <david.douard@logilab.fr>
parents:
diff changeset
137
14
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
138 if __name__ == "__main__":
07e2cbf140df several improvements; add an internal state reader
David Douard <david.douard@logilab.fr>
parents: 11
diff changeset
139 main()

mercurial