1.
MATLAB: Simulate the performance of a Viterbi decoder for the en-coder [1 + D2
1 + D + D2] over an additive Gaussian noise channel for BPSK modulated symbols. In your
simulations assume the initial state is the all-zero state, and run blocks of 1000 information
bits per block, terminating each block with two 0's to drive the encoder into the all-zero state.
Plot the BER vs SNR. Give the analytical expression for the bit-error-rate.
clc
clear all
number_of_bits=input('enter the number of input bits');
block_length=1000;
for k=1:(number_of_bits/block_length)-1
Infobits=randint(1,block_length-2);
Infobits((block_length*k+1):block_length*(k+1))=[0 0 Infobits];
end
Infobits(number_of_bits+1:number_of_bits+2)=[0 0];
%% rate 1/2 convolutional encoder G=[1+D2 1 +D+D2]
coded_bits=[];
for i=1:number_of_bits
out1(i)=xor(Infobits(i+2),Infobits(i));
out2(i)=xor(xor(Infobits(i),Infobits(i+1)),Infobits(i+2));
coded_bits=[coded_bits out1(i) out2(i)];
end
coded_bpsk=2*coded_bits-1;
%% SNR vs BER
snrdb=0:5;
snr=10.^(snrdb/10);
index=1;
for j=1:length(snrdb)
noise=sqrt(1/snr(j)).*randn(1,2*number_of_bits);
rec_signal=coded_bpsk+noise;
%% viterbi decoding
[metric,state_transition,b_state_transition,dec_symbol]=viterbi_decode(rec_
signal);
no_of_error=length(find(dec_symbol~=Infobits(3:number_of_bits+2)));
BER(index)=no_of_error/number_of_bits;
index=index+1;
end
semilogy(snrdb,BER,'k');
hold on
%% Theoritical BER
BER_T=zeros(1,size(snr,2));
for i=1:size(snr,2)
for j=0:5
BER_T(i)=BER_T(i)+((2^j)*(j+1)*qfunc(sqrt((j+5)*snr(i))));
end
end
semilogy(snrdb,BER_T,'-*m');
grid on
xlabel('snr(dB)');
ylabel('BER')
function [dist,s_path,state,re_code] = viterbi_decode(r)
s_out=[-1 -1; 1 1; -1 1; 1 -1; 1 1; -1 -1; 1 -1; -1 1];
h=round(size(r,2))/2;
d(2:4,1)=inf;
d(1)=0;
s_m=[0 2 1 3];
state_matrix=zeros(1,h+1);
m=1;
for i=1:2:size(r,2)
for j=1:8
h_dist(j,m)=norm(r(i:i+1)-s_out(j,:))^2;
end
m=m+1;
end
h_dist(1,1)=inf;
% path metric %
dist=trellis(h_dist,d,h);
%state transition%
s_path=path(dist,h,h_dist,state_matrix,s_m);
ts=1;
for i=1:h+1
state(1,ts:ts+1)=dec2bin(s_path(i),2);
if ts~=h+1
state(1,ts+2)=' ';
ts=ts+3;
end
end
%regenrated input%
re_code=regenerate(s_path,h);
function[d]=trellis(h_dist,d,h)
for i=1:h
x=1;
for j=1:4
if j==3 || j==4
x=2;
elseif j==1 || j==2
x=1;
end
v1=d(x,i)+h_dist(j,i);
v2=d(x+2,i)+h_dist(j+4,i);
d(j,i+1)=min(v1,v2);
end
end
function [state] = path(dist,h,hamming,state,s_m)
x=1;
j=1;
for i=h:-1:1
if j==3 || j==4
x=2;
elseif j==1 || j==2
x=1;
end
v1=dist(x,i)+hamming(j,i);
v2=dist(x+2,i)+hamming(j+4,i);
if v1~=v2
ad=min(v1,v2);
if ad==v1
b=x;
elseif ad==v2
b=x+2;
end
else
if dist(x,i)>dist(x+2,i)
b=x+2;
else
end
end
b=x;
end
state(1,i)=s_m(b);
j=b;
function[way]=regenerate(path,h)
for i=1:h
if path(i)==0
if path(i+1)==0
way(i)=0;
elseif path(i+1)==2
way(i)=1;
end
elseif path(i)==2
if path(i+1)==1
way(i)=0;
elseif path(i+1)==3
way(i)=1;
end
elseif path(i)==1
if path(i+1)==0
way(i)=0;
elseif path(i+1)==2
way(i)=1;
end
elseif path(i)==3
if path(i+1)==1
way(i)=0;
elseif path(i+1)==3
way(i)=1;
end
end
end
Fig. SNR vs BER