RFmx and FetchSpectrum using Python

Hello

I'm trying to use RFmx through Python.NET.  The code works until I have try to catch the spectrum using FetchSpectrum():

# create the name of the variable before you pass it to the method .NET
spectrum = RFmxSpecAnMXSpectrum

# This does not work!
specAn.Spectrum.Results.FetchSpectrum (", timeout, spectrum)

Here's the complete code:

import clr
import sys

# location of the assemblies
assy_path = r'C:\Program files (x 86) \National Instruments\MeasurementStudioVS2010\DotNET\Assemblies\Current'
sys. Path.Append (assy_path)

CLR. AddReference ("NationalInstruments.RFmx.SpecAnMX.Fx40")
CLR. AddReference ("NationalInstruments.RFmx.InstrMX.Fx40")

from NationalInstruments import *.
from NationalInstruments.RFmx.InstrMX import *.
from NationalInstruments.RFmx.SpecAnMX import *.

# The VSA settings
resourceName = "5606_slave."
centerFrequency = 10.0e9 # Hz
referenceLevel = - 10 # dBm
externalAttenuation = 0.00 # dB
Timeout = 10 seconds #.
span = 1.0e + 6 # Hz
RBW = 100e3
averagingCount = 10

instrSession = RFmxInstrMX(resourceName, '')

# configuration VSA
rbwAuto = RFmxSpecAnMXSpectrumRbwAutoBandwidth.True
rbwFilterType = RFmxSpecAnMXSpectrumRbwFilterType.Gaussian
averagingEnabled = RFmxSpecAnMXSpectrumAveragingEnabled.False
averagingType = RFmxSpecAnMXSpectrumAveragingType.Rms
#specAn = instrSession.GetSpecAnSignalConfiguration ();
specAn = RFmxSpecAnMXExtension.GetSpecAnSignalConfiguration (instrSession)
specAn.ConfigureRF('',centerFrequency,referenceLevel,externalAttenuation)
specAn.Spectrum.Configuration.ConfigureSpan('', span)
specAn.Spectrum.Configuration.ConfigureRbwFilter (", rbwAuto, rbw, rbwFilterType)
specAn.Spectrum.Configuration.ConfigureAveraging (", averagingEnabled, averagingCount, averagingType)
specAn.SelectMeasurements (", RFmxSpecAnMXMeasurementTypes.Spectrum, bool ())
specAn.Commit ("")

# run acquisition
specAn.Initiate('','')

# find the peak power in the spectrum
_, pkAmp, pkFreq, freqRes = specAn.Spectrum.Results.FetchMeasurement (", timeout, float (), float (), float ())
Print "peak power: dBm {:0.2f} to {:0.6f} GHz'.format(pkAmp,pkFreq*1e-9)}}"

"""
A .NET method may require user pass the variable name that it wishes to
change as an argument. Python is not compatible "pass by reference", but the
the variable name must exist in python, before he can be handed over to the .NET method.
The .NET method will simply point to the new instance.

http://nbviewer.IPython.org/GitHub/jonnojohnson/Agilent/BLOB/master/Python_Automation/Python_Automation.ipynb
"""

# create the name of the variable before you pass it to the method .NET
spectrum = RFmxSpecAnMXSpectrum

# This does not work!
specAn.Spectrum.Results.FetchSpectrum (", timeout, spectrum)

instrSession.Close)

Here is the error:

Traceback (most recent call changed):

File "", line 3, in
specAn.Spectrum.Results.FetchSpectrum ("", timeout, spectrum)

ArgumentException: Object of type 'System.RuntimeType' cannot be converted to type ' NationalInstruments.Spectrum'1 [System.Single] & ".
at System.RuntimeType.TryChangeType (Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
at System.Reflection.MethodBase.CheckArguments (Object [] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, sig Signature)
at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck (Object obj, BindingFlags invokeAttr, Binder binder, Object [] parameters, CultureInfo culture)
at System.Reflection.RuntimeMethodInfo.Invoke (Object obj, BindingFlags invokeAttr, Binder binder, Object [] parameters, CultureInfo culture)
to Python.Runtime.MethodBinder.Invoke (inst IntPtr, IntPtr args, kw, MethodBase info, methodinfo MethodInfo [] IntPtr)

Any ideas?

Ah! That's about all. Here's what worked:

spectrum = NationalInstruments.Spectrum [System.Single] (0)
_, spectrum is specAn.Spectrum.Results.FetchSpectrum('',timeout,spectrum)

Here is the result and the final code for future reference:

import clr
import sys
Import os
import matplotlib.pyplot as plt

Import numpy as np

# To Program Files Windows environment variable

Program_Files = os.environ ['ProgramFiles (x 86) ""]
assy_path = os.path.join (program_files,
"National Instruments",.
"MeasurementStudioVS2010,"
"DotNET"
"Assemblies."
'Current')

sys. Path.Append (assy_path)

CLR. AddReference ("NationalInstruments.Common")
CLR. AddReference ("NationalInstruments.RFmx.SpecAnMX.Fx40")
CLR. AddReference ("NationalInstruments.RFmx.InstrMX.Fx40")

from NationalInstruments.RFmx.InstrMX import *.
from NationalInstruments.RFmx.SpecAnMX import *.
import NationalInstruments
Import System

# The VSA settings
resourceName = "5606_slave."
centerFrequency = 25.0e9 # Hz
referenceLevel = #-10 dBm
externalAttenuation = # 0.00 dB
Timeout = 10 # seconds
span = 1.0e + 6 # Hz
RBW = 100e3 # Hz
averagingCount = 10

instrSession = RFmxInstrMX(resourceName, '')

# Get the model number
_, Model = instrSession.GetInstrumentModel (", Str())

# configuration VSA
rbwAuto = RFmxSpecAnMXSpectrumRbwAutoBandwidth.True
rbwFilterType = RFmxSpecAnMXSpectrumRbwFilterType.Gaussian
averagingEnabled = RFmxSpecAnMXSpectrumAveragingEnabled.False
averagingType = RFmxSpecAnMXSpectrumAveragingType.Rms
specAn = RFmxSpecAnMXExtension.GetSpecAnSignalConfiguration (instrSession)
specAn.ConfigureRF('',centerFrequency,referenceLevel,externalAttenuation)
specAn.Spectrum.Configuration.ConfigureSpan('', span)
specAn.Spectrum.Configuration.ConfigureRbwFilter (", rbwAuto, rbw, rbwFilterType)
specAn.Spectrum.Configuration.ConfigureAveraging (", averagingEnabled, averagingCount, averagingType)
specAn.SelectMeasurements (", RFmxSpecAnMXMeasurementTypes.Spectrum, bool ())
specAn.Commit ("")

# run acquisition
specAn.Initiate('','')

# get x data
spectrum = NationalInstruments.Spectrum [System.Single] (0)
_, spectrum is specAn.Spectrum.Results.FetchSpectrum('',timeout,spectrum)

# get data
analogwaveform = NationalInstruments.AnalogWaveform [System.Single] (0)
_, analogwaveform is specAn.Spectrum.Results.FetchPowerTrace('',timeout,analogwaveform)

close a session #.
instrSession.Close)

# calculate the frequency and power
startFrequency = spectrum. StartFrequency
frequencyIncrement = spectrum. FrequencyIncrement
sampleCount = spectrum. SampleCount
stopFrequency = startFrequency + frequencyIncrement *(sampleCount+1)
freqArray = np.linspace (startFrequency, stopFrequency, sampleCount)
current = list (analogwaveform. GetRawData())

results of tracking #.
XScale = 1e-3
PLT. Plot ((freqArray-centerFrequency) * XScale, Power, LineWidth = 2.0)
#plt.ylim(referenceLevel-100.,referenceLevel)
PLT.yticks (NP.linspace(referenceLevel-100.,referenceLevel,11))
PLT.xticks(NP.linspace(-span,span,11)/2*XScale)
PLT. XLIM(-span/2*XScale,span/2*XScale)
PLT. Grid (true)
PLT.xlabel (' from center frequency (kHz) ")
PLT.ylabel ('Power (dBm) ")
PLT.title ("{} {:0.6f} GHz spectrum".format(model,centerFrequency*1e-9), fontsize = 'medium')
PLT. Show()

Tags: NI Software

Similar Questions

Maybe you are looking for