-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhp8903.py
executable file
·120 lines (89 loc) · 4.37 KB
/
hp8903.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from enum import Enum
import re
import fasteners
import threading
# TODO
#command += 'FA30KZ' # Freq Start
#command += 'FB50KZ' # Freq Stop
#command += 'FN10KZ'# Freq Increment
#command += 'AN1MV' # Amplitude Increment (mV)
#command += 'W1' # Sweep on (0-Off)
class Measurement(Enum):
AC_VOLT = "M1"
SINAD = "M2"
DISTORTION = "M3"
DC_VOLT = "S1"
SNR = "S2"
DISTORTION_LEVEL = "S3"
class Filters(Enum):
HP_OFF = "H0"
HP_400 = "H1"
HP_PSOPH_BP = "H2"
LP_OFF = "L0"
LP_30KHZ = "L1"
LP_80KHZ = "L2"
class Trigger(Enum):
TRIG_FREERUN = "T0"
TRIG_HOLD = "T1"
TRIG_SETTLING = "T3"
class HP8903:
_lock = threading.Lock()
def __init__(self, gpib):
self.gpib = gpib
self._lock = HP8903._lock
def read_left(self):
return parse_exp_notation(self.gpib.send_with_return("RL"))
def read_right(self):
return parse_exp_notation(self.gpib.send_with_return("RR"))
@fasteners.locked
def generic_sweep(self, init_command, start, end, steps_per_octave, conversion_function, persistor):
self.gpib.init()
self.gpib.send_command_with_return_eoi(init_command)
increase_factor = 2**(1/steps_per_octave)
if (end < start): # Swap parameters if provided in the wrong direction
(start, end) = (end, start)
current = start
while current < end:
self.gpib.send_with_return(conversion_function(current))
persistor(current, self)
current *= increase_factor
def measure_preset_freq_level(self, start_freq, max_freq, steps_per_octave, amplitude, persistor):
init_command = hp8903_freq(start_freq) + hp8903_ampl(amplitude) + hp8903_meas(Measurement.AC_VOLT) + hp8903_filter(Filters.HP_OFF) + hp8903_filter(Filters.LP_OFF) + hp8903_trigger(Trigger.TRIG_FREERUN)
self.generic_sweep(init_command, start_freq, max_freq, steps_per_octave, hp8903_freq, persistor)
def measure_preset_thd_level(self, start_ampl, max_ampl, steps_per_octave, frequency, persistor):
init_command = hp8903_freq(frequency) + hp8903_ampl(start_ampl) + hp8903_meas(Measurement.DISTORTION) + hp8903_filter(Filters.HP_OFF) + hp8903_filter(Filters.LP_OFF) + hp8903_trigger(Trigger.TRIG_FREERUN)
self.generic_sweep(init_command, start_ampl, max_ampl, steps_per_octave, hp8903_ampl, persistor)
def measure_preset_thd_freq(self, start_freq, max_freq, steps_per_octave, amplitude, persistor):
init_command = hp8903_freq(start_freq) + hp8903_ampl(amplitude) + hp8903_meas(Measurement.DISTORTION) + hp8903_filter(Filters.HP_OFF) + hp8903_filter(Filters.LP_OFF) + hp8903_trigger(Trigger.TRIG_FREERUN)
self.generic_sweep(init_command, start_freq, max_freq, steps_per_octave, hp8903_freq, persistor)
def measure_thdlv_level(self, start_ampl, max_ampl, steps_per_octave, frequency, persistor):
init_command = hp8903_freq(frequency) + hp8903_ampl(start_ampl) + hp8903_meas(Measurement.DISTORTION_LEVEL) + hp8903_filter(Filters.HP_OFF) + hp8903_filter(Filters.LP_OFF) + hp8903_trigger(Trigger.TRIG_FREERUN)
self.generic_sweep(init_command, start_ampl, max_ampl, steps_per_octave, hp8903_ampl, persistor)
def measure_thdlv_freq(self, start_freq, max_freq, steps_per_octave, amplitude, persistor):
init_command = hp8903_freq(start_freq) + hp8903_ampl(amplitude) + hp8903_meas(Measurement.DISTORTION_LEVEL) + hp8903_filter(Filters.HP_OFF) + hp8903_filter(Filters.LP_OFF) + hp8903_trigger(Trigger.TRIG_FREERUN)
self.generic_sweep(init_command, start_freq, max_freq, steps_per_octave, hp8903_freq, persistor)
def measure_snr_level(self, start_ampl, max_ampl, steps_per_octave, frequency, persistor):
init_command = hp8903_freq(frequency) + hp8903_ampl(start_ampl) + hp8903_meas(Measurement.SNR) + hp8903_filter(Filters.HP_OFF) + hp8903_filter(Filters.LP_OFF) + hp8903_trigger(Trigger.TRIG_FREERUN)
self.generic_sweep(init_command, start_ampl, max_ampl, steps_per_octave, hp8903_ampl, persistor)
def parse_exp_notation(string):
#return string
pattern = re.compile(r"""([+-][0-9]+)E([+-][0-9]+)""", re.VERBOSE)
match = pattern.match(string)
if match is not None:
mantisse = float(match.group(1))
exponent = float(match.group(2))
return (mantisse * (10 ** exponent))
else:
return None
def hp8903_freq(freq_in_hz):
# KZ for Kilohertz
return "FR" + str(int(freq_in_hz)) + "HZ"
def hp8903_ampl(ampl_in_v):
# MV for Millivolts
return "AP" + str(int(ampl_in_v * 1000)) + "E-3VL"
def hp8903_meas(measurement):
return measurement.value
def hp8903_filter(filter):
return filter.value
def hp8903_trigger(trigger):
return trigger.value