Computer Networking Programs With Graph

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Gaussian distribution Program:

clc;
clear all;
close all;
% Taking parameters of Gaussian Distribution
m= input('enter the mean value:');
sd= input('enter the standard deviation value:');
%plotting pdf of gaussian distribution
x=linspace(m-4*sd,m+4*sd,1000);
e=exp(1);
f=(1/(sd*sqrt(2*pi)))*e.^((-1/2)*((x-m)/sd).^2);
subplot(2,1,1)
plot(x,f)
%pkg load statistics; %loading statistics package
%Generating vector of random numbers of gaussian distribution
Y=random('Normal', m,sd,100,1);
% displaying first 5 numbers in random vector
disp('displaying first 5 numbers in random vector')
disp(Y(1:5));
%Tracing random numbers Y by fitdist function
% fitdist function fit the distribution for given random numbers
% fitdist function generate probability distribution object
pd1=fitdist(Y,'Normal');
disp('Printing mean and Standard deviation after distribution fitting');
disp(pd1)
%Tracing random numbers Y by histfit function
% histfit function plot the histogram and pdf function for given random numbers
subplot(2,1,2)
histfit(Y,5,'Normal'); % 5 is no.of histogram bins
Waveforms:

Exponential Distribution Program:


clc;
clear all;
close all;
% Taking parameters of Exponential Distribution
m= input('enter the mean value:');
%plotting pdf of Exponential distribution
x=linspace(0,5*m,1000);
e=exp(1);
f=(1/m)*e.^(-x/m);
subplot(2,1,1)
plot(x,f)
%pkg load statistics; %loading statistics package
%Generating vector of random numbers of Exponential distribution
Y=random('Exponential',m,100,1);
% displaying first 5 numbers in random vector
disp('displaying first 5 numbers in random vector')
disp(Y(1:5));
%Tracing random numbers Y by fitdist function
% fitdist function fit the distribution for given random numbers
% fitdist function generate probability distribution object
pd1=fitdist(Y,'Exponential');
disp('Printing mean after distribution fitting');
disp(pd1)
%Tracing random numbers Y by histfit function
% histfit function plot the histogram and pdf function for given random numbers
subplot(2,1,2)
histfit(Y,5,'Exponential'); % 5 is no.of histogram bins

Waveforms:
Program for Maximal PN sequence generation:
clc;
close all;
clear all;
x=[ones(1,8), zeros(1,8)]; % initial state of LFSR
nb=16; %no.of bits in LFSR
dcn=zeros(2*(2^nb-1),1); %creating arrays with zeros
%Following loop simulates LFSR for each clock cycle
for i=1: 2*(2^nb-1)
dcn(i)=binvec2dec(x);
y1=xor(x(16),x(14));
y2=xor(y1,x(13));
y3=xor(y2,x(11));
x=[y3,x(1:15)];
end
%ploting PN sequence
stem(dcn);
%finding Time period of PN sequence
v1=dcn(1);
p1=find(dcn==v1); %finding repetitions of first number
np=p1(2)-p1(1); % finding time period
fprintf('\n %d time period of PN sequence is ',np)
if (np==(2^nb-1))
fprintf('\n generated pn sequence is Maximal PN sequence');
else
fprintf(' \n generated pn sequence is non-Maximal PN sequence');
end
% Testing randomness of PN sequence using runstest function
%pkg load statistics % only for octave software
h=runstest(dcn); %h=0 indicates randomness
h
%ploting first 50 samples PN sequence
stem(dcn(1:50));

Waveform

Program for non Maximal PN sequence generation:


clc;
close all;
clear all;
x=[ones(1,8), zeros(1,8)]; % initial state of LFSR
nb=16; %no.of bits in LFSR
dcn=zeros(2*(2^nb-1),1); %creating arrays with zeros
%Following loop simulates LFSR for each clock cycle
for i=1: 2*(2^nb-1)
dcn(i)=binvec2dec(x);
y1=xor(x(16),x(14));
y2=xor(y1,x(13));
y3=xor(y2,x(15));
x=[y3,x(1:15)];
end
%ploting PN sequence
stem(dcn);
%finding Time period of PN sequence
v1=dcn(1);
p1=find(dcn==v1); %finding repetitions of first number
np=p1(2)-p1(1); % finding time period
fprintf('\n %d time period of PN sequence is ',np)
if (np==(2^nb-1))
fprintf('\n generated pn sequence is Maximal PN sequence');
else
fprintf(' \n generated pn sequence is non-Maximal PN sequence');
end
% Testing randomness of PN sequence using runstest function
%pkg load statistics % only for octave software
h=runstest(dcn); %h=0 indicates randomness
h
%ploting first 50 samples PN sequence
stem(dcn(1:50));

Waveform:
Program for encryption and decryption:
clc;
close all;
clear all;
p_t=input('Enter plain text message:'); % enter test in lower case letters
str_num=zeros(length(p_t),1); % creating array of zeros
for i=1:length(p_t)
str_num(i)= p_t(i)+ 0; % converting character into ASCII value
end
str_num=str_num-97; %97 is ASCII value of ‘a’
%ENCRYPTION METHOD
key=input('enter the key:'); %enter the number
for i=1:length(str_num)
str_num(i)=mod(str_num(i)+key,26);
end
str_num=str_num+65; % 65 is ASCII value of ‘A’
ch_text=char(str_num);
fprintf('\n cipher text after encryption is %s',ch_text);
%DECRYPTION METHOD
ch_t=ch_text;
str_num=zeros(length(ch_t),1);
for i=1:length(ch_t)
str_num(i)= ch_t(i)+ 0; % converting character into ASCII value
end
str_num=str_num-65; % 65 is ASCII value of ‘A’
for i=1:length(str_num)
str_num(i)=mod(str_num(i)-key,26);
end
str_num=str_num+97; %97 is ASCII value of ‘a’
p_text=char(str_num);
fprintf('\n plain text after decryption is %s',p_text);
Hamming code program:
% Error detection and Correction by Hamming Code
clear all; close all;clc;
%parity check matrix
H=[1 0 1 0 1 0 1;0 1 1 0 0 1 1;0 0 0 1 1 1 1];
%Generator matrix
G=[1 1 0 1;1 0 1 1;1 0 0 0;0 1 1 1;0 1 0 0;0 0 1 0;0 0 0 1];
%4 bit message signal
msg=[1 0 0 1]';
%encoding message
Hcode=mod(G*msg,2);
H_tx=Hcode;% Transmission
% Corruption during transmission
H_tx(3)=xor(H_tx(3),1); % corruption of 3rd bit
%At reciever side
Hrc=H_tx;
disp(' recieved code is ')
Hrc
%error detecting by parity check matrix
zsy=mod(H*Hrc,2) %syndrome vector
% detecting position of error
err_position=0;
for i =1:7
if sum(H(:,i)==zsy)==3
err_position=i;
end
end
if (err_position==0)
disp('No error');
else
disp('error postion is ');
disp(err_position)
%correction of error bit
Hrc(err_position)=xor(Hrc(err_position),1);
disp (' hamming code vector after correction is ');
disp(Hrc)
end
%encoding of message signal
R=[0 0 0 0;0 0 0 0;1 0 0 0;0 0 0 0;0 1 0 0;0 0 1 0;0 0 0 1]';
msg_Rx=R*Hrc; %encoded message signal at Reciever side
disp('message after decoding')
disp(msg_Rx)

Shortest Path Program:


% Routing Algorithm
clc;
clear all;
close all;
S_Nodes=[1,1,1,2,2,2,3,3,3,4,4,4];
D_Nodes=[2,3,4,1,3,4,1,2,4,1,2,3];
W=[5,6,7,5,2,10,6,7,12,14,8,10];
N=sparse(S_Nodes,D_Nodes,W);
view(biograph(N,[],'showWeights','on'));
X=graphallshortestpaths(N);
source=input('enter source node');
destin=input('enter destination node');
disp('shortest path is ');
S_Path=X(source,destin)
Waveform:

You might also like