Parvfect / HelixWorks

Code for the Channels and Decoding Methods
0 stars 1 forks source link

Verify BEC results with Encoding #9

Closed Parvfect closed 1 year ago

Parvfect commented 1 year ago

Involves setup of Encoding Pipeline and adjusting codebase and then verifying results.

Parvfect commented 1 year ago

Went smoother than I thought, just need to fix #8 to get the final results

from graph import TannerGraph
from Hmatrixbaby import ParityCheckMatrix
from bec import generate_input_arr, generate_erasures
import numpy as np

def get_code_words(input_arr, G, ffield=2):
    """ Converts the input array to Code Words for given Generator Matrix """
    # Ensure the product dimensions are aligned
    return np.dot(input_arr, G) % ffield

def bec_channel_simulation(dv, dc, k, n, ffield=2):
    """ Simulates complete Channel Simulation for Binary Erasure Channel including encoding """

    # Initialize ParityCheckMatrix class
    ParityMatrix = ParityCheckMatrix(dv, dc, k, n, ffield)

    # Get the Connections for the Variable Nodes
    Harr = ParityMatrix.get_H_arr()

    # Initialize Tanner Graph and establish it's connections
    graph = TannerGraph(dv, dc, k, n)
    graph.establish_connections(Harr)

    # Create Parity Matrix using generated Harr
    H = ParityMatrix.createHMatrix(Harr)

    # Get Generator Matrix for the corresponding Parity Matrix
    G = ParityMatrix.get_G_from_H(H)

    # Get the Code words by encoding an input array (here - generated randomly)
    C = get_code_words(generate_input_arr(k), G, ffield)

    # Simulate Passing Code through Channel
    C_post_channel = generate_erasures(C, 0.1)

    # Use Tanner Graph to Decode and get Frame Error Rate Curve for the Channel
    graph.assign_values(C_post_channel)
    graph.frame_error_rate(input_arr=C_post_channel, plot=True)

if __name__ == "__main__":
    dv, dc, k, n = 3, 6, 50, 100
    ffield = 2
    bec_channel_simulation(dv, dc, k, n)
Parvfect commented 1 year ago

Does not seem to work - but current G.H = 0, so something weird happening, probably making it hard for the decoder. Let's evaluate

image
Parvfect commented 1 year ago

Yup looks like for Large k-n G.H^T is not equal to zero - look at these two figures

image image

[[1. 2. 2. ... 1. 1. 1.] [1. 2. 4. ... 1. 1. 1.] [2. 1. 2. ... 1. 1. 1.] ... [1. 5. 3. ... 1. 1. 1.] [1. 2. 1. ... 1. 1. 1.] [3. 2. 4. ... 1. 3. 1.]]

What is going on???

Parvfect commented 1 year ago

Pivot switches in rref sometimes make column swtiches - need to be taken care of. Currently fine since G.HT = 0.

Parvfect commented 1 year ago

Not completely sure if rref allows column switches - something to look into

Parvfect commented 1 year ago

TODO - For a fixed small H and small G and one specific code word - one erasure not getting fixed, verify on paper why decoding is not happening

Parvfect commented 1 year ago

Done. Looks like it was the shuffling of Harr within the graph.establish_connections when it is passed as a parameter. Code does need some cleanup and to write tests, but can be postponed until we have a coupon collector simulator.

Code in question

def establish_connections(self, Harr=None):
        """ Establishes connections between variable nodes and check nodes """

        # In case Harr is sent as a parameter
        if Harr is None:
            self.Harr = ParityCheckMatrix(self.dv, self.dc, self.k, self.n).get_H_arr()
        else:
            self.Harr = np.array(Harr)

        Harr = self.Harr//self.dc

        # Divide Harr into dv parts
        Harr = [Harr[i:i+self.dv] for i in range(0, len(Harr), self.dv)]

        # Establish connections
        for (i,j) in enumerate(Harr):
            for k in j:
                self.vns[i].add_link(self.cns[k])
                self.cns[k].add_link(self.vns[i])
Parvfect commented 1 year ago

Results -

Image

Have verified over and over - but will need to go back and add some automated tests