I want to acquire 1000 samples and associate a timestamp with them, the way I am doing it is by collecting 1 sample and then using a loop to repeat the process 1000 times. Doing this I have no issue with the timestamp, however I am getting the wrong voltage values and I am not sure why.
some additional info: I am sending a 2kHz square wave to channel 1 (from a function generator) that has a dc offset of 2.5 V and a 2kHz sine wave to channel 2 (from wavegen 1) and I am triggering off of channel 2
If anyone can help, it’ll be greatly appreciated! I have pasted my code below:
Question
Thomas1983
I want to acquire 1000 samples and associate a timestamp with them, the way I am doing it is by collecting 1 sample and then using a loop to repeat the process 1000 times. Doing this I have no issue with the timestamp, however I am getting the wrong voltage values and I am not sure why.
some additional info: I am sending a 2kHz square wave to channel 1 (from a function generator) that has a dc offset of 2.5 V and a 2kHz sine wave to channel 2 (from wavegen 1) and I am triggering off of channel 2
If anyone can help, it’ll be greatly appreciated! I have pasted my code below:
# Declare ctype variables
dwRead = c_uint32()
hdwf = c_int()
sts = c_byte()
hzAcq = c_double(100000) # 100 kHz sampling rate
nSamples = 1
timestamp = []
def OscilloscopeSet():
# Set up acquisition for both channels
dwf.FDwfAnalogInBufferSizeSet(hdwf, c_int(nSamples))
dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(0), c_bool(True))
dwf.FDwfAnalogInChannelRangeSet(hdwf, c_int(0), c_double(10)) # Set the range to 10V
dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(1), c_bool(True))
dwf.FDwfAnalogInChannelRangeSet(hdwf, c_int(1), c_double(10)) # Set the range to 10V
dwf.FDwfAnalogInAcquisitionModeSet(hdwf, c_int(0)) # Acquisition mode
dwf.FDwfAnalogInFrequencySet(hdwf, hzAcq)
# Configure the signal generation
dwf.FDwfAnalogOutNodeEnableSet(hdwf, c_int(0), c_int(0), c_bool(True))
dwf.FDwfAnalogOutNodeFunctionSet(hdwf, c_int(0), c_int(0), c_ubyte(1))
dwf.FDwfAnalogOutNodeFrequencySet(hdwf, c_int(0), c_int(0), c_double(2000))
dwf.FDwfAnalogOutNodeAmplitudeSet(hdwf, c_int(0), c_int(0), c_double(1))
dwf.FDwfAnalogOutOffsetSet(hdwf, c_int(0), c_double(2))
dwf.FDwfAnalogOutConfigure(hdwf, c_int(0), c_bool(True))
# Set up trigger
dwf.FDwfAnalogInTriggerAutoTimeoutSet(hdwf, c_double(2)) # 2 second auto trigger timeout
dwf.FDwfAnalogInTriggerSourceSet(hdwf, c_ubyte(2)) # trigsrcDetectorAnalogIn
dwf.FDwfAnalogInTriggerTypeSet(hdwf, c_int(0)) # trigtypeEdge
dwf.FDwfAnalogInTriggerChannelSet(hdwf, c_int(1)) # Channel 2
dwf.FDwfAnalogInTriggerLevelSet(hdwf, c_double(2)) # Trigger level set to 2V
dwf.FDwfAnalogInTriggerHysteresisSet(hdwf, c_double(0.01)) # 0.01V
dwf.FDwfAnalogInTriggerConditionSet(hdwf, c_int(0)) # trigcondRisingPositive
dwf.FDwfAnalogInConfigure(hdwf, c_bool(False), c_bool(False)) # Acquisition mode
# Wait at least 2 seconds for the offset to stabilize
time.sleep(2)
OscilloscopeSet()
def acquire_samples():
time.sleep(0.01)
dwf.FDwfAnalogInStatus(hdwf, False, None)
channel1_samples = c_double()
channel2_samples = c_double()
dwf.FDwfAnalogInStatusSample(hdwf, c_int(0), byref(channel1_samples)) # get channel 1 data
dwf.FDwfAnalogInStatusSample(hdwf, c_int(1), byref(channel2_samples)) # get channel 2 data
return channel1_samples, channel2_samples
# Create the file for writing
file_name = 'Voltage.txt'
with open(file_name, 'w') as f:
f.write("Timestamp, Channel 2, Channel 1\n")
for j in range(1000):
print("test")
channel1_value, channel2_value = acquire_samples()
start_time = time.perf_counter()
timestamp.append(start_time)
f.write(f"{timestamp[j]}, {channel2_value}, {channel1_value}\n")
print(f'Data successfully saved to {file_name}')
dwf.FDwfAnalogOutReset(hdwf, c_int(0))
dwf.FDwfDeviceCloseAll()
Link to comment
Share on other sites
14 answers to this question
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now