Пример #1
0
    def create_sinusoid_file(self, sample_rate, samples):
        cal = Calibration()
        cal.current_offset[:7] = -3000
        cal.current_gain[:7] = [1e-3, 1e-4, 1e-5, 1e-6, 1e-7, 1e-8, 1e-9]
        cal.voltage_offset[:2] = -3000
        cal.voltage_gain[:2] = [1e-3, 1e-4]
        cal.data = cal.save(bytes([0] * 32))

        fh = io.BytesIO()
        d = DataRecorder(fh, calibration=cal)

        stream_buffer = StreamBuffer(1.0, [100], sample_rate)
        stream_buffer.calibration_set(cal.current_offset, cal.current_gain,
                                      cal.voltage_offset, cal.voltage_gain)
        d.stream_notify(stream_buffer)
        data = self.create_sinusoid_data(sample_rate, samples)

        chunk_size = (sample_rate // 2) * 2
        for i in range(0, 2 * samples, chunk_size):
            stream_buffer.insert_raw(data[i:(i + chunk_size)])
            stream_buffer.process()
            d.stream_notify(stream_buffer)

        d.close()
        fh.seek(0)
        return fh
Пример #2
0
    def _create_large_file(self, samples=None):
        """Create a large file.

        :param samples: The total number of samples which will be rounded
            to a full USB packet.
        :return: (BytesIO file, samples)
        """
        sample_rate = 2000000
        packets_per_burst = 128
        bursts = int(
            np.ceil(samples / (SAMPLES_PER_PACKET * packets_per_burst)))
        stream_buffer = StreamBuffer(1.0, [100], sample_rate)
        samples_total = SAMPLES_PER_PACKET * packets_per_burst * bursts

        fh = io.BytesIO()
        d = DataRecorder(fh)
        d.stream_notify(stream_buffer)
        for burst_index in range(bursts):
            packet_index = burst_index * packets_per_burst
            frames = usb_packet_factory_signal(packet_index,
                                               count=packets_per_burst,
                                               samples_total=samples_total)
            stream_buffer.insert(frames)
            stream_buffer.process()
            d.stream_notify(stream_buffer)
        d.close()
        fh.seek(0)

        # dfr = datafile.DataFileReader(fh)
        # dfr.pretty_print()
        # fh.seek(0)

        return fh, samples_total
Пример #3
0
    def create_sinusoid_file(self, file_duration, input_sample_rate, output_sample_rate,
                             stream_buffer_duration=None, chunk_size=None):
        stream_buffer_duration = 1.0 if stream_buffer_duration is None else float(stream_buffer_duration)
        min_duration = 400000 / output_sample_rate
        stream_buffer_duration = max(stream_buffer_duration, min_duration)
        chunk_size = 1024 if chunk_size is None else int(chunk_size)
        cal = Calibration()
        cal.current_offset[:7] = -3000
        cal.current_gain[:7] = [1e-3, 1e-4, 1e-5, 1e-6, 1e-7, 1e-8, 1e-9]
        cal.voltage_offset[:2] = -3000
        cal.voltage_gain[:2] = [1e-3, 1e-4]
        cal.data = cal.save(bytes([0] * 32))

        fh = io.BytesIO()
        d = DataRecorder(fh, calibration=cal)

        buffer = DownsamplingStreamBuffer(stream_buffer_duration, [100], input_sample_rate, output_sample_rate)
        buffer.calibration_set(cal.current_offset, cal.current_gain, cal.voltage_offset, cal.voltage_gain)
        d.stream_notify(buffer)
        input_samples = int(file_duration * input_sample_rate)
        data = self.create_sinusoid_data(input_sample_rate, input_samples)

        i = 0
        while i < input_samples:
            i_next = min(i + chunk_size, input_samples)
            buffer.insert_raw(data[i:i_next])
            buffer.process()
            d.stream_notify(buffer)
            i = i_next

        d.close()
        fh.seek(0)
        return fh
Пример #4
0
def run(cmd_queue, filehandle, sampling_frequency, calibration):
    r = DataRecorder(filehandle, sampling_frequency, calibration)
    b = StreamBuffer(int(sampling_frequency), [], sampling_frequency)
    b.calibration_set(calibration.current_offset, calibration.current_gain,
                      calibration.voltage_offset, calibration.voltage_gain)
    while True:
        cmd, args = cmd_queue.get()
        if cmd == 'stream_notify':
            raw_data, voltage_range = args
            b.voltage_range = voltage_range
            b.insert_raw(raw_data)
            b.process()
            r.stream_notify(b)
        elif cmd == 'close':
            r.close()
            break
Пример #5
0
    def test_truncated(self):
        stream_buffer = StreamBuffer(400.0, [10], 1000.0)
        stream_buffer.suppress_mode = 'off'

        fh = io.BytesIO()
        d = DataRecorder(fh)
        d.stream_notify(stream_buffer)
        count = 16
        for idx in range(0, 160, count):
            data = usb_packet_factory(idx, count)
            stream_buffer.insert(data)
            stream_buffer.process()
            d.stream_notify(stream_buffer)
        fh.seek(0)
        #r = datafile.DataFileReader(fh)
        #r.pretty_print()
        r = DataReader().open(fh)
    def _create_file(self, packet_index, count=None):
        stream_buffer = StreamBuffer(401.0, [200, 100], 1000.0)
        stream_buffer.suppress_mode = 'off'
        if packet_index > 0:
            data = usb_packet_factory(0, packet_index - 1)
            stream_buffer.insert(data)
            stream_buffer.process()

        fh = io.BytesIO()
        d = DataRecorder(fh)
        d.stream_notify(stream_buffer)
        data = usb_packet_factory(packet_index, count)
        stream_buffer.insert(data)
        stream_buffer.process()
        d.stream_notify(stream_buffer)
        d.close()
        fh.seek(0)
        return fh
Пример #7
0
    def _export_jls(self, data):
        cfg = self._cfg
        sampling_frequency = data.sample_frequency
        stream_buffer = StreamBuffer(sampling_frequency * 2, [],
                                     sampling_frequency=sampling_frequency)
        stream_buffer.calibration_set(data.calibration.current_offset,
                                      data.calibration.current_gain,
                                      data.calibration.voltage_offset,
                                      data.calibration.voltage_gain)
        stream_buffer.voltage_range = data.cmdp['Plugins/#state/voltage_range']
        data_recorder = DataRecorder(cfg['filename'],
                                     calibration=data.calibration.data,
                                     sampling_frequency=sampling_frequency)
        data_recorder.stream_notify(stream_buffer)

        try:
            for block in data:
                log.info('export_jls iteration')
                stream_buffer.insert_raw(block['signals']['raw']['value'])
                stream_buffer.process()
                data_recorder.stream_notify(stream_buffer)
        finally:
            data_recorder.close()
Пример #8
0
    def _create_file(self, packet_index, count=None):
        stream_buffer = StreamBuffer(2000, [10], 1000.0)
        stream_buffer.suppress_mode = 'off'
        if packet_index > 0:
            data = usb_packet_factory(0, packet_index - 1)
            stream_buffer.insert(data)
            stream_buffer.process()

        fh = io.BytesIO()
        d = DataRecorder(fh, sampling_frequency=1000)
        d.stream_notify(stream_buffer)
        data = usb_packet_factory(packet_index, count)
        stream_buffer.insert(data)
        stream_buffer.process()
        d.stream_notify(stream_buffer)
        d.close()
        fh.seek(0)

        # from joulescope import datafile
        # dfr = datafile.DataFileReader(fh)
        # dfr.pretty_print()
        # fh.seek(0)
        return fh