Mercurial > ~darius > hgwebdir.cgi > pyinst
annotate rs_fsp7_noisetest.py @ 56:91b476ebc0f2
Run through 2to3
author | Daniel O'Connor <doconnor@gsoft.com.au> |
---|---|
date | Tue, 08 Dec 2020 14:00:45 +1030 |
parents | 42621291eb9b |
children | 23c96322cfb6 |
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 |
51
03b06d71f0fd
Allow bandwith to be set (maintains a 10:1 ratio of vid:res BW)
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
50
diff
changeset
|
52 def setup(r, freq, span, sweeps, bw): |
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 | |
49
22187590594b
Turn averaging on otherwise we throw away all but the last sweep.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
48
diff
changeset
|
72 # Turn averaging on |
22187590594b
Turn averaging on otherwise we throw away all but the last sweep.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
48
diff
changeset
|
73 r.write("AVER:STAT ON") |
22187590594b
Turn averaging on otherwise we throw away all but the last sweep.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
48
diff
changeset
|
74 |
46 | 75 # 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
|
76 r.write("SWE:COUN %d" % (sweeps)) |
46 | 77 |
78 # Set resolution bandwidth | |
51
03b06d71f0fd
Allow bandwith to be set (maintains a 10:1 ratio of vid:res BW)
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
50
diff
changeset
|
79 r.write("SENS1:BAND:RES %f Hz" % (bw)) |
46 | 80 |
81 # Set video bandwidth (10x res BW) | |
51
03b06d71f0fd
Allow bandwith to be set (maintains a 10:1 ratio of vid:res BW)
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
50
diff
changeset
|
82 r.write("SENS1:BAND:VID %f Hz" % (bw * 10)) |
46 | 83 |
84 def getnoise(r): | |
85 # Trigger the sweep | |
86 r.write("INIT;*WAI") | |
87 | |
88 # Wait for it to be done | |
89 r.write("*OPC?") | |
51
03b06d71f0fd
Allow bandwith to be set (maintains a 10:1 ratio of vid:res BW)
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
50
diff
changeset
|
90 opc = scpi.getdata(r.read(None), int) |
46 | 91 #print "OPC - %d" % (opc) |
92 assert(opc == 1) | |
93 | |
94 # Set data format | |
95 r.write("FORM:DATA ASC") | |
96 | |
97 # Read noise value | |
98 r.write("CALC:MARK1:FUNC:NOIS:RES?") | |
99 data = r.read(10) | |
100 #print "Data - " + data | |
101 | |
102 return float(data) | |
103 | |
104 def setnoise(r, en): | |
105 if en: | |
106 val = "ON" | |
107 else: | |
108 val = "OFF" | |
109 r.write("DIAG:SERV:NSO " + val) | |
110 | |
111 def calcnf(enrdb, offdb, ondb): | |
48 | 112 # Not possible but noisy results may result in it happening |
46 | 113 if ondb <= offdb: |
114 return 0 | |
115 ydb = ondb - offdb | |
116 y = 10 ** (ydb / 10) | |
117 enr = 10 ** (enrdb / 10) | |
118 nf = 10 * math.log10(enr / (y - 1)) | |
119 return nf | |
120 | |
121 def donoisetest(r, enr): | |
56 | 122 print("Acquiring with noise off..") |
46 | 123 setnoise(r, False) |
124 off = getnoise(r) | |
56 | 125 print("Acquiring with noise on..") |
46 | 126 setnoise(r, True) |
127 on = getnoise(r) | |
128 return off, on, calcnf(enr, off, on) | |
129 | |
130 if __name__ == '__main__': | |
54
42621291eb9b
Improve usage message.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
53
diff
changeset
|
131 parser = optparse.OptionParser(usage = '%prog [options] address frequency', |
42621291eb9b
Improve usage message.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
53
diff
changeset
|
132 description = 'Configures a Rhode Schwartz FSP7 spectrum analyser to do a noise figure test', |
42621291eb9b
Improve usage message.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
53
diff
changeset
|
133 epilog = 'video bandwidth is set to 10 times the resolution bandwidth') |
42621291eb9b
Improve usage message.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
53
diff
changeset
|
134 parser.add_option('-s', '--span', dest = 'span', default = 1e6, help = 'Span frequency in Hz (default: %default)', type = float) |
42621291eb9b
Improve usage message.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
53
diff
changeset
|
135 parser.add_option('-i', '--input', dest = 'input', default = None, help = 'Frequency used to compute ENR (defaults to frequency)', type = float) |
42621291eb9b
Improve usage message.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
53
diff
changeset
|
136 parser.add_option('-w', '--sweeps', dest = 'sweeps', default = 20, help = 'Number of sweeps (default: %default)', type = int) |
42621291eb9b
Improve usage message.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
53
diff
changeset
|
137 parser.add_option('-b', '--bw', dest = 'bw', default = 1000, help = 'Resolution bandwidth in Hz (default: %default)', type = float) |
42621291eb9b
Improve usage message.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
53
diff
changeset
|
138 parser.add_option('-r', '--repeat', dest = 'repeat', help = 'Number of repetitions, if not specified do one and ask to continue', type = int) |
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
|
139 |
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 (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
|
141 |
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 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
|
143 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
|
144 |
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 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
|
146 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
|
147 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
|
148 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
|
149 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
|
150 |
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 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
|
152 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
|
153 |
fa728cf34f50
- Calculate ENR (based on table on the back of our noise source)
Daniel O'Connor <darius@dons.net.au>
parents:
46
diff
changeset
|
154 # 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
|
155 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
|
156 |
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 # 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
|
158 r = rsib.RSIBDevice(addr) |
46 | 159 |
160 # ID instrument | |
161 r.write('*IDN?') | |
56 | 162 print("ID is " + r.read(5)) |
46 | 163 |
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 # Setup parameters |
51
03b06d71f0fd
Allow bandwith to be set (maintains a 10:1 ratio of vid:res BW)
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
50
diff
changeset
|
165 setup(r, freq, options.span, options.sweeps, options.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
|
166 |
46 | 167 r.write("INIT:CONT OFF") |
168 | |
53
d90acb55ce31
Log some stats after multiple runs.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
52
diff
changeset
|
169 nfs = [] |
56 | 170 print("Centre: %.1f Mhz, Span %.1f Mhz, Input %.1f MHz, BW %.1f kHz, %d sweeps, ENR %.2f dB" % (freq / 1e6, options.span / 1e6, options.input / 1e6, options.bw / 1e3, options.sweeps, enr)) |
52
c1891d9074c1
Add option to run for N iterations and exit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
51
diff
changeset
|
171 while options.repeat == None or options.repeat > 0: |
46 | 172 off, on, nf = donoisetest(r, enr) |
56 | 173 print("Off %.3f dBm/Hz, on %.3f dBm/Hz, NF %.2f dB" % (off, on, nf)) |
53
d90acb55ce31
Log some stats after multiple runs.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
52
diff
changeset
|
174 nfs.append(nf) |
52
c1891d9074c1
Add option to run for N iterations and exit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
51
diff
changeset
|
175 if options.repeat == None: |
56 | 176 print("Press enter to perform a new measurement") |
52
c1891d9074c1
Add option to run for N iterations and exit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
51
diff
changeset
|
177 sys.stdin.readline() |
c1891d9074c1
Add option to run for N iterations and exit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
51
diff
changeset
|
178 else: |
c1891d9074c1
Add option to run for N iterations and exit.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
51
diff
changeset
|
179 options.repeat -= 1 |
53
d90acb55ce31
Log some stats after multiple runs.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
52
diff
changeset
|
180 |
d90acb55ce31
Log some stats after multiple runs.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
52
diff
changeset
|
181 if len(nfs) > 1: |
d90acb55ce31
Log some stats after multiple runs.
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
52
diff
changeset
|
182 nfs = numpy.array(nfs) |
56 | 183 print("NF min: %.1f dBm/Hz, max: %.1f dBm/Hz, avg: %.1f dBm/hz, stddev: %.1f" % ( |
184 nfs.min(), nfs.max(), nfs.sum() / len(nfs), numpy.std(nfs))) |