Skip to content

Instantly share code, notes, and snippets.

@japm48
Created February 4, 2024 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save japm48/41bc963efec39e506c17e419c3d1e13f to your computer and use it in GitHub Desktop.
Save japm48/41bc963efec39e506c17e419c3d1e13f to your computer and use it in GitHub Desktop.
import numpy as np
import sionna as sn
import tensorflow as tf
###
# System params
BATCH_SIZE = 1
q_mod = 2 # QPSK
# 1/4-rate repetition code (32, 8). Not odd to try to use powers of 2 where possible.
N_CODED_BITS, K_RAW_BITS = 32, 8
genmat_rep = np.tile(np.eye(K_RAW_BITS), reps=N_CODED_BITS//K_RAW_BITS)
ebno_db = 100.
n0 = sn.utils.ebnodb2no(
ebno_db,
num_bits_per_symbol=q_mod,
coderate=K_RAW_BITS / N_CODED_BITS)
###
# TX/RX chain
source = sn.utils.BinarySource()
encoder = sn.fec.linear.LinearEncoder(genmat_rep)
mapper = sn.mapping.Mapper('qam', num_bits_per_symbol=q_mod)
channel = sn.channel.FlatFadingChannel(1, 1, add_awgn=True, return_channel=True)
demapper = sn.mapping.Demapper('app', 'qam', num_bits_per_symbol=q_mod)
decoder = sn.fec.linear.OSDecoder(encoder=encoder, t=4)
###
# Simulation
# TX
in_bits = source([BATCH_SIZE, K_RAW_BITS])
print(f'{in_bits=}')
coded_bits = encoder(in_bits)
x = mapper(coded_bits)
# Channel
xt = x[..., np.newaxis] # For some reason channel does not like the last dimension !=1
y, h = channel((xt, n0))
sigma_mat = tf.constant(np.atleast_2d(n0).astype('complex64')) # Channel covariance matrix
# RX
x_hat, n0_eff = sn.mimo.lmmse_equalizer(y, h, sigma_mat)
llr = demapper([x_hat[..., 0], n0_eff])
print(f'{llr=}')
coded_bits_hat = decoder(llr)
in_bits_hat = coded_bits_hat[0, 0:K_RAW_BITS]
print(f'{in_bits_hat=}')
ok_dec = np.array_equal(in_bits.numpy(), in_bits_hat.numpy())
print(f'Ok = {ok_dec}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment