Exemplo n.º 1
0
def AcquireData(board):
    # No pre-trigger samples in NPT mode
    preTriggerSamples = 0

    # TODO: Select the number of samples per record.
    postTriggerSamples = 2048

    # TODO: Select the number of records per DMA buffer.
    recordsPerBuffer = 10

    # TODO: Select the number of buffers per acquisition.
    buffersPerAcquisition = 10
    
    # TODO: Select the active channels.
    channels = ats.CHANNEL_A | ats.CHANNEL_B
    channelCount = 0
    for c in ats.channels:
        channelCount += (c & channels == c)

    # TODO: Should data be saved to file?
    saveData = False
    dataFile = None
    if saveData:
        dataFile = open(os.path.join(os.path.dirname(__file__),
                                     "data.bin"), 'wb')

    # Compute the number of bytes per record and per buffer
    memorySize_samples, bitsPerSample = board.getChannelInfo()
    bytesPerSample = (bitsPerSample.value + 7) // 8
    samplesPerRecord = preTriggerSamples + postTriggerSamples
    bytesPerRecord = bytesPerSample * samplesPerRecord
    bytesPerBuffer = bytesPerRecord * recordsPerBuffer * channelCount

    # TODO: Select number of DMA buffers to allocate
    bufferCount = 4

    # Allocate DMA buffers

    sample_type = ctypes.c_uint8
    if bytesPerSample > 1:
        sample_type = ctypes.c_uint16

    buffers = []
    for i in range(bufferCount):
        buffers.append(ats.DMABuffer(board.handle, sample_type, bytesPerBuffer))
    
    # Set the record size
    board.setRecordSize(preTriggerSamples, postTriggerSamples)

    recordsPerAcquisition = recordsPerBuffer * buffersPerAcquisition

    # Configure the board to make an NPT AutoDMA acquisition
    board.beforeAsyncRead(channels,
                          -preTriggerSamples,
                          samplesPerRecord,
                          recordsPerBuffer,
                          recordsPerAcquisition,
                          ats.ADMA_EXTERNAL_STARTCAPTURE | ats.ADMA_NPT)
    


    # Post DMA buffers to board
    for buffer in buffers:
        board.postAsyncBuffer(buffer.addr, buffer.size_bytes)

    start = time.clock() # Keep track of when acquisition started
    try:
        board.startCapture() # Start the acquisition
        print("Capturing %d buffers. Press <enter> to abort" %
              buffersPerAcquisition)
        buffersCompleted = 0
        bytesTransferred = 0
        while (buffersCompleted < buffersPerAcquisition and not
               ats.enter_pressed()):
            # Wait for the buffer at the head of the list of available
            # buffers to be filled by the board.
            buffer = buffers[buffersCompleted % len(buffers)]
            board.waitAsyncBufferComplete(buffer.addr, timeout_ms=5000)
            buffersCompleted += 1
            bytesTransferred += buffer.size_bytes

            # TODO: Process sample data in this buffer. Data is available
            # as a NumPy array at buffer.buffer

            # NOTE:
            #
            # While you are processing this buffer, the board is already
            # filling the next available buffer(s).
            #
            # You MUST finish processing this buffer and post it back to the
            # board before the board fills all of its available DMA buffers
            # and on-board memory.
            #
            # Samples are arranged in the buffer as follows:
            # S0A, S0B, ..., S1A, S1B, ...
            # with SXY the sample number X of channel Y.
            #
            # A 12-bit sample code is stored in the most significant bits of
            # each 16-bit sample value.
            #
            # Sample codes are unsigned by default. As a result:
            # - 0x0000 represents a negative full scale input signal.
            # - 0x8000 represents a ~0V signal.
            # - 0xFFFF represents a positive full scale input signal.
            # Optionaly save data to file
            if dataFile:
                buffer.buffer.tofile(dataFile)

            # Add the buffer to the end of the list of available buffers.
            board.postAsyncBuffer(buffer.addr, buffer.size_bytes)
    finally:
        board.abortAsyncRead()
    # Compute the total transfer time, and display performance information.
    transferTime_sec = time.clock() - start
    print("Capture completed in %f sec" % transferTime_sec)
    buffersPerSec = 0
    bytesPerSec = 0
    recordsPerSec = 0
    if transferTime_sec > 0:
        buffersPerSec = buffersCompleted / transferTime_sec
        bytesPerSec = bytesTransferred / transferTime_sec
        recordsPerSec = recordsPerBuffer * buffersCompleted / transferTime_sec
    print("Captured %d buffers (%f buffers per sec)" %
          (buffersCompleted, buffersPerSec))
    print("Captured %d records (%f records per sec)" %
          (recordsPerBuffer * buffersCompleted, recordsPerSec))
    print("Transferred %d bytes (%f bytes per sec)" %
          (bytesTransferred, bytesPerSec))
Exemplo n.º 2
0
def AcquireData(boards):

    # TODO: Select the total acquisition length in seconds
    acquisitionLength_sec = 1.

    # TODO: Select the number of samples in each DMA buffer
    samplesPerBuffer = 204800

    # TODO: Select the active channels.
    channels = ats.CHANNEL_A
    channelCount = 0
    for c in ats.channels:
        channelCount += (c & channels == c)

    # TODO: Should data be saved to file?
    saveData = False
    dataFile = None
    if saveData:
        dataFile = open(os.path.join(os.path.dirname(__file__), "data.bin"),
                        'wb')

    # Make sure that boards[0] is the system's master
    if boards[0].boardId != 1:
        raise ValueError("The first board passed should be the master.")
    for board in boards:
        if board.systemId != boards[0].systemId:
            raise ValueError("All the boards should be of the same system.")

    # Compute the number of bytes per record and per buffer
    memorySize_samples, bitsPerSample = boards[0].getChannelInfo()
    bytesPerSample = (bitsPerSample.value + 7) // 8
    bytesPerBuffer = bytesPerSample * samplesPerBuffer * channelCount
    # Calculate the number of buffers in the acquisition
    samplesPerAcquisition = int(samplesPerSec * acquisitionLength_sec + 0.5)
    buffersPerAcquisition = ((samplesPerAcquisition + samplesPerBuffer - 1) //
                             samplesPerBuffer)

    # TODO: Select number of DMA buffers to allocate
    bufferCount = 4

    buffers = []
    for b in range(len(boards)):
        # Allocate DMA buffers

        sample_type = ctypes.c_uint8
        if bytesPerSample > 1:
            sample_type = ctypes.c_uint16

        buffers.append([])
        for i in range(bufferCount):
            buffers[b].append(
                ats.DMABuffer(board.handle, sample_type, bytesPerBuffer))

        boards[b].beforeAsyncRead(
            channels,
            0,  # Must be 0
            samplesPerBuffer,
            1,  # Must be 1
            0x7FFFFFFF,  # Ignored
            ats.ADMA_EXTERNAL_STARTCAPTURE | ats.ADMA_CONTINUOUS_MODE
            | ats.ADMA_FIFO_ONLY_STREAMING)

        # Post DMA buffers to board
        for buffer in buffers[b]:
            boards[b].postAsyncBuffer(buffer.addr, buffer.size_bytes)

    start = time.clock()  # Keep track of when acquisition started
    try:
        boards[0].startCapture()  # Start the acquisition
        buffersPerAcquisitionAllBoards = len(boards) * buffersPerAcquisition
        print("Capturing {} buffers. Press <enter> to abort".format(
            buffersPerAcquisitionAllBoards))
        buffersCompletedPerBoard = 0
        buffersCompletedAllBoards = 0
        bytesTransferredAllBoards = 0
        while (buffersCompletedPerBoard < buffersPerAcquisition
               and not ats.enter_pressed()):
            for b in range(len(boards)):
                # Wait for the buffer at the head of the list of available
                # buffers to be filled by the board.
                buffer = buffers[b][buffersCompletedPerBoard % len(buffers[b])]
                boards[b].waitAsyncBufferComplete(buffer.addr, timeout_ms=5000)
                buffersCompletedAllBoards += 1
                bytesTransferredAllBoards += buffer.size_bytes

                # TODO: Process sample data in this buffer. Data is available
                # as a NumPy array at buffer.buffer

                # NOTE:
                #
                # While you are processing this buffer, the board is already
                # filling the next available buffer(s).
                #
                # You MUST finish processing this buffer and post it back to the
                # board before the board fills all of its available DMA buffers
                # and on-board memory.
                #
                # Samples are arranged in the buffer as follows:
                # S0A, S0B, ..., S1A, S1B, ...
                # with SXY the sample number X of channel Y.
                #
                # A 12-bit sample code is stored in the most significant bits of
                # each 16-bit sample value.
                #
                # Sample codes are unsigned by default. As a result:
                # - 0x0000 represents a negative full scale signal.
                # - 0x8000 represents a ~0V signal.
                # - 0xFFFF represents a positive full scale signal.
                # Optionaly save data to file
                if dataFile:
                    buffer.buffer.tofile(dataFile)

                # Add the buffer to the end of the list of available buffers.
                boards[b].postAsyncBuffer(buffer.addr, buffer.size_bytes)
            buffersCompletedPerBoard += 1
    finally:
        board.abortAsyncRead()

    # Compute the total transfer time, and display performance information.
    transferTime_sec = time.clock() - start
    print("Capture completed in %f sec" % transferTime_sec)
    buffersPerSec = 0
    bytesPerSec = 0
    if transferTime_sec > 0:
        buffersPerSec = buffersCompletedAllBoards / transferTime_sec
        bytesPerSec = bytesTransferredAllBoards / transferTime_sec
    print("Captured %d buffers (%f buffers per sec)" %
          (buffersCompletedAllBoards, buffersPerSec))
    print("Transferred %d bytes (%f bytes per sec)" %
          (bytesTransferredAllBoards, bytesPerSec))
Exemplo n.º 3
0
def acquire_data(daq_params, alazar_params, board, verbose=True):
    rec_avg_all = []
    rec_readout = []
    
    # No pre-trigger samples in NPT mode
    preTriggerSamples = 0

    # Select the number of samples per record.
    post_trigger_samples = alazar_params.post_trigger_samples

    # Select the number0 of records per DMA buffer.
    records_per_buffer = alazar_params.records_per_buffer #2**10 # up to 2**14

    # Select the number of buffers per acquisition.
    buffers_per_acquisition = alazar_params.buffers_per_acquisition
    
    records_per_acquisition = records_per_buffer * buffers_per_acquisition
    
    # Select the active channels.
    channels = ats.CHANNEL_A | ats.CHANNEL_B
    channelCount = 0
    for c in ats.channels:
        channelCount += (c & channels == c)

    # Should data be saved to file?
    saveData = False
    dataFile = None
    if saveData:
        dataFile = open(os.path.join(os.path.dirname(__file__),
                                     "data.bin"), 'wb')

    # Compute the number of bytes per record and per buffer 
    memorySize_samples, bitsPerSample = board.getChannelInfo()
    bytesPerSample = (bitsPerSample.value + 7) // 8
    samplesPerRecord = preTriggerSamples + post_trigger_samples
    bytesPerRecord = bytesPerSample * samplesPerRecord
    bytesPerBuffer = bytesPerRecord * records_per_buffer * channelCount

    # Select number of DMA buffers to allocate
    buffer_count = alazar_params.buffer_count

    # Allocate DMA buffers

    sample_type = ctypes.c_uint8
    if bytesPerSample > 1:
        sample_type = ctypes.c_uint16

    buffers = []
    for i in range(buffer_count):
        buffers.append(ats.DMABuffer(board.handle, sample_type, bytesPerBuffer))
    
    # Set the record size
    board.setRecordSize(preTriggerSamples, post_trigger_samples)

    # Configure the board to make an NPT AutoDMA acquisition
    board.beforeAsyncRead(channels,
                          -preTriggerSamples,
                          samplesPerRecord,
                          records_per_buffer,
                          records_per_acquisition,
                          ats.ADMA_EXTERNAL_STARTCAPTURE | ats.ADMA_NPT)

    index_avg_start = daq_params.readout_start
    index_avg_end = daq_params.readout_start + daq_params.readout_duration - 1

    index_ch = [None]*2
    index_ch[0] = np.arange(0,post_trigger_samples*records_per_buffer) # channel A
    index_ch[1] = post_trigger_samples*records_per_buffer + np.arange(0,post_trigger_samples*records_per_buffer) # channel B
    
    rec_all_raw = [None]*2
    rec_avg_all = [None]*2
    rec_readout = [[]]*2

    # Post DMA buffers to board
    for buffer in buffers:
        board.postAsyncBuffer(buffer.addr, buffer.size_bytes)

    start = time.clock() # Keep track of when acquisition started
    
    # start SRS DG535 triggers
    dg535_control.set_state(1)
    
    try:
        board.startCapture() # Start the acquisition
        if verbose:
            print("Capturing %d buffers. Press <enter> to abort" %
                  buffers_per_acquisition)
        buffersCompleted = 0
        bytesTransferred = 0
        while (buffersCompleted < buffers_per_acquisition and not
               ats.enter_pressed()):
            # Wait for the buffer at the head of the list of available
            # buffers to be filled by the board.
            buffer = buffers[buffersCompleted % len(buffers)]
            board.waitAsyncBufferComplete(buffer.addr, timeout_ms=5000)
            buffersCompleted += 1
            bytesTransferred += buffer.size_bytes

            #
            for idx, idx_ch in enumerate(index_ch):
                rec_all_raw[idx] = np.reshape(buffer.buffer[idx_ch], (records_per_buffer, post_trigger_samples))
                
            #
            rec_all = rotate_iq(daq_params.iq_angle_deg, rec_all_raw)
            
            #
            for idx in [0, 1]:
                rec_avg_all[idx] = np.mean(rec_all[idx], axis=0) # is this just the avg of the last loop?
                rec_readout[idx] = np.concatenate((rec_readout[idx], np.mean(rec_all[idx][:,index_avg_start:index_avg_end], axis=1)))
            
            # NOTE:
            #
            # While you are processing this buffer, the board is already
            # filling the next available buffer(s).
            #
            # You MUST finish processing this buffer and post it back to the
            # board before the board fills all of its available DMA buffers
            # and on-board memory.
            #
            # Samples are arranged in the buffer as follows:
            # S0A, S0B, ..., S1A, S1B, ...
            # with SXY the sample number X of channel Y.
            #
            # Sample code are stored as 8-bit values.
            #
            # Sample codes are unsigned by default. As a result:
            # - 0x00 represents a negative full scale input signal.
            # - 0x80 represents a ~0V signal.
            # - 0xFF represents a positive full scale input signal.
            # Optionaly save data to file
            if dataFile:
                buffer.buffer.tofile(dataFile)

            # Add the buffer to the end of the list of available buffers.
            board.postAsyncBuffer(buffer.addr, buffer.size_bytes)
    finally:
        board.abortAsyncRead()
        
    # stop SRS DG535 triggers
    dg535_control.set_state(0)
    
    # Compute the total transfer time, and display performance information.
    if verbose:
        transferTime_sec = time.clock() - start
        print("Capture completed in %f sec" % transferTime_sec)
        buffersPerSec = 0
        bytesPerSec = 0
        recordsPerSec = 0
        if transferTime_sec > 0:
            buffersPerSec = buffersCompleted / transferTime_sec
            bytesPerSec = bytesTransferred / transferTime_sec
            recordsPerSec = records_per_buffer * buffersCompleted / transferTime_sec
        print("Captured %d buffers (%f buffers per sec)" %
              (buffersCompleted, buffersPerSec))
        print("Captured %d records (%f records per sec)" %
              (records_per_buffer * buffersCompleted, recordsPerSec))
        print("Transferred %d bytes (%f bytes per sec)" %
              (bytesTransferred, bytesPerSec))
    
    return (rec_avg_all, rec_readout)
Exemplo n.º 4
0
def AcquireData(board):
    # TODO: Select the number of pre-trigger samples
    preTriggerSamples = 1024

    # TODO: Select the number of samples per record.
    postTriggerSamples = 1024

    # TODO: Select the number of records in the acquisition.
    recordsPerCapture = 100

    # TODO: Select the amount of time to wait for the acquisition to
    # complete to on-board memory.
    acquisition_timeout_sec = 10

    # TODO: Select the active channels.
    channels = ats.CHANNEL_A | ats.CHANNEL_B
    channelCount = 0
    for c in ats.channels:
        channelCount += (c & channels == c)

    # TODO: Should data be saved to file?
    saveData = False
    dataFile = None
    if saveData:
        dataFile = open(os.path.join(os.path.dirname(__file__), "data.bin"),
                        'wb')

    # Compute the number of bytes per record and per buffer
    memorySize_samples, bitsPerSample = board.getChannelInfo()
    bytesPerSample = (bitsPerSample.value + 7) // 8
    samplesPerRecord = preTriggerSamples + postTriggerSamples
    bytesPerRecord = bytesPerSample * samplesPerRecord

    # Calculate the size of a record buffer in bytes. Note that the
    # buffer must be at least 16 bytes larger than the transfer size.
    bytesPerBuffer = (bytesPerSample * (samplesPerRecord + 0))

    # Set the record size
    board.setRecordSize(preTriggerSamples, postTriggerSamples)

    # Configure the number of records in the acquisition
    board.setRecordCount(recordsPerCapture)

    start = time.clock()  # Keep track of when acquisition started
    board.startCapture()  # Start the acquisition
    print("Capturing %d record. Press <enter> to abort" % recordsPerCapture)
    buffersCompleted = 0
    bytesTransferred = 0
    while not ats.enter_pressed():
        if not board.busy():
            # Acquisition is done
            break
        if time.clock() - start > acquisition_timeout_sec:
            board.abortCapture()
            raise Exception("Error: Capture timeout. Verify trigger")
        time.sleep(10e-3)

    captureTime_sec = time.clock() - start
    recordsPerSec = 0
    if captureTime_sec > 0:
        recordsPerSec = recordsPerCapture / captureTime_sec
    print("Captured %d records in %f rec (%f records/sec)" %
          (recordsPerCapture, captureTime_sec, recordsPerSec))

    sample_type = ctypes.c_uint8
    if bytesPerSample > 1:
        sample_type = ctypes.c_uint16

    buffer = ats.DMABuffer(board.handle, sample_type, bytesPerBuffer + 16)

    # Transfer the records from on-board memory to our buffer
    print("Transferring %d records..." % recordsPerCapture)

    for record in range(recordsPerCapture):
        if ats.enter_pressed():
            break
        for channel in range(channelCount):
            channelId = ats.channels[channel]
            if channelId & channels == 0:
                continue
            board.read(
                channelId,  # Channel identifier
                buffer.addr,  # Memory address of buffer
                bytesPerSample,  # Bytes per sample
                record + 1,  # Record (1-indexed)
                -preTriggerSamples,  # Pre-trigger samples
                samplesPerRecord)  # Samples per record
            bytesTransferred += bytesPerRecord

            # Records are arranged in the buffer as follows:
            # R0A, R1A, R2A ... RnA, R0B, R1B, R2B ...
            #
            # Sample code are stored as 8-bit values.
            #
            # Sample codes are unsigned by default. As a result:
            # - 0x00 represents a negative full scale input signal.
            # - 0x80 represents a ~0V signal.
            # - 0xFF represents a positive full scale input signal.

            # Optionaly save data to file
            if dataFile:
                buffer.buffer[:samplesPerRecord].tofile(dataFile)

            if ats.enter_pressed():
                break

    # Compute the total transfer time, and display performance information.
    transferTime_sec = time.clock() - start
    bytesPerSec = 0
    if transferTime_sec > 0:
        bytesPerSec = bytesTransferred / transferTime_sec
    print("Transferred %d bytes (%f bytes per sec)" %
          (bytesTransferred, bytesPerSec))
Exemplo n.º 5
0
def AcquireData(board):
    # TODO: Select the number of pre-trigger samples
    preTriggerSamples = 1024

    # TODO: Select the number of samples per record.
    postTriggerSamples = 1024

    # TODO: Select the number of records in the acquisition.
    recordsPerCapture = 100

    # TODO: Select the amount of time to wait for the acquisition to
    # complete to on-board memory.
    acquisition_timeout_sec = 10

    # TODO: Select the active channels.
    channels = ats.CHANNEL_A | ats.CHANNEL_B
    channelCount = 0
    for c in ats.channels:
        channelCount += (c & channels == c)

    # TODO: Should data be saved to file?
    saveData = False
    dataFile = 'TEST'
    if saveData:
        dataFile = open(os.path.join(os.path.dirname(__file__),
                                     "data.bin"), 'w')

    # Compute the number of bytes per record and per buffer
    memorySize_samples, bitsPerSample = board.getChannelInfo()
    bytesPerSample = (bitsPerSample.value + 7) // 8
    samplesPerRecord = preTriggerSamples + postTriggerSamples
    bytesPerRecord = bytesPerSample * samplesPerRecord

    # Calculate the size of a record buffer in bytes. Note that the
    # buffer must be at least 16 bytes larger than the transfer size.
    bytesPerBuffer = (bytesPerSample *
                      (samplesPerRecord + 0))

    # Set the record size
    board.setRecordSize(preTriggerSamples, postTriggerSamples)

    # Configure the number of records in the acquisition
    board.setRecordCount(recordsPerCapture)

    start = time.clock() # Keep track of when acquisition started
    board.startCapture() # Start the acquisition
    # print("Capturing %d record. Press <enter> to abort" % recordsPerCapture)
    buffersCompleted = 0
    bytesTransferred = 0
    while not ats.enter_pressed():
        if not board.busy():
            # Acquisition is done
            break
        if time.clock() - start > acquisition_timeout_sec:
            board.abortCapture()
            raise Exception("Error: Capture timeout. Verify trigger")
        time.sleep(10e-3)

    captureTime_sec = time.clock() - start
    recordsPerSec = 0
    if captureTime_sec > 0:
        recordsPerSec = recordsPerCapture / captureTime_sec
    # print("Captured %d records in %f rec (%f records/sec)" %
    #       (recordsPerCapture, captureTime_sec, recordsPerSec))

    buffer = ats.DMABuffer(bytesPerSample, bytesPerBuffer + 16)

    # Transfer the records from on-board memory to our buffer
    print("Transferring %d records..." % recordsPerCapture)

    for record in range(recordsPerCapture):
        if ats.enter_pressed():
            break
        for channel in range(channelCount):
            channelId = ats.channels[channel]
            if channelId & channels == 0:
                continue
            board.read(channelId,             # Channel identifier
                       buffer.addr,           # Memory address of buffer
                       bytesPerSample,        # Bytes per sample
                       record + 1,            # Record (1-indexed)
                       -preTriggerSamples,    # Pre-trigger samples
                       samplesPerRecord)      # Samples per record
            bytesTransferred += bytesPerRecord;

            # Records are arranged in the buffer as follows:
            # R0A, R1A, R2A ... RnA, R0B, R1B, R2B ...
            #
            # Sample code are stored as 8-bit values.
            #
            # Sample codes are unsigned by default. As a result:
            # - 0x00 represents a negative full scale input signal.
            # - 0x80 represents a ~0V signal.
            # - 0xFF represents a positive full scale input signal.

            # Optionaly save data to file
            if dataFile:
                buffer.buffer[:samplesPerRecord].tofile(dataFile)

            if ats.enter_pressed():
                break

    # Compute the total transfer time, and display performance information.
    transferTime_sec = time.clock() - start
    bytesPerSec = 0
    if transferTime_sec > 0:
        bytesPerSec = bytesTransferred / transferTime_sec
    print("Transferred %d bytes (%f bytes per sec)" %
          (bytesTransferred, bytesPerSec))
Exemplo n.º 6
0
def AcquireData(board, fft_module, recordLength_samples):
    # TODO: Specify the number of records per DMA buffer
    recordsPerBuffer = 10

    # TODO: Specify the total number of buffers to capture
    buffersPerAcquisition = 10

    # TODO: Select the active channels.
    channels = ats.CHANNEL_A
    channelCount = 0
    for c in ats.channels:
        channelCount += (c & channels == c)

    # TODO: Select the FFT output format
    outputFormat = ats.FFT_OUTPUT_FORMAT_U16_LOG

    # TODO: Select the presence of NPT footers
    footer = ats.FFT_FOOTER_NONE

    # Compute the number of bytes per record and per buffer
    memorySize_samples, bitsPerSample = board.getChannelInfo()

    # TODO: Should data be saved to file?
    saveData = False
    dataFile = None
    if saveData:
        dataFile = open(os.path.join(os.path.dirname(__file__), "data.bin"),
                        'wb')

    # Configure the FFT
    fftLength_samples = 1
    while fftLength_samples < recordLength_samples:
        fftLength_samples *= 2

    bytesPerOutputRecord = fft_module.fftSetup(channels, recordLength_samples,
                                               fftLength_samples, outputFormat,
                                               footer, 0)

    bytesPerBuffer = bytesPerOutputRecord * recordsPerBuffer

    # TODO: Select number of DMA buffers to allocate
    bufferCount = 4

    # Allocate DMA buffers
    if ((outputFormat == ats.FFT_OUTPUT_FORMAT_U8_LOG)
            or (outputFormat == ats.FFT_OUTPUT_FORMAT_U8_AMP2)):
        sample_type = ctypes.c_uint8
    elif ((outputFormat == ats.FFT_OUTPUT_FORMAT_U16_LOG)
          or (outputFormat == ats.FFT_OUTPUT_FORMAT_U16_AMP2)):
        sample_type = ctypes.c_uint16
    elif (outputFormat == ats.FFT_OUTPUT_FORMAT_U32):
        sample_type = ctypes.c_uint32
    elif ((outputFormat == ats.FFT_OUTPUT_FORMAT_REAL_S32)
          or (outputFormat == ats.FFT_OUTPUT_FORMAT_IMAG_S32)):
        sample_type = ctypes.c_int32
    else:
        sample_type = ctypes.c_float

    buffers = []
    for i in range(bufferCount):
        buffers.append(ats.DMABuffer(board.handle, sample_type,
                                     bytesPerBuffer))

    # Configure the board to make an NPT AutoDMA acquisition
    board.beforeAsyncRead(
        channels, 0, bytesPerOutputRecord, recordsPerBuffer, 0x7FFFFFFF,
        ats.ADMA_EXTERNAL_STARTCAPTURE | ats.ADMA_NPT | ats.ADMA_DSP)

    # Post DMA buffers to board
    for buffer in buffers:
        board.postAsyncBuffer(buffer.addr, buffer.size_bytes)

    start = time.clock()  # Keep track of when acquisition started
    try:
        board.startCapture()  # Start the acquisition
        print("Capturing %d buffers. Press <enter> to abort" %
              buffersPerAcquisition)
        buffersCompleted = 0
        bytesTransferred = 0
        while (buffersCompleted < buffersPerAcquisition
               and not ats.enter_pressed()):
            # Wait for the buffer at the head of the list of available
            # buffers to be filled by the board.
            timeout_ms = 5000
            buffer = buffers[buffersCompleted % len(buffers)]
            board.dspGetBuffer(buffer.addr, timeout_ms)
            buffersCompleted += 1
            bytesTransferred += buffer.size_bytes

            # TODO: Process sample data in this buffer. Data is available
            # as a NumPy array at buffer.buffer

            # NOTE:
            #
            # While you are processing this buffer, the board is already
            # filling the next available buffer(s).
            #
            # You MUST finish processing this buffer and post it back to the
            # board before the board fills all of its available DMA buffers
            # and on-board memory.
            #
            # Samples are arranged in the buffer as follows:
            # S0A, S0B, ..., S1A, S1B, ...
            # with SXY the sample number X of channel Y.
            #
            # A 12-bit sample code is stored in the most significant bits of
            # each 16-bit sample value.
            #
            # Sample codes are unsigned by default. As a result:
            # - 0x0000 represents a negative full scale input signal.
            # - 0x8000 represents a ~0V signal.
            # - 0xFFFF represents a positive full scale input signal.

            # Optionaly save data to file
            if dataFile:
                buffer.buffer.tofile(dataFile)

            # Add the buffer to the end of the list of available buffers.
            board.postAsyncBuffer(buffer.addr, buffer.size_bytes)

    finally:
        board.dspAbortCapture()
        if dataFile:
            dataFile.close()

    # Compute the total transfer time, and display performance information.
    transferTime_sec = time.clock() - start
    print("Capture completed in %f sec" % transferTime_sec)
    buffersPerSec = 0
    bytesPerSec = 0
    recordsPerSec = 0
    if transferTime_sec > 0:
        buffersPerSec = buffersCompleted / transferTime_sec
        bytesPerSec = bytesTransferred / transferTime_sec
        recordsPerSec = recordsPerBuffer * buffersCompleted / transferTime_sec
    print("Captured %d buffers (%f buffers per sec)" %
          (buffersCompleted, buffersPerSec))
    print("Captured %d records (%f records per sec)" %
          (recordsPerBuffer * buffersCompleted, recordsPerSec))
    print("Transferred %d bytes (%f bytes per sec)" %
          (bytesTransferred, bytesPerSec))
Exemplo n.º 7
0
def AcquireData_OCT_RT(board, preTriggerSamples, postTriggerSamples, recordsPerBuffer, buffersPerAcquisition,channel_sel):



	# TODO: Select the active channels.


	if channel_sel == "B":
		channels = ats.CHANNEL_B
	elif channel_sel =="A":
		channels = ats.CHANNEL_A
	channelCount = 0
	for c in ats.channels:
		channelCount += (c & channels == c)


	# Compute the number of bytes per record and per buffer
	memorySize_samples, bitsPerSample = board.getChannelInfo()
	bytesPerSample = (bitsPerSample.value + 7) // 8
	samplesPerRecord = preTriggerSamples + postTriggerSamples
	bytesPerRecord = bytesPerSample * samplesPerRecord
	bytesPerBuffer = bytesPerRecord * recordsPerBuffer * channelCount


	# TODO: Select number of DMA buffers to allocate
	bufferCount = 4

	# Allocate DMA buffers

	sample_type = ctypes.c_uint8
	if bytesPerSample > 1:
		sample_type = ctypes.c_uint16

	buffers = []
	for i in range(bufferCount):
		buffers.append(ats.DMABuffer(board.handle, sample_type, bytesPerBuffer))

	# Set the record size
	board.setRecordSize(preTriggerSamples, postTriggerSamples)

	recordsPerAcquisition = recordsPerBuffer * buffersPerAcquisition

	# Configure the board to make an NPT AutoDMA acquisition
	board.beforeAsyncRead(channels,
						  -preTriggerSamples,
						  samplesPerRecord,
						  recordsPerBuffer,
						  recordsPerAcquisition,
						  ats.ADMA_EXTERNAL_STARTCAPTURE | ats.ADMA_TRADITIONAL_MODE)



	# Post DMA buffers to board
	for buffer in buffers:
		board.postAsyncBuffer(buffer.addr, buffer.size_bytes)

	# start = time.perf_counter() # Keep track of when acquisition started
	try:
		board.startCapture() # Start the acquisition
		print("Capturing %d buffers. Press <enter> to abort" %
			  buffersPerAcquisition)
		buffersCompleted = 0
		bytesTransferred = 0
		while (buffersCompleted < buffersPerAcquisition and not
			   ats.enter_pressed()):
			# Wait for the buffer at the head of the list of available
			# buffers to be filled by the board.
			buffer = buffers[buffersCompleted % len(buffers)]
			board.waitAsyncBufferComplete(buffer.addr, timeout_ms=5000)
			buffersCompleted += 1
			bytesTransferred += buffer.size_bytes

			# TODO: Process sample data in this buffer. Data is available
			# as a NumPy array at buffer.buffer

			# NOTE:
			#
			# While you are processing this buffer, the board is already
			# filling the next available buffer(s).
			#
			# You MUST finish processing this buffer and post it back to the
			# board before the board fills all of its available DMA buffers
			# and on-board memory.
			#
			# Samples are arranged in the buffer as follows:
			# S0A, S0B, ..., S1A, S1B, ...
			# with SXY the sample number X of channel Y.
			#
			# A 12-bit sample code is stored in the most significant bits of
			# each 16-bit sample value.
			#
			# Sample codes are unsigned by default. As a result:
			# - 0x0000 represents a negative full scale input signal.
			# - 0x8000 represents a ~0V signal.
			# - 0xFFFF represents a positive full scale input signal.
			# Optionaly save data to file
			# print(buffer.buffer)
			# if dataFile:
			#     buffer.buffer.tofile(dataFile)
			n = 0
			sampleValue = []
			for i in range(0, len(buffer.buffer)):
				val = bin(buffer.buffer[i] >> 4)
				val = int(val, 2)
				sampleValue.append(val)
			sampleValue = np.array(sampleValue)
			reshapedSampleValue = sampleValue.reshape(recordsPerBuffer*channelCount, -1)
			data_ampl = reshapedSampleValue
			data_ampl_avg = np.mean(data_ampl, axis = 0)
			data_ampl_avg = np.transpose(data_ampl_avg)
			return data_ampl_avg

			# Add the buffer to the end of the list of available buffers.
			board.postAsyncBuffer(buffer.addr, buffer.size_bytes)
	finally:
		board.abortAsyncRead()