Пример #1
0
def test_decode_stalled():
    a = np.array( [0x04, # 0 sample (chan 0), framecount coming
                   ], dtype=np.uint16)
    result = cDecode.process( a )
    (N,samples,channels,did_overflow,framestamp)=result
    assert N==0
    assert len(samples)==0
    assert len(channels)==0
    assert did_overflow==0
    assert framestamp is None
Пример #2
0
def test_decode1():
    a = np.array( [0x00, # 0 sample (chan 0)
                   0x04, # 0 sample (chan 0), framecount coming
                   0x01, # framecount word 1
                   0x00, # framecount word 2
                   0x00, # framecount word 3
                   0x00, # framecount word 4
                   0x02, # tcnt word
                   0x00, # 0 sample (chan 0)
                   ], dtype=np.uint16)
    result = cDecode.process( a )
    (N,samples,channels,did_overflow,framestamp)=result
    assert N==7
    expected_samples = np.array([0,0],dtype=np.uint16)
    expected_channels = np.array([0,0],dtype=np.uint8)

    assert np.allclose(samples,expected_samples)
    assert samples.shape==expected_samples.shape
    assert samples.dtype==expected_samples.dtype

    assert np.allclose(channels,expected_channels)
    assert channels.shape==expected_channels.shape
    assert channels.dtype==expected_channels.dtype
    def update_analog_input(self):
        """call this function frequently to avoid overruns"""
        new_data_raw = self._trigger_device.get_analog_input_buffer_rawLE()
        self.ain_raw_word_queue.put( new_data_raw )
        data_raw = np.hstack((new_data_raw,self.old_data_raw))
        newdata_all = []
        chan_all = []
        any_overflow = False
        #cum_framestamps = []
        while len(data_raw):
            result = cDecode.process( data_raw )
            (N,samples,channels,did_overflow,framestamp)=result
            if N==0:
                # no data was able to be processed
                break
            data_raw = data_raw[N:]
            newdata_all.append( samples )
            chan_all.append( channels )
            if did_overflow:
                any_overflow = True
            # Save framestamp data.
            # This is not done yet:
            ## if framestamp is not None:
            ##     cum_framestamps.append( framestamp )
        self.old_data_raw = data_raw # save unprocessed data for next run

        if any_overflow:
            # XXX should move to logging the error.
            raise AnalogDataOverflowedError()

        if len(chan_all)==0:
            # no data
            return
        chan_all=np.hstack(chan_all)
        newdata_all=np.hstack(newdata_all)
        USB_channel_numbers = np.unique(chan_all)
        #print len(newdata_all),'new samples on channels',USB_channel_numbers

        ## F_OSC = 8000000.0 # 8 MHz
        ## adc_prescaler = 128
        ## downsample = 20 # maybe 21?
        ## n_chan = 3
        ## F_samp = F_OSC/adc_prescaler/downsample/n_chan
        ## dt=1.0/F_samp
        ## ## print '%.1f Hz sampling. %.3f msec dt'%(F_samp,dt*1e3)
        ## MAXLEN_SEC=0.3
        ## #MAXLEN = int(MAXLEN_SEC/dt)
        MAXLEN = 5000 #int(MAXLEN_SEC/dt)
        ## ## print 'MAXLEN',MAXLEN
        ## ## print

        for USB_chan in USB_channel_numbers:
            vi=self.viewer.usb_device_number2index[USB_chan]
            cond = chan_all==USB_chan
            newdata = newdata_all[cond]

            oldidx = self.viewer.channels[vi].index
            olddata = self.viewer.channels[vi].data

            if len(oldidx):
                baseidx = oldidx[-1]+1
            else:
                baseidx = 0.0
            newidx = np.arange(len(newdata),dtype=np.float)+baseidx

            tmpidx = np.hstack( (oldidx,newidx) )
            tmpdata = np.hstack( (olddata,newdata) )

            if len(tmpidx) > MAXLEN:
                # clip to MAXLEN
                self.viewer.channels[vi].index = tmpidx[-MAXLEN:]
                self.viewer.channels[vi].data = tmpdata[-MAXLEN:]
            else:
                self.viewer.channels[vi].index = tmpidx
                self.viewer.channels[vi].data = tmpdata
Пример #4
0
def easy_decode(data_raw, gain, offset, top):
    """decode output of CamTrig device

    **Arguments**
    data_raw : array of uint16
      The raw data from the device
    gain : float
      The gain of the CamTrig device's clock relative to host clock
    offset : float
      The offset of the CamTrig device's clock relative to host clock
    top : int
      The maximum timer value on CamTrig

    **Returns**
    r : recarray
      The decoded results
    """

    newdata_all = []
    chan_all = []
    any_overflow = False
    cum_timestamps = []
    timestamp_idxs = []
    current_index = 0

    prevdata = None
    prevchan = None

    # Decode all data in this loop
    while len(data_raw):
        result = cDecode.process(data_raw)
        (N, samples, channels, did_overflow, framestamp) = result
        if N == 0:
            # no data was able to be processed
            print 'no data break'
            break
        data_raw = data_raw[N:]
        newdata_all.append(samples)
        chan_all.append(channels)
        current_index += len(samples)

        if framestamp is not None:
            frame, tcnt = framestamp
            f = frame + float(tcnt) / float(top)
            cum_timestamps.append(f * gain + offset)
            timestamp_idxs.append(current_index - 1)  # index of last sample
        if did_overflow:
            any_overflow = True

    if not len(newdata_all):
        return None
    newdata_all = np.hstack(newdata_all)
    chan_all = np.hstack(chan_all)

    # Now sanitize the results

    cum_timestamps = np.array(cum_timestamps)
    timestamp_idxs = np.array(timestamp_idxs)
    # XXX this assumes that no samples are missing... do piecewise if not true.
    gain2, offset2, resids2 = get_gain_offset_resids(input=timestamp_idxs,
                                                     output=cum_timestamps)
    timestamps_all = np.arange(len(newdata_all)) * gain2 + offset2
    USB_channels = np.unique(chan_all).tolist()
    USB_channels.sort()

    arrays = []
    names = []
    shortest = np.inf
    timestamps = None
    for chan in USB_channels:
        cond = chan_all == chan
        if timestamps is None:
            # Ignore the minute timestamp differences between channels
            timestamps = timestamps_all[cond]
        this_data = newdata_all[cond]

        shortest = min(shortest, len(this_data))

        arrays.append(this_data)
        names.append(str(chan))

    arrays.append(timestamps)
    names.append('timestamps')

    for i in range(len(arrays)):
        if len(arrays[i]) > shortest:
            arrays[i] = arrays[i][:shortest]  # trim all to same length
    r = np.core.records.fromarrays(arrays, names=names)
    return r
Пример #5
0
def easy_decode(data_raw, gain, offset, top):
    """decode output of CamTrig device

    **Arguments**
    data_raw : array of uint16
      The raw data from the device
    gain : float
      The gain of the CamTrig device's clock relative to host clock
    offset : float
      The offset of the CamTrig device's clock relative to host clock
    top : int
      The maximum timer value on CamTrig

    **Returns**
    r : recarray
      The decoded results
    """

    newdata_all = []
    chan_all = []
    any_overflow = False
    cum_timestamps = []
    timestamp_idxs = []
    current_index = 0

    prevdata = None
    prevchan = None

    # Decode all data in this loop
    while len(data_raw):
        result = cDecode.process(data_raw)
        (N, samples, channels, did_overflow, framestamp) = result
        if N == 0:
            # no data was able to be processed
            print "no data break"
            break
        data_raw = data_raw[N:]
        newdata_all.append(samples)
        chan_all.append(channels)
        current_index += len(samples)

        if framestamp is not None:
            frame, tcnt = framestamp
            f = frame + float(tcnt) / float(top)
            cum_timestamps.append(f * gain + offset)
            timestamp_idxs.append(current_index - 1)  # index of last sample
        if did_overflow:
            any_overflow = True

    if not len(newdata_all):
        return None
    newdata_all = np.hstack(newdata_all)
    chan_all = np.hstack(chan_all)

    # Now sanitize the results

    cum_timestamps = np.array(cum_timestamps)
    timestamp_idxs = np.array(timestamp_idxs)
    # XXX this assumes that no samples are missing... do piecewise if not true.
    gain2, offset2, resids2 = get_gain_offset_resids(input=timestamp_idxs, output=cum_timestamps)
    timestamps_all = np.arange(len(newdata_all)) * gain2 + offset2
    USB_channels = np.unique(chan_all).tolist()
    USB_channels.sort()

    arrays = []
    names = []
    shortest = np.inf
    timestamps = None
    for chan in USB_channels:
        cond = chan_all == chan
        if timestamps is None:
            # Ignore the minute timestamp differences between channels
            timestamps = timestamps_all[cond]
        this_data = newdata_all[cond]

        shortest = min(shortest, len(this_data))

        arrays.append(this_data)
        names.append(str(chan))

    arrays.append(timestamps)
    names.append("timestamps")

    for i in range(len(arrays)):
        if len(arrays[i]) > shortest:
            arrays[i] = arrays[i][:shortest]  # trim all to same length
    r = np.core.records.fromarrays(arrays, names=names)
    return r
Пример #6
0
    def update_analog_input(self):
        """call this function frequently to avoid overruns"""
        new_data_raw = self._trigger_device.get_analog_input_buffer_rawLE()
        data_raw = np.hstack((new_data_raw, self.old_data_raw))
        self.ain_data_raw = new_data_raw
        newdata_all = []
        chan_all = []
        any_overflow = False
        #cum_framestamps = []
        while len(data_raw):
            result = cDecode.process(data_raw)
            (N, samples, channels, did_overflow, framestamp) = result
            if N == 0:
                # no data was able to be processed
                break
            data_raw = data_raw[N:]
            newdata_all.append(samples)
            chan_all.append(channels)
            if did_overflow:
                any_overflow = True
            # Save framestamp data.
            # This is not done yet:
            ## if framestamp is not None:
            ##     cum_framestamps.append( framestamp )
        self.old_data_raw = data_raw  # save unprocessed data for next run

        if any_overflow:
            # XXX should move to logging the error.
            self.ain_overflowed = 1
            raise AnalogDataOverflowedError()

        if len(chan_all) == 0:
            # no data
            return
        chan_all = np.hstack(chan_all)
        newdata_all = np.hstack(newdata_all)
        USB_channel_numbers = np.unique(chan_all)
        #print len(newdata_all),'new samples on channels',USB_channel_numbers

        ## F_OSC = 8000000.0 # 8 MHz
        ## adc_prescaler = 128
        ## downsample = 20 # maybe 21?
        ## n_chan = 3
        ## F_samp = F_OSC/adc_prescaler/downsample/n_chan
        ## dt=1.0/F_samp
        ## ## print '%.1f Hz sampling. %.3f msec dt'%(F_samp,dt*1e3)
        ## MAXLEN_SEC=0.3
        ## #MAXLEN = int(MAXLEN_SEC/dt)
        MAXLEN = 5000  #int(MAXLEN_SEC/dt)
        ## ## print 'MAXLEN',MAXLEN
        ## ## print

        for USB_chan in USB_channel_numbers:
            vi = self.viewer.usb_device_number2index[USB_chan]
            cond = chan_all == USB_chan
            newdata = newdata_all[cond]

            oldidx = self.viewer.channels[vi].index
            olddata = self.viewer.channels[vi].data

            if len(oldidx):
                baseidx = oldidx[-1] + 1
            else:
                baseidx = 0.0
            newidx = np.arange(len(newdata), dtype=np.float) + baseidx

            tmpidx = np.hstack((oldidx, newidx))
            tmpdata = np.hstack((olddata, newdata))

            if len(tmpidx) > MAXLEN:
                # clip to MAXLEN
                self.viewer.channels[vi].index = tmpidx[-MAXLEN:]
                self.viewer.channels[vi].data = tmpdata[-MAXLEN:]
            else:
                self.viewer.channels[vi].index = tmpidx
                self.viewer.channels[vi].data = tmpdata