Jump to content
  • 0

Syncing AnalogIn and DigitalIn data from Analog Discovery 2 using python


NeedHelpSyncing

Question

Hi,

How would I make sure that the data collected from both the digitalIn and the analogIn is from the same stretch of time? I have data going into the both scopes on the Analog Discovery 2 and data going into 5 of the digitalIn inputs. I am trying to use the external trigger T1 to trigger both analog and digital data collection. I am doing this through python.

from ctypes import *
from dwfconstants import *
import time
import matplotlib.pyplot as plt
import sys
import numpy


def analogSetup(dwf, hdwf, analogFreq, nSamples, trigSeconds):
    dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(0), c_int(1))
    dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(1), c_int(1))
    dwf.FDwfAnalogInTriggerSourceSet(hdwf, c_ubyte(11)) # 11 = external trigger T1
    dwf.FDwfAnalogInChannelRangeSet(hdwf, c_int(0), c_double(20))
    dwf.FDwfAnalogOutNodeOffsetSet(hdwf, c_int(1), c_double(-10))
    dwf.FDwfAnalogInFrequencySet(hdwf, analogFreq)
    dwf.FDwfAnalogInTriggerPositionSet(hdwf, c_double(trigSeconds))  


def digitalSetup(dwf, hdwf, digitalFreq, nSamples, hzDI, trigSamples):
    dwf.FDwfDigitalInInternalClockInfo(hdwf, byref(hzDI))
    print("DigitanIn base freq: "+str(hzDI.value))
    divider = int(hzDI.value/digitalFreq)
    dwf.FDwfDigitalInDividerSet(hdwf, c_int(divider))
    dwf.FDwfDigitalInSampleFormatSet(hdwf, c_int(16)) # 16bit per sample format
    dwf.FDwfDigitalInTriggerSourceSet(hdwf, c_ubyte(11)) # digital trigger with T1
    dwf.FDwfDigitalInBufferSizeSet(hdwf, c_int(nSamples)) # set number of sample to acquire
    dwf.FDwfDigitalInTriggerPositionSet(hdwf, c_int(trigSamples)) # set number of sample to acquire after trigger

def triggerSetup(dwf, hdwf):
    dwf.FDwfAnalogInTriggerAutoTimeoutSet(hdwf, c_double(10)) # 10 second auto trigger timeout
    dwf.FDwfAnalogInTriggerTypeSet(hdwf, c_int(0)) # trigtypeEdge
    dwf.FDwfAnalogInTriggerChannelSet(hdwf, c_int(0)) # channel 1
    dwf.FDwfAnalogInTriggerLevelSet(hdwf, c_double(5)) # 5V max
    dwf.FDwfAnalogInTriggerHysteresisSet(hdwf, c_double(0.01)) # 0.01V
    dwf.FDwfAnalogInTriggerConditionSet(hdwf, c_int(0)) # trigcondRisingPositive 

def frameRequest(dwf, hdwf):
    dwf.FDwfAnalogOutNodeEnableSet(hdwf, c_int(0), c_int(0), c_bool(True))
    dwf.FDwfAnalogOutNodeFunctionSet(hdwf, c_int(0), c_int(0), funcPulse)
    dwf.FDwfAnalogOutNodeFrequencySet(hdwf, c_int(0), c_int(0), c_double(50000))
    dwf.FDwfAnalogOutNodeOffsetSet(hdwf,  c_int(0), c_int(0), c_double(0))
    dwf.FDwfAnalogOutNodeAmplitudeSet(hdwf, c_int(0), c_int(0), c_double(float(3.3)))
    dwf.FDwfAnalogOutWaitSet(hdwf, c_int(0), c_double(5)) # wait length
    dwf.FDwfAnalogOutNodeSymmetrySet(hdwf, c_int(0), AnalogOutNodeCarrier, c_double(2))
    dwf.FDwfAnalogOutConfigure(hdwf, c_int(0), c_bool(True))
    

if sys.platform.startswith("win"):
    dwf = cdll.LoadLibrary("dwf.dll")
elif sys.platform.startswith("darwin"):
    dwf = cdll.LoadLibrary("/Library/Frameworks/dwf.framework/dwf")
else:
    dwf = cdll.LoadLibrary("libdwf.so")

#declare ctype variables
hdwf = c_int()
sts = c_byte()
sts_d = c_byte()
hzAcq = c_double(10e6)
DIG_ACQ_FREQ = 10e6
AN_ACQ_FREQ  = 10e6
nSamples = 1000
nSamples_d = 1000
rgdSamples1 = (c_double*nSamples)()
rgdSamples2 = (c_double*nSamples)()
rgwSamples = (c_uint16*int(nSamples))()
cAvailable = c_int()
cAvailable_d = c_int()
cLost = c_int()
cCorrupted = c_int()
fLost = 0
fCorrupted = 0
hzDI = c_double()

trigSamples = int(nSamples/2) # Digital Samples are number of samples after we see the trigger
trigSeconds = (1.0/int(hzAcq.value))*(nSamples-trigSamples) # Analog samples are seeing the trigger at this time



#print DWF version
version = create_string_buffer(16)
dwf.FDwfGetVersion(version)
print("DWF Version: "+str(version.value))

#open device
print("Opening first device")
if dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf)) != 1 or hdwf.value == hdwfNone.value:
    szerr = create_string_buffer(512)
    dwf.FDwfGetLastErrorMsg(szerr)
    print(str(szerr.value))
    print("failed to open device")
    quit()

print("Digital Setup")
digitalSetup(dwf, hdwf, DIG_ACQ_FREQ, nSamples, hzDI,trigSamples)
print("Analog Setup")
analogSetup(dwf, hdwf, hzAcq, nSamples,trigSeconds)


print("Starting oscilloscope")
dwf.FDwfDigitalInConfigure(hdwf, c_int(0), c_int(1))
dwf.FDwfAnalogInConfigure(hdwf, c_int(0), c_int(1))



print("Frame Request")
frameRequest(dwf, hdwf)


sts_a = c_byte()
while True:
    dwf.FDwfDigitalInStatus(hdwf, c_int(1), byref(sts))
    dwf.FDwfAnalogInStatus(hdwf, c_int(1), byref(sts_a))
    print("Digital Status:", str(sts.value))
    print("Analog Status:", str(sts_a.value))
    if sts.value == 2 & sts_a.value == 2: # done
        break
    time.sleep(1)
print("Digital and Analog done")

dwf.FDwfDigitalInStatusData(hdwf, rgwSamples, 2*nSamples) # Need to do 2*samples because this function is in bytes 
dwf.FDwfAnalogInStatusData(hdwf, c_int(0), rgdSamples1, nSamples)
dwf.FDwfAnalogInStatusData(hdwf, c_int(1), rgdSamples2, nSamples)



dwf.FDwfAnalogOutReset(hdwf, c_int(0))
dwf.FDwfAnalogInReset(hdwf)
dwf.FDwfDigitalInReset(hdwf)
dwf.FDwfDeviceCloseAll()

print("Recording done")



dsamples_mask =[]
for i in range(len(rgwSamples)):
    dsamples_mask.append(int(rgwSamples[i]) & 0b10000)

plt.plot(numpy.fromiter(dsamples_mask, dtype = numpy.uint16))
plt.plot(numpy.fromiter(rgdSamples1, dtype = numpy.float64), color='orange')
plt.plot(numpy.fromiter(rgdSamples2, dtype = numpy.float64), color = 'green')

plt.show()

This is what I have so far. I tried to use some math to figure out what the trigger positions is supposed to be given a digital trigger position, but that hasn't really worked out. The best I can get is shifting around the analog and digital plot by changing the triggerPositionSet value, but I need it so that both digital and analog is captured at the exact same time.

For the thing I'm measuring, I KNOW that the analog and digital lines up, I just can't figure it out in code.

 

Please let me know if any clarification is needed.

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

Hi @NeedHelpSyncing,

The Analog inputs, analog outputs, digital inputs, and digital outputs each have their own separate buffers within the device (which are somewhat adjustable in size by choosing different device Configurations), but otherwise they do not affect the other instruments acquisition efforts. But the four main "systems" have access to the trigger bus so the acquisitions can be synchronized.

Thanks,
JColvin

Link to comment
Share on other sites

  • 0

I have a similar issue I need to read the analog channel and the digital channel to determine the powerup behavior of my micro controller.
the Idea is to monitor the 3.3V LDO output and a GPIO to determine the reset timings.
I can get the analog signal without any problem, however I cannot get the the digital signal correctly using the example advised by attila.

the analog and digital signal seem completely out of sync. what am I missing?



 

Link to comment
Share on other sites

  • 0

Hi @smcdaid

The ADC, Trigger, DIO have different processing (analog to digital converstion, pipelining...), triggering and path to buffer time lengths. These are compensated from software, by actually capturing with a few less or more samples after the detected trigger event, depending on the selected trigger source and sample rate(s). To compensate for the mixed mode (Scope/Logic) delay between analog/digital-in, trigger the digital-in on analog-in, as it is in the earlier example.
dwf.FDwfDigitalInTriggerSourceSet(hdwf, c_ubyte(trigsrcAnalogIn))
For other triggering (or even for this) schemes you can make the analog-in or digital-in to take a few samples less or more, or process/represent the captures shifted. With other triggering schemes make sure the independently running instruments trigger on the same trigger event and not on consequent ones (if this is important for the measurement)
dwf.FDwfDigitalInTriggerPositionSet(hdwf, c_int(int(cSamples/2-1)))

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...