-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHP8560_spectrum_analyzer.py
93 lines (77 loc) · 3.85 KB
/
HP8560_spectrum_analyzer.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import time
class spectrum_analyzer_hp8560(object):
'''Class deffinition for HP8560A Spectrum Analyzer'''
def __init__(self, visa_address):
self.rm = visa.ResourceManager()
self.spectrum_analyzer_hp8560 = self.rm.open_resource(visa_address)
instrument_id = self.spectrum_analyzer_hp8560.query('ID?').encode("utf-8").rstrip()
print "Communicating with: " + instrument_id
return
def default_setup(self):
'''Reset spectrum_analyzer_hp8560 to default'''
self.spectrum_analyzer_hp8560.clear()
def read_freq_ctrl(self,code):
'''
See page 5-9 in the Operation and Programming Manual for more ...
Code:
FA: Start Frequency
FB: Stop Frequency
CF: Center frequency
SS: Center Frequency Step
SP: Span
'''
return float(self.spectrum_analyzer_hp8560.query(code+'?').encode("utf-8").rstrip())
def set_measurement_range(self, startFreq = None, stopFreq = None, centerFreq=None,
centerFreqStep = None, span=None, verbose=True):
'''Set measurement frequency range'''
time.sleep(1)
if not(startFreq == None):
self.spectrum_analyzer_hp8560.write('FA '+str(int(startFreq))+'HZ');
if not(stopFreq == None):
self.spectrum_analyzer_hp8560.write('FB '+str(int(stopFreq))+'HZ');
if not(centerFreq == None):
self.spectrum_analyzer_hp8560.write('CF '+str(int(centerFreq))+'HZ');
if not(centerFreqStep == None):
self.spectrum_analyzer_hp8560.write('SS '+str(int(centerFreqStep))+'HZ');
if not(span == None):
self.spectrum_analyzer_hp8560.write('SP '+str(int(span))+'HZ');
if verbose:
print ("Start Frequency = {} Hz".format(self.read_freq_ctrl('FA')))
print ("Stop Frequency = {} Hz".format(self.read_freq_ctrl('FB')))
print ("Center Frequency = {} Hz".format(self.read_freq_ctrl('CF')))
print ("Center Frequency Step = {} Hz".format(self.read_freq_ctrl('SS')))
print ("Span = {} Hz".format(self.read_freq_ctrl('SP')))
def read_spectrum(self,frequency):
'''
Inputs: Frequency: value on the frequency axis
Returns: Amplitude (dB) of the spectrum at the specified frequency
'''
self.spectrum_analyzer_hp8560.write('MKN '+str(frequency))
return float(self.spectrum_analyzer_hp8560.query('MKA?').encode("utf-8").rstrip())
def center_around_peak(self, verbose=True):
'''Comment'''
for i in range(2):
self.spectrum_analyzer_hp8560.write('MKPK')
time.sleep(1.5)
self.spectrum_analyzer_hp8560.write('MKCF')
time.sleep(1.5)
if verbose:
print ("Maximum peak detected at {} Hz with amplitude of {} dBm".format(self.read_freq_ctrl('CF'),(self.read_marker_amplitude())))
CF = (self.read_freq_ctrl('CF')+self.read_freq_ctrl('CF')+self.read_freq_ctrl('CF'))/3
AMP= (self.read_marker_amplitude()+self.read_marker_amplitude()+self.read_marker_amplitude())/3
return [CF,AMP]
def set_marker_freq(self,frequency):
'''Comment'''
self.spectrum_analyzer_hp8560.write('MKN '+str(frequency))
def read_marker_amplitude(self):
'''Comment'''
return float(self.spectrum_analyzer_hp8560.query('MKA?').encode("utf-8").rstrip())
def set_marker_avg(self,state=0):
'''Turns On/Off video averaging (0/1)'''
self.spectrum_analyzer_hp8560.write('MKNOISE {};'.format(state))
def toggle_v_avg(self):
'''Turns On/Off video averaging'''
self.spectrum_analyzer_hp8560.write('VAVG')
def check_if_done(self):
while (int(self.spectrum_analyzer_hp8560.query('DONE?').encode("utf-8").rstrip())==0):
continue