def getGChMCK(design_p, N, K, runsim):
    p = design_p
    n = int(ma.log(N, 2))
    err = np.zeros(N)
    print "MC(K)..." + str(runsim)
    for i in range(runsim):
        UN = np.random.randint(2, size=N)
        UN_decoded = ec.polarSCdecode(pl.BSCN(p, ec.polarencode(UN, len(UN))),
                                      len(UN), p)
        err = err + np.logical_xor(UN, UN_decoded)

    aZN = err / runsim
    sZN = np.sort(aZN)
    good_channels_all = aZN.argsort().tolist()[:K]
    good_channels = good_channels_all[:K]
    ber_exp = np.log10(sZN).tolist()
    rgood_channels = ec.bitreverseorder(good_channels, n)
    rgood_channels_all = ec.bitreverseorder(good_channels_all, n)

    f2 = open(
        "./simresults/GC/GCMK_" + str(N) + "_" + str(p).replace(".", "p") +
        "_" + str(K) + ".txt", 'w')
    json.dump(rgood_channels, f2)

    f3 = open("./simresults/GC/GCMK_ALL" + str(N) + ".txt", 'w')
    json.dump(rgood_channels_all, f3)

    return (rgood_channels, ber_exp[:K], ber_exp)
示例#2
0
def send_polarfile(XN,N,channel_p,compound_plist_u,derate,use_adjusted_Rate): 

    #----------R and G  here compound plist is not important
	I_ord=pcon.getreliability_order(N)
	compound_plist=list(compound_plist_u) #best channel first
	compound_plist.sort()
	Ratelist = pl.getRatelist(compound_plist,derate)       #best rate first
	Glist=[int(N*r) for r in Ratelist]
	Glist=adjustG(Glist)
	R=pl.getRatelist([channel_p],derate)[0]  #calculates rate for given channel
	G=int(R*N)
	

	Iterhistory={} 
	# for first one Tx
	Iter=0
	Iter_p=compound_plist[0]
	Iter_R=Ratelist[0]
	Iter_G=Glist[0]
	Iter_I=I_ord[:Iter_G]

	
	#------------------for filing Tx side
	# reverse arikan :: THIS IS OF SIZE N 
	UN_N=ec.polarencode(XN,N) 
	
	UN=ec.getUN(UN_N,Iter_I,False)
	Iter_UN_ind=range(len(UN))
	Iter_UN=[UN[i] for i in Iter_UN_ind]
		
	#picking data from frozen channels
	F=list(set(range(N))-set(Iter_I))
	FD=ec.getUN(UN_N,F,True)
	
	Iter_XN=XN
	
	#--------------------Note channel_p used for flipping
	Iter_YN=pl.BSCN(channel_p,Iter_XN)
		
	#-----------------------decoding based on this tx only
	
	#-----------------Rx side
	Iter_UN_hat=ec.polarSCdecodeG(Iter_YN,N,Iter_p,Iter_I,list(FD),False)	
	Iter_UN_decoded=ec.getUN(Iter_UN_hat,Iter_I,False)
		
	#storage needed for final decoding
	Iterhistory[Iter]=[Iter_UN_ind,Iter_UN_decoded,Iter_YN]
	
	
	final_Iter=Iter
	final_decoded= Iterhistory[0][1]
	final_UN_hat=Iter_UN_hat
	
	final_XN=ec.polarencode(final_UN_hat,N)
	
	achieved_rate=float(len(UN))/((final_Iter+1)*N)
	return (achieved_rate,np.array(final_XN))
def get_LLRdictWU(channel_plist, design_p, I, N, runsim, RI):
    LLRdictWU = {}
    G = len(I)
    stamp = datetime.now().strftime("%y-%m-%d_%H-%M-%S")
    f2name = "./simresults/llrsgndict" + "-" + str(N) + "-" + str(
        design_p).replace(".", "p") + "-" + stamp + ".txt"
    f2 = open(
        "./simresults/llrsgndict" + "-" + str(N) + "-" +
        str(design_p).replace(".", "p") + "-" + stamp + ".txt", 'w')
    n = int(ma.log(N, 2))

    for channel_p in channel_plist:
        print "\nrunning for " + str(channel_p) + "..."

        LLRdictWU[str(channel_p)] = []

        for i in range(runsim):
            UN = np.random.randint(2, size=G)
            FD = np.random.randint(2, size=N - len(I))
            #the following are intermediate steps of encoding
            # done to get VN
            # same as XN=ec.polarencodeG(UN,N,I,list(FD),False)
            VN = ec.formVN_u(list(UN), N, I, list(FD))
            XN = ec.polarencrec(VN, n - 1, n)
            YN = pl.BSCN(channel_p, XN)
            (llr, UN_hat) = ec.polarSCdecodeG(YN, N, design_p, I, FD, True)

            UN_decoded = ec.getUN(UN_hat, I, False)

            #---------------------------------

            LLRdictWU[str(channel_p)].append([
                ec.getchannel(llr, RI, False),
                "".join(str(x) for x in ec.getchannel(VN, RI, False)),
                "".join(str(x) for x in ec.getchannel(UN_hat, RI, False))
            ])

    json.dump(LLRdictWU, f2)
    for channel_p in channel_plist:
        with open(
                "./simresults/llrsgndict" + "-" + str(N) + "-" +
                str(design_p).replace(".", "p") + "-On-" +
                str(channel_p).replace(".", "p") + "-" + stamp + ".csv",
                'wb') as resultFile:
            wr = csv.writer(resultFile, dialect='excel')
            wr.writerow(["", "LLR in RI"] + [""] * (N - 1) + ["Data in RI"] +
                        ["Recovered Data in RI"])
            wr.writerow([""] + RI)
            for sim in range(runsim):
                wr.writerow(
                    [str(sim + 1)] + LLRdictWU[str(channel_p)][sim][0] +
                    [str('"') + LLRdictWU[str(channel_p)][sim][1] + str('"')] +
                    [str('"') + LLRdictWU[str(channel_p)][sim][2] + str('"')])
    return f2name
def send_polar(UN, N, channel_p, compound_plist_u, derate, use_adjusted_Rate):
    I_ord = pcon.getreliability_order(N)
    compound_plist = list(compound_plist_u)  #best channel first
    compound_plist.sort()
    Ratelist = pl.getRatelist(compound_plist, derate)  #best rate first
    Glist = [int(N * r) for r in Ratelist]

    Glist = adjustG(Glist)

    R = pl.getRatelist([channel_p],
                       derate)[0]  #calculates rate for given channel
    G = int(R * N)

    Iterhistory = {}
    # for first one Tx
    Iter = 0
    Iter_UN = UN
    Iter_p = compound_plist[0]
    Iter_R = Ratelist[0]
    Iter_G = Glist[0]
    Iter_I = I_ord[:Iter_G]
    Iter_UN_ind = range(len(UN))
    Iter_UN = [UN[i] for i in Iter_UN_ind]

    Iter_D = np.zeros(N - Iter_G, dtype=int).tolist()  #frozen data
    Iter_XN = ec.polarencodeG(Iter_UN, N, Iter_I, list(Iter_D),
                              False)  #data goes in as per R.I

    #--------------------Note channel_p used for flipping
    Iter_YN = pl.BSCN(channel_p, Iter_XN)

    #-----------------------decoding based on this tx only
    Iter_UN_hat = ec.polarSCdecodeG(Iter_YN, N, Iter_p, Iter_I, list(Iter_D),
                                    False)
    Iter_UN_decoded = ec.getUN(Iter_UN_hat, Iter_I, False)

    #storage needed for final decoding
    Iterhistory[Iter] = [Iter_UN_ind, Iter_UN_decoded, Iter_YN]

    final_Iter = Iter
    final_decoded = Iterhistory[0][1]
    achieved_rate = float(len(UN)) / ((final_Iter + 1) * N)
    return (achieved_rate, np.array(final_decoded))
示例#5
0
def polarfile(XN, channel_p, design_p, I):

    p = channel_p
    N = len(XN)
    n = int(ma.log(N, 2))

    #Tx side
    UN = ec.polarencode(XN, N)  # reverse arikan
    #picking data from frozen channels
    F = list(set(range(N)) - set(I))
    FD = ec.getUN(UN, F, True)

    YN = pl.BSCN(p, XN)

    #rx side
    UN_decoded = ec.polarSCdecodeG(YN, N, design_p, I, FD, False)
    XN_decoded = ec.polarencode(UN_decoded, N)

    return XN_decoded
def polarchannel_derate_sim(N, channel_p, design_p, derate, runsim,
                            BER_needed):
    p = channel_p
    I_ord = pcon.getreliability_order(N)
    R = pl.getRatelist([channel_p],
                       derate)[0]  #calculates rate for given channel
    G = int(R * N)
    I = I_ord[:G]

    if BER_needed:
        errcnt = np.zeros(G)

    block_errorcnt = 0
    #print float(len(I))/N
    #UN=np.random.randint(2,size=G)
    #print UN
    for i in range(runsim):
        #print i
        UN = np.random.randint(2, size=G)
        FD = np.zeros(N - G, dtype=int).tolist()  #frozen data
        XN = ec.polarencodeG(UN, N, I, list(FD), False)
        YN = pl.BSCN(p, XN)
        UN_hat = ec.polarSCdecodeG(YN, N, design_p, I, list(FD), False)
        UN_decoded = ec.getUN(UN_hat, I, False)

        if BER_needed:
            errcnt = errcnt + np.logical_xor(UN, UN_decoded)

        if UN.tolist() != UN_decoded.tolist():
            block_errorcnt += 1
        #print UN,YN,UN_decoded
    if BER_needed:
        berN = errcnt / runsim
        ber_exp = np.log10(berN).tolist()

    block_error = float(block_errorcnt) / runsim

    if BER_needed:
        return (ber_exp, float(G) / N, block_error)
    else:
        return (float(G) / N, block_error)
示例#7
0
def frac_goodchannel(channel_plist, design_p, I, N, LT, runsim, RI,
                     LLRdict_needed):
    LLRdict = {}
    Fdict = {}
    G = len(I)
    stamp = datetime.now().strftime("%y-%m-%d_%H-%M-%S")
    #f1=open("./simresults/llrdict"+"-"+str(N)+"-"+str(design_p).replace(".","p")+"-"+stamp+".txt",'w')
    f2 = open(
        "./simresults/llrsgndict" + "-" + str(N) + "-" +
        str(design_p).replace(".", "p") + "-" + stamp + ".txt", 'w')
    FD = np.zeros(N - len(I), dtype=int).tolist()
    for channel_p in channel_plist:
        print "\nrunning for " + str(channel_p) + "..."

        LLRdict[str(channel_p)] = []
        Fdict[str(channel_p)] = []

        for i in range(runsim):

            UN = np.random.randint(2, size=len(I))
            UN_encoded = ec.polarencodeG(UN, N, I, list(FD), False)
            YN = pl.BSCN(channel_p, UN_encoded)
            (llr, d) = ec.polarSCdecodeG(YN, N, design_p, I, list(FD), True)
            L = abs(llr)

            #---------------------------------

            LLRdict[str(channel_p)].append(ec.getchannel(llr, RI, False))
            LLRgoodchannels = abs(np.array(
                LLRdict[str(channel_p)][i][:G])).tolist()
            num_goodchannel = sum(llr > LT for llr in LLRgoodchannels)
            Fdict[str(channel_p)].append(float(num_goodchannel) / N)

    #json.dump(LLRdict,f1);
    json.dump(LLRdict, f2)
    if LLRdict_needed:
        return (LLRdict, Fdict)
    else:
        return Fdict
def polarchannelsim(N, channel_p, design_p, I, runsim, BER_needed):
    p = channel_p
    G = len(I)  #number of good channels
    FD = np.zeros(N - G, dtype=int).tolist()  #frozen data

    if BER_needed:
        errcnt = np.zeros(G)

    block_errorcnt = 0
    #print float(len(I))/N
    #UN=np.random.randint(2,size=G)
    #print UN
    for i in range(runsim):
        #print i
        UN = np.random.randint(2, size=G)

        XN = ec.polarencodeG(UN, N, I, list(FD), False)

        YN = pl.BSCN(p, XN)
        UN_hat = ec.polarSCdecodeG(YN, N, design_p, I, list(FD), False)
        UN_decoded = ec.getUN(UN_hat, I, False)
        if BER_needed:
            errcnt = errcnt + np.logical_xor(UN, UN_decoded)

        if UN.tolist() != UN_decoded.tolist():
            block_errorcnt += 1
        #print UN,YN,UN_decoded
    if BER_needed:
        berN = errcnt / runsim
        ber_exp = np.log10(berN).tolist()

    block_error = float(block_errorcnt) / runsim

    if BER_needed:
        return (ber_exp, block_error)
    else:
        return block_error
示例#9
0
def P_allgood(channel_plist, design_p, I, N, LT, runsim, RI, LLRdict_needed):
    LLRdict = {}
    Pdict = {}

    stamp = datetime.now().strftime("%y-%m-%d_%H-%M-%S")
    f1 = open(
        "./simresults/llrdict" + "-" + str(N) + "-" +
        str(design_p).replace(".", "p") + "-" + stamp + ".txt", 'w')

    FD = np.zeros(N - len(I), dtype=int).tolist()
    for channel_p in channel_plist:
        print "\nrunning for " + str(channel_p) + "..."

        Count_all_good = 0
        LLRdict[str(channel_p)] = []

        for i in range(runsim):

            UN = np.random.randint(2, size=len(I))
            UN_encoded = ec.polarencodeG(UN, N, I, list(FD), False)
            YN = pl.BSCN(channel_p, UN_encoded)
            (llr, d) = ec.polarSCdecodeG(YN, N, design_p, I, list(FD), True)
            L = abs(llr)

            #---------------------------------
            llr_Gmin = min(ec.getchannel(L, I, False))
            LLRdict[str(channel_p)].append(ec.getchannel(L, RI, False))
            Count_all_good += (llr_Gmin >= LT)

        Pdict[str(channel_p)] = float(Count_all_good) / runsim

    json.dump(LLRdict, f1)
    if LLRdict_needed:
        return (LLRdict, Pdict)
    else:
        return Pdict
def send_rateless_kRx(UN, N, channel_p, compound_plist_u, derate,
                      use_adjusted_Rate):
    #print "In send rateless..."
    #Reliability ordering
    #print "R_order"
    I_ord = pcon.getreliability_order(N)
    #print I_ord
    #compound channel
    compound_plist = list(compound_plist_u)  #best channel first
    compound_plist.sort()
    Ratelist = pl.getRatelist(compound_plist, derate)  #best rate first
    Glist = [int(N * r) for r in Ratelist]

    #print "Compund Channel"
    #print plist
    #print Glist
    #print "will be working with below to meet R R/2 R/3 R/4 constraint"
    Glist = adjustG(Glist)
    #print Glist

    #given Channel might not be an entry in compound but within bounds
    R = pl.getRatelist([channel_p],
                       derate)[0]  #calculates rate for given channel
    G = int(R * N)

    #print "Actual channel"
    #print channel_p
    #print G
    if use_adjusted_Rate:
        G = Glist[compound_plist.index(channel_p)]

#----------------------------------------------------Iterations start

    Iterhistory = {}  #contains indexes of UN sent in each iteration

    decoded = False
    # for first Tx
    Iter = 0
    Iter_UN = UN
    Iter_p = compound_plist[0]
    Iter_R = Ratelist[0]
    Iter_G = Glist[0]
    Iter_I = I_ord[:Iter_G]
    Iter_UN_ind = range(len(UN))
    #Iter_data={}
    #print float(len(Iter_I))/N

    #print "Forward decoding"
    while not decoded:

        #print "Iter"+str(Iter)

        Iter_UN = [UN[i] for i in Iter_UN_ind]

        Iter_D = np.zeros(N - Iter_G, dtype=int).tolist()  #frozen data
        Iter_XN = ec.polarencodeG(Iter_UN, N, Iter_I, list(Iter_D),
                                  False)  #data goes in as per R.I

        #--------------------Note channel_p used for flipping
        Iter_YN = pl.BSCN(channel_p, Iter_XN)

        #-----------------------decoding based on this tx only
        Iter_UN_hat = ec.polarSCdecodeG(Iter_YN, N, Iter_p, Iter_I,
                                        list(Iter_D), False)
        Iter_UN_decoded = ec.getUN(Iter_UN_hat, Iter_I, False)

        #storage needed for final decoding
        Iterhistory[Iter] = [Iter_UN_ind, Iter_UN_decoded, Iter_YN]

        #pprint(Iterhistory)

        #For simulation of Rx knows channel case
        #Assuming Rx knows the capacity of the channel
        #hence as long as the rate used is above
        #the capacity is declares Not decodable
        #the rate at which decoding is possible and the rate achieved is same
        #print Iterhistory

        if not is_decodable_kRx(G, Iter_G):

            # picking out all the channels that are suspected to be bad in past
            # iterations and putting them for next iteration.
            # Note first iteration Whole UN is sent
            # in next only suspected bad channels are sent
            prev_I = Iter_I
            Iter += 1

            #New channel params
            Iter_p = compound_plist[Iter]
            Iter_R = Ratelist[Iter]
            Iter_G = Glist[Iter]
            Iter_I = I_ord[:Iter_G]

            tosend_ind = []
            for i in range(Iter):
                #picking out the bad channels from prev iterations
                sent_ind = Iterhistory[i][0]
                sent_ind_last_iter = sent_ind[:Glist[Iter - 1]]

                bad_ind = sent_ind_last_iter[Iter_G:]
                #print bad_ind
                tosend_ind.extend(bad_ind)

            Iter_UN_ind = list(set(tosend_ind))

            Iter_UN_ind.sort()
            #print Iter_UN_ind

        else:
            #the final reliable good channels is that of last iteration
            final_Iter = Iter
            final_G = Iter_G
            #channel_p is already updated
            final_p = Iter_p
            final_I = I_ord[:final_G]
            decoded = True

    #pprint(Iterhistory)

    #final decoding
    #number retrodecoding needed = iter-1

    #print "Final Decoding..."
    #print final_p
    #print final_G
    #print Iter

    for Iter in range(final_Iter - 1, -1, -1):
        #print "Here"
        #print "Iter"+str(Iter)+"-"*10

        #print "Prev frozen"
        Prev_correct_ind = Iterhistory[Iter + 1][0]
        Prev_correct_data = Iterhistory[Iter + 1][1]
        #print Prev_correct_ind
        #print Prev_correct_data

        #print "Inc frozen needed"
        IncFreeze_ind_UN = [
            i for i in Prev_correct_ind if i in Iterhistory[Iter][0]
        ]
        #basically intersection with some order picking the indexes of the prev iter frozen data needed in this iter i.e, 12
        #print IncFreeze_ind_UN

        #picking the data i.e. u12
        IncFreeze_ind_ind = [
            Prev_correct_ind.index(j) for j in IncFreeze_ind_UN
        ]
        IncFreeze_data = [Prev_correct_data[k] for k in IncFreeze_ind_ind]

        #print IncFreeze_data

        #finding the channels where 12 went in this iter 16 15 14 "13"<--- here
        #i.e, removing the top channel_G channels (as they are good) from top Iter_i_G channels
        Iter_G = Glist[Iter]
        IncFreeze_ind = I_ord[:Iter_G][final_G:]
        #print "Inc Frozen Data going in"
        #print IncFreeze_ind

        #print "Frozen zeroes"
        Iter_D = np.zeros(N - Iter_G,
                          dtype=int).tolist()  #frozen data as per iteration
        #print len(Iter_D)

        Iter_YN = Iterhistory[Iter][2]
        Iter_UN_hat = ec.polarIncFrzSCdecodeG(Iter_YN, N, final_p, final_I,
                                              list(Iter_D), IncFreeze_ind,
                                              IncFreeze_data, False)
        Iter_UN_decoded = ec.getUN(Iter_UN_hat, final_I, False)

        #print Iter_UN_decoded

        #history update
        Iterhistory_ind_upd = list(Prev_correct_ind)
        Iterhistory_ind_upd.extend(
            Iterhistory[Iter][0][:final_G])  #ie adding 5,6,11 to 4,10,12
        Iterhistory_data_upd = np.hstack(
            (Prev_correct_data, Iter_UN_decoded))  #same as extend

        #print "Update"
        #print Iterhistory_ind_upd
        #print Iterhistory_data_upd

        #print "sorted"

        (Iterhistory[Iter][0],
         Iterhistory[Iter][1]) = ml.sortAextend(Iterhistory_ind_upd,
                                                Iterhistory_data_upd.tolist())
        #print Iterhistory[Iter][0]
        #print Iterhistory[Iter][1]

    #pprint(Iterhistory)
    final_decoded = Iterhistory[0][1]
    achieved_rate = float(len(UN)) / ((final_Iter + 1) * N)
    return (achieved_rate, np.array(final_decoded))
json.dump(len(B), f1)
f1.write("\n")
json.dump("sim ran :" + str(runsim), f1)
f1.write("\n")
#=======================================================Simulation
#Frozen data
D = np.zeros(N - len(I), dtype=int).tolist()

#---------------------------------------------finding lambda-threshold
lambda_thresholdlist = np.zeros(N)
lambda_threshold1list = np.zeros(N)
lambda_threshold2list = np.zeros(N)
for i in range(runsim_th):
    UN = np.random.randint(2, size=len(I))
    UN_encoded = ec.polarencodeG(UN, N, I, list(D), False)
    YN = pl.BSCN(design_p, UN_encoded)
    (llr, d) = ec.polarSCdecodeG(YN, N, design_p, I, list(D), True)
    L = abs(llr) / runsim_th
    lambda_thresholdlist = lambda_thresholdlist + L

lambda_threshold1 = min(ec.getchannel(lambda_thresholdlist, I, False))
lambda_threshold2 = max(ec.getchannel(lambda_thresholdlist, B, False))
lambda_threshold = (lambda_threshold1 + lambda_threshold2) / 2

print[lambda_threshold1, lambda_threshold2, lambda_threshold]
json.dump([lambda_threshold1, lambda_threshold2, lambda_threshold], f1)
f1.write("\n")

used_lambda_threshold = lambda_threshold1
print used_lambda_threshold
json.dump("used_lambda_threshold" + str(used_lambda_threshold), f1)
示例#12
0
D = np.zeros(N - len(I), dtype=int).tolist()

# ---------------------------------------------finding lambda-threshold
# threshold if found for design=channel and design<channel(channel being the second best in compound channel)

lambda_Emindict = {}
lambda_minEdict = {}

for channel_p in channel_plist:
    lambda_thresholdlist = np.zeros(N)
    lambda_Emin = 0

    for i in range(runsim_th):
        UN = np.random.randint(2, size=len(I))
        UN_encoded = ec.polarencodeG(UN, N, I, list(D), False)
        YN = pl.BSCN(channel_p, UN_encoded)

        (llr, d) = ec.polarSCdecodeG(YN, N, design_p, I, list(D), True)

        L = abs(llr) / runsim_th

        # expected value of min of good channels
        lambda_Emin += min(ec.getchannel(L, I, False))

        # expected Value of all channels(a vector)
        lambda_thresholdlist = lambda_thresholdlist + L

    lambda_Emindict[str(channel_p)] = lambda_Emin

    # minimum of expected value of goodchannels
    lambda_minE = min(ec.getchannel(lambda_thresholdlist, I, False))
示例#13
0
def send_rateless_file_kRx(XN,N,channel_p,compound_plist_u,derate,use_adjusted_Rate): 

	I_ord=pcon.getreliability_order(N)
	compound_plist=list(compound_plist_u) #best channel first
	compound_plist.sort()
	Ratelist = pl.getRatelist(compound_plist,derate)       #best rate first
	Glist=[int(N*r) for r in Ratelist]
	
	#print "will be working with below to meet R R/2 R/3 R/4 constraint"
	Glist=adjustG(Glist)
	R=pl.getRatelist([channel_p],derate)[0]  #calculates rate for given channel
	G=int(R*N)

	if use_adjusted_Rate:
		G=Glist[compound_plist.index(channel_p)]	
	
    #----------------------------------------------------Iterations start
	
	Iterhistory={} #contains indexes of UN sent in each iteration
	decoded=False
	
	#------------------for filing Tx side
	# reverse arikan :: THIS IS OF SIZE N 
	UN_N=ec.polarencode(XN,N) 
		
	# for first Tx
	Iter=0
	Iter_p=compound_plist[0]
	Iter_R=Ratelist[0]
	Iter_G=Glist[0]
	Iter_I=I_ord[:Iter_G]
	UN=ec.getUN(UN_N,Iter_I,False)
	
	#print Iter_I
	#print UN
	Iter_UN=UN
	Iter_UN_ind=range(len(UN))  
	
	F=list(set(range(N))-set(Iter_I))
	FD=ec.getUN(UN_N,F,True)
	
	#-------------------------------------------Forward decoding	
    #in case of first iteration FD, XN is used
    #from next iteration more bits from reverse Arikan UN is used
	while not decoded:
		
		if Iter==0:
			Iter_XN=XN
			
		else:
			Iter_UN=[UN[i] for i in Iter_UN_ind]		 
			Iter_D=np.zeros(N-Iter_G,dtype=int).tolist()      #frozen data
			Iter_XN=ec.polarencodeG(Iter_UN,N,Iter_I,list(Iter_D),False)   #data goes in as per R.I
		
		#--------------------Note channel_p used for flipping
		Iter_YN=pl.BSCN(channel_p,Iter_XN)
		
		#-----------------------decoding based on this tx only
		if Iter==0:
			Iter_UN_hat=ec.polarSCdecodeG(Iter_YN,N,Iter_p,Iter_I,list(FD),False)
		else:	
			Iter_UN_hat=ec.polarSCdecodeG(Iter_YN,N,Iter_p,Iter_I,list(Iter_D),False)		
		
		Iter_UN_decoded=ec.getUN(Iter_UN_hat,Iter_I,False)
		
		#storage needed for final decoding
		Iterhistory[Iter]=[Iter_UN_ind,Iter_UN_decoded,Iter_YN]
				
		#For simulation of Rx knows channel case
		#Assuming Rx knows the capacity of the channel
		#hence as long as the rate used is above
		#the capacity is declares Not decodable	
		#the rate at which decoding is possible and the rate achieved is same

		
		
		if not is_decodable_kRx(G,Iter_G):
			#print "here"
			
			# picking out all the channels that are suspected to be bad in past
			# iterations and putting them for next iteration.
			# Note first iteration Whole UN is sent
			# in next only suspected bad channels are sent
			
			prev_I=Iter_I
			Iter+=1
			
			#New channel params
			Iter_p=compound_plist[Iter]
			Iter_R=Ratelist[Iter]
			Iter_G=Glist[Iter]
			Iter_I=I_ord[:Iter_G]
			
			tosend_ind=[]
			for i in range(Iter):
				#picking out the bad channels from prev iterations
				sent_ind=Iterhistory[i][0]
				sent_ind_last_iter=sent_ind[:Glist[Iter-1]]
				
					
				bad_ind=sent_ind_last_iter[Iter_G:]
				tosend_ind.extend(bad_ind)
			
			Iter_UN_ind=list(set(tosend_ind))
			
			Iter_UN_ind.sort()

			
		else:
			#the final reliable good channels is that of last iteration
			final_Iter=Iter
			final_G=Iter_G
			final_p=Iter_p
			final_I=I_ord[:final_G]
			
			#~ if Iter=0:
				#~ final_XN=ec.polarencode(Iter_UN_hat,N) #in case first iteration is last
			
			decoded= True
			
	#------------------------------------------------final decoding
	#number retrodecoding needed = iter-1
	#print Iter
	for Iter in range(final_Iter-1,-1,-1):
			#print "retro"+str(Iter)
			Prev_correct_ind=Iterhistory[Iter+1][0]
			Prev_correct_data=Iterhistory[Iter+1][1]
			IncFreeze_ind_UN=[i for i in Prev_correct_ind if i in Iterhistory[Iter][0]] 
			#basically intersection with some order picking the indexes of the prev iter frozen data needed in this iter i.e, 12
			#print IncFreeze_ind_UN
			
			#picking the data i.e. u12
			IncFreeze_ind_ind=[Prev_correct_ind.index(j) for j in IncFreeze_ind_UN]
			IncFreeze_data=[Prev_correct_data[k] for k in IncFreeze_ind_ind]
			
			#print IncFreeze_data
			
			#finding the channels where 12 went in this iter 16 15 14 "13"<--- here
			#i.e, removing the top channel_G channels (as they are good) from top Iter_i_G channels
			Iter_G=Glist[Iter]
			IncFreeze_ind=I_ord[:Iter_G][final_G:]
			if Iter==0:
				Iter_D=FD
			else:	
				Iter_D=np.zeros(N-Iter_G,dtype=int).tolist()      #frozen data as per iteration
	
			Iter_YN=Iterhistory[Iter][2]
			
			Iter_UN_hat=ec.polarIncFrzSCdecodeG(Iter_YN,N,final_p,final_I,list(Iter_D),IncFreeze_ind,IncFreeze_data,False)
			
			Iter_UN_decoded=ec.getUN(Iter_UN_hat,final_I,False)
				
			#history update
			Iterhistory_ind_upd=list(Prev_correct_ind)
			Iterhistory_ind_upd.extend(Iterhistory[Iter][0][:final_G]) #ie adding 5,6,11 to 4,10,12
			Iterhistory_data_upd=np.hstack((Prev_correct_data,Iter_UN_decoded)) #same as extend
			(Iterhistory[Iter][0],Iterhistory[Iter][1])=ml.sortAextend(Iterhistory_ind_upd,Iterhistory_data_upd.tolist())

	final_decoded= Iterhistory[0][1]
	final_XN=ec.polarencode(Iter_UN_hat,N)
	achieved_rate=float(len(UN))/((final_Iter+1)*N)
	return (achieved_rate,np.array(final_XN))
示例#14
0
	start = timer()
	for i in range(runsim):
		XN=ec.polarencodeG(UN,N,I,list(D))
		#XN=ec.polarencodeGR(UN,N,I,list(D))
	end = timer()

	TE=(end-start)/runsim
	print "Time for encoding:"+str(TE)
	json.dump("Time for encoding:"+str(TE)	,f1) ;f1.write("\n")


	#---------------------------------------------------channel

	start = timer()
	for i in range(runsim):
		YN=pl.BSCN(channel_p,XN)
	end = timer()

	TCh=(end-start)/runsim
	print "Time for channel:"+str(TCh)
	json.dump("Time for channel:"+str(TCh)	,f1) ;f1.write("\n")





	#-------------------------------------------------Decoding
	start = timer()
	for i in range(runsim):
		UN_hat=ec.polarSCdecodeG(YN,N,design_p,I,list(D))
	UN_decoded=ec.getUN_sUN_hat,I)
示例#15
0
#=======================================================Simulation
#Frozen data
D = np.zeros(N - len(I), dtype=int).tolist()

for psim in plist:
    print "running for " + str(psim) + "...\n"
    #(psimI,psimE)=pcon.getGChZCL(psim,N,tolerable_error)
    #(psimI,psimE)=pcon.getGChZCK(psim,N,int(K*p/psim))
    psimI = pcon.getGCHsim("MK_ALL", N, psim, int(K * p / psim))
    good_channels[psim] = psimI
    print psimI
    psim_llr_sum = np.zeros(N)
    for i in range(runsim):
        UN = np.random.randint(2, size=len(I))
        UN_encoded = ec.polarencodeG(UN, N, I, list(D), False)
        YN = pl.BSCN(psim, UN_encoded)
        (L, d) = ec.polarSCdecodeG(YN, N, p, I, list(D), True)
        #print abs(L)/runsim
        psim_llr_sum += abs(L) / runsim

    #psim_llr=[float(x)/runsim for x in psim_llr_sum]
    psim_llr = psim_llr_sum.tolist()
    print psim_llr
    if p == psim:
        #sets the threshold of lambda as the min of goodchannels
        #for the highest rate channel

        lambda_threshold = min(ec.getchannel(psim_llr, I, False))
        print ec.getchannel(psim_llr, I, False)

    llr_dict[psim] = psim_llr