Open goblin opened 4 years ago
pyldpc/encoder.py at master · hichamjanati/pyldpc https://github.com/hichamjanati/pyldpc/blob/master/pyldpc/encoder.py
def encode(tG, v, snr, seed=None):
"""Encode a binary message and adds Gaussian noise.
Parameters
----------
tG: array or scipy.sparse.csr_matrix (m, k). Transposed coding matrix
obtained from `pyldpc.make_ldpc`.
v: array (k, ) or (k, n_messages) binary messages to be encoded.
snr: float. Signal-Noise Ratio. SNR = 10log(1 / variance) in decibels.
Returns
-------
y: array (n,) or (n, n_messages) coded messages + noise.
"""
n, k = tG.shape
rng = utils.check_random_state(seed)
d = utils.binaryproduct(tG, v) #encode
x = (-1) ** d #BPSK modulation
sigma = 10 ** (- snr / 20)
e = rng.randn(*x.shape) * sigma #noise
y = x + e
return y #you can easy to set `return d`.then you can get one only encoded.
@lingr7 yeah, I saw that function. That's the one that adds noise along with encoding. Not sure what you mean.
yes it's true. We can set a default snr=None
to mean no noise, in the meantime if you set a very large snr, the decoding would be trivial.
A related issue: I also think that it would be useful for teaching purposes to have separate functions for encoding and for adding noise. Conceptually, encoding and adding noise are logically totally separate, so I find it unhelpful that they are bundled together. With the existing set up, it's not very natural to take the same encoded string, corrupt it with varying amounts of noise, and look at the decoded results in each case.
The function to encode a given message WITHOUT adding noise seems to be missing. How can I encode a message that is meant to be transmitted over a real medium? The medium itself will add plenty of noise, I don't need any extra added.