Mercurial > ~darius > hgwebdir.cgi > pyinst
annotate rs_fsp7_noisetest.py @ 51:03b06d71f0fd
Allow bandwith to be set (maintains a 10:1 ratio of vid:res BW)
author | Daniel O'Connor <doconnor@gsoft.com.au> |
---|---|
date | Thu, 21 Mar 2013 11:11:10 +1030 |
parents | d143ef509fb7 |
children | c1891d9074c1 |
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): | |
122 print "Acquiring with noise off.." | |
123 setnoise(r, False) | |
124 off = getnoise(r) | |
125 print "Acquiring with noise on.." | |
126 setnoise(r, True) | |
127 on = getnoise(r) | |
128 return off, on, calcnf(enr, off, on) | |
129 | |
130 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
|
131 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
|
132 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
|
133 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
|
134 parser.add_option('-w', '--sweeps', dest = 'sweeps', default = 20, help = 'Number of sweeps', type = int) |
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
|
135 parser.add_option('-b', '--bw', dest = 'bw', default = 1000, help = 'Bandwidth (Hz)', type = float) |
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
|
136 |
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 (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
|
138 |
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 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
|
140 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
|
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 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
|
143 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
|
144 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
|
145 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
|
146 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
|
147 |
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 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
|
149 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
|
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 # 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
|
152 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
|
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 # 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
|
155 r = rsib.RSIBDevice(addr) |
46 | 156 |
157 # ID instrument | |
158 r.write('*IDN?') | |
159 print "ID is " + r.read(5) | |
160 | |
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
|
161 # 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
|
162 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
|
163 |
46 | 164 r.write("INIT:CONT OFF") |
165 | |
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
|
166 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) |
46 | 167 while True: |
168 off, on, nf = donoisetest(r, enr) | |
50
d143ef509fb7
- Print configuration details before starting
Daniel O'Connor <doconnor@gsoft.com.au>
parents:
49
diff
changeset
|
169 print "Off %.3f dBm/Hz, on %.3f dBm/Hz, NF %.2f dB" % (off, on, nf) |
46 | 170 print "Press enter to perform a new measurement" |
171 sys.stdin.readline() | |
172 |