hichamjanati / pyldpc

Creation of LDPC codes & simulation of coding and decoding binary data. Applications to sound and image files.
BSD 3-Clause "New" or "Revised" License
119 stars 32 forks source link

Missing function to encode without noise #17

Open goblin opened 4 years ago

goblin commented 4 years ago

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.

lingr7 commented 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.
goblin commented 4 years ago

@lingr7 yeah, I saw that function. That's the one that adds noise along with encoding. Not sure what you mean.

hichamjanati commented 4 years ago

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.

jrsuw commented 4 months ago

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.