final report

23
POLAR CODES OVER WIRELESS FADING CHANNELS SIDDHARTH DANGI, ARJUN SINGH ABSTRACT. We investigate the performance of polar codes over wireless fading channels. We measure the potential coding and diversity gains by modelling the channel as a BSEC (”binary sym- metric erasure channel”). In effect, we treat deep fades as erasures and other error events as causing bit flips. Polar codes, introduced by Erdal Arikan, are a class of codes that achieve the symmetric capacity I (W ) of any binary-input discrete memoryless channel (B-DMC), while maintaining complexity O(N log N ) for both encoding and decoding. Given N independent and iden- tical copies of a B-DMC W , a polar code synthesizes a second set of N channels {W (i) N } with the property that, when N is large, ap- proximately N · I (W ) of these channels have a symmetric capacity of 1, and approximately N · (1 - I (W )) channels have a symmetric capacity of 0. One can then send data only through those channels with a symmetric capacity of 1. 1. BACKGROUND 1.1. Channel Parameters. The two channel parameters of interest are the symmetric capacity I (W ) , X y∈Y X x∈X 1 2 W (y|x) log W (y|x) 1 2 W (y|0) + 1 2 W (y|1) and the Bhattacharyya parameter Z (W ) , X y∈Y p W (y|0)W (y|1). These parameters are measures of rate and reliability, respectively. I (W ) is the highest rate at which reliable communication is possible across W using the inputs of W with equal frequency. Z (W ) is an upper bound on the probability of MAP decision error when W is used to transmit a single bit (a-priori equiprobable). 1.2. Polar Encoding. A polar encoder for block length N =2 n is defined recursively as shown in figure 1. First, the input vector u N 1 is transformed into s N 1 , where s 2i-1 = u 2i-1 u 2i and s 2i = u 2i , for 1

Upload: thuy-dung-nguyen

Post on 28-Dec-2015

8 views

Category:

Documents


0 download

DESCRIPTION

REO

TRANSCRIPT

POLAR CODES OVER WIRELESS FADING CHANNELS

SIDDHARTH DANGI, ARJUN SINGH

ABSTRACT. We investigate the performance of polar codes overwireless fading channels. We measure the potential coding anddiversity gains by modelling the channel as a BSEC (”binary sym-metric erasure channel”). In effect, we treat deep fades as erasuresand other error events as causing bit flips.Polar codes, introduced by Erdal Arikan, are a class of codes thatachieve the symmetric capacity I(W ) of any binary-input discretememoryless channel (B-DMC), while maintaining complexity O(N logN)for both encoding and decoding. Given N independent and iden-tical copies of a B-DMC W , a polar code synthesizes a second setof N channels {W (i)

N }with the property that, when N is large, ap-proximately N ·I(W ) of these channels have a symmetric capacityof 1, and approximately N ·(1−I(W )) channels have a symmetriccapacity of 0. One can then send data only through those channelswith a symmetric capacity of 1.

1. BACKGROUND

1.1. Channel Parameters. The two channel parameters of interestare the symmetric capacity

I(W ) ,∑y∈Y

∑x∈X

1

2W (y|x) log W (y|x)

12W (y|0) + 1

2W (y|1)

and the Bhattacharyya parameter

Z(W ) ,∑y∈Y

√W (y|0)W (y|1).

These parameters are measures of rate and reliability, respectively.I(W ) is the highest rate at which reliable communication is possibleacross W using the inputs of W with equal frequency. Z(W ) is anupper bound on the probability of MAP decision error when W isused to transmit a single bit (a-priori equiprobable).

1.2. Polar Encoding. A polar encoder for block length N = 2n isdefined recursively as shown in figure 1. First, the input vector uN1is transformed into sN1 , where s2i−1 = u2i−1 ⊕ u2i and s2i = u2i, for

1

2 SIDDHARTH DANGI, ARJUN SINGH

1 ≤ i ≤ N2

. Next, the operator RN is applied, which is a ”reverseshuffle” operation that produces vN1 = (s1, s3, ...sN−1, s2, s4, ..., sN).Finally, vN/2

1 and vNN/2+1 are fed recursively into encoders for blocklength N

2.

FIGURE 1. Polar encoder structure for block length N

Depending on the choice of code rate R, a subset of the indices,denoted as A, is chosen such that the set of bits {ui,∀i ∈ A} donot contain data. Instead, these bits will be fixed, and known at thereceiver in advance. Accordingly, these bits are known as the ”frozenbits”, and the set A is known as the frozen set. See section ?? formore background on the choice of both the frozen set and the frozenbits. Since the encoding structure as defined above is linear, a polar

POLAR CODES OVER WIRELESS FADING CHANNELS 3

encoder for block length N can be represented simply by the matrixequation x = uGN . As an example, when N = 4, the matrix is

G4 =

1 0 0 01 0 1 01 1 0 01 1 1 1

If a rate of 1

2is desired, one could for instance freeze indices 1 and 3.

Setting the frozen bits to 0 would then give the following encodingrepresentation:

x = (u2, u4) ·[1 0 1 01 1 1 1

]+ (0, 0) ·

[1 0 0 01 1 0 0

]1.2.1. Choice of Frozen Set. For the polar encoder and decoder, thefrozen set should be chosen to include the indices for which corre-sponding newly synthesized channels have either the

• highest symmetric capacities (closest to 1)• lowest Bhattacharyya parameters (closest to 0)

As shown in [1], both methods achieve symmetric capacity. The sec-ond method, however, gives an explicit bound on the probability ofblock error (independent of the code rate):

P (block error) = O(N−14 )

Furthermore, in the initial set of N channels are binary erasure chan-nels (BECs), there exist nice recursive formulas for calculating theBhattacharyya parameters of the newly synthesized channels as fol-lows:

Z(W

(2j−1)N

)= 2Z

(W

(j)N/2

)− Z

(W

(j)N/2

)2Z(W

(2j)N

)= Z

(W

(j)N/2

)2If the initial set of identical channels are not BECs, one do the follow-ing:

1. Calculate the symmetric capacity I(W ) of one of the initialchannels W .

2. Treat the initial channels as BECs with erasure probabilities1− I(W ).

3. Use the above recursive formulas to approximate the Bhat-tacharyya parameters for the newly synthesized channels.

4 SIDDHARTH DANGI, ARJUN SINGH

1.3. Polar Decoding. Many of the coding results in [1] are derivedassuming a successive cancellation (SC) decoder that generates itsdecision uN1 , in the order i from 1 to N , as follows:

ui ,

{ui if i ∈ Ac

hi(yN1 , u

i−1i ), if i ∈ A

In the above expression, the hi are decision functions that are definedas:

hi(yN1 , u

i−1i ) ,

{0, if W

(i)N (yN1 ,ui−1

1 |0)W

(i)N (yN1 ,ui−1

1 |1)≥ 1

1, otherwiseBecause these decisions are not formed using the ML rule (since theytreat future frozen bits as unknown), the SC decoder above is subop-timal. In exchange, however, the following recursive formulas canbe used for efficient computation of the decision functions:

W(2i−1)2N

(y2N1 , u2i−21 |u2i−1

)=

∑u2i

1

2W

(i)N

(yN1 , u

2i−21,o ⊕ u2i−21,e |u2i−1 ⊕ u2i

W(i)N

(y2NN+1, u

2i−21,e |u2i

)W

(2i)2N

(y2N1 , u2i−11 |u2i

)=

1

2W

(i)N

(yN1 , u

2i−21,o ⊕ u2i−21,e |u2i−1 ⊕ u2i

W(i)N

(y2NN+1, u

2i−21,e |u2i

)Furthermore, one can still achieve the symmetric capacity using thissuboptimal decoding strategy.

POLAR CODES OVER WIRELESS FADING CHANNELS 5

2. IMPLEMENTATION

For our experiments, we simulated a Rayleigh fading channel inMATLAB. Our channel had two paths, with a delay spread of Td =10 µs, and a Doppler spread of Ds = 100 Hz. We implemented anOFDM system using QPSK modulation, with a bandwidth of W =1.25 MHz and Nc = 128 subcarriers.

3. RESULTS

4. CONCLUSION

doesn’t work well because we treat a channel as a BSEC when itis, in fact, NOT.

6 SIDDHARTH DANGI, ARJUN SINGH

REFERENCES

[1] E. Arikan. Channel polarization: A method for constructing capacity-achieving codes for symmetric binary-input memoryless channels. CoRR,abs/0807.3917, 2008.

POLAR CODES OVER WIRELESS FADING CHANNELS 7

5. SIMULATION CODE

func t ion coded bi t s = polar encoder ( b i t s )

N = length ( b i t s ) ;

b i t s = reshape ( b i t s , 2 ,N/2) ’ ;b i t s ( : , 1 ) = mod( b i t s ( : , 1 ) + b i t s ( : , 2 ) , 2 ) ;

i f (N == 2)coded bi t s = [ b i t s ( : , 1 ) ; b i t s ( : , 2 ) ] ;

e l s ecoded bi t s = [ polar encoder ( b i t s ( : , 1 ) ) ; polar encoder (

b i t s ( : , 2 ) ) ] ;end

end

8 SIDDHARTH DANGI, ARJUN SINGH

% funct ion decoded bits = polar decoder ( y , i n f o i n d i c e s ,pmf W)

%% N = length ( y ) ;% y = c a s t ( y , ’ int32 ’ ) ;% i n f o i n d i c e s = c a s t ( i n f o i n d i c e s , ’ int32 ’ ) ;% decoded bits = zeros (N, 1 ) ;% f o r i = i n f o i n d i c e s ’% i f ( i == 1)% l r = p W m( 1 , y , 0 , 0 ,N, pmf W(−1 , 1 ) , pmf W( 1 , 0 ) ) /

p W( 1 , y , 0 , 1 ,N, pmf W(−1 , 1 ) , pmf W( 1 , 0 ) ) ;% e l s e% l r = p W m( i , y , c a s t ( decoded bits ( 1 : i −1) , ’ in t32 ’ )

, 0 ,N, pmf W(−1 , 1 ) , pmf W( 1 , 0 ) ) /p W( i , y , c a s t (decoded bits ( 1 : i −1) , ’ int32 ’ ) , 1 ,N, pmf W(−1 , 1 ) , pmf W( 1 ,

0 ) ) ;% end% l r% decoded bits ( i ) = l r < 1 ;%% end%% endfunct ion [ decoded bits hadnan ] = polar decoder ( y ,

f rozen inds , pmf W)

N = length ( y ) ;

decoded bits = zeros (N, 1 ) ;hadnan = f a l s e ;f o r i = 1 :N

i f ( any ( f rozen inds == i ) )decoded bits ( i ) = 0 ; % assume a l l frozen b i t s are 0

e l s ei f ( i == 1)

%l r 2 = p W m( 1 , y , 0 , 0 ,N, pmf W(−1 , 1 ) , pmf W( 1 ,0 ) ) /p W m( 1 , y , 0 , 1 ,N, pmf W(−1 , 1 ) , pmf W( 1 ,0 ) ) ;

%l r = p W( 1 , c a s t ( y , ’ int32 ’ ) , 0 , 0 ,N, pmf W(−1 , 1 ), pmf W( 1 , 0 ) ) /p W( 1 , c a s t ( y , ’ int32 ’ ) , 0 , 1 ,N,

pmf W(−1 , 1 ) , pmf W( 1 , 0 ) ) ;l n l r = p Wln ( 1 , c a s t ( y , ’ i n t 3 2 ’ ) , 0 , 0 ,N, pmf W(−1 ,

1 ) , pmf W( 1 , 0 ) ) − p Wln ( 1 , c a s t ( y , ’ i n t 3 2 ’ ), 0 , 1 ,N, pmf W(−1 , 1 ) , pmf W( 1 , 0 ) ) ;

i f isnan ( l n l r )hadnan = true ;

end%i f isnan ( l r ) && ˜ isnan ( l n l r )

POLAR CODES OVER WIRELESS FADING CHANNELS 9

% disp ( ’ hadnan f o r l r but not l n l r ’ ) ;%end

e l s e%l r 2 = p W m( i , y , decoded bits ( 1 : i −1) , 0 ,N, pmf W

(−1 , 1 ) , pmf W( 1 , 0 ) ) /p W m( i , y , decoded bits( 1 : i −1) , 1 ,N, pmf W(−1 , 1 ) , pmf W( 1 , 0 ) ) ;

%l r = p W( c a s t ( i , ’ int32 ’ ) , c a s t ( y , ’ int32 ’ ) , c a s t( decoded bits ( 1 : i −1) , ’ in t32 ’ ) , 0 ,N, pmf W(−1 ,

1 ) , pmf W( 1 , 0 ) ) /p W( c a s t ( i , ’ int32 ’ ) , c a s t (y , ’ int32 ’ ) , c a s t ( decoded bits ( 1 : i −1) , ’ int32’ ) , 1 ,N, pmf W(−1 , 1 ) , pmf W( 1 , 0 ) ) ;

l n l r = p Wln ( c a s t ( i , ’ i n t 3 2 ’ ) , c a s t ( y , ’ i n t 3 2 ’ ), c a s t ( decoded bits ( 1 : i −1) , ’ i n t 3 2 ’ ) , 0 ,N,

pmf W(−1 , 1 ) , pmf W( 1 , 0 ) ) − p Wln ( c a s t ( i , ’i n t 3 2 ’ ) , c a s t ( y , ’ i n t 3 2 ’ ) , c a s t ( decoded bits( 1 : i −1) , ’ i n t 3 2 ’ ) , 1 ,N, pmf W(−1 , 1 ) , pmf W( 1 ,

0 ) ) ;i f isnan ( l n l r )

hadnan = true ;end%i f isnan ( l r ) && ˜ isnan ( l n l r )% disp ( ’ hadnan f o r l r but not l n l r ’ ) ;%end

end%decoded bits ( i ) = 1 − ( l r >= 1) ;decoded bits ( i ) = l n l r < 0 ;

endend

end

10 SIDDHARTH DANGI, ARJUN SINGH

func t ion symbols out = d e c l a r e e r a s u r e s ( b i t s o u t , h out ,threshold , b i t s per symbol )

h mag = abs ( h out ) ;e r a s e d b i t i n d i c e s = h mag < threshold ;

%dummy = repmat ( indices be low threshold ’ , b i ts per symbol, 1 ) ;

%e r a s e d b i t i n d i c e s = reshape (dummy, b i t s per symbol ∗length ( h mag ) , 1 ) ;

symbols out = and ( ˜ e r a s e d b i t i n d i c e s , b i t s o u t ) + −1 ∗e r a s e d b i t i n d i c e s ;

POLAR CODES OVER WIRELESS FADING CHANNELS 11

func t ion [ b i t s o u t , h h at r ep e a te d d e i n t er l e av e d , h hat ]= ofdm rayleigh channel ( b i t s i n , SNR, W, Nc , Doppler , M, i n t e r l e a v e , i n t e r l e a v e l e n g t h )

% M−QAMmodulator = modem.qammod( ’M’ , M, ’ InputType ’ , ’ b i t ’ ) ;demodulator = modem. qamdemod( modulator ) ;

% Figure out how many times we need to loopbi t s per ofdm block = log2 (M) ∗Nc ;num ofdm blocks = length ( b i t s i n ) /

b i t s per ofdm block ;i f mod( length ( b i t s i n ) , b i t s per o fdm block ) ˜= 0

e r r o r ( [ ’ The number of input b i t s ( ’ , num2str ( length (b i t s i n ) ) , ’ ) must be a mult ip le of log2 (M) ∗ Nc ( ’ ,num2str ( b i t s per o fdm block ) , ’ ) ’ ] ) ;

end

b i t s o u t = zeros ( length ( b i t s i n ) , 1 ) ;h hat = zeros ( num ofdm blocks∗Nc , 1 ) ;

% Path delays and gainsTaus = [0 1e−5] ;As = [0 −3];f c = 1e9 ;

% Cyc l i c p r e f i x = delay spread ∗ W + a small b u f f e rL = c e i l ( Taus ( end ) ∗ W) + 2 ;

% Channel i n i t i a l i z a t i o nchan = rayle ighchan (1/W, Doppler , Taus , As ) ;chan . StorePathGains = true ;chan . R e s e t B e f o r e F i l t e r i n g = f a l s e ;

% i n t e r l e a v ei f ( i n t e r l e a v e )

num junk bits = mod( i n t e r l e a v e l e n g t h − mod( length (b i t s i n ) , i n t e r l e a v e l e n g t h ) , i n t e r l e a v e l e n g t h ) ;

b i t s i n = [ b i t s i n ; zeros ( num junk bits , 1 ) −2];tmp len = length ( b i t s i n ) ;b i t s i n = reshape ( b i t s i n , tmp len/

i n t e r l e a v e l e n g t h , i n t e r l e a v e l e n g t h ) ;b i t s i n = reshape ( b i t s i n ’ , tmp len , 1 ) ;b i t s i n = b i t s i n ( b i t s i n ˜= −2) ;

end

ofdm symbols = modulate ( modulator , b i t s i n ) ;ofdm symbols out = zeros ( length ( ofdm symbols ) , 1 ) ;

12 SIDDHARTH DANGI, ARJUN SINGH

f o r i =1: num ofdm blocks

d h a t i n = ofdm symbols ( ( i −1)∗Nc+1: i ∗Nc) ;d = i f f t ( d h a t i n ) ;

% Cyc l i c p r e f i xx = [ d (Nc−L+2:Nc , : ) ; d ] ;

% Add Noisey = f i l t e r ( chan , x ) ;y = awgn( y , SNR, 3 . 0 1 0 3 ) ;

a = chan . PathGains ( 1 , : ) ;

h = zeros (Nc , 1 ) ;f o r i i =(−chan . ChannelFi l terDelay +1) : L

htmp = 0 ;f o r j j = 1 : length ( Taus )

htmp = htmp + a ( j j ) ∗ s i n c ( i i − 1 − Taus ( j j ) ∗W)∗exp(− 1 j ∗ 2 ∗ pi ∗ f c ∗ Taus ( j j ) ) ;

endh ( i i +chan . ChannelFi l terDelay ) = htmp ;

endh hat ( ( i −1)∗Nc+1: i ∗Nc) = f f t ( h ) ;

% S t r i p c y c l i c pre f ix , f f t , and d e t e c td hat out = f f t ( y ( L : end ) ) ./

h hat ( ( i −1)∗Nc+1: i ∗Nc) ;ofdm symbols out ( ( i −1)∗Nc+1: i ∗Nc) = d hat out ;

end

b i t s o u t = demodulate ( demodulator , ofdm symbols out ) ;

dummy = repmat ( h hat ’ , log2 (M) , 1 ) ;h hat repeated = reshape (dummy, log2 (M) ∗ length ( h hat ) , 1 ) ;

h h a t r e p e a t e d d e i n t e r l e a v e d = h hat repeated ;

% un−i n t e r l e a v ei f ( i n t e r l e a v e )

num junk bits = mod( i n t e r l e a v e l e n g t h − mod( length (b i t s o u t ) , i n t e r l e a v e l e n g t h ) , i n t e r l e a v e l e n g t h ) ;

b i t s o u t = [ b i t s o u t ; zeros ( num junk bits , 1 ) −2];

tmp len = length ( b i t s o u t ) ;

POLAR CODES OVER WIRELESS FADING CHANNELS 13

b i t s o u t = reshape ( b i t s o u t , i n t e r l e a v e l e n g t h ,tmp len/ i n t e r l e a v e l e n g t h ) ;

b i t s o u t = reshape ( b i t s o u t ’ , tmp len , 1 ) ;b i t s o u t = b i t s o u t ( b i t s o u t ˜= −2) ;h h a t r e p a t e d d e i n t e r l e a v e d = [

h h a t r e p e a t e d d e i n t e r l e a v e d ; zeros ( num junk bits ,1 )+ I n f ] ;

h h a t r e p e a t e d d e i n t e r l e a v e d = reshape (h h a t r ep e a te d d e in t er l e av e d , i n t e r l e a v e l e n g t h ,tmp len/ i n t e r l e a v e l e n g t h ) ;

h h a t r e p e a t e d d e i n t e r l e a v e d = reshape (h h a t r ep e a te d d e in t er l e av e d ’ , tmp len , 1 ) ;

h h a t r e p e a t e d d e i n t e r l e a v e d =h h a t r e p e a t e d d e i n t e r l e a v e d (h h a t r e p e a t e d d e i n t e r l e a v e d ˜= I n f ) ;

end

end

14 SIDDHARTH DANGI, ARJUN SINGH

func t ion [ ber , c a p a c i t y ] = s imula te everyth ing (SNR,num blocks )

bsec P W = @( e p , f p ) @( y , x ) e p ∗ ( ( y == −1 && x == 0) + (y == −1 && x == 1) ) . . .

+ f p ∗ ( ( y == 0 && x == 1) + ( y ==1 && x == 0) ) . . .

+ (1 − e p − f p ) ∗ ( ( y == 0 && x== 0) + ( y == 1 && x == 1) ) ;

Nc = 1 2 8 ;W = 1 . 2 5 e6 ;Doppler = 1 0 0 ;M = 4 ;b i t s per symbol = log2 (M) ;b lo c k l en g th = 5 1 2 ;e r a s u r e t h r e s h o l d = . 4 ;

[ f l i p p r o b a b i l i t y , e r a s u r e p r o b a b i l i t y ] = s im rayle igh (SNR,W, Nc , Doppler , M, e r a s u r e t h r e s h o l d ) ;

e r a s u r e p r o b a b i l i t y = e r a s u r e p r o b a b i l i t y ∗1 . 2f l i p p r o b a b i l i t y = f l i p p r o b a b i l i t y ∗1 . 2

P W bsec = bsec P W ( e r a s u r e p r o b a b i l i t y ,f l i p p r o b a b i l i t y ) ;

c a p a c i t y = I W ( P W bsec )e q e r a s u r e p r o b a b i l i t y = 1−c a p a c i t y ;

Zs = zeros ( b lock length , 1 ) ;f o r i =1 : b l oc k l en g th

Zs ( i ) = Z W( i , b lock length , e q e r a s u r e p r o b a b i l i t y ) ;end

[ vals , i n d i c e s ] = s o r t ( Zs ) ;num info bi t s = f l o o r ( c a p a c i t y ∗ b lo c k l en g t h ) ;i n f o i n d i c e s = i n d i c e s ( 1 : num info bi t s ) ;

%i n f o i n d i c e s = s o r t ( i n f o i n d i c e s ) ;f r o z e n i n d i c e s = i n d i c e s ( num info bi t s +1: end ) ;g loba l map ;map = java . u t i l . Hashtable ;

%% Simulat ion% Frozen b i t s are 0i n f o b i t s = zeros ( num info bi t s ∗num blocks , 1 ) ;i n p u t b i t s = zeros ( b l oc k l en g th ∗num blocks , 1 ) ;coded bi t s = zeros ( b l o ck l en g th ∗num blocks , 1 ) ;f o r i =1 : num blocks

f i r s t b i t i n d e x = ( i −1) ∗ b lo c k l en g t h + 1 ;

POLAR CODES OVER WIRELESS FADING CHANNELS 15

l a s t b i t i n d e x = i ∗ b lo c k l en g t h ;f i r s t i n f o i n d e x = ( i −1)∗num info bi t s + 1 ;l a s t i n f o i n d e x = i ∗num info bi t s ;po lar in put = zeros ( b lock length , 1 ) ;t m p i n f o b i t s = round ( rand ( num info bi ts , 1 ) ) ;po lar in put ( i n f o i n d i c e s ) = t m p i n f o b i t s ;i n p u t b i t s ( f i r s t b i t i n d e x : l a s t b i t i n d e x ) =

po lar in put ;i n f o b i t s ( f i r s t i n f o i n d e x : l a s t i n f o i n d e x ) =

t m p i n f o b i t s ;coded bi t s ( f i r s t b i t i n d e x : l a s t b i t i n d e x ) =

polar encoder ( po lar in put ) ;end

t i c[ output coded bi ts , h out ] = ofdm rayleigh channel (

coded bi ts , SNR, W, Nc , Doppler , M) ;disp ( ’Chan ’ ) ;toc

[ output symbols ] = d e c l a r e e r a s u r e s ( output coded bi ts ,h out , e rasure threshold , b i t s per symbol ) ;

o u t p u t i n f o b i t s = zeros ( num info bi t s ∗num blocks , 1 ) ;o u t p u t b i t s = zeros ( b lo c k l en g th ∗num blocks , 1 ) ;nan indices = [ ] ;f o r i =1 : num blocks

f i r s t b i t i n d e x = ( i −1) ∗ b lo c k l en g t h + 1 ;l a s t b i t i n d e x = i ∗ b lo c k l en g t h ;f i r s t i n f o i n d e x = ( i −1)∗num info bi t s + 1 ;l a s t i n f o i n d e x = i ∗num info bi t s ;t i c[ polar output hadnan ] = polar decoder ( output symbols (

f i r s t b i t i n d e x : l a s t b i t i n d e x ) , f r o z e n i n d i c e s ,P W bsec ) ;

disp ( ’ Dec ’ )toci f hadnan

disp ( ’ hadnan ’ ) ;nan indices = [ nan indices i ] ;

endo u t p u t b i t s ( f i r s t b i t i n d e x : l a s t b i t i n d e x ) =

polar output ;o u t p u t i n f o b i t s ( f i r s t i n f o i n d e x : l a s t i n f o i n d e x ) =

polar output ( i n f o i n d i c e s ) ;end

% f o r i =num blocks :−1:1

16 SIDDHARTH DANGI, ARJUN SINGH

% f i r s t b i t i n d e x = ( i −1) ∗ b lo c k l en g t h + 1 ;% l a s t b i t i n d e x = i ∗ b lo c k l en g th ;% f i r s t i n f o i n d e x = ( i −1)∗num info bi t s + 1 ;% l a s t i n f o i n d e x = i ∗num info bi t s ;% i f any ( i == nan indices )% coded bi t s ( f i r s t b i t i n d e x : l a s t b i t i n d e x ) = [ ] ;% output coded bi t s ( f i r s t b i t i n d e x : l a s t b i t i n d e x )

= [ ] ;% i n f o b i t s ( f i r s t i n f o i n d e x : l a s t i n f o i n d e x ) = [ ] ;% o u t p u t i n f o b i t s ( f i r s t i n f o i n d e x : l a s t i n f o i n d e x

) = [ ] ;% end% end

num chan errors = sum( coded bi t s ˜= output coded bi t s )num erasures = sum( output symbols == −1)[ junk , ber ] = b i t e r r ( o u t p u t i n f o b i t s , i n f o b i t s ) ;

f i g u r e ;hold on ;e r r o r i d x u n s c a l e d = f ind ( output coded bi t s ˜= coded bi t s ) ;e r r o r i d x = c e i l ( e r r o r i d x u n s c a l e d /bi t s per symbol ) ;p l o t ( abs ( h out ) , ’− ’ , ’ LineWidth ’ , 1 )p l o t ( e r r o r i d x , abs ( h out ( e r r o r i d x ) ) , ’ r . ’ )p l o t ( ones ( length ( h out ) , 1 ) ∗ erasure threshold , ’ g ’ , ’

LineWidth ’ , 1 )

POLAR CODES OVER WIRELESS FADING CHANNELS 17

func t ion output = Z W( i ,N, p erasure )

i f (mod( i , 2 ) == 1)i f ( i == 1) && (N == 1)

output = p erasure ;e l s e

tmpZ = Z W( c e i l ( i /2) , N/2 , p erasure ) ;output = 2 ∗ tmpZ − tmpZ ˆ 2 ;

ende l s e

output = Z W( i /2 , N/2 , p erasure ) ˆ 2 ;end

end

18 SIDDHARTH DANGI, ARJUN SINGH

func t ion output = I W (pmf W)

output = 0 ;X = [ 0 , 1 ] ;Y = [ 0 , 1 , −1 ] ;

f o r y = Yf o r x = X

tmp = pmf W( y , x ) ;i f ( tmp ˜= 0)

output = output + tmp ∗ log2 ( tmp / ( 0 . 5∗pmf W( y, 0 ) + 0 . 5∗pmf W( y , 1 ) ) ) ;

endend

end

output = 0 . 5 ∗ output ;

end

POLAR CODES OVER WIRELESS FADING CHANNELS 19

# include ”mex . h”# include <math . h># include <iostream># include <sstream># include <map># include <l i m i t s>//# def ine MEMOIZE 4//

using std : : numer ic l imi t s ;using std : : cout ;using std : : endl ;

s td : : map<std : : s t r i n g , double> map ;std : : map <std : : s t r i n g , double> : : i t e r a t o r hmiter ;double p W( i n t i , i n t ∗ y , i n t ∗ u hat , i n t u , i n t N, double

pe , double pf ) ;void mexFunction ( i n t nlhs , mxArray ∗plhs [ ] , i n t nrhs , const

mxArray ∗prhs [ ] ){

i n t i = mxGetScalar ( prhs [ 0 ] ) ;i n t ∗ yf = ( i n t ∗ ) mxGetPr ( prhs [ 1 ] ) ;i n t ∗ u h a t f= ( i n t ∗ ) mxGetPr ( prhs [ 2 ] ) ;i n t u = mxGetScalar ( prhs [ 3 ] ) ;i n t N = mxGetScalar ( prhs [ 4 ] ) ;double pe = mxGetScalar ( prhs [ 5 ] ) ;double pf = mxGetScalar ( prhs [ 6 ] ) ;double output = 4 ;

i n t ∗ y = ( i n t ∗ ) mxCalloc ( mxGetNumberOfElements ( prhs [ 1 ] ) ,s i z e o f ( i n t ) ) ;

i n t ∗ u hat = ( i n t ∗ ) mxCalloc ( mxGetNumberOfElements ( prhs[ 2 ] ) , s i z e o f ( i n t ) ) ;

i n t i i ;f o r ( i i = 0 ; i i < mxGetNumberOfElements ( prhs [ 2 ] ) ; i i ++){

u hat [ i i ] = u h a t f [ i i ] ;}f o r ( i i = 0 ; i i < mxGetNumberOfElements ( prhs [ 1 ] ) ; i i ++){

y [ i i ] = yf [ i i ] ;}double out = p W( i , y , u hat , u , N, pe , pf ) ;plhs [ 0 ] = mxCreateDoubleScalar ( out ) ;

}

i n l i n e i n t ∗ xorr ( i n t ∗ uhat , i n t s i z e ){

i n t ∗ r e t v a l = ( i n t ∗ ) mxCalloc ( s i z e /2 , s i z e o f ( i n t ) ) ;

20 SIDDHARTH DANGI, ARJUN SINGH

i n t i ;f o r ( i = 0 ; i < s i z e /2; i ++){

r e t v a l [ i ] = uhat [2∗ i ] ˆ uhat [2∗ i + 1 ] ;}re turn r e t v a l ;

}

i n l i n e i n t ∗ s p l i t ( i n t ∗ y , i n t s ize , i n t bottom ) {i n t ∗ r e t v a l = ( i n t ∗ ) mxCalloc ( s ize , s i z e o f ( i n t ) ) ;i n t i ;

f o r ( i = 0 ; i < s i z e ; i ++){r e t v a l [ i ] = y [ s i z e ∗ bottom + i ] ;

}re turn r e t v a l ;

}

i n l i n e i n t ∗ evens ( i n t ∗ u hat , i n t s i z e ) {i n t ∗ r e t v a l = ( i n t ∗ ) mxCalloc ( s i z e /2 , s i z e o f ( i n t ) ) ;i n t i ;f o r ( i = 0 ; i < s i z e /2; i ++){

r e t v a l [ i ] = u hat [2∗ i + 1 ] ;}re turn r e t v a l ;

}

i n l i n e double eln ( double x ){

i f ( x == 0) {re turn numeric l imi ts<double > : : quiet NaN ( ) ;

} e l s e {re turn log ( x ) ;

}}

i n l i n e double elnsum ( double x , double y ){

i f ( x < ( y − log (1 e200 ) ) ) {re turn y ;

}i f ( y < ( x − log (1 e200 ) ) ) {

re turn x ;}

i f ( isnan ( x ) ) {re turn y ;

} e l s e i f ( isnan ( y ) ) {re turn x ;

POLAR CODES OVER WIRELESS FADING CHANNELS 21

}

i f ( x > y ) {double d i f f = y − x ;i f ( ! f i n i t e ( exp ( d i f f ) ) ) {

re turn ( x > y ? x : y ) ;}re turn x + eln (1 + exp ( d i f f ) ) ;

} e l s e {double d i f f = x − y ;i f ( ! f i n i t e ( exp ( d i f f ) ) ) {

re turn ( x > y ? x : y ) ;}re turn y + eln (1 + exp ( d i f f ) ) ;

}}

i n l i n e double elnproduct ( double x , double y ){

i f ( isnan ( x ) | | isnan ( y ) ) {re turn numeric l imi ts<double > : : quiet NaN ( ) ;

} e l s e {re turn x + y ;

}

}

double p W( i n t i , i n t ∗ y , i n t ∗ u hat , i n t u , i n t N, doublepe , double pf ) {

//FILE∗ f i l e = fopen (”/ Users/ar jun/blah . t t t t t ” , ”a ” ) ;// i f ( f i l e == NULL) {// p r i n t f (” e r r o r opening f i l e ” ) ;//}// f p r i n t f ( f i l e , ”%d %d\n” , i , N) ;// f c l o s e ( f i l e ) ;double output ;

# i f d e f MEMOIZEstd : : s t r ings t ream ss ;ss << i ;f o r ( i n t i i i = 0 ; i i i < i −1; i i i ++){

ss << y [ i i i ] << u hat [ i i i ] ;}ss << y [ i −1] ;f o r ( i n t i i i = i ; i i i < N; i i i ++){

ss << y [ i i i ] ;}ss << u << N << pe << pf ;

22 SIDDHARTH DANGI, ARJUN SINGH

std : : s t r i n g key = ss . s t r ( ) ;hmiter = map . f ind ( key ) ;i f ( hmiter != map . end ( ) ) {

output = hmiter−>second ;re turn output ;

}# endi f

i f ( i % 2 == 1) {

i f ( i == 1) {i f (N == 1) {

// output = ( y [ 0 ] == −1) ? pe : ( ( u != y [ 0 ] ) ? pf :(1 − pe − pf ) ) ;

output = ( y [ 0 ] == −1) ? e ln ( pe ) : ( ( u != y [ 0 ] ) ?e ln ( pf ) : e ln (1 − pe − pf ) ) ;

} e l s e {//output = . 5 ∗ ( ( p W( 1 , s p l i t ( y , N/2 , 0 ) , u hat ,

u , N/2 , pe , pf ) ∗ p W( 1 , s p l i t ( y , N/2 , 1 ) , u hat, 0 , N/2 , pe , pf ) ) +

// (p W( 1 , s p l i t ( y , N/2 , 0 ) , u hat , !u , N/2 , pe , pf ) ∗ p W( 1 , s p l i t ( y , N/2 , 1 ) , u hat, 1 , N/2 , pe , pf ) ) ) ;

double f i r s t t e r m = elnproduct (p W( 1 , s p l i t ( y , N/2 , 0 ) , u hat , u , N/2 , pe , pf ) , p W( 1 , s p l i t ( y ,N/2 , 1 ) , u hat , 0 , N/2 , pe , pf ) ) ;

double second term = elnproduct (p W( 1 , s p l i t ( y , N/2 , 0 ) , u hat , ! u , N/2 , pe , pf ) , p W( 1 , s p l i t ( y ,N/2 , 1 ) , u hat , 1 , N/2 , pe , pf ) ) ;

double sum = elnsum ( f i r s t t e r m , second term ) ;output = elnproduct ( . 5 , sum) ;mxFree ( y ) ;

}

} e l s e {//output = . 5 ∗ (// p W ( ( i +1) /2 , s p l i t ( y , N/2 , 0 ) , xorr ( u hat , i −1)

, u , N/2 , pe , pf ) ∗p W ( ( i +1) /2 , s p l i t ( y , N/2 , 1 ) ,evens ( u hat , i −1) , 0 , N/2 , pe , pf ) +

// p W ( ( i +1) /2 , s p l i t ( y , N/2 , 0 ) , xorr ( u hat , i −1), ! u , N/2 , pe , pf ) ∗p W ( ( i +1) /2 , s p l i t ( y , N/2 , 1 ) ,evens ( u hat , i −1) , 1 , N/2 , pe , pf ) ) ;

double f i r s t t e r m = elnproduct (p W ( ( i +1) /2 , s p l i t ( y, N/2 , 0 ) , xorr ( u hat , i −1) , u , N/2 , pe , pf ) , p W( ( i +1) /2 , s p l i t ( y , N/2 , 1 ) , evens ( u hat , i −1) , 0 ,

N/2 , pe , pf ) ) ;

POLAR CODES OVER WIRELESS FADING CHANNELS 23

double second term = elnproduct (p W ( ( i +1) /2 , s p l i t ( y, N/2 , 0 ) , xorr ( u hat , i −1) , ! u , N/2 , pe , pf ) , p W( ( i +1) /2 , s p l i t ( y , N/2 , 1 ) , evens ( u hat , i −1) , 1 ,

N/2 , pe , pf ) ) ;double sum = elnsum ( f i r s t t e r m , second term ) ;output = elnproduct ( . 5 , sum) ;mxFree ( y ) ;mxFree ( u hat ) ;

}} e l s e {

i f ( i == 2) {//output = . 5 ∗ ( p W( 1 , s p l i t ( y , N/2 , 0 ) , 0 , u ˆ u hat

[ 0 ] , N/2 , pe , pf ) ∗ p W( 1 , s p l i t ( y , N/2 , 1 ) , 0 , u ,N/2 , pe , pf ) ) ;

double f i r s t t e r m = elnproduct (p W( 1 , s p l i t ( y , N/2 ,0 ) , 0 , u ˆ u hat [ 0 ] , N/2 , pe , pf ) , p W( 1 , s p l i t ( y ,N/2 , 1 ) , 0 , u , N/2 , pe , pf ) ) ;

output = elnproduct ( . 5 , f i r s t t e r m ) ;mxFree ( y ) ;mxFree ( u hat ) ;

} e l s e {//output = . 5∗p W( i /2 , s p l i t ( y , N/2 , 0 ) , xorr ( u hat ,

i −2) , u ˆ u hat [ ( i −1)−1] , N/2 , pe , pf ) ∗p W( i /2 ,s p l i t ( y , N/2 , 1 ) , evens ( u hat , i −2) , u , N/2 , pe ,pf ) ;

double f i r s t t e r m = elnproduct (p W( i /2 , s p l i t ( y , N/2 , 0 ) , xorr ( u hat , i −2) , u ˆ u hat [ ( i −1)−1] , N/2 ,pe , pf ) , p W( i /2 , s p l i t ( y , N/2 , 1 ) , evens ( u hat , i−2) , u , N/2 , pe , pf ) ) ;

output = elnproduct ( . 5 , f i r s t t e r m ) ;mxFree ( y ) ;mxFree ( u hat ) ;

}

}

# i f d e f MEMOIZEmap[ key ] = output ;

# endi fre turn output ;

}