├── README.md ├── shortspa.m └── testldpc.m /README.md: -------------------------------------------------------------------------------- 1 | # LDPC-coded-OFDM-system-on-AWGN 2 | LDPC-coded OFDM system on AWGN using Sum Product Algorithm (SPA) 3 | -------------------------------------------------------------------------------- /shortspa.m: -------------------------------------------------------------------------------- 1 | %LDPC coded OFDM system in AWGN 2 | clc 3 | clear all 4 | M=2;%assigning the M-ary for psk modulation 5 | cp=0; % Cylic prefix length 6 | 7 | H=[1 1 0 1 0 0;0 1 1 0 1 0;0 0 1 1 0 1]; %parity chk matrix 8 | G=[0 1 1 1 0 0;1 1 0 0 1 0;1 1 1 0 0 1]; %generator polynomials 9 | %size 10 | [a,b] = size(H); 11 | [c,d] = size(G); 12 | datasize=c; 13 | Nfft=d; 14 | %code rate, row weight and column weight 15 | %Wr=3, %Wc=1-2, R=irregular; 16 | frame=100; 17 | 18 | SNR1=0:1:20;%assigning the value of snr in decibels 19 | SNR=10.^(SNR1/10);%converting the snr db value to dynamic range 20 | 21 | %==== ITU Pedestrian channel B ==== 22 | h=zeros(1,43); 23 | p=[-3.92 -4.82 -8.82 -11.92 -11.72 -27.82]; 24 | cc=sqrt(10.^(p./10)); 25 | ind=[1 3 10 15 27 43]; 26 | h(ind)=cc; 27 | KK=length(h); 28 | 29 | for i=1:length(SNR) 30 | error=0; 31 | for jj=1:frame;%MONTE CARLO LOOP 32 | 33 | %---------------------------------TRANSMITTER---------------------------------- 34 | msg=randint(1,c,M); % Random data 35 | 36 | %----------LDPC Encoding------------ 37 | code=mod(msg*G,2); % modulo-2 Encoding 38 | %----------interleaving---------- 39 | intrlvcode=randintrlv(code,4831); 40 | %-----------PSK modulation------------------ 41 | modulator=modem.pskmod('M',2,'PhaseOffset',0,'SymbolOrder','gray','InputType','integer');%definng the 8-psk modulator objec 42 | mod_code=modulate(modulator,intrlvcode);%modulate with the input 43 | %-----------IFFT & CP------------------ 44 | y_ifft=ifft(mod_code,Nfft)*sqrt(Nfft);%performing the inverse fast fourier transform 45 | y_ifft_cp=[y_ifft(end-cp+1:end) y_ifft];%addition of cyclic prefix with last k sample 46 | 47 | %---------------------------------CHANNEL---------------------------------- 48 | 49 | %----------mulipath fading---------- 50 | U=conv(h,y_ifft_cp); 51 | %-------------AWGN Noise-------------------- 52 | sigma=sqrt(1/(SNR(i)*log2(M)*2*0.5));%to compute the value of sigma 53 | w=sigma*(randn(1,length(U)));%generating complex additive gaussian noise 54 | 55 | ytr=w+U;%adding the complex additive gaussian noise to the y_ifft_cp 56 | 57 | 58 | %---------------------------------RECEIVER--------------------------------- 59 | %---------- Discard last Nch-1 received values resulting from convolution 60 | ytr(end-KK+2:end) = []; 61 | %-----------IFFT & CP removal------------------ 62 | ytr(1:cp) = []; %to remove the gaurd interval 63 | Y=fft(ytr,Nfft)/sqrt(Nfft);%performing the fast fourier transform 64 | %-----------Zero Force (ZF) one-tap equalizer-------------------- 65 | H=fft(h,Nfft); 66 | Yeq=Y./H; 67 | %-----------PSK de-modulation------------------ 68 | % demodulator=modem.pskdemod('M',2,'PhaseOffset',0,'SymbolOrder','gray');%defining the 8 psk demodulator object 69 | % demod=demodulate(demodulator,Y);%perform demodulation 70 | %-----------deinterleaving-------------------- 71 | deintrlvdemod=randdeintrlv(Yeq,4831); 72 | 73 | LLR=(2*deintrlvdemod)/(sigma*sigma); 74 | 75 | %-----------sum product algorithm Decoding---- 76 | %----Stage 1 : Initialization(Q=H*q) 77 | 78 | for x=1:a 79 | for y=1:b 80 | Q(x,y)= H(x,y)*LLR(y); 81 | end 82 | end 83 | 84 | for iterations=1:40 85 | 86 | %----Stage 2 : Horizontal step 87 | Tanh = tanh (Q./2); %Tanh of Q 88 | varb= Tanh; %defining a variable 89 | varb(varb==0)=1; 90 | cvec = prod (varb,2); % column vector (product of all elements in the same row) 91 | prct=repmat (cvec,1,b); %repeating the column vector N 92 | 93 | 94 | for x=1:a 95 | for y=1:b 96 | if varb(x,y)~=1 97 | L(x,y)= prct(x,y)/varb(x,y); %excluding same elemnt of the product 98 | 99 | if L(x,y)<-0.9999 100 | L(x,y)=-0.9999; 101 | elseif L(x,y)>0.9999 102 | L(x,y)=0.9999; 103 | end 104 | 105 | R(x,y)= log ((1+L(x,y))/(1-L(x,y))); 106 | 107 | else 108 | R(x,y)=0; 109 | end 110 | end 111 | end 112 | 113 | 114 | softout = zeros(1,b); 115 | %-----------LDPC Hard decision----------------- 116 | 117 | vertical_sum=sum (R); 118 | for y=1:b 119 | softout(y)= LLR(y)+ vertical_sum(y); 120 | if softout(y) < 0 121 | out(y)= 1; 122 | else 123 | out(y)= 0; 124 | end 125 | end 126 | 127 | 128 | %-----------Break point----------------- 129 | stop=mod(out*H',2); 130 | if stop == zeros(1,a) 131 | break; 132 | end 133 | 134 | %----stage 3 : Vertical step 135 | for x=1:a 136 | for y=1:b 137 | if H(x,y)~=0 138 | Q(x,y)=LLR(y)+ vertical_sum(y)-R(x,y); %updated Q 139 | else 140 | Q(x,y)=0; 141 | end 142 | end 143 | end 144 | 145 | 146 | 147 | end 148 | %-------------------------------------------------------------------------- 149 | [err ratio] = biterr(msg,out(4:6));%comparing the error in the data received to the input 150 | error=error+err; 151 | 152 | end 153 | error1(i)=error/(3*log2(M)*frame); 154 | end 155 | 156 | 157 | %computing BER performance in AWGN 158 | p=0.5*erfc(sqrt(SNR)); 159 | 160 | %plotting the results 161 | figure(1) 162 | semilogy(SNR1,p); 163 | hold on 164 | semilogy(SNR1,error1,'ro-'); 165 | xlabel('SNR in dB') 166 | ylabel('Bir error rate') 167 | title('Bit error rate vs SNR for AWGN channel cp=64') 168 | grid on 169 | -------------------------------------------------------------------------------- /testldpc.m: -------------------------------------------------------------------------------- 1 | %LDPC coded OFDM system in AWGN 2 | clc 3 | clear all 4 | M=2;%assigning the M-ary for psk modulation 5 | cp=0; % Cylic prefix length 6 | 7 | H=[1 1 0 1 0 0;0 1 1 0 1 0;0 0 1 1 0 1]; %parity chk matrix 8 | G=[0 1 1 1 0 0;1 1 0 0 1 0;1 1 1 0 0 1]; %generator polynomials 9 | %size 10 | [a,b] = size(H); 11 | [c,d] = size(G); 12 | datasize=c; 13 | Nfft=datasize*2; 14 | %code rate, row weight and column weight 15 | %Wr=3, %Wc=1-2, R=irregular; 16 | frame=10; 17 | 18 | SNR1=0:1:6;%assigning the value of snr in decibels 19 | SNR=10.^(SNR1/10);%converting the snr db value to dynamic range 20 | 21 | %==== ITU Pedestrian channel B ==== 22 | % h=zeros(1,43); 23 | % p=[-3.92 -4.82 -8.82 -11.92 -11.72 -27.82]; 24 | % cc=sqrt(10.^(p./10)); 25 | % ind=[1 3 10 15 27 43]; 26 | % h(ind)=cc; 27 | % KK=length(h); 28 | 29 | for i=1:length(SNR) 30 | error=0; 31 | for jj=1:frame;%MONTE CARLO LOOP 32 | 33 | %---------------------------------TRANSMITTER---------------------------------- 34 | msg=randint(1,c,M); % Random data 35 | 36 | %----------LDPC Encoding------------ 37 | code=mod(msg*G,2); % modulo-2 Encoding 38 | %----------interleaving---------- 39 | intrlvcode=randintrlv(code,4831); 40 | %-----------PSK modulation------------------ 41 | modulator=modem.pskmod('M',2,'PhaseOffset',0,'SymbolOrder','gray','InputType','integer');%definng the 8-psk modulator objec 42 | mod_code=modulate(modulator,intrlvcode);%modulate with the input 43 | %-----------IFFT & CP------------------ 44 | % y_ifft=ifft(y,Nfft)*sqrt(Nfft);%performing the inverse fast fourier transform 45 | % y_ifft_cp=[y_ifft(end-cp+1:end) y_ifft];%addition of cyclic prefix with last k sample 46 | 47 | %---------------------------------CHANNEL---------------------------------- 48 | 49 | %----------mulipath fading---------- 50 | %U=conv(h,y1_ifft_cp); 51 | %-------------AWGN Noise-------------------- 52 | sigma=sqrt(1/(SNR(i)*log2(M)*2*0.5));%to compute the value of sigma 53 | w=sigma*(randn(1,length(mod_code)));%generating complex additive gaussian noise 54 | 55 | ytr=w+mod_code;%adding the complex additive gaussian noise to the y_ifft_cp 56 | 57 | 58 | %---------------------------------RECEIVER--------------------------------- 59 | %---------- Discard last Nch-1 received values resulting from convolution 60 | %ytr(end-KK+2:end) = []; 61 | %-----------IFFT & CP removal------------------ 62 | % ytr(1:cp) = []; %to remove the gaurd interval 63 | % Y=fft(ytr,Nfft)/sqrt(Nfft);%performing the fast fourier transform 64 | %-----------Zero Force (ZF) one-tap equalizer-------------------- 65 | % H=fft(h,Nfft); 66 | % Yeq=Y1./H; 67 | %-----------PSK de-modulation------------------ 68 | % demodulator=modem.pskdemod('M',2,'PhaseOffset',0,'SymbolOrder','gray');%defining the 8 psk demodulator object 69 | % demod=demodulate(demodulator,Y);%perform demodulation 70 | %-----------deinterleaving-------------------- 71 | deintrlvdemod=randdeintrlv(ytr,4831); 72 | 73 | %LLR=((2*deintrlvdemod)/(sigma*sigma)); 74 | %LLR=deintrlvdemod; 75 | %-----------sum product algorithm Decoding---- 76 | %----Stage 1 : Initialization(Q=H*q) 77 | 78 | 79 | for x=1:a 80 | for y=1:b 81 | Q(x,y)= H(x,y)*deintrlvdemod(y); 82 | end 83 | end 84 | 85 | for iterations=1:50 86 | 87 | %----Stage 2 : Horizontal step 88 | 89 | sgn = sign (Q); %Tanh of Q 90 | varb= sgn; %defining a variable 91 | varb(varb==0)=1; 92 | 93 | nozero=Q; 94 | nozero(nozero==0)=inf; 95 | nozero=abs(nozero); 96 | 97 | 98 | cvec = prod (varb,2); % column vector (product of all elements in the same row) 99 | 100 | 101 | for x=1:a 102 | for y=1:b 103 | if H(x,y)~=0 104 | 105 | [C,I]=min(nozero(x,:)); 106 | if I==y 107 | store=nozero(x,y); 108 | nozero(x,y)=max(nozero(x,:)); 109 | C=min(nozero(x,:)); 110 | nozero(x,y)=store; 111 | end 112 | R(x,y)=(C.*varb(x,y)).*cvec(x); 113 | else 114 | R(x,y)=0; 115 | end 116 | end 117 | end 118 | %-----------LDPC Hard decision----------------- 119 | vertical_sum=sum (R); 120 | for y=1:b 121 | softout(y)= deintrlvdemod(y)+ vertical_sum(y); 122 | if softout(y) < 0 123 | out(y)= 1; 124 | else 125 | out(y)= 0; 126 | end 127 | end 128 | %-----------Break point----------------- 129 | stop=mod(out*H',2); 130 | if stop == 0 131 | break; 132 | end 133 | 134 | %----stage 3 : Vertical step 135 | for x=1:a 136 | for y=1:b 137 | if H(x,y)~=0 138 | Q(x,y)=deintrlvdemod(y)+ vertical_sum(y)-R(x,y); %updated Q 139 | else 140 | Q(x,y)=0; 141 | end 142 | end 143 | end 144 | 145 | 146 | 147 | end 148 | 149 | %-------------------------------------------------------------------------- 150 | [err ratio] = biterr(msg,out(4:6));%comparing the error in the data received to the input 151 | error=error+err; 152 | 153 | end 154 | error1(i)=error/(3*log2(M)*frame); 155 | end 156 | 157 | 158 | %computing BER performance in AWGN 159 | p=0.5*erfc(sqrt(SNR)); 160 | 161 | %plotting the results 162 | figure(2) 163 | semilogy(SNR1,p); 164 | hold on 165 | semilogy(SNR1,error1,'ro-'); 166 | xlabel('SNR in dB') 167 | ylabel('Bir error rate') 168 | title('Bit error rate vs SNR for AWGN channel cp=64') 169 | grid on 170 | --------------------------------------------------------------------------------