0% found this document useful (0 votes)
25 views15 pages

SIMULATION CODES of DC

Digital communication simulation codes

Uploaded by

alureomprakash49
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views15 pages

SIMULATION CODES of DC

Digital communication simulation codes

Uploaded by

alureomprakash49
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Name of Student: Roll No:

Title of Experiment: Simulation Code for Random Process

clc;
clear all;
% Set frequency & time vector (random process)
w0=2*pi*1/3; % Frequency is 1/3hz
t=0:1/10:10; %time runs from 0 to 10 seconds with 0.1sec increments
for k=1:4;
Th=2*pi*rand(1);
plot(t, sin(w0*t+Th));
hold all;
end
hold off;
grid on;
axis ([0 10 -2 2])
title ('Realization of process x(t)');
xlabel ('t(sec)');
ylabel ('volt');

Output:
Name of Student: Roll No:

Title of Experiment: Simulation of BER for BPSK in presence of noise

Code for simulation of BER performance of BPSK over a AWGN channel

clear;
clc;
N=10000000; %Number of input bits
EbN0dB = -5:1:27;
data=randn(1,N)>=0; % Generating a uniformly distributed random 1s and 0s
bpskModulated = 2*data-1; % Mapping 0->-1 and 1->1
M=2; % Number of Constellation points M=2^k for BPSK k=1
Rm=log2(M); % Rm=log2(M) for BPSK M=2
Rc=1; %Number of Constellation points M=2^k for BPSK k=1
BER = zeros(1,length(EbN0dB)); %Place holder for BER values for each Eb/N0
index=1;
for k=EbN0dB,
%Adding noise with variance according to the required Eb/N0
EbN0 = 10.^(k/10); %Converting Eb/N0 dB value to linear scale
noiseSigma = sqrt(1./(2*Rm*Rc*EbN0)); %Standard deviation for AWGN Noise
noise = noiseSigma*randn(1,length(bpskModulated));
received = bpskModulated + noise;
estimatedBits=(received>=0);
BER(index) = sum(xor(data,estimatedBits))/length(data);
index=index+1;
end
for
plotHandle=plot(EbN0dB,log10(BER),'r--');
set(plotHandle,'LineWidth',1.5);
title('SNR per bit (Eb/N0) Vs BER Curve for BPSK Modulation Scheme');
xlabel('SNR per bit (Eb/N0) in dB');
ylabel('Bit Error Rate (BER) in dB');
grid on;
hold on;
theoreticalBER = 0.5*erfc(sqrt(10.^(EbN0dB/10)));
plotHandle=plot(EbN0dB,log10(theoreticalBER),'k*');
set(plotHandle,'LineWidth',1.5);
legend('Simulated','Theoretical');
grid on;

Output:
Name of Student: Roll No:
Title of Experiment: Simulation of Entropy

hx=0;
px=0:.01:1;
for i=1:length(px)
q=1-px(i);
hx(i)=-px(i)*log2(px(i))-q*log2(q);
end
plot(px,hx);
grid on;
xlabel('Probability p(x)'); ylabel('Entropy H(x)');
Output:
Name of Student: Roll No:
Title of Experiment: Simulation of Linear Block Codes

% Linear Block coding


%Clear all;
clear all;
clc;
n=input('Enter no of codes bits: ');
k=input('Enter no of messages bits: ');
lp=n-k;
p=input('Enter parity matrix: ');

%Identity Matrix
ik=eye(k);

% Encoding
disp('Linear Block Codes : Encoding');
disp('Generator Matrix:');

%Concatenate arrays along specified dimension


g=cat(2,ik,p);
disp(g);

d=input('Enter message to be coded:');

%Matrix Multiplication
c1=mtimes(d,g);

%Modulus after division


disp('The codeword is :');
c=mod(c1,2);
disp(c);

% Decoding
disp('Linear Block Codes : Decoding');
r=input('Enter Received bit:');
disp('Transpose of parity matrix:');
pt=p.';
disp(pt);
ilp=eye(lp);
disp('Parity check matrix:');
h=cat(2,pt,ilp);
disp(h);
disp('Transpose parity check matrix:');
ht=h.';
disp(ht);
s1=mtimes(r,ht);
disp('Syndrome matrix:');
s=mod(s1,2);
disp(s);

if (s == 0)
disp('The received code is correct.');
else
for i=1:n

m=xor(s,ht(i,:));

if (m==0)
row =i;
break ;
end
end

r(1,row) = ~r(1,row);
disp('Correct codeword is:');
disp('c=');
disp(r);
end;

OUTPUT:

Enter no of codes bits: 7


Enter no of messages bits: 4
Enter parity matrix: [1 0 1;1 1 1;1 1 0;0 1 1]
Linear Block Codes : Encoding
Generator Matrix:
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
Enter message to be coded: [1 1 0 0]
The codeword is :
1 1 0 0 0 1 0
Linear Block Codes : Decoding
Enter Received bit: [1 1 1 0 0 1 0]
Transpose of parity matrix:
1 1 1 0
0 1 1 1
1 1 0 1
Parity check matrix:
1 1 1 0 1 0 0
0 1 1 1 0 1 0
1 1 0 1 0 0 1
Transpose parity check matrix:
1 0 1
1 1 1
1 1 0
0 1 1
1 0 0
0 1 0
0 0 1
Syndrome matrix:
1 1 0
Correct codeword is:
c=
1 1 0 0 0 1 0
Name of Student: Roll No:
Title of Experiment: Simulation of Huffman Encoding & Decoding

clear all;
clc;
n=input('Enter no. of inputs:');
p=input('Enter probabilities of messages:');
symbols=[1:n];

[dict,avglen]=huffmandict(symbols,p);
disp('Huffman Dicitionary :');
disp(dict);
disp('Huffman bit length :');
disp(avglen);
temp=dict;
for i=1:length(temp)
temp{i,2}=num2str(temp{i,2});
end
disp(temp);

sig=input('Enter string of message');


disp('Huffman codes for given messages:');
sample_code=huffmanenco(sig,dict);
disp(sample_code);message_rece=input('Enter received bit string:');
message_orig=huffmandeco(message_rece,dict);
disp(message_orig);
err=isequal(sig,message_orig);
if err==1
disp('Message recovered correctly');
else
disp('Error in decoding');
end ;
OUTPUT:
Enter no. of inputs:6
Enter probabilities of messages:[0.3 0.25 0.2 0.12 0.08 0.05]
Huffman Dicitionary :
[1] [1x2 double]
[2] [1x2 double]
[3] [1x2 double]
[4] [1x3 double]
[5] [1x4 double]
[6] [1x4 double]
Huffman bit length :
2.3800

[1] '0 0'


[2] '0 1'
[3] '1 1'
[4] '1 0 1'
[5] '1 0 0 0'
[6] '1 0 0 1'

Enter string of message[1 3 2 4]


Huffman codes for given messages:
0 0 1 1 0 1 1 0 1

Enter received bit string:[0 0 1 1 0 1 1 0 1]


1 3 2 4

Message recovered correctly


Name of Student: Roll No:
Title of Experiment: Simulation of M-ary QAM

% Demonstration of Eb/N0 Vs SER for M-QAM modulation scheme


clear;clc;
% Input Fields
N=10000; %Number of input symbols
EbN0dB = -6:2:12; %Define EbN0dB range for simulation
M=16; %for 16-QAM modulation.
%
refArray =1/sqrt(10)*[-3-3j,-3-1j,-3+3j,-3+1j,-1-3j,-1-1j,-1+3j,-1+1j,3-3j,3-1j,3+3j,3+1j,1-3j,1-
1j,1+3j,1+1j];
symErrSimulated = zeros(1,length(EbN0dB));
k=log2(M);
EsN0dB = EbN0dB + 10*log10(k);
%---Generating a uniformly distributed random numbers in the set [0,1,2,..,M-1]
data=ceil(M.*rand(N,1))-1;
s=refArray(data+1); %16-QAM Constellation mapping with Gray coding
%--- Reference Constellation for demodulation and Error rate computation--
refI = real(refArray);
refQ = imag(refArray);
%---Place holder for Symbol Error values for each Es/N0 for particular M value--
index=1;
figure(1);
subplot(1,2,1);
plot(real(s),imag(s),'r*');
title('Constellation diagram for Transmitted Symbols');
xlabel('Inphase component');
ylabel('Quadrature component');
subplot(1,2,2);
for x=EsN0dB,
%
%Channel Noise for various Es/N0
%
%Adding noise with variance according to the required Es/N0
noiseVariance = 1/(10.^(x/10));%Standard deviation for AWGN Noise
noiseSigma = sqrt(noiseVariance/2);
%Creating a complex noise for adding with M-QAM modulated signal
%Noise is complex since M-QAM is in complex representation
noise = noiseSigma*(randn(size(s))+1i*randn(size(s)));
received = s + noise;
% I-Q Branching
r_i = real(received);
r_q = imag(received);
%---Decision Maker-Compute (r_i-s_i)^2+(r_q-s_q)^2 and choose the smallest
r_i_repmat = repmat(r_i,M,1);
r_q_repmat = repmat(r_q,M,1);
plot(r_i,r_q,'*');
title(['Constellation diagram for Received Symbols Eb/N0=' num2str(x-10*log10(k)) 'dB']);
xlabel('Inphase component');
ylabel('Quadrature component');
pause;
distance = zeros(M,N); %place holder for distance metric
minDistIndex=zeros(N,1);
for j=1:N
%---Distance computation - (r_i-s_i)^2+(r_q-s_q)^2 --------------
distance(:,j) = (r_i_repmat(:,j)-refI').^2+(r_q_repmat(:,j)-refQ').^2;
% -- capture the index in the array where the minimum distance occurs
[dummy,minDistIndex(j)]=min(distance(:,j));
end
y = minDistIndex - 1;%The index becomes the decoded symbol
% Symbol Error Rate Calculation
dataCap = y;
symErrSimulated(1,index) = sum(dataCap~=data)/N;
index=index+1;
end
%----- Compute Theoretical Symbol Error Rates ---------------------
%EsN0lin = 10.^(EsN0dB/10);
EbN0lin = 10.^(EbN0dB/10);
symErrTheory = 2*(1-1/sqrt(M))*erfc(sqrt(3/2*k*EbN0lin/(M-1)));
% Plotting commands
figure(2);
semilogy(EbN0dB,symErrTheory,'r-');hold on;
semilogy(EbN0dB,symErrSimulated,'b*');
legend('16QAM-Theory','16QAM-Sim');
xlabel('Eb/N0(dB)');
ylabel('Symbol Error Rate (Ps)');
grid on;
Output:-

You might also like