Professional Documents
Culture Documents
Bjrn S. Rer o u Christopher M. Kellett Technical Report Version as of November 13, 2008
We provide some example Matlab code as a supplement to the paper [6]. This technical report is not intended as a standalone introduction to the belief propagation algorithm, but instead only aims to provide some technical material, which didnt t into the paper.
1 Introduction
For an excellent introduction and mathematical treatise of modern iterative decoding theory, we refer to [4]. Worth mentioning is also the survey paper [2] on factor graphs and the sum-product algorithm, the superclass that contains belief propagation. This current technical note provides Matlab code to implement the dynamical system formulation of the belief propagation algorithm and a few related concepts, as detailed in [6]. More conventional implementations that is, from a coding perspective exist and some are publicly available [3]. The Matlab code examples detailed in this report can be found, along with the most up-to-date version of this report itself, at [5]. Our presentation diers also in another aspect from the standard ones: Unlike the information theory convention, where messages and codewords are represented by row vectors, we throughout use column vectors as this is standard in dynamical systems. Of course this does not lead to dierences other than representational ones. This report is organized as follows: In Section 2 we give a simple example on how one can generate a very basic random parity-check matrix and compute a corresponding generator matrix. Section 3 details the channel transmission and Section 4 provides code to implement the belief propagation algorithm as a dynamical system. The output trajectories obtained using this Matlab code can then be plotted using the routine in
School of Electrical Engineering and Computer Science, The University of Newcastle, Australia, Bjoern.Rueffer@Newcastle.edu.au, Chris.Kellett@newcastle.edu.au
Section 5. In Section 6 we provide a more advanced method for generating parity-check matrices with prescribed degree distribution.
A simple random parity-check matrix can be generated using the code in Listing 1, and code for generating more involved parity-check matrices is given in Section 6. Using only Gauss elimination and possibly by swapping columns, H can be brought into the form QHP = Im A , (1) where the invertible matrix Q Fmm encodes the steps of the Gauss elimination 2 (swapping and adding rows of H), P Fnn is a permutation matrix to encode the 2 swapping of columns in H, A Fmk , and Im is the m m identity matrix. 2 A generator matrix for H is a matrix G Fnk such that HG = 0. According to (1) 2 we can take G to be A G=P , (2) Ik since QHG = Im A P 1 P A = 2A = 0 Ik (in Fm ) . 2
Now G maps message vectors m Fk to codewords c = Gm Fn , i.e., to elements 2 2 of the null-space C = CH = {x Fn : Hx = 0} of H, which is also termed the set of 2 codewords or just the code. The MATLAB code examples in Listings 1 and 2 can be used to generate a very basic parity-check matrix and to obtain a generator matrix from a given parity-check matrix. Listing 1: A crude way to obtain a simple parity-check matrix, by just specifying the dimensions m and n and a density of non-zero elements in H of at least d (0, 1).
1 2 3 4 5 6 7 8
function H = g e n e r a t e H (m, n , d )
% H = GENERATE H ( m , n , d ) % % generate a m by n by the parity check matrix , where zero the and density one ) is % influenced parameter d ( between
9 10 11 12
while not ( a l l (sum(H,1) >=2) && a l l (sum(H,2) >=2)) , H=H+abs ( sprand (m, n , d )) >0; H=mod(H, 2 ) ; end
Listing 2: A code example to construct a generator matrix for a given parity-check matrix. Some consistency checks (e.g., to see if n > m) are omitted.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
function [G]= g e n e r a t o r m a t r i x (H ) ;
% function % H compute [ G] = g e n e r a t o r m a t r i x ( H ) ; a generator matrix G given a sparse parity check matrix
colperm=1:n ; f o r j =1:m,
% find row to put as new row j
k=min(max( find (Hp( j , : ) ) , j ) ) ; i f isempty ( k ) , disp ( [ problem in row num2str ( j , 0 ) ] ) ; continue ; end temp = Hp ( : , j ) ; Hp ( : , j )=Hp ( : , k ) ; Hp ( : , k)=temp ; temp=colperm ( k ) ; colperm ( k)=colperm ( j ) ; colperm ( j )=temp ; end
% swap rows % adjust indices !
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
t 1=f u l l (Hp( j , : ) ) ; f o r k=K , t 2=f u l l (Hp( k , : ) ) ; temp=xor ( t1 , t 2 ) ; Hp( k , : ) = sparse ( temp ) ; end end end % f o r
% now Hp = [ I d m A ]
A = Hp ( : ,m+1:n ) ;
%c o m p u t e G
end % f u n c t i o n
where pYi |Xi is the density of the conditional probability (xi , yi ) P (Yi yi |Xi = xi ). To compute ui , we actually have to know , or at least make a guess about . The guessing step, which we will omit here, is called estimation. For simplicity we assume that the receiver knows . Substituting the density formulas for the N (1, 2 ) and N (1, 2 ) distributions into (4) we obtain 4yi (5) ui = 2 . 2 So at this stage we can encode a k-bit message to an n-bit codeword, transmit it through a noisy channel (using BPSK) and compute a-priori log-likelihood ratios.
function [ ] =H2DS(H)
% H2DS ( H ) % % generate matrices f o r dynamical system a s s o c i a t e d to H, i . e . , P , % S , and q , which a r e s t o r e d a s g l o b a l v a r i a b l e s , a s w e l l a s m and % n, the dimensions of H
q m n
the
amount
of
nonzero
elements
these
matrices :
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
k =0; f o r j =1:n , I=find (H( : , j ) ) ; f o r x =1: length ( I ) , f o r y=x +1: length ( I ) , P( k+x , k+y )=1; P( k+y , k+x )=1; end end k=k+length ( I ) ; end
% find S ( structure for the nonlinearity S)
k =0; f o r i =1:m, J=find (H( i , : ) ) ; f o r x =1: length ( J ) , f o r y=x +1: length ( J ) , S ( k+x , k+y )=1; S ( k+y , k+x )=1; end end k=k+length ( J ) ; end
% compute matrix B
function y = S ( x )
% S the % % y=S ( x ) % formula % row in nonlinearity is to the the vector in t h e BP f e e d b a c k by system the to atanh the ith
obtained of S x
applying
those global
indices matrix
corresponding
global S
y=ones ( q , 1 ) ; f o r i =1: q ,
12 13 14 15 16
Listing 5: A function to calculate output trajectories for the dynamical system mimicking BP, where u Rn is the vector of a-priori LLRs and T N is the number of iterations to make
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
function y = i t e r a t e B P (T, u )
% y = i t e r a t e B P (T , u ) This f u n c t i o n implements % system ; the output t r a j e c t o r y i s returned for % input u. The initial state is always zero . t h e BP d y n a m i c a l f i n a l t i m e T and
global B P S x1 x2 x1 x2
q n m
y=zeros ( n , T+1); f o r t =1:T, x 1 k 1=P x 2 k+Bu ; x 2 k 1=S ( x 1 k ) ; y ( : , t )=B x 2 k + u ; x 1 k=x 1 k 1 ; x 2 k=x 2 k 1 ; end y ( : , T+1)=B x 2 k + u ;
function p l o t B P o u t p u t ( y , mono , f i l e n a m e )
% p l o t B P o u t p u t ( y , mono , f i l e n a m e ) P l o t % If mono is supplied ( regardless of its a given output then trajectory . in value ) , plot
global n
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
T=s i z e ( y ) ; T=T(2) 1; clf ; i f nargin >1, plot ( 0 : T, y , ko - ) % m o n o c h r o m e else plot ( 0 : T, y , o- ) % c o l o r end grid on ; axis ( [ 0 T min(min( y )) .5 max(max( y ) ) + . 5 ] ) LEGEND= [ ] ; f o r k =1:n , LEGEND=[LEGEND; s t r c a t ( output , num2str ( k ) ) ] ; end legend (LEGEND) xlabel ( time ) ; ylabel ( LLR ) ; i f nargin==3, i f isstr ( filename ) , s a v e a s ( gcf , f i l e n a m e , eps ) ; end end
i xi1 i xi1 ,
i
(6) (7)
(x) =
cf. [4, p.79], where i is the fraction of edges that connect to a variable node of degree i and i is the fraction of edges that connect to a check node of degree i.
function [ H, f i n a l c o l u m n w e i g h t s , f i n a l r o w w e i g h t s ] = . . . MacKayNealCreateCode ( n , r , v , h )
% % MacKay % % Date % % Inputs : % % % Outputs : % % % % Sample p a r a m e t e r s f o r r a t e 1/2 , l e n g t h 100 , (3 ,6) r e g u l a r code % n = 100; % r = 0.5; % Code % Code length rate Neal algorithm 3 for constructing 2008 code rate (r), (h) (H) , generated column column weight polynomial p a r i t y check matrix
November (n) ,
length row
weight x n
polynomial
n (1 r ) and row
p a r i t y check
matrix
weight
polynomials
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
% v = [0 % h = [0
1.0]; 1.0];
% Column % Row
distributions as
as
polynomial
0 0 0 0
distribution
polynomial
% % Initialisation
% alpha % each
will column
contain
the
column
weight
for
out
alpha
to
the
appropriate
length
beta = [ beta , i ] ; end end while ( length ( beta ) = m) beta = [ beta i ] ; end
% Construction
fill
out
beta
to
the
appropriate
% length
for i = 1 : n
% construct column i
having
% many e n t r i e s . C est la guerre . The a c t u a l row w e i g h t s t h u s % c o n s t r u c t e d are c a l c u l a t e d below . You s h o u l d c h e c k t h a t % they re not too far off ...
while ( ( ( b e t a t e m p ( temp row ) == 0 ) && . . . (max( b e t a t e m p ) > 0 ) ) | | . . . ( ( b e t a t e m p ( temp row ) <= 1)))
10
temp row = mod( temp row +1,m)+1; end c = [ c temp row ] ; b e t a t e m p ( temp row ) = 10; end
% decrement entries in beta
r o w w e i g h t s = H ones ( n , 1 ) ; f o r i = 1 :max( r o w w e i g h t s ) c ou nt = 0 ; f o r j = 1 : length ( r o w w e i g h t s ) i f ( r o w w e i g h t s ( j )== i ) cou nt = cou nt + 1 ; end end f i n a l r o w w e i g h t s ( i ) = coun t / length ( r o w w e i g h t s ) ; end
7 Example
In Listing 8 we show some exemplary use of the provided Matlab functions. Suppose we would like to generate a simple random 10 15 parity-check matrix, see what it looks like, and compute a generator matrix. Here we have n = 15, m = 10, and hence k = 5. In Figure 7 we see the structure of these two matrices.
11
Now let us generate a random message vector m Fn , encode this message to a 2 codeword x, and transmit it via an AWGN channel with standard deviation = 0.5. Now we could base a hard decision on this vector u and see if that yields a codeword, i.e., in Matlab we could try any(mod(H(double(u<0)),2)), which should return 1 if we have a codeword. We may nd at this stage that we do not have found a valid codeword yet. So let us perform belief propagation decoding, using the dynamical system implementation given in Section 4. Listing 8: Generating a parity-check and a generator matrix as in Section 7. Exercise the encoding, channel transmission and decoding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
n=10; m=n1; H=s p a l l o c ( n1,n , 2 ( n 1 ) ) ; f o r i =1:n1, H( i , i )=1; H( i , i +1)=1; end G=g e n e r a t o r m a t r i x (H ) ; spy (H) % s e e what H l o o k s spy (G) % s e e what G l o o k s H2DS(H ) ; % s e t u p f o r t h e BP a l g o r i t h m
like like
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
H=g e n e r a t e H ( 1 0 , 1 5 , . 0 5 ) ; % 1 5 1 0 i s 5 a s we a l l k n o w
%% % at this s t a g e H might this step not have full rank , in that case we just % repeat %%
global H spy (H) % s e e what H l o o k s s a v e a s ( gcf , H.eps , eps ) ; G=g e n e r a t o r m a t r i x (H ) ; spy (G) % s e e what G l o o k s s a v e a s ( gcf , G.eps , eps ) ; H2DS(H ) ; % s e t u p f o r t h e BP a l g o r i t h m
like
like
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
12
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
[m, n]= s i z e (H ) ; k=nm; message=d o u b l e ( rand ( k , 1 ) > 0 . 5 ) ; % a k v e c t o r o f 0 s a n d 1 s x=G message ; % x i s now a n n v e c t o r o f 0 s a n d 1 s ( a c o d e w o r d ) x t i l d e=b p s k ( x ) ; % m a k e t h a t 1 s a n d 1 s ( c o d e w o r d i n c h a n n e l s y m b o l s ) i s c o d e w o r d ( h a r d d e c i s i o n ( x t i l d e ) ) % e v e r y t h i n g OK u p t o h e r e ?
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % % Simulate transmission through a n AWGN c h a n n e l . Actually , we to % repeat as often as needed u n t i l the channel c o r r u p t s the % t r a n s m i t t e d c o d e w o r d , s o t h a t we w i l l h a v e t h e o p p o r t u n i t y % employ % % % REPEAT FROM HERE TO VIEW PLOTS OF DIFFERENT TRAJECTORIES % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% the iterative decoder .
sigma = 0 . 5 ; % t h e c h a n n e l p a r a m e t e r
% generate a channel output which at first is not a codeword :
y=x t i l d e+randn ( n , 1 ) sigma 2 ; % t h e c h a n n e l a d d s t h e n o i s e TRIES=100; t =0; while i s c o d e w o r d ( h a r d d e c i s i o n ( y ) ) && t<TRIES , y=x t i l d e+randn ( n , 1 ) sigma ; t=t +1; end
% iterations
t h e BP a l g o r i t h m
( actually ,
our
implementation ,
% we d o T HALF i t e r a t i o n s . % % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
T=50; % ( h a l f ) i t e r a t i o n s
for
BP t o
perform
13
87 88 89 90 91 92 93 94 95 96 97 98
clf ; else performing BP Y=i t e r a t e B P (T, u ) ; % t h a t m e a n s T ( h a l f ) i t e r a t i o n s f o r k =1:T, i f any (mod(H d o u b l e (Y( : , k ) < 0 ) , 2 ) ) , s t r c a t ( found a codeword at iteration _ , num2str ( k ) ) break ; end end plot ( 0 : T, Y, o- ) end
0 1 2 3
8
4 5
10
6 7 8 9 10 11 0 2 4 6 8 nz = 60 10 12 14 16
12
14
16 0 2 nz = 29 4 6
Figure 1: The graphical output of Listing 8: H on the left and G on the right.
14
References
[1] S.-Y. Chung, G. D. Forney, T. J. Richardson, and R. L. Urbanke. On the design of low-density parity-check codes within 0.0045 dB of the Shannon limit. IEEE Communications Letters, 5(2):5860, Feb. 2001. [2] F. R. Kschischang, B. J. Frey, and H.-A. Loeliger. Factor graphs and the sum-product algorithm. IEEE Transactions on Information Theory, 47(2):498519, 2001. [3] R. M. Neal. Software for Low Density Parity Check (LDPC) codes. online, 2001. http://www.cs.toronto.edu/ radford/software-online.html. [4] T. Richardson and R. Urbanke. Modern Coding Theory. Cambridge University Press, New York, 2008. [5] B. S. Rer and C. M. Kellett. u Example Matlab code to implement Belief Propagation as a Dynamical System, Nov. 2008. Available at http://sigpromu.org/systemanalysis. [6] B. S. Rer, C. M. Kellett, P. M. Dower, and S. R. Weller. Belief Propagation as u a Dynamical System: The Linear Case and Open Problems. Submitted, Nov. 2008. Preprint available at http://sigpromu.org/systemanalysis/publications.html.
15