def test_generator_matrix(self): assert np.array_equal(self.code.generator_matrix, [ komm.int2binlist(i) for i in [ 0x8888, 0xa0a0, 0xaa00, 0xc0c0, 0xcc00, 0xf000, 0xaaaa, 0xcccc, 0xf0f0, 0xff00, 0xffff ] ])
def _simulate(self): phase_offset = self._parameters['phase_offset'] labeling = self._parameters['labeling'] noise_power_db = self._parameters['noise_power_db'] if self._parameters['square']: orders = 4**self._parameters['log_order_0'] base_amplitudes = self._parameters['base_amplitude_0'] else: orders = ( 2**self._parameters['log_order_0'], 2**self._parameters['log_order_1']) base_amplitudes = (self._parameters['base_amplitude_0'], self._parameters['base_amplitude_1']) modulation = komm.QAModulation(orders, base_amplitudes, phase_offset, labeling) modulation._constellation = np.round(modulation._constellation, 12) # Only for pedagogical reasons awgn = komm.AWGNChannel() order = modulation.order num_symbols = 100*order noise_power = 10**(noise_power_db / 10) awgn.signal_power = modulation.energy_per_symbol awgn.snr = awgn.signal_power / noise_power num_bits = modulation.bits_per_symbol * num_symbols bits = np.random.randint(2, size=num_bits) sentword = modulation.modulate(bits) recvword = awgn(sentword) self.output = { 'title': str(modulation), 'constellation': modulation.constellation, 'labels': [''.join(str(b) for b in komm.int2binlist(modulation.labeling[i], width=modulation.bits_per_symbol)) for i in range(order)], 'gaussian_clouds': recvword, }
def _simulate(self): order = 2**self._parameters['log_order'] amplitude = self._parameters['amplitude'] phase_offset = self._parameters['phase_offset'] labeling = self._parameters['labeling'] noise_power_db = self._parameters['noise_power_db'] modulation = komm.PSKModulation(order, amplitude, phase_offset, labeling) modulation._constellation = np.round(modulation._constellation, 12) # Only for pedagogical reasons awgn = komm.AWGNChannel() num_symbols = 200*order noise_power = 10**(noise_power_db / 10) awgn.signal_power = modulation.energy_per_symbol awgn.snr = awgn.signal_power / noise_power num_bits = modulation.bits_per_symbol * num_symbols bits = np.random.randint(2, size=num_bits) sentword = modulation.modulate(bits) recvword = awgn(sentword) self.output = { 'title': str(modulation), 'constellation': modulation.constellation, 'labels': [''.join(str(b) for b in komm.int2binlist(modulation.labeling[i], width=modulation.bits_per_symbol)) for i in range(order)], 'gaussian_clouds': recvword, }
def test_terminated_convolutional_code_encoders(mode, feedforward_polynomials): convolutional_code = komm.ConvolutionalCode( feedforward_polynomials=feedforward_polynomials) code = komm.TerminatedConvolutionalCode(convolutional_code, num_blocks=5, mode=mode) for i in range(2**code.dimension): message = komm.int2binlist(i, width=code.dimension) assert np.array_equal( code.encode(message, method='generator_matrix'), code.encode(message, method='finite_state_machine'))
def test_lfsr_sequence(): lfsr = komm.LFSRSequence( feedback_polynomial=komm.BinaryPolynomial.from_exponents([5, 2, 0])) assert np.array_equal(lfsr.bit_sequence, [ 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1 ]) lfsr = komm.LFSRSequence(feedback_polynomial=0b10000001001) assert np.array_equal( lfsr.bit_sequence[:200], komm.int2binlist(0xcd698970bd55fe82a5e2bdd4dc8e3ff01c3f713e33eb2c9200, 200))
def test_fsm_forward_backward(): # Lin.Costello.04, p. 572-575. fsm = komm.FiniteStateMachine(next_states=[[0,1], [1,0]], outputs=[[0,3], [2,1]]) input_posteriors = fsm.forward_backward( observed_sequence=-np.array([(0.8, 0.1), (1.0, -0.5), (-1.8, 1.1), (1.6, -1.6)]), metric_function=lambda y, z: 0.5 * np.dot(z, (-1)**komm.int2binlist(y, width=len(z))), initial_state_distribution=[1, 0], final_state_distribution=[1, 0]) with np.errstate(divide='ignore'): llr = np.log(input_posteriors[:, 0] / input_posteriors[:, 1]) assert np.allclose(-llr, [0.48, 0.62, -1.02, 2.08], atol=0.05) # Abrantes.10, p.434-437 fsm = komm.FiniteStateMachine(next_states=[[0,2], [0,2], [1,3], [1,3]], outputs=[[0,3], [3,0], [1,2], [2,1]]) input_posteriors = fsm.forward_backward( observed_sequence=-np.array([(0.3, 0.1), (-0.5, 0.2), (0.8, 0.5), (-0.5, 0.3), (0.1, -0.7), (1.5, -0.4)]), metric_function=lambda y, z: 2.5 * np.dot(z, (-1)**komm.int2binlist(y, width=len(z))), initial_state_distribution=[1, 0, 0, 0], final_state_distribution=[1, 0, 0, 0]) with np.errstate(divide='ignore'): llr = np.log(input_posteriors[:,0] / input_posteriors[:,1]) assert np.allclose(-llr, [1.78, 0.24, -1.97, 5.52, -np.inf, -np.inf], atol=0.05)
def test_terminated_convolutional_code_zero_termination( feedforward_polynomials, feedback_polynomials): convolutional_code = komm.ConvolutionalCode(feedforward_polynomials, feedback_polynomials) code = komm.TerminatedConvolutionalCode(convolutional_code, num_blocks=5, mode='zero-termination') for message_int in range(2**code.dimension): print(message_int, 2**code.dimension) message = komm.int2binlist(message_int, width=code.dimension) tail = np.dot(message, code._tail_projector) % 2 input_sequence = komm.pack(np.concatenate([message, tail]), width=convolutional_code._num_input_bits) _, fs = convolutional_code._finite_state_machine.process( input_sequence, initial_state=0) assert fs == 0
def constellation_demo(modulation, noise_power_db, xlim, ylim): awgn = komm.AWGNChannel() num_symbols = 10000 noise_power = 10**(noise_power_db / 10) awgn.signal_power = modulation.energy_per_symbol awgn.snr = awgn.signal_power / noise_power num_bits = modulation.bits_per_symbol * num_symbols bits = np.random.randint(2, size=num_bits) sentword = modulation.modulate(bits) recvword = awgn(sentword) _, ax = plt.subplots(figsize=(16, 10)) ax.scatter(recvword.real, recvword.imag, color='xkcd:light blue', s=1) ax.scatter(modulation.constellation.real, modulation.constellation.imag, color='xkcd:blue', s=8**2) for (i, point) in enumerate(modulation.constellation): binary_label = ''.join( str(b) for b in komm.int2binlist(modulation.labeling[i], width=modulation.bits_per_symbol)) ax.text(point.real, point.imag + 0.075 * xlim[0], binary_label, horizontalalignment='center') ax.set_title(repr(modulation)) ax.set_xlabel('Re') ax.set_ylabel('Im') ax.axis('square') ax.set_xlim(xlim) ax.set_ylim(ylim) ax.grid() info_text = 'SNR = {:.1f} dB\n'.format(10 * np.log10(awgn.snr)) info_text += 'Eb/N0 = {:.1f} dB'.format( 10 * np.log10(awgn.snr / modulation.bits_per_symbol)) ax.text(1.125 * xlim[1], 0.0, info_text, horizontalalignment='left', verticalalignment='center') plt.show()
def test_convolutional_stream_decoder(): # Abrantes.10, p. 307. code = komm.ConvolutionalCode(feedforward_polynomials=[[0b111, 0b101]]) traceback_length = 12 convolutional_decoder = komm.ConvolutionalStreamDecoder(code, traceback_length, input_type='hard') recvword = np.array([1,1, 0,0, 0,0, 0,0, 1,0, 0,1, 0,0, 0,1, 0,1, 1,1]) recvword_ = np.concatenate([recvword, np.zeros(traceback_length*code.num_output_bits, dtype=np.int)]) message_hat = convolutional_decoder(recvword_) message_hat_ = message_hat[traceback_length :] assert np.array_equal(message_hat_, [1, 0, 1, 1, 1, 0, 1, 1, 0, 0]) @pytest.mark.parametrize('feedforward_polynomials, feedback_polynomials, message, codeword', [ ([[0o7, 0o5]], None, komm.int2binlist(0xcd698970bd55fe82a5e2bdd4dc8e3ff01c3f713e33eb2c9200, 200), komm.int2binlist(0xbe84a1facdf49b0d258444495561c0d11f496cd12589847e89bdce6ce5555b0039b0e5589b37e56cebe5612bd2bdf7dc0000, 400)), ([[0o117, 0o155]], None, komm.int2binlist(0xcd698970bd55fe82a5e2bdd4dc8e3ff01c3f713e33eb2c9200, 200), komm.int2binlist(0x3925a704c66355eb62f33de3c4512d01a6d681376ccec5f7fb8091ba4ff29b35456641cf63217ab7fd748a0560b5d4dc0000, 400)), ([[0o31, 0o27, 0o00], [0o00, 0o12, 0o15]], None, komm.int2binlist(0xcd698970bd55fe82a5e2bdd4dc8e3ff01c3f713e33eb2c9200, 200), komm.int2binlist(0x6c889449f6801e93daf4e498ccf75404897d7459ce571f1581a4d05b2011986c0c8501d4000, 300)), ([[0o27, 0o31]], [0o27], komm.int2binlist(0xcd698970bd55fe82a5e2bdd4dc8e3ff01c3f713e33eb2c9200, 200), komm.int2binlist(0x525114c160c91f2ac5511933f5d6ea2eceb9f48cc779f998d9d86a762d57df2a23daa7551f298d762d85d6e70e526b2c0000, 400)), ]) def test_convolutional_stream_encoder_2(feedforward_polynomials, feedback_polynomials, message, codeword):
def metric_function(y, z): s = komm.int2binlist(y, width=len(z)) return np.count_nonzero(z != s)
def metric_function(y, z): y = (-1)**komm.int2binlist(y, width=len(z)) return -np.dot(z, y)
def modulation_power(self): X = QAMModem(self.mod_order) x = np.reshape(np.fliplr(int2binlist(np.arange(0, self.mod_order, 1), int(np.sqrt(self.mod_order))).T), (1, self.mod_order * int(np.sqrt(self.mod_order)))) xx = X.modulate(x[0]) mod_power = np.sqrt(np.mean((xx) * (xx.T).conj().T)) return mod_power