Mercurial > ~darius > hgwebdir.cgi > pyinst
annotate rs_fsp7_noisetest.py @ 47:fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
- Accept options to set frequency, span, etc
- Actually call the setup routine
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Thu, 31 Jan 2013 11:24:49 +1030 |
parents | a6e5c97b4767 |
children | 386e1cb04799 |
rev | line source |
---|---|
46 | 1 #!/usr/bin/env python |
2 | |
3 # Copyright (c) 2012 | |
4 # Daniel O'Connor <darius@dons.net.au>. All rights reserved. | |
5 # | |
6 # Redistribution and use in source and binary forms, with or without | |
7 # modification, are permitted provided that the following conditions | |
8 # are met: | |
9 # 1. Redistributions of source code must retain the above copyright | |
10 # notice, this list of conditions and the following disclaimer. | |
11 # 2. Redistributions in binary form must reproduce the above copyright | |
12 # notice, this list of conditions and the following disclaimer in the | |
13 # documentation and/or other materials provided with the distribution. | |
14 # | |
15 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
16 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
17 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
18 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE | |
19 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
20 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
21 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
22 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
23 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
24 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
25 # SUCH DAMAGE. | |
26 # | |
27 | |
28 import math | |
47
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
29 import numpy |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
30 import optparse |
46 | 31 import rsib |
47
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
32 import scipy |
46 | 33 import scpi |
34 import sys | |
35 | |
47
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
36 def findenr(frq): |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
37 # ENR values from the noise source |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
38 enrdb = numpy.array([15.55, 15.96, 15.68, 15.11, 15.07, 14.84, 14.77, 14.82, 14.86, 14.79, 14.83, 14.93, 14.93, 15.07, 15.19, 15.08, 15.14, 14.87, 14.97, 14.59]) |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
39 enrfrq = numpy.array([0.01e9, 0.1e9, 1.0e9, 2.0e9, 3.0e9, 4.0e9, 5.0e9, 6.0e9, 7.0e9, 8.0e9, 9.0e9, 10.0e9, 11.0e9, 12.0e9, 13.0e9, 14.0e9, 15.0e9, 16.0e9, 17.0e9, 18.0e9]) |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
40 |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
41 # Convert back to linear values |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
42 enr = 10 ** (enrdb / 10) |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
43 |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
44 # Interpolate |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
45 rtn = scipy.interp([frq], enrfrq, enr) |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
46 |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
47 # Convert to dB |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
48 rtndb = 10 * math.log10(rtn) |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
49 |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
50 return rtndb |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
51 |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
52 def setup(r, freq, span, sweeps): |
46 | 53 # Reset to defaults |
54 r.write("*RST") | |
55 | |
56 # Set to single sweep mode | |
57 r.write("INIT:CONT OFF") | |
58 | |
59 # Enable display updates | |
60 r.write("SYST:DISP:UPD ON") | |
61 | |
62 # Set frequency range | |
47
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
63 r.write("SENSE1:FREQ:CENT %f Hz" % (freq)) |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
64 r.write("SENSE1:FREQ:SPAN %f Hz" % (span)) |
46 | 65 |
66 # Switch marker 1 on in screen A | |
67 r.write("CALC:MARK1 ON") | |
68 | |
69 # Enable noise measurement | |
70 r.write("CALC:MARK1:FUNC:NOIS ON") | |
71 | |
72 # Set number of sweeps | |
47
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
73 r.write("SWE:COUN %d" % (sweeps)) |
46 | 74 |
75 # Set resolution bandwidth | |
47
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
76 r.write("SENS1:BAND:RES 1kHz") |
46 | 77 |
78 # Set video bandwidth (10x res BW) | |
47
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
79 r.write("SENS1:BAND:VID 10kHz") |
46 | 80 |
81 def getnoise(r): | |
82 # Trigger the sweep | |
83 r.write("INIT;*WAI") | |
84 | |
85 # Wait for it to be done | |
86 r.write("*OPC?") | |
87 opc = scpi.getdata(r.read(60), int) | |
88 #print "OPC - %d" % (opc) | |
89 assert(opc == 1) | |
90 | |
91 # Set data format | |
92 r.write("FORM:DATA ASC") | |
93 | |
94 # Read noise value | |
95 r.write("CALC:MARK1:FUNC:NOIS:RES?") | |
96 data = r.read(10) | |
97 #print "Data - " + data | |
98 | |
99 return float(data) | |
100 | |
101 def setnoise(r, en): | |
102 if en: | |
103 val = "ON" | |
104 else: | |
105 val = "OFF" | |
106 r.write("DIAG:SERV:NSO " + val) | |
107 | |
108 def calcnf(enrdb, offdb, ondb): | |
109 # Not possible but weak results may give it | |
110 if ondb <= offdb: | |
111 return 0 | |
112 ydb = ondb - offdb | |
113 y = 10 ** (ydb / 10) | |
114 enr = 10 ** (enrdb / 10) | |
115 nf = 10 * math.log10(enr / (y - 1)) | |
116 return nf | |
117 | |
118 def donoisetest(r, enr): | |
119 print "Acquiring with noise off.." | |
120 setnoise(r, False) | |
121 off = getnoise(r) | |
122 print "Acquiring with noise on.." | |
123 setnoise(r, True) | |
124 on = getnoise(r) | |
125 return off, on, calcnf(enr, off, on) | |
126 | |
127 if __name__ == '__main__': | |
47
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
128 parser = optparse.OptionParser(usage = '%prog [options] address frequency') |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
129 parser.add_option('-s', '--span', dest = 'span', default = 1e6, help = 'Span frequency (Hz)', type = float) |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
130 parser.add_option('-i', '--input', dest = 'input', default = None, help = 'Input frequency (Hz) for down converters', type = float) |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
131 parser.add_option('-w', '--sweeps', dest = 'sweeps', default = 20, help = 'Number of sweeps', type = int) |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
132 |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
133 (options, args) = parser.parse_args() |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
134 |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
135 if len(args) != 2: |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
136 parser.error('Must supply the specan address and centre frequency') |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
137 |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
138 addr = args[0] |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
139 try: |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
140 freq = float(args[1]) |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
141 except ValueError: |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
142 parser.error('Unable to parse frequency') |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
143 |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
144 if options.input == None: |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
145 options.input = freq |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
146 |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
147 # Compute ENR at frequency of interest |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
148 enr = findenr(options.input) |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
149 |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
150 # Connect to the analyser |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
151 r = rsib.RSIBDevice(addr) |
46 | 152 |
153 # ID instrument | |
154 r.write('*IDN?') | |
155 print "ID is " + r.read(5) | |
156 | |
47
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
157 # Setup parameters |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
158 setup(r, freq, options.span, options.sweeps) |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
159 |
46 | 160 r.write("INIT:CONT OFF") |
161 | |
162 while True: | |
163 off, on, nf = donoisetest(r, enr) | |
47
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
164 print "Off %.3f dB, on %.3f dB, ENR %.2f, NF %.2f" % (off, on, enr, nf) |
46 | 165 print "Press enter to perform a new measurement" |
166 sys.stdin.readline() | |
167 |