Ejemplo n.º 1
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))
Ejemplo n.º 2
0
def send_rateless_LTPT_sim_bin(ERR_DICT, DP, N, compound_plist_u, channel_p,
                               derate, LTPTdict, runsim):

    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)

    for i in range(runsim):
        UN = np.random.randint(2, size=Glist[0])
        (achievedrate_sim,
         UN_decoded) = send_rateless_LTPT(UN, N, channel_p, compound_plist_u,
                                          derate, LTPTdict, True)

        #Number of times this rate occurs
        try:
            ERR_DICT[format(achievedrate_sim, '.' + str(DP) + 'f')][0] += 1
        except:
            ERR_DICT[format(achievedrate_sim, '.' + str(DP) + 'f')] = [1, 0]

        #ERRORS
        if UN.tolist() != UN_decoded.tolist():
            ERR_DICT[format(achievedrate_sim, '.' + str(DP) + 'f')][1] += 1

    return ERR_DICT
Ejemplo n.º 3
0
def send_rateless_file_LTPT_sim(N,compound_plist_u,channel_p,derate,LTPTdict,runsim,BER_needed):

	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)
	
	if BER_needed:
		errcnt=np.zeros(G)
	
	block_errorcnt=0
	achievedrate=0
	for i in range(runsim):
		XN=np.random.randint(2,size=N)
		(achievedrate_sim,XN_decoded)=send_rateless_file_LTPT(XN,N,channel_p,compound_plist_u,derate,LTPTdict,True)
		achievedrate+=float(achievedrate_sim)/runsim
		if BER_needed:
			errcnt=errcnt+np.logical_xor(XN,XN_decoded)
						
		if XN.tolist()!=XN_decoded.tolist():
			block_errorcnt+=1
	used_rate=float(Glist[0])/N	
	if BER_needed:		
		berN=errcnt/runsim
		ber_exp=np.log10(berN).tolist()
	
	block_error=float(block_errorcnt)/runsim
		
	if BER_needed:
		return (achievedrate,used_rate,ber_exp,block_error)
	else:
		return (achievedrate,used_rate,block_error)
Ejemplo n.º 4
0
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))
Ejemplo n.º 5
0
def send_rateless_file_kRx_sim(N, compound_plist_u, channel_p, derate, runsim,
                               BER_needed):

    compound_plist = list(compound_plist_u)  #best channel first
    compound_plist.sort()

    Ratelist = pl.getRatelist(compound_plist_u, derate)  #best rate first
    Glist = [int(N * r) for r in Ratelist]
    Glist = adjustG(Glist)

    if BER_needed:
        errcnt = np.zeros(G)

    block_errorcnt = 0

    I_ord = pcon.getreliability_order(N)
    I = I_ord[:Glist[0]]
    #print I

    for i in range(runsim):
        XN = np.random.randint(2, size=N)
        (achievedrate,
         XN_decoded) = send_rateless_file_kRx(XN, N, channel_p,
                                              compound_plist_u, derate, True)
        #achievedrate will be same for all simulations for a given value of derate
        if BER_needed:
            errcnt = errcnt + np.logical_xor(XN, XN_decoded)

        if XN.tolist() != XN_decoded.tolist():
            block_errorcnt += 1
    used_rate = float(Glist[0]) / N
    if BER_needed:
        berN = errcnt / runsim
        ber_exp = np.log10(berN).tolist()

    block_error = float(block_errorcnt) / runsim

    if BER_needed:
        return (achievedrate, used_rate, ber_exp, block_error)
    else:
        return (achievedrate, used_rate, block_error)
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
def send_polar_sim(N, compound_plist_u, channel_p, derate, runsim, BER_needed):

    compound_plist = list(compound_plist_u)  #best channel first
    compound_plist.sort()

    Ratelist = pl.getRatelist(compound_plist_u, derate)  #best rate first
    Glist = [int(N * r) for r in Ratelist]
    Glist = adjustG(Glist)

    if BER_needed:
        errcnt = np.zeros(G)

    block_errorcnt = 0
    #UN=np.random.randint(2,size=Glist[0])
    #print UN
    for i in range(runsim):
        #print i
        UN = np.random.randint(2, size=Glist[0])
        (achievedrate, UN_decoded) = send_polar(UN, N, channel_p,
                                                compound_plist_u, derate, True)
        #print achievedrate
        #achievedrate will be same for all simulations for a given value of derate

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

        if UN.tolist() != UN_decoded.tolist():
            block_errorcnt += 1
    used_rate = float(Glist[0]) / N
    if BER_needed:
        berN = errcnt / runsim
        ber_exp = np.log10(berN).tolist()

    block_error = float(block_errorcnt) / runsim

    if BER_needed:
        return (achievedrate, used_rate, ber_exp, block_error)
    else:
        return (achievedrate, used_rate, block_error)
Ejemplo n.º 8
0
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))
Ejemplo n.º 9
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))