def test_get_exg_ch(self): self.assertEqual(get_exg_ch(EChannelType.EXG_ADS1292R_1_CH1_24BIT), (0, 0)) self.assertEqual(get_exg_ch(EChannelType.EXG_ADS1292R_1_CH2_24BIT), (0, 1)) self.assertEqual(get_exg_ch(EChannelType.EXG_ADS1292R_2_CH1_24BIT), (1, 0)) self.assertEqual(get_exg_ch(EChannelType.EXG_ADS1292R_2_CH2_24BIT), (1, 1))
def _process_exg_signal(self, ch_type: EChannelType, y: np.ndarray) -> np.ndarray: chip_id, ch_id = get_exg_ch(ch_type) exg_reg = self.get_exg_reg(chip_id) gain = exg_reg.get_ch_gain(ch_id) ch_dtype = ChDataTypeAssignment[ch_type] resolution = 8 * ch_dtype.size sensitivity = EXG_ADC_REF_VOLT / (2 ** (resolution - 1) - 1) # According to formula in Shimmer ECG User Guide y_volt = (y - EXG_ADC_OFFSET) * sensitivity / gain return y_volt
def test_post_process_exg_signal(self): exg_reg1 = ExGRegister(b'\x03\x80\x10\x40\x40\x00\x00\x00\x02\x01') exg1_gain = 4 exg_reg2 = ExGRegister(b'\x03\x80\x10\x20\x20\x00\x00\x00\x02\x01') exg2_gain = 2 chip_gain = { 0: exg1_gain, 1: exg2_gain, } samples = { EChannelType.EXG_ADS1292R_1_CH1_24BIT: np.random.randn(1000), EChannelType.EXG_ADS1292R_2_CH2_24BIT: np.random.randn(1000), EChannelType.EXG_ADS1292R_1_CH1_16BIT: np.random.randn(1000), EChannelType.EXG_ADS1292R_2_CH2_16BIT: np.random.randn(1000), } samples_w_ts = {**samples, EChannelType.TIMESTAMP: np.arange(1000)} m_br = Mock(spec=ShimmerBinaryReader) m_br.get_exg_reg.side_effect = lambda x: exg_reg1 if x == 0 else exg_reg2 m_br.read_data.side_effect = lambda: (dict(samples_w_ts), ((), ())) type(m_br).sample_rate = PropertyMock(return_value=1) type(m_br).enabled_sensors = PropertyMock(return_value=[]) type(m_br).has_sync = PropertyMock(return_value=False) type(m_br).has_global_clock = PropertyMock(return_value=False) type(m_br).start_timestamp = PropertyMock(return_value=0) type(m_br).exg_reg1 = PropertyMock(return_value=exg_reg1) type(m_br).exg_reg2 = PropertyMock(return_value=exg_reg2) reader = ShimmerReader(bin_reader=m_br, post_process=False) reader.load_file_data() for ch_type in samples: np.testing.assert_equal(samples[ch_type], reader[ch_type]) reader = ShimmerReader(bin_reader=m_br, post_process=True) reader.load_file_data() for ch in samples: bit = 16 if '16' in ch.name else 24 gain = chip_gain[get_exg_ch(ch)[0]] expected = (samples[ch] - 0) * 2.420 / (2**(bit - 1) - 1) / gain actual = reader[ch] np.testing.assert_almost_equal(actual, expected)