def __init__(self, channel, v_range, hz_acq, n_samples): gr.sync_block.__init__(self, name="AD2_AnalogIn_Record_f", in_sig=None, out_sig=[numpy.float32]) #print DWF version print("DWF Version: " + dwf.FDwfGetVersion()) #open device with configure self.hdwf = dwf.Dwf(-1, 1) # 1:Scope Buffer Size 16k #set up acquisition self.dwf_ai = dwf.DwfAnalogIn(self.hdwf) self.dwf_ai.channelEnableSet(channel, True) self.dwf_ai.channelRangeSet(channel, v_range) self.dwf_ai.acquisitionModeSet(self.dwf_ai.ACQMODE.RECORD) self.dwf_ai.frequencySet(hz_acq) self.dwf_ai.recordLengthSet(-1) #infinite time buffer_size = self.dwf_ai.bufferSizeGet() print "BufferSize " + str(buffer_size) #wait at least 2 seconds for the offset to stabilize time.sleep(2) #begin acquisition self.dwf_ai.configure(False, True) print("begin acquisition") self.rgdSamples = [0.0] * n_samples self.cSamples = 0 self.fLost = False self.fCorrupted = False
def ad_get_dwf_version(): """ Get version of dwf library. :return: string: version """ ver = dwf.FDwfGetVersion() print("DWF Version: " + ver) return ver
def start_recording(): #print DWF version print("DWF Version: " + dwf.FDwfGetVersion()) #constants HZ_ACQ = 20e3 N_SAMPLES = 100000 #open device print("Opening first device") dwf_ao = dwf.DwfAnalogOut() print("Preparing to read sample...") print("Generating sine wave...") dwf_ao.nodeEnableSet(0, dwf_ao.NODE.CARRIER, True) dwf_ao.nodeFunctionSet(0, dwf_ao.NODE.CARRIER, dwf_ao.FUNC.SINE) dwf_ao.nodeFrequencySet(0, dwf_ao.NODE.CARRIER, 1.0) dwf_ao.nodeAmplitudeSet(0, dwf_ao.NODE.CARRIER, 2.0) dwf_ao.configure(0, True) #set up acquisition dwf_ai = dwf.DwfAnalogIn(dwf_ao) dwf_ai.channelEnableSet(0, True) dwf_ai.channelRangeSet(0, 5.0) dwf_ai.acquisitionModeSet(dwf_ai.ACQMODE.RECORD) dwf_ai.frequencySet(HZ_ACQ) dwf_ai.recordLengthSet(5) #wait at least 2 seconds for the offset to stabilize time.sleep(2) #begin acquisition dwf_ai.configure(False, True) print(" waiting to finish") rgdSamples = [] cSamples = 0 fLost = False fCorrupted = False while cSamples < N_SAMPLES: sts = dwf_ai.status(True) if cSamples == 0 and sts in (dwf_ai.STATE.CONFIG, dwf_ai.STATE.PREFILL, dwf_ai.STATE.ARMED): # Acquisition not yet started. continue cAvailable, cLost, cCorrupted = dwf_ai.statusRecord() cSamples += cLost if cLost > 0: fLost = True if cCorrupted > 0: fCorrupted = True if cAvailable == 0: continue if cSamples + cAvailable > N_SAMPLES: cAvailable = N_SAMPLES - cSamples # get samples rgdSamples.extend(dwf_ai.statusData(0, cAvailable)) cSamples += cAvailable print("Recording finished") if fLost: print("Samples were lost! Reduce frequency") if cCorrupted: print("Samples could be corrupted! Reduce frequency") with open("record.csv", "w") as f: for v in rgdSamples: f.write("%s\n" % v) plt.plot(rgdSamples) plt.savefig('test.png', bbox_inches='tight') scaled = np.int16(rgdSamples / np.max(np.abs(rgdSamples)) * 32767) write('test.wav', 20000, scaled) """plt.show()""" update_pic()
DWF Python Example Modified by: MURAMATSU Atsushi <*****@*****.**> Revised: 2016-04-21 Original Author: Digilent, Inc. Original Revision: 11/24/2014 Requires: Python 2.7, 3.3 or later """ import dwf import math #print DWF version print("DWF Version: " + dwf.FDwfGetVersion()) #open device print("Opening first device") dwf_di = dwf.DwfDigitalIn() dwf_do = dwf.DwfDigitalOut(dwf_di) print("Configuring Digital Out / In...") # generate counter for i in range(16): dwf_do.enableSet(i, True) dwf_do.dividerSet(i, 0x01 << i) dwf_do.counterSet(i, 1000, 1000) dwf_do.configure(True)
import dwf version = dwf.FDwfGetVersion() print("DWF version: " + version) hwdf = dwf.FDwfDeviceOpen() print("HDWF" + str(hwdf))
def plot(self): ''' plot some random stuff ''' # random data #print DWF version print("DWF Version: " + dwf.FDwfGetVersion()) #open device print("Opening first device") dwf_ao = dwf.DwfAnalogOut() print("Preparing to read sample...") # print("Generating sine wave...") # dwf_ao.nodeEnableSet(0, dwf_ao.NODE.CARRIER, True) # dwf_ao.nodeFunctionSet(0, dwf_ao.NODE.CARRIER, dwf_ao.FUNC.SINE) # dwf_ao.nodeFrequencySet(0, dwf_ao.NODE.CARRIER, 1.0) # dwf_ao.nodeAmplitudeSet(0, dwf_ao.NODE.CARRIER, 2.0) # dwf_ao.configure(0, True) #set up acquisition dwf_ai = dwf.DwfAnalogIn(dwf_ao) dwf_ai.channelEnableSet(0, True) dwf_ai.channelRangeSet(0, 5.0) dwf_ai.acquisitionModeSet(dwf_ai.ACQMODE.RECORD) dwf_ai.frequencySet(HZ_ACQ) dwf_ai.recordLengthSet(N_SAMPLES / HZ_ACQ) #wait at least 2 seconds for the offset to stabilize time.sleep(1) #begin acquisition dwf_ai.configure(False, True) print(" waiting to finish") self.rgdSamples = [] cSamples = 0 fLost = False fCorrupted = False while cSamples < N_SAMPLES: sts = dwf_ai.status(True) if cSamples == 0 and sts in (dwf_ai.STATE.CONFIG, dwf_ai.STATE.PREFILL, dwf_ai.STATE.ARMED): # Acquisition not yet started. continue cAvailable, cLost, cCorrupted = dwf_ai.statusRecord() cSamples += cLost if cLost > 0: fLost = True if cCorrupted > 0: fCorrupted = True if cAvailable == 0: continue if cSamples + cAvailable > N_SAMPLES: cAvailable = N_SAMPLES - cSamples # get samples self.rgdSamples.extend(dwf_ai.statusData(0, cAvailable)) cSamples += cAvailable print("Recording finished") if fLost: print("Samples were lost! Reduce frequency") if cCorrupted: print("Samples could be corrupted! Reduce frequency") with open("record.csv", "w") as f: for v in self.rgdSamples: f.write("%s\n" % v) """plt.show()""" scaled = np.int16(self.rgdSamples / np.max(np.abs(self.rgdSamples)) * 32767) here = sys.path[0] sound_path = os.path.join(here, "test.wav") write(sound_path, 20000, scaled) # create an axis self.ax = self.figure.add_subplot(2, 1, 1) # discards the old graph self.ax.clear() # plot data sample_time = np.arange(len(self.rgdSamples)) / HZ_ACQ self.new_rgdSamples = [i * 1000 for i in self.rgdSamples] self.ax.plot(sample_time, self.new_rgdSamples) self.ax.set_xlabel('Time (s)') self.ax.set_ylabel('Voltage (mV)') samplingFrequency, signalData = wavfile.read('test.wav') self.bx = self.figure.add_subplot(2, 1, 2) self.bx.specgram(signalData, NFFT=512, noverlap=256, Fs=samplingFrequency, cmap=plt.cm.get_cmap("jet")) self.bx.set_xlabel('Time (s)') self.bx.set_ylabel('Frequency (Hz)') self.figure.subplots_adjust(hspace=0.5) # refreshs canvas self.canvas.draw()