Exemplo n.º 1
0
def ad_open_device_out_in():
    dwf_ao_handler, dwf_ai_handler = None, None

    if ad_print_devices_info():
        print("Configuring device")
        dwf_ao_handler = dwf.DwfAnalogOut()
        dwf_ai_handler = dwf.DwfAnalogIn(dwf_ao_handler)

    return dwf_ao_handler, dwf_ai_handler
Exemplo n.º 2
0
    def __init__(self, channel, amplituide, sampl_freq, buff_size):
        gr.sync_block.__init__(self,
            name="AD2_AnalogOut_Custom_f",
            in_sig=[numpy.float32],
            out_sig=None)

        self.rgdSamples = [0] * buff_size
        self.CHANNEL = channel
        self.dwf_ao = dwf.DwfAnalogOut()
        self.dwf_ao.nodeEnableSet(self.CHANNEL, self.dwf_ao.NODE.CARRIER, True)
        self.dwf_ao.nodeFunctionSet(self.CHANNEL, self.dwf_ao.NODE.CARRIER, self.dwf_ao.FUNC.CUSTOM)
        self.dwf_ao.nodeFrequencySet(self.CHANNEL, self.dwf_ao.NODE.CARRIER, int(frequency/buff_size))
        self.dwf_ao.nodeAmplitudeSet(self.CHANNEL, self.dwf_ao.NODE.CARRIER, amplituide)
        self.dwf_ao.nodeDataSet(self.CHANNEL, self.dwf_ao.NODE.CARRIER, self.rgdSamples)
        self.dwf_ao.configure(self.CHANNEL, True)
        self.done = 0
Exemplo n.º 3
0
 def __init__(self, channel, amplituide, offset):
     gr.sync_block.__init__(self,
                            name="AnalogOut_Sine_i",
                            in_sig=[numpy.int32],
                            out_sig=None)
     self.CHANNEL = channel
     self.freqHz = 0
     self.dwf_ao = dwf.DwfAnalogOut()
     self.dwf_ao.nodeEnableSet(self.CHANNEL, self.dwf_ao.NODE.CARRIER, True)
     self.dwf_ao.nodeFunctionSet(self.CHANNEL, self.dwf_ao.NODE.CARRIER,
                                 self.dwf_ao.FUNC.SINE)
     self.dwf_ao.nodeFrequencySet(self.CHANNEL, self.dwf_ao.NODE.CARRIER,
                                  int(self.freqHz))
     self.dwf_ao.nodeAmplitudeSet(self.CHANNEL, self.dwf_ao.NODE.CARRIER,
                                  amplituide)
     self.dwf_ao.nodeOffsetSet(self.CHANNEL, self.dwf_ao.NODE.CARRIER,
                               offset)
     self.dwf_ao.configure(self.CHANNEL, True)
Exemplo n.º 4
0
    def __init__(self, device_number=0, config=0):
        """
        Connect to device with optional configuration.

        It connects to device with device_number as listed by the enumerate_devices() function of this module.
        Note that the default value of 0 will simply connect to the first device found.
        The possible configurations are also returned by enumerate_devices(). Note that enumerate devices is run at time
        of module import and stored in the variable devices. Note that the information returned by enumerate_devices()
        (or stored in the variable devices) can be diplayed in more readable form with the function print_device_list()
        of this module.

        :param device_number: the device number (default: 0)
        :type device_number: int
        :param config: configuration number (default: 0)
        :type config: int
        """
        self.logger = logging.getLogger(__name__)
        super().__init__(device_number, config)

        self.AnalogIn = dwf.DwfAnalogIn(self)
        self.AnalogOut = dwf.DwfAnalogOut(self)
        self.DigitalIn = dwf.DwfDigitalIn(self)
        self.DigitalOut = dwf.DwfDigitalOut(self)

        # Not sure yet what these do:
        self.AnalogIO = dwf.DwfAnalogIO(self)
        self.DigitalIO = dwf.DwfDigitalIO(self)

        # create short name references
        self.ai = self.AnalogIn
        self.ao = self.AnalogOut
        self.di = self.DigitalIn
        self.do = self.DigitalOut

        self.basic_analog_return_std = False  # will be overwritten by preset_basic_analog()
        self._read_timeout = 1  # will be overwritten by preset_basic_analog()
        self._last_ao0 = 0  # will be overwritten by write_analog()
        self._last_ao1 = 0  # will be overwritten by write_analog()
        self._time_stabilized = time.time(
        )  # will be overwritten by write_analog()
        self.preset_basic_analog()

        self.logger.debug('DfwController object created')
Exemplo n.º 5
0
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()
Exemplo n.º 6
0
   Original Author:  Digilent, Inc.
   Original Revision: 10/17/2013

   Requires:                       
       Python 2.7, 3.3 or later
"""

import dwf
import time

#print DWF version
print("DWF Version: " + dwf.FDwfGetVersion())

#open device
print("Opening first device...")
dwf_ao = dwf.DwfAnalogOut()

print("Generating sine wave...")
# enable two channels
dwf_ao.nodeEnableSet(0, dwf_ao.NODE.CARRIER, True)
dwf_ao.nodeEnableSet(1, dwf_ao.NODE.CARRIER, True)
# for second channel set master the first channel
dwf_ao.masterSet(1, 0)
# slave channel is controlled by the master channel
# it is enough to set trigger, wait, run and repeat paramters for master channel

# configure enabled channels
dwf_ao.nodeFunctionSet(-1, dwf_ao.NODE.CARRIER, dwf_ao.FUNC.SINE)
dwf_ao.nodeFrequencySet(-1, dwf_ao.NODE.CARRIER, 1000.0)
dwf_ao.nodeAmplitudeSet(-1, dwf_ao.NODE.CARRIER, 1.0)
    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()
Exemplo n.º 8
0
 def __init__(self, device_handle=-1):
     super().__init__(2, has_sync=False)
     self.device = dwf.DwfAnalogOut(device_handle)
     self.device.autoConfigureSet(3)  # turn on dynamic settings