<p>Here is the code for the "SPECTRUM" commands approach:</p>
<p style="margin-left:36px;"><span style="font-size:9.8pt;"><span style="font-family:"JetBrains Mono";"><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">theDll = ctypes.util.find_library(</span></span><span style="background:#2b2b2b;"><span style="color:#d1dacc;">"C:</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">\\</span></span><span style="background:#2b2b2b;"><span style="color:#d1dacc;">Tektronix</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">\\</span></span><span style="background:#2b2b2b;"><span style="color:#d1dacc;">RSA_API</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">\\</span></span><span style="background:#2b2b2b;"><span style="color:#d1dacc;">lib</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">\\</span></span><span style="background:#2b2b2b;"><span style="color:#d1dacc;">x64</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">\\</span></span><span style="background:#2b2b2b;"><span style="color:#d1dacc;">RSA_API.dll"</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">)<br>
rsa = ctypes.cdll.LoadLibrary(theDll)<br>
search_connect(rsa)<br>
specSet = config_spectrum(rsa)</span></span><br>
<span style="background:#2b2b2b;"><span style="color:#d2d9e1;">trace = acquire_spectrum(rsa</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">specSet</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">measurement_time)<br>
freq = create_frequency_array(specSet)</span></span></span></span></p>
<p style="margin-left:36px;"><span style="font-size:9.8pt;"><span style="font-family:"JetBrains Mono";"><span style="background:#2b2b2b;"><span style="color:#eed3bc;">def </span></span><span style="background:#2b2b2b;"><span style="color:#ffcf83;">config_spectrum</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">(rsa</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">center_frequency=</span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">15e6</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">reference_level=-</span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">20</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">frequency_span=</span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">30e6</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">res_bandwidth=</span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">10e3</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">):<br>
    rsa.SPECTRUM_SetEnable(c_bool(</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">True</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">))</span></span></span></span></p>
<p style="margin-left:36px;"><span style="font-size:9.8pt;"><span style="font-family:"JetBrains Mono";"><span style="color:#d2d9e1;"><span style="background:#2b2b2b;">rsa.CONFIG_SetCenterFreq(c_double(center_frequency))<br>
    rsa.CONFIG_SetReferenceLevel(c_double(reference_level))</span></span></span></span></p>
<p style="margin-left:36px;"><span style="font-size:9.8pt;"><span style="font-family:"JetBrains Mono";"><span style="color:#d2d9e1;"><span style="background:#2b2b2b;">rsa.SPECTRUM_SetDefault()<br>
    specSet = RSA_API.Spectrum_Settings()<br>
    rsa.SPECTRUM_GetSettings(byref(specSet))</span></span></span></span></p>
<p style="margin-left:36px;"><span style="font-size:9.8pt;"><span style="font-family:"JetBrains Mono";"><span style="color:#d2d9e1;"><span style="background:#2b2b2b;">specSet.window = RSA_API.SpectrumWindows.SpectrumWindow_Kaiser<br>
    specSet.verticalUnit = RSA_API.SpectrumVerticalUnits.SpectrumVerticalUnit_dBm<br>
    specSet.span = frequency_span<br>
    specSet.rbw = res_bandwidth</span></span></span></span></p>
<p style="margin-left:36px;"><span style="font-size:9.8pt;"><span style="font-family:"JetBrains Mono";"><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">rsa.SPECTRUM_SetSettings(specSet)<br>
    rsa.SPECTRUM_GetSettings(byref(specSet))<br>
    rsa.SPECTRUM_SetTraceType(RSA_API.SpectrumTraces.SpectrumTrace1</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, True, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">RSA_API.SpectrumDetectors.SpectrumDetector_PosPeak)</span></span></span></span></p>
<p style="margin-left:36px;"><span style="font-size:9.8pt;"><span style="font-family:"JetBrains Mono";"><span style="background:#2b2b2b;"><span style="color:#eed3bc;">def </span></span><span style="background:#2b2b2b;"><span style="color:#ffcf83;">acquire_spectrum</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">(rsa</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">specSet</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">measurement_time):<br>
    ready = c_bool(</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">False</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">)<br>
    traceArray = c_float * specSet.traceLength<br>
    traceData = traceArray()<br>
    outTracePoints = c_int(</span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">0</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">)<br>
    traceSelector = RSA_API.SpectrumTraces.SpectrumTrace1</span></span></span></span></p>
<p style="margin-left:36px;"><span style="font-size:9.8pt;"><span style="font-family:"JetBrains Mono";"><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">rsa.DEVICE_Run()<br>
    time.sleep(measurement_time)<br>
    rsa.SPECTRUM_AcquireTrace()<br>
    </span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">while not </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">ready.value:<br>
        rsa.SPECTRUM_WaitForDataReady(c_int(</span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">100</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">)</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">byref(ready))<br>
    rsa.SPECTRUM_GetTrace(traceSelector</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">specSet.traceLength</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">byref(traceData)</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">,<br>
                          </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">byref(outTracePoints))<br>
    rsa.DEVICE_Stop()<br>
    </span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">return </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">np.array(traceData)</span></span></span></span></p>
<p>And here is the code for the "DPX" approach:</p>
<p style="margin-left:36px;"><span style="font-size:9.8pt;"><span style="font-family:"JetBrains Mono";"><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">freq = config_DPX(rsa)<br>
fb = acquire_dpx_frame(rsa</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">measurement_time)<br>
trace = extract_dpx_spectrum(fb)</span></span></span></span></p>
<p style="margin-left:36px;"><span style="font-size:9.8pt;"><span style="font-family:"JetBrains Mono";"><span style="background:#2b2b2b;"><span style="color:#eed3bc;">def </span></span><span style="background:#2b2b2b;"><span style="color:#ffcf83;">config_DPX</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">(rsa</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">center_frequency=</span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">15e6</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">reference_level=-</span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">20</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">frequency_span=</span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">30e6</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">res_bandwidth=</span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">10e3</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">):<br>
    </span></span><span style="background:#2b2b2b;"><span style="font-style:italic;"><span style="color:#cadcc6;">"""<br>
    </span></span></span><a href="https://forum.tek.com/viewtopic.php?f=588&t=139112"><span style="background:#2b2b2b;"><span style="font-style:italic;"><span style="color:#cadcc6;">https://forum.tek.com/viewtopic.php?f=588&t=139112</span></span></span></a><br>
<span style="background:#2b2b2b;"><span style="font-style:italic;"><span style="color:#cadcc6;">    """<br>
    </span></span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">yTop = reference_level<br>
    yBottom = yTop - </span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">100<br>
    </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">yUnit = RSA_API.VerticalUnitType.VerticalUnit_dBm</span></span></span></span></p>
<p style="margin-left:36px;"><span style="font-size:9.8pt;"><span style="font-family:"JetBrains Mono";"><span style="color:#d2d9e1;"><span style="background:#2b2b2b;">dpxSet = RSA_API.DPX_SettingStruct()<br>
    rsa.CONFIG_SetCenterFreq(c_double(center_frequency))<br>
    rsa.CONFIG_SetReferenceLevel(c_double(reference_level))</span></span></span></span></p>
<p style="margin-left:36px;"><span style="font-size:9.8pt;"><span style="font-family:"JetBrains Mono";"><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">rsa.DPX_SetEnable(c_bool(</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">True</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">))<br>
    rsa.DPX_SetParameters(c_double(frequency_span)</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">c_double(res_bandwidth)</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">c_int(</span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">801</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">)</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">c_int(</span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">1</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">)</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">,<br>
                          </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">yUnit</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">c_double(yTop)</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">c_double(yBottom)</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">c_bool(</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">False</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">)</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">,<br>
                          </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">c_double(</span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">1.0</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">)</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">c_bool(</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">False</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">))<br>
    rsa.DPX_SetSogramParameters(c_double(</span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">1e-3</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">)</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">c_double(</span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">1e-3</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">)</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">,<br>
                                </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">c_double(reference_level)</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">c_double(reference_level - </span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">100</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">))<br>
    rsa.DPX_Configure(c_bool(</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">True</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">)</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">c_bool(</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">True</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">))</span></span></span></span></p>
<p style="margin-left:36px;"><span style="font-size:9.8pt;"><span style="font-family:"JetBrains Mono";"><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">rsa.DPX_SetSpectrumTraceType(RSA_API.SpectrumTraces.SpectrumTrace1</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">RSA_API.TraceType.TraceTypeMaxHold)</span></span></span></span></p>
<p style="margin-left:36px;"><span style="font-size:9.8pt;"><span style="font-family:"JetBrains Mono";"><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">rsa.DPX_GetSettings(byref(dpxSet))<br>
    dpxFreq = np.linspace((center_frequency - frequency_span / </span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">2</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">)</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">(center_frequency + frequency_span / </span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">2</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">)</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">dpxSet.bitmapWidth)<br>
    </span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">return </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">dpxFreq</span></span></span></span></p>
<p style="margin-left:36px;"><span style="font-size:9.8pt;"><span style="font-family:"JetBrains Mono";"><span style="background:#2b2b2b;"><span style="color:#eed3bc;">def </span></span><span style="background:#2b2b2b;"><span style="color:#ffcf83;">acquire_dpx_frame</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">(rsa</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">measurement_time):<br>
    frameAvailable = c_bool(</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">False</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">)<br>
    ready = c_bool(</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">False</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">)<br>
    fb = RSA_API.DPX_FrameBuffer()</span></span></span></span></p>
<p style="margin-left:36px;"><span style="font-size:9.8pt;"><span style="font-family:"JetBrains Mono";"><span style="color:#d2d9e1;"><span style="background:#2b2b2b;">rsa.DEVICE_Run()<br>
    rsa.DPX_Reset()<br>
    time.sleep(measurement_time)</span></span></span></span></p>
<p style="margin-left:36px;"><span style="font-size:9.8pt;"><span style="font-family:"JetBrains Mono";"><span style="background:#2b2b2b;"><span style="color:#eed3bc;">while not </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">frameAvailable.value:<br>
        rsa.DPX_IsFrameBufferAvailable(byref(frameAvailable))<br>
        </span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">while not </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">ready.value:<br>
            rsa.DPX_WaitForDataReady(c_int(</span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">100</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">)</span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">, </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">byref(ready))<br>
    rsa.DPX_GetFrameBuffer(byref(fb))<br>
    rsa.DPX_FinishFrameBuffer()<br>
    rsa.DEVICE_Stop()<br>
    </span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">return </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">fb</span></span></span></span></p>
<p style="margin-left:36px;"><span style="font-size:9.8pt;"><span style="font-family:"JetBrains Mono";"><span style="background:#2b2b2b;"><span style="color:#eed3bc;">def </span></span><span style="background:#2b2b2b;"><span style="color:#ffcf83;">extract_dpx_spectrum</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">(fb):<br>
    </span></span><span style="background:#2b2b2b;"><span style="color:#d8d8d8;"># When converting a ctypes pointer to a numpy array, we need to<br>
    # explicitly specify its length to dereference it correctly<br>
    </span></span><span style="background:#2b2b2b;"><span style="color:#d8d8da;">dpxBitmap </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">= np.array(fb.spectrumBitmap[:fb.spectrumBitmapSize])</span></span></span></span></p>
<p style="margin-left:36px;"><span style="font-size:9.8pt;"><span style="font-family:"JetBrains Mono";"><span style="background:#2b2b2b;"><span style="color:#d8d8d8;"># Grab trace data and convert from W to dBm<br>
    # </span></span><a href="http://www.rapidtables.com/convert/power/Watt_to_dBm.htm"><span style="background:#2b2b2b;"><span style="color:#d8d8d8;">http://www.rapidtables.com/convert/power/Watt_to_dBm.htm</span></span></a><br>
<span style="background:#2b2b2b;"><span style="color:#d8d8d8;">    # Note: fb.spectrumTraces is a pointer to a pointer, so we need to<br>
    # go through an additional dereferencing step<br>
    </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">raw_trace = np.array(fb.spectrumTraces[</span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">0</span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">][:fb.spectrumTraceLength])  </span></span><span style="background:#2b2b2b;"><span style="color:#d8d8d8;"># 0 indicates trace 1<br>
    </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">trace = </span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">10 </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">* np.log10(</span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">1000 </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">* raw_trace) + </span></span><span style="background:#2b2b2b;"><span style="color:#cadbe7;">30<br>
    </span></span><span style="background:#2b2b2b;"><span style="color:#eed3bc;">return </span></span><span style="background:#2b2b2b;"><span style="color:#d2d9e1;">trace</span></span></span></span></p>
<p style="margin-left:36px;"></p>