Aim:
To characterize and analyze a Digital modulation technique over fading channel with channel coding
and to realize its BER in AWGN channel.

Program:
clc;
close all;
clear all;
% Sampling Operation
f=100;%input signal frequency
A=1;
fs=20*f;%sampling frequency;
Ts=1/fs;
ncyle=20;
x=0:1/fs:ncyle/f;% n1 nuber of samples have tobe selected
s=A*sin(2*pi*f*x);
subplot(3,1,1);
plot(x,s);
title('Analog Signal');
ylabel('Amplitude(volts)--->');
xlabel('Time(sec)--->');
subplot(3,1,2);
stem(s);grid on; title('Sampled Sinal'); ylabel('Amplitude(volts)--->'); xlabel('Sample
Instants--->');
n=12;%Number of bits in PCM
L=2^n;% Number of levels.
vmax=A;
vmin=-vmax;
del=(vmax-vmin)/L; %step size parameter

part=vmin:del:vmax;% level are between vmin and vmax with difference of del
code=vmin-(del/2):del:vmax+(del/2);% Contaion Quantized valuses
[ind,q]=quantiz(s,part,code);% Quantization process

% ind contain index number and q contain quantized values
l1=length(ind);
l2=length(q);
for i=1:l1;
if(ind(i)~=0)% To make index as binary decimal so started from 0 to
N
ind(i)=ind(i)-1;
end
i=i+1;
end
for i=1:l2;
if(q(i)==vmin-(del/2))% To make quantize value inbetween the levels
q(i)=vmin+(del/2);
end
end
subplot(3,1,3);
stem(q);grid on;% Display the Quantize values
title('Quantized Signal');
ylabel('Amplitude--->');
xlabel('Sample Instant--->');

% Encoding Process
code=de2bi(ind,'left-msb');% Cnvert the decimal to binary
k=1;
for i=1:l1;
for j=1:n;
coded(k)=code(i,j);% convert code matrix to a coded row vector
j=j+1;
k=k+1;
end
i=i+1;
end
figure(2); grid on;

% coded1=coded;
stairs(coded);% Display the encoded signal
axis([0 100 -2 3]); title('PCM Encoded Signal Bit Stream');
ylabel('Amplitude--->');
xlabel('Time--->');

% D=[1 0 1 1 0 0 1 1
ln=length(coded);
c=[];
k=4;blk=ln/k;
for i=1:blk;
if i==1
m=coded(1:k);
else
m=coded(k*(i-1)+1:k*i);
end
[c1]=lbcenc(m);
[c]=[c c1];
end
s =(2*coded)-1; % BPSK modulation 0 -> -1; 1 -> 0
s1 =(2*c)-1;% BPSK modulation LBC
N=length(coded);
snrdb=0.5:1:15;
for i1=1:length(snrdb);
x=awgn(s,snrdb(i1),'measured');
a1=awgn(s1,snrdb(i1),'measured');%lbc
y=x;

%Rayleigh Fadding
h = (1/sqrt(2))*[randn(1,N) + j*randn(1,N)]; % Rayleigh channel
% % Channel and Noise addition
y1 = h.*s;
% received signal after fadding
x1=awgn(y1,snrdb(i1),'measured');
x1=x1./h;
N1=length(c);
for j=1:1:N;
if y(j)>0
z(j)=1; %awgn
else
z(j)=0;
end
if x1(j)>0
z1(j)=1; %fadding+AWGN
else
z1(j)=0;
end
end
for i=1:N1;
if a1(i)>0
z2(i)=1; %AWGN+LBC
else
z2(i)=0;
end
end
k1=7;blk=N1/7;
r2=[];
for i=1:blk;
if i==1
m=z2(1:k1);
else
m=z2((k1*(i-1))+1:k1*i);
end
[r1]=lbcdec(m);
[r2]=[r2 r1];
end
m=[];
for i=1:blk;
if i==1
m1=r2(1:k1-3);
else
m1=r2(k1*(i-1)+1:(k1*i)-3);
end
[m]=[m m1];
end
error=length(find(z~=coded));
ber(i1)=error/N;
error=length(find(z1~=coded));
berf(i1)=error/N;
error=length(find(m~=coded));
berflb(i1)=error/N;
end
snrlin=10.^(snrdb./10);
figure(3)
semilogy(snrdb,ber,'-bo',snrdb,berflb,'-mh',snrdb,berf,'-x');
legend('Practical AWGN','AWGN+LBC','practical Fadding');
grid on;
title('Performance analysis of BPSK in presence of LBC');
xlabel('Signal to noise ratio in dB');
ylabel('Bit error rate');

%The possible code vector generation
for i = 1:2^k;
for j = k:-1:1;
if rem(i-1,2^(-j+k+1))>=2^(-j+k)
u(i,j)=1;
else
u(i,j)=0;
end
end
end
disp('The possible codes for (7,4) LBC is');
for i=1:1:(2^k);
[c1]=lbcenc(u(i,:));
cw(i,:)=c1;
end
cw
function [c]=lbcdec(r)
g=[ 1 0 0 0 1 0 1;
0 1 0 0 1 1 1;
0 0 1 0 1 1 0;
0 0 0 1 0 1 1;];
h=[1 1 1 0 1 0 0;0 1 1 1 0 1 0;1 1 0 1 0 0 1];
ht=h';
s=rem(r*ht,2);
n=length(r);
zr=[0 0 0];
if s==zr
c=r;
else
c=r;
for i=1:n;
if s==ht(i,:);
c(i)=~r(i);
break;
end
end
end
end
function [c]=lbcdec(r)
g=[ 1 0 0 0 1 0 1;
0 1 0 0 1 1 1;
0 0 1 0 1 1 0;
0 0 0 1 0 1 1;];
h=[1 1 1 0 1 0 0;0 1 1 1 0 1 0;1 1 0 1 0 0 1];
ht=h';
s=rem(r*ht,2);
n=length(r);
zr=[0 0 0];
if s==zr
c=r;
else
c=r;
for i=1:n;
if s==ht(i,:);
c(i)=~r(i);
break;
end
end
end
end