def test_complex_to_raw(): """Test complex-to-raw conversion.""" raw_list = [0, 0, 127, 128, 255, 255] complex_list = [-0.9953-0.9953j, -0.0031+0.0047j, 0.9969+0.9969j] raw = np.array(raw_list, dtype=np.uint8) complex_array = np.array(complex_list, dtype=np.complex64) actual_raw = block_data.complex_to_raw(complex_array) npt.assert_array_equal(actual_raw, raw)
def plot_sample_histogram(self, ax): """Plot sample value histogram.""" raw = block_data.complex_to_raw(self.unsynced) hist = [0] * 256 for value in raw: hist[value] += 1 ax.plot(np.array(hist) / len(self.unsynced) / 2 * 100) ax.set_ylim(0, 100) ax.set_xlim(0, 255) ax.set_xlabel('Raw byte value') ax.set_ylabel('Occurrence (%)') ax.set_title('Sample value histogram') ax.grid()
def test_card_reader(): """Basic test for card_reader""" stream = io.BytesIO("# Some comments\n" "# more comments\n" "1000.5425 10 r0+Om5==\n" "1000.5442 20 aaaaaa==") blocks = list(block_data.card_reader(stream)) timestamps, indices, data = zip(*blocks) chars = [tuple(block_data.complex_to_raw(x)) for x in data] assert timestamps == (1000.5425, 1000.5442) assert indices == (10, 20) assert chars == [(175, 79, 142, 155), (105, 166, 154, 105)]
def _main(): parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) parser.add_argument('input', nargs='?', type=argparse.FileType('rb'), default='-', help="input data ('-' streams from stdin)") parser.add_argument('-i', '--integrate', type=int, default=100, help="Number of blocks to integrate over") setting_keys = ['block_size', 'block_history'] config, args = settings.load_args(parser, setting_keys) blocks = block_reader(args.input, config.block_size, config.block_history) fft_sum = np.zeros(config.block_size, dtype=np.float) hist_sum = np.zeros(256) fft_freqs = np.fft.fftfreq(config.block_size, 1. / config.block_size) fft_freqs = np.fft.fftshift(fft_freqs) cnt = 0 for _, _, block in blocks: samples = complex_to_raw(block) for s in samples: hist_sum[s] += 1 fft = np.fft.fft(block) fft_mag = np.abs(fft) fft_sum += fft_mag cnt += 1 if cnt == args.integrate: plt.subplot(1, 2, 1) plt.plot(fft_freqs, np.fft.fftshift(fft_sum / args.integrate)) plt.subplot(1, 2, 2) plt.plot(hist_sum * 1. / args.integrate, '.-') plt.xlim([0, 255]) plt.tight_layout() plt.show() fft_sum = np.zeros(config.block_size, dtype=np.float) hist_sum = np.zeros(256) cnt = 0
def test_block_reader(): """Test block_reader size and history.""" stream = io.BytesIO("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b" "\x0c\x0d") blocks = list(block_data.block_reader(stream, 3, 1)) indices = [b[1] for b in blocks] data = [b[2] for b in blocks] raw = [list(block_data.complex_to_raw(d)) for d in data] expected_raw = [[0x7f, 0x7f, 0x00, 0x01, 0x02, 0x03], [0x02, 0x03, 0x04, 0x05, 0x06, 0x07], [0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b]] # Note: The last few samples, [0x0a, 0x0b, 0x0c, 0x0d], will be skipped # since it does not fill an entire block_data. assert raw == expected_raw assert indices == range(len(data))
def test_raw_to_complex_inverse(): """Test that raw-to-complex is inverse of complex-to-raw.""" expected = np.arange(256, dtype=np.uint8) actual = block_data.complex_to_raw(block_data.raw_to_complex(expected)) npt.assert_array_equal(actual, expected)