Hi Guys,
I am using the Analog discovery 2 board and I want to generate a custom signal with a sampling of 100 Hz. I have modified the record code provided in the SDK and it is working perfectly , but code isn't working with sampling frequencies (hzAcq) lower than 1 kHz, it simply doesn't go into acquisition and doesn't give anything.
Could you pelase let me know what is the issue.
Below is the code:
#declare ctype variables
hdwf = c_int()
sts = c_byte()
hzAcq =c_double(1000) #sampling freq
nSamples = 4096
rgdSamples = (c_double*nSamples)()
cAvailable = c_int()
cLost = c_int()
cCorrupted = c_int()
fLost = 0
fCorrupted = 0
fs = hzAcq.value
fl = 100
fu = 1000
N = nSamples
t = np.linspace(0,N-1,N)/fs
freqmod = np.linspace(fl,fu,N)
signal = np.sin( 2*np.pi*(t))
X = signal
# samples between -1 and +1
for i in range(0,len(rgdSamples)):
rgdSamples[i] =float(X[i])
#open device
dwf.FDwfDeviceOpen(c_int(-1), byref(hdwf))
if hdwf.value == hdwfNone.value:
szerr = create_string_buffer(512)
dwf.FDwfGetLastErrorMsg(szerr)
print(str(szerr.value))
print("failed to open device")
quit()
# enable wavegen channel 1, set the waveform to sine, set the frequency to 1 Hz, the amplitude to 2v and start the wavegen
dwf.FDwfAnalogOutNodeEnableSet(hdwf, c_int(0), AnalogOutNodeCarrier, c_bool(True))
# dwf.FDwfAnalogOutNodeFunctionSet(hdwf, c_int(0), AnalogOutNodeCarrier, funcSine)
dwf.FDwfAnalogOutNodeFunctionSet(hdwf, c_int(0), AnalogOutNodeCarrier, funcCustom)
dwf.FDwfAnalogOutNodeDataSet(hdwf, c_int(0), AnalogOutNodeCarrier, rgdSamples, c_int(nSamples))
dwf.FDwfAnalogOutNodeFrequencySet(hdwf, c_int(0), AnalogOutNodeCarrier, c_double(hzAcq.value/nSamples))
dwf.FDwfAnalogOutNodeAmplitudeSet(hdwf, c_int(0), AnalogOutNodeCarrier, c_double(1))
# enable scope channel 1, set the input range to 5v, set acquisition mode to record, set the sample frequency to 100kHz and set the record length to 2 seconds
dwf.FDwfAnalogInChannelEnableSet(hdwf, c_int(0), c_bool(True))
dwf.FDwfAnalogInChannelRangeSet(hdwf, c_int(0), c_double(5))
dwf.FDwfAnalogInAcquisitionModeSet(hdwf, acqmodeRecord)
dwf.FDwfAnalogInFrequencySet(hdwf,hzAcq)
dwf.FDwfAnalogInRecordLengthSet(hdwf, c_double(nSamples/hzAcq.value)) # -1 infinite record length
#wait at least 2 seconds for the offset to stabilize
time.sleep(2)
print("Starting oscilloscope")
dwf.FDwfAnalogInConfigure(hdwf, c_int(0), c_int(1))
dwf.FDwfAnalogOutConfigure(hdwf, c_int(0), c_bool(True))
cSamples = 0
while cSamples < nSamples:
dwf.FDwfAnalogInStatus(hdwf, c_int(1), byref(sts))
if cSamples == 0 and (sts == DwfStateConfig or sts == DwfStatePrefill or sts == DwfStateArmed) :
# Acquisition not yet started.
continue
# get the number of samples available, lost & corrupted
dwf.FDwfAnalogInStatusRecord(hdwf, byref(cAvailable), byref(cLost), byref(cCorrupted))
cSamples += cLost.value
# set the lost & corrupted flags
if cLost.value :
fLost = 1
if cCorrupted.value :
fCorrupted = 1
# skip reading samples if there aren't any
if cAvailable.value==0 :
print ("No Values")
continue
# cap the available samples if the buffer would overflow from what's really available
if cSamples+cAvailable.value > nSamples :
cAvailable = c_int(nSamples-cSamples)
# Read channel 1's available samples into the buffer
dwf.FDwfAnalogInStatusData(hdwf, c_int(0), byref(rgdSamples, sizeof(c_double)*cSamples), cAvailable) # get channel 1 data
cSamples += cAvailable.value
# reset wavegen to stop it, close the device
dwf.FDwfAnalogOutReset(hdwf, c_int(0))
dwf.FDwfDeviceCloseAll()
# generate a graph image from the samples, and store it in a bytes buffer
# plt.plot(np.fromiter(rgdSamples, dtype = np.float))
# bio = BytesIO()
# plt.savefig(bio, format="png")
plot(t,X,np.fromiter(rgdSamples, dtype = np.float))