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
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
def test_load_save_load(self): c1 = Calibration().load(CAL1, keys=[]) self.assertFalse(c1.signed) data = c1.save(private_key=PRIVATE_KEY) c2 = Calibration().load(data, keys=[PUBLIC_KEY]) np.testing.assert_allclose(CAL1_VK, c2.voltage_offset) np.testing.assert_allclose(CAL1_VG, c2.voltage_gain) np.testing.assert_allclose(CAL1_IK, c2.current_offset) np.testing.assert_allclose(CAL1_1G, c2.current_gain)
def test_save_load_unsigned(self): c1 = Calibration() c1.current_offset = CAL1_IK[:7] c1.current_gain = CAL1_1G[:7] c1.voltage_offset = CAL1_VK c1.voltage_gain = CAL1_VG data = c1.save() c2 = Calibration().load(data, keys=[]) self.assertFalse(c2.signed) np.testing.assert_allclose(CAL1_VK, c2.voltage_offset) np.testing.assert_allclose(CAL1_VG, c2.voltage_gain) np.testing.assert_allclose(CAL1_IK, c2.current_offset) np.testing.assert_allclose(CAL1_1G, c2.current_gain)
def test_save_load_signed(self): c1 = Calibration() c1.current_offset = CAL1_IK[:7] c1.current_gain = CAL1_1G[:7] c1.voltage_offset = CAL1_VK c1.voltage_gain = CAL1_VG data = c1.save(PRIVATE_KEY) # with open(os.path.join(MYPATH, 'calibration_01.dat'), 'wb') as f: # f.write(data) c2 = Calibration().load(data, keys=[PUBLIC_KEY]) self.assertTrue(c2.signed) np.testing.assert_allclose(CAL1_VK, c2.voltage_offset) np.testing.assert_allclose(CAL1_VG, c2.voltage_gain) np.testing.assert_allclose(CAL1_IK, c2.current_offset) np.testing.assert_allclose(CAL1_1G, c2.current_gain)