-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathget_spectrum_data.py
55 lines (39 loc) · 1.87 KB
/
get_spectrum_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#RSA3000B Get Spectrum Data example
#built and tested using python 3.5.2 with an RSA3408B
import visa # http://github.com/hgrecco/pyvisa
import numpy # http://www.numpy.org/
import pylab # http://matplotlib.org/
visa_address = 'TCPIP::###.###.###.###'
#replace string with your instrument visa address; I used LAN for remote comm
rm = visa.ResourceManager()
sa = rm.open_resource(visa_address)
sa.timeout = 10000 #enough time for the scope to process some comamnds, like "inst:sel" window changes
print(sa.query('*idn?')) #check to see that we're connected to a correct, responding instrument
sa.write('*rst') #set to default
print(sa.query('*opc?'))
sa.write('inst:sel "SANORMAL"') #set to Spectrum Analyzer NORMAL display
print(sa.query('*opc?'))
sa.write('init:cont OFF') #"READ" commands capture a spectrum, so we turn acquisition off
print(sa.query('*opc?'))
myData = sa.query_binary_values('read:spectrum?', datatype='f') #without numpy
#same as...
#sa.write('read:spectrum?')
#myData = sa.read_raw()
#read_raw is due to the format of the spectrum data, described below and in the Programmer Manual
#<Num_digit><Num_byte><Data(1)><Data(2)>...<Data(n)>
#Where
#<Num_digit> is the number of digits in <Num_byte>.
#<Num_byte> is the number of bytes of the data that follow.
#<Data(n)> is the amplitude spectrum in dBm.
#4-byte little endian floating-point format specified in IEEE 488.2
#n: Max 400000 (= 800 points×500 frames)
#now do stuff with myData.
#with numpy
myData2 = sa.query_binary_values('read:spectrum?', datatype='f', container=numpy.array)
#save to CSV example using numpy.
myData2.tofile('C:\\file_location', sep='\r\n', format='%s')
#graph example using pylab and numpy.
pylab.plot(myData2)
pylab.show()
#note that horizontal values are indices, not freq. values.
#for Freq values, rescale based on Display:Spectrum: queries.