Skip to content

Agilent Signal Analyzer

Bases: VisaDriver

A driver for controlling Agilent Spectrum Analyzers via the VISA interface.

This class provides methods to control and query Agilent Spectrum Analyzers, including setting and querying frequency span, center frequency, resolution bandwidth (RBW), video bandwidth (VBW), sweep time, and data format. It also includes methods to retrieve calculated data, trace data, and maximum point data.

Usage

from driverlib.agilent import AgilentSA

agilent_sa = AgilentSA("TCPIP0::123.456.789.012::inst0::INSTR")

freqs, data = agilent_sa.get_calc_data()

Parameters:

Name Type Description Default
resource_location str

The VISA resource name used to connect to the device.

required

Initialize the AgilentSA object with the specified resource location.

The constructor configures the communication termination character as a newline character.

Parameters:

Name Type Description Default
resource_location str

The VISA resource name used to connect to the device.

required
Source code in driverlib/agilent/agilent_sa.py
32
33
34
35
36
37
38
39
40
def __init__(self, resource_location: str):
    """Initialize the AgilentSA object with the specified resource location.

    The constructor configures the communication termination character as a newline character.

    Args:
        resource_location (str): The VISA resource name used to connect to the device.
    """
    super().__init__(resource_location, "\n")

idn property

idn

Retrieve the identification string.

Returns:

Name Type Description
str str

The identification string of the device.

close

close()

Close the connection to the device.

Source code in driverlib/visa_driver.py
114
115
116
def close(self):
    """Close the connection to the device."""
    self.rm.close()

get_error

get_error()

Retrieve the error message from the device.

Returns:

Name Type Description
str

The error message.

Source code in driverlib/visa_driver.py
127
128
129
130
131
132
133
def get_error(self):
    """Retrieve the error message from the device.

    Returns:
        str: The error message.
    """
    return self.ask("SYST:ERR?")

print_error

print_error()

Print eventual errors occurred.

Source code in driverlib/visa_driver.py
135
136
137
def print_error(self):
    """Print eventual errors occurred."""
    print(f"Errors: {self.get_error()}", end="")

reset

reset()

Reset instrument to factory default state. Does not clear volatile memory.

Source code in driverlib/visa_driver.py
139
140
141
142
def reset(self):
    """Reset instrument to factory default state. Does not clear volatile memory."""
    self.write("*RST")
    self.write("*WAI")

clear

clear()

Clear event register, error queue -when power is cycled-.

Source code in driverlib/visa_driver.py
144
145
146
147
def clear(self):
    """Clear event register, error queue -when power is cycled-."""
    self.write("*CLS")
    self.write("*WAI")

lookup_resources

lookup_resources()

Look for all the available resources.

Source code in driverlib/visa_driver.py
149
150
151
152
153
154
155
156
157
158
159
160
def lookup_resources(self):
    """Look for all the available resources."""
    instruments = self.rm.list_resources()
    print(f"Found {len(instruments)} instruments:")
    for location in instruments:
        try:
            with OpenResource(self.rm, location, self.endline) as instr:
                idn = instr.query("*IDN?")
        except VisaIOError:
            idn = None

        print(f"Resource named: {idn if idn else 'Unable to determine'} @ '{location}'")

get_span

get_span()

Query the frequency span of the spectrum analyzer.

Returns:

Name Type Description
float float

The frequency span in Hz.

Source code in driverlib/agilent/agilent_sa.py
42
43
44
45
46
47
48
def get_span(self) -> float:
    """Query the frequency span of the spectrum analyzer.

    Returns:
        float: The frequency span in Hz.
    """
    return float(self.ask(":FREQuency:SPAN?"))

set_span

set_span(value)

Set the frequency span of the spectrum analyzer.

Parameters:

Name Type Description Default
value float

The desired frequency span in Hz.

required
Source code in driverlib/agilent/agilent_sa.py
50
51
52
53
54
55
56
def set_span(self, value: float):
    """Set the frequency span of the spectrum analyzer.

    Args:
        value (float): The desired frequency span in Hz.
    """
    self.write(f":FREQuency:SPAN {value}")

get_center

get_center()

Query the center frequency of the spectrum analyzer.

Returns:

Name Type Description
float float

The center frequency in Hz.

Source code in driverlib/agilent/agilent_sa.py
60
61
62
63
64
65
66
def get_center(self) -> float:
    """Query the center frequency of the spectrum analyzer.

    Returns:
        float: The center frequency in Hz.
    """
    return float(self.ask(":FREQuency:CENTer?"))

set_center

set_center(value)

Set the center frequency of the spectrum analyzer.

Parameters:

Name Type Description Default
value float

The desired center frequency in Hz.

required
Source code in driverlib/agilent/agilent_sa.py
68
69
70
71
72
73
74
def set_center(self, value: float):
    """Set the center frequency of the spectrum analyzer.

    Args:
        value (float): The desired center frequency in Hz.
    """
    self.write(f":FREQuency:CENTer {value}")

get_rbw

get_rbw()

Query the resolution bandwidth (RBW) of the spectrum analyzer.

Returns:

Name Type Description
int int

The RBW in Hz.

Source code in driverlib/agilent/agilent_sa.py
78
79
80
81
82
83
84
def get_rbw(self) -> int:
    """Query the resolution bandwidth (RBW) of the spectrum analyzer.

    Returns:
        int: The RBW in Hz.
    """
    return int(float(self.ask(":BANDwidth:RESolution?")))

set_rbw

set_rbw(value)

Set the resolution bandwidth (RBW) of the spectrum analyzer.

Parameters:

Name Type Description Default
value int

The desired RBW in Hz.

required
Source code in driverlib/agilent/agilent_sa.py
86
87
88
89
90
91
92
def set_rbw(self, value: int):
    """Set the resolution bandwidth (RBW) of the spectrum analyzer.

    Args:
        value (int): The desired RBW in Hz.
    """
    self.write(f":BANDwidth:RESolution {int(value)}")

get_vbw

get_vbw()

Query the video bandwidth (VBW) of the spectrum analyzer.

Returns:

Name Type Description
int int

The VBW in Hz.

Source code in driverlib/agilent/agilent_sa.py
 96
 97
 98
 99
100
101
102
def get_vbw(self) -> int:
    """Query the video bandwidth (VBW) of the spectrum analyzer.

    Returns:
        int: The VBW in Hz.
    """
    return int(float(self.ask(":BANDwidth:VIDeo?")))

set_vbw

set_vbw(value)

Set the video bandwidth (VBW) of the spectrum analyzer.

Parameters:

Name Type Description Default
value int

The desired VBW in Hz.

required
Source code in driverlib/agilent/agilent_sa.py
104
105
106
107
108
109
110
def set_vbw(self, value: int):
    """Set the video bandwidth (VBW) of the spectrum analyzer.

    Args:
        value (int): The desired VBW in Hz.
    """
    self.write(f":BANDwidth:VIDeo {int(value)}")

get_sweep_time

get_sweep_time()

Query the sweep time of the spectrum analyzer.

Returns:

Type Description
Optional[float]

Optional[float]: The sweep time in seconds, or None if set to auto.

Source code in driverlib/agilent/agilent_sa.py
114
115
116
117
118
119
120
121
122
123
def get_sweep_time(self) -> Optional[float]:
    """Query the sweep time of the spectrum analyzer.

    Returns:
        Optional[float]: The sweep time in seconds, or None if set to auto.
    """
    sweep_time = self.ask(":SENSe:SWEep:TIME?")
    if sweep_time.lower() == "auto":
        return None
    return float(sweep_time)

set_sweep_time

set_sweep_time(value)

Set the sweep time of the spectrum analyzer.

Parameters:

Name Type Description Default
value Optional[float]

The desired sweep time in seconds, or None to set to auto.

required
Source code in driverlib/agilent/agilent_sa.py
125
126
127
128
129
130
131
132
133
134
def set_sweep_time(self, value: Optional[float]):
    """Set the sweep time of the spectrum analyzer.

    Args:
        value (Optional[float]): The desired sweep time in seconds, or None to set to auto.
    """
    if value is None:
        self.write(":SENSe:SWEep:TIME:AUTO ON")
    else:
        self.write(f":SENSe:SWEep:TIME {value}")  # time in seconds

get_data_format

get_data_format()

Query the data format for trace data from the spectrum analyzer.

Returns:

Name Type Description
_FORMATS_TYPE _FORMATS_TYPE

The current data format.

Source code in driverlib/agilent/agilent_sa.py
138
139
140
141
142
143
144
def get_data_format(self) -> _FORMATS_TYPE:
    """Query the data format for trace data from the spectrum analyzer.

    Returns:
        _FORMATS_TYPE: The current data format.
    """
    return self.ask(":FORMat:TRACe:DATA?")  # type: ignore

set_data_format

set_data_format(value='ASCii')

Set the data format for trace data from the spectrum analyzer.

Parameters:

Name Type Description Default
value _FORMATS_TYPE

The desired data format.

'ASCii'
Source code in driverlib/agilent/agilent_sa.py
146
147
148
149
150
151
152
def set_data_format(self, value: _FORMATS_TYPE = "ASCii"):
    """Set the data format for trace data from the spectrum analyzer.

    Args:
        value (_FORMATS_TYPE): The desired data format.
    """
    self.write(f":FORMat:TRACe:DATA {value}")

get_calc_data

get_calc_data()

Retrieve the calculated data from the spectrum analyzer.

Returns:

Type Description
Tuple[ndarray, ndarray]

Tuple[np.ndarray, np.ndarray]: The frequencies and amplitude as np.arrays.

Source code in driverlib/agilent/agilent_sa.py
156
157
158
159
160
161
162
163
164
165
166
167
def get_calc_data(self) -> Tuple[np.ndarray, np.ndarray]:
    """Retrieve the calculated data from the spectrum analyzer.

    Returns:
        Tuple[np.ndarray, np.ndarray]: The frequencies and amplitude as np.arrays.
    """
    d = self.ask(":CALCulate:DATA?")
    d = [float(f) for f in d.split(",")]
    d = np.array(d)
    x = d[::2]
    y = d[1::2]
    return x, y

get_trace

get_trace(trace=1)

Retrieve the trace data for a specified trace number from the spectrum analyzer.

Parameters:

Name Type Description Default
trace int

The trace number. Defaults to 1.

1

Returns:

Type Description

np.ndarray: The trace data as a numpy array.

Source code in driverlib/agilent/agilent_sa.py
169
170
171
172
173
174
175
176
177
178
179
def get_trace(self, trace: int = 1):
    """Retrieve the trace data for a specified trace number from the spectrum analyzer.

    Args:
        trace (int, optional): The trace number. Defaults to 1.

    Returns:
        np.ndarray: The trace data as a numpy array.
    """
    raw = self.ask(f":TRACe:DATA? TRACE{trace}")
    return np.asarray([float(f) for f in raw.split(",")])

get_freq_and_trace

get_freq_and_trace(trace=1, center=None, span=None)

Retrieves the trace data and corresponding frequency points from the spectrum analyzer.

This method allows specifying a trace number, and optionally the center frequency and span to use for frequency calculations. If the center frequency and span are not provided, the current settings of the spectrum analyzer are used.

Parameters:

Name Type Description Default
trace int

The trace number to retrieve data from. Defaults to 1.

1
center Optional[float]

The center frequency in Hz. If None, the current center frequency of the spectrum analyzer is used. Defaults to None.

None
span Optional[float]

The frequency span in Hz. If None, the current span of the spectrum analyzer is used. Defaults to None.

None

Returns:

Type Description
Tuple[ndarray, ndarray]

Tuple[np.ndarray, np.ndarray]: A tuple containing two numpy arrays. The first array contains the trace data points, and the second contains the corresponding frequency points in Hz.

Source code in driverlib/agilent/agilent_sa.py
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
def get_freq_and_trace(
    self,
    trace: int = 1,
    center: Optional[float] = None,
    span: Optional[float] = None,
) -> Tuple[np.ndarray, np.ndarray]:
    """
    Retrieves the trace data and corresponding frequency points from the spectrum analyzer.

    This method allows specifying a trace number, and optionally the center frequency and span
    to use for frequency calculations. If the center frequency and span are not provided,
    the current settings of the spectrum analyzer are used.

    Args:
        trace (int): The trace number to retrieve data from. Defaults to 1.
        center (Optional[float]): The center frequency in Hz. If None, the current center
            frequency of the spectrum analyzer is used. Defaults to None.
        span (Optional[float]): The frequency span in Hz. If None, the current span of the
            spectrum analyzer is used. Defaults to None.

    Returns:
        Tuple[np.ndarray, np.ndarray]: A tuple containing two numpy arrays. The first array
            contains the trace data points, and the second contains the corresponding
            frequency points in Hz.
    """
    # Get the current center frequency and span if not provided
    if span is None:
        span = self.get_span()
    if center is None:
        center = self.get_center()

    # Ensure the data format is set to ASCII.
    self.set_data_format("ASCii")

    # Retrieve the trace data from the specified trace number
    data = self.get_trace(trace=trace)

    # Calculate the frequency points corresponding to each data point in the trace
    freqs = np.linspace(center - span / 2, center + span / 2, len(data))

    return freqs, data

get_max_point

get_max_point(marker=1)

Find the maximum point on the specified marker and returns its frequency.

Parameters:

Name Type Description Default
marker int

The marker number. Defaults to 1.

1

Returns:

Name Type Description
float float

The frequency of the maximum point in Hz.

Source code in driverlib/agilent/agilent_sa.py
223
224
225
226
227
228
229
230
231
232
233
def get_max_point(self, marker: int = 1) -> float:
    """Find the maximum point on the specified marker and returns its frequency.

    Args:
        marker (int, optional): The marker number. Defaults to 1.

    Returns:
        float: The frequency of the maximum point in Hz.
    """
    self.write(f":CALCulate:MARKer{marker}:MAXimum")
    return float(self.ask(f":CALCulate:MARKer{marker}:X?"))

set_trace_parameters_and_get

set_trace_parameters_and_get(center, span, rbw=100, vbw=30, swt=None)

Configure and measure.

This method sets the center frequency, span, resolution bandwidth (RBW), video bandwidth (VBW), and sweep time for the spectrum analyzer. It then retrieves the trace data displayed on the screen along with the corresponding frequency values.

Parameters:

Name Type Description Default
center float

Center frequency in Hz.

required
span float

Frequency span in Hz.

required
rbw int

Resolution bandwidth in Hz. Defaults to 100.

100
vbw int

Video bandwidth in Hz. Defaults to 30.

30
swt Optional[float]

Total sweep time in seconds. None for auto. Defaults to None.

None

Returns:

Type Description
ndarray

Tuple[np.ndarray, np.ndarray]: A tuple containing two numpy arrays. The first array

ndarray

contains the trace data, and the second contains the corresponding frequencies.

Source code in driverlib/agilent/agilent_sa.py
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
def set_trace_parameters_and_get(
    self, center: float, span: float, rbw: int = 100, vbw: int = 30, swt: Optional[float] = None
) -> Tuple[np.ndarray, np.ndarray]:
    """Configure and measure.

    This method sets the center frequency, span, resolution bandwidth (RBW), video bandwidth
        (VBW), and sweep time for the spectrum analyzer. It then retrieves the trace data
        displayed on the screen along with the corresponding frequency values.

    Args:
        center (float): Center frequency in Hz.
        span (float): Frequency span in Hz.
        rbw (int): Resolution bandwidth in Hz. Defaults to 100.
        vbw (int): Video bandwidth in Hz. Defaults to 30.
        swt (Optional[float]): Total sweep time in seconds. None for auto. Defaults to None.

    Returns:
        Tuple[np.ndarray, np.ndarray]: A tuple containing two numpy arrays. The first array
        contains the trace data, and the second contains the corresponding frequencies.
    """
    self.center = center
    self.span = span
    self.rbw = rbw
    self.vbw = vbw
    self.sweep_time = swt

    self.write(":DISPlay:WINdow:TRACe:Y:SCALe:SPACing LOGarithmic")

    return self.get_freq_and_trace(trace=1, span=span, center=center)