0% found this document useful (0 votes)
31 views

cs-2 Lab Codes

This document contains code for implementing convolutional encoding. It takes user input for the number of generator sequences, length of each sequence, values of the generator matrix, and message bits. It initializes various arrays to store the generator matrix, state matrix, message bits, and output bits. It then performs convolutional encoding by multiplying the message bits with the generator matrix and storing the results in the output bit array.

Uploaded by

Bharath
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

cs-2 Lab Codes

This document contains code for implementing convolutional encoding. It takes user input for the number of generator sequences, length of each sequence, values of the generator matrix, and message bits. It initializes various arrays to store the generator matrix, state matrix, message bits, and output bits. It then performs convolutional encoding by multiplying the message bits with the generator matrix and storing the results in the output bit array.

Uploaded by

Bharath
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

shannon

clc;
clear all;
close all;
N=input('enter no of symbols');
P1=input('enter probabilities of each symbols');
P=sort(P1,'descend');
su1=sum(P);
aq=num2str(su1);
su2=str2double(aq);
if(su2~=1
h=msgbox('sum of probability should be one','Error','error');
return;
else
disp('total probability sum is one');
end
[r1, c1]=size(P);
%pause(5);
if(N~=c1)
h=msgbox('probability dimension is not matching with given no of
symbols','Error','error');
return;
end
m=zeros(1,N);
m1=zeros(1,N);
H=0;
result1=zeros(1,N);
f=zeros(1,N);
g=zeros(1,N);
k=zeros(1,N);
N_bar=0;
for i=1:N
H=H+P(i)*log2(1/(P(i)));

m(i)=1+log2(1/P(i));
m1(i)=log2(1/P(i));
temp=m1(i)-fix(m1(i));
if(temp==0)
m(i)=m1(i);
end
r=fix(m(i));
N_bar=N_bar+r*P(i);
if(i==1)
f(i)=0;
else
f(i)=P(i-1)+f(i-1);
end
for j=1:r
if(j==1)
g(j)=f(i)*2;
else
g(j)=g(j-1)*2;
end
k(j)=fix(g(j));
if(g(j)>=1)
g(j)=g(j)-1;
end
if(j==1)
result1=sprintf('%0*d',1,(k(j)));

else
if(k(j)==0)
result2= sprintf('%0*d',1,(k(j)));
result1=strcat(result1,result2);
else
result1=strcat(result1,num2str(k(j)));
end
end

end
result{i}=result1; % matrix elements assigned with strings

end
eff=H/N_bar;
disp('entropy');H
disp('averge no of bits per symbol');N_bar
disp('efficiency');eff
disp('binary of symols');result
disp('code for symbol with probabilities');P
pause(.5)
disp('are');result
disp('respectively')
shannon fano

clc;
clear all;
close all;
disp('Enter the probabilities:'); %ss=[0.25 0.125 0.5 0.125];
%ss=[0.25 0.125 0.0625 0.0625 0.0625 0.25 0.0625 0.125];
ss=[0.4 0.2 0.12 0.08 0.08 0.08 0.04] %ss=[0.4 0.3 0.2 0.1]
%ss=[0.45 0.15 0.1 0.1 0.08 0.08 0.04]
%ss=[0.2 0.15 0.03 0.05 0.45 0.08 0.04]
%outputs = string of codewords,average codeword length
ss=ss./sum(ss); %if occurrences are inputted, probabilities are gained
ss=sort(ss,'descend'); %the probabilities are sorted in descending order
%siling=ceil(log2(1/ss(1))); %initial length is computed
siling=log2(1/ss(1)); %initial length is computed
siling=round(siling,1,'significant');
sf=0;
fano=0;
%initializations for Pk
n=1;Hx=0; %initializations for entropy H(X)
for i=1:length(ss)
Hx=Hx+ ss(i)*log2(1/ss(i)); %solving for entropy
end
for k=1:length(ss)
info(k)=-(log2(ss(k))); %Information
end

for j=1:length(ss)-1
fano=fano+ss(j);
sf=[sf 0]+[zeros(1,j) fano]; %solving for Information for every codeword
siling=[siling 0]+[zeros(1,j) ceil(log2(1/ss(j+1)))];
%solving for length every codeword
end
for r=1:length(sf)
esf=sf(r);
for p=1:siling(r)
esf=mod(esf,1)*2;
h(p)=esf-mod(esf,1); %converting Pk into a binary number
end
hh(r)=h(1)*10^(siling(r)-1); %initializtion for making the binary a whole number
for t=2:siling(r)
hh(r)=hh(r)+h(t)*10^(siling(r)-t);
%making the binary a whole number
end %e.g. 0.1101 ==> 1101
end
c={'0','1'};
disp('Codeword');
for i=1:length(hh)
u=1; rting the codes into a string
for t=siling(i):-1:1
f=floor(hh(i)/10^(t-1)); %1001 ==>1 (getting the first highest unit of a number)
hh(i)=mod(hh(i),10^(t-1));
%1001 ==>001(eliminating the first highest unit of a number)

if f==1
if u==1
d=c{2};
%conversion part (num(1001) to str(1001))
else
d=[d c{2}];
end
else
if u==1
d=c{1};
else
d=[d c{1}];
end
end
codex{i,:}={d};
u=u+1;
end
disp([d])
end

tao=siling(1)*ss(1); %initialization for codeword length


for u=1:length(ss)-1 %computing for codeword length
tao=tao+siling(u+1)*ss(u+1);

end
T=tao/n; %computing for average codeword length
B=[flipud(rot90(ss)),flipud(rot90(siling)),flipud(rot90(info))];
disp(['Probability',' Length',' Information'])
disp(B)
disp(['Entropy H(X) = ',num2str(Hx),'bits/symbol'])
disp(['Average length,L = ',num2str(T),'bits/symbol'])
eff=((Hx/T)*100); %Coding efficiency
disp(['Efficiency=',num2str(eff),'%'])
redu=100-eff; %Redundancy
disp(['Redundancy=',num2str(redu),'%'])
huffman

clc;
clear all;
s=input('Enter symbols- ') %format ['a','b','c','d','e','f'];
p=input('Enter value of probabilty- ') %format
[0.22,0.20,0.18,0.15,0.13,0.12];
if length(s)~=length(p)
error('Wrong entry.. enter again- ')
end
i=1;
for m=1:length(p)
for n=1:length(p)
if(p(m)>p(n))
a=p(n); a1=s(n);
p(n)=p(m);s(n)=s(m);
p(m)=a; s(m)=a1;
end
end
end
display(p) %arranged prob. in descending order.
tempfinal=[0];
sumarray=[];
w=length(p);
lengthp=[w];
b(i,:)=p;
while(length(p)>2)
tempsum=p(length(p))+p(length(p)-1);
sumarray=[sumarray,tempsum];
p=[p(1:length(p)-2),tempsum];
p=sort(p,'descend');
i=i+1;
b(i,:)=[p,zeros(1,w-length(p))];
w1=0;
lengthp=[lengthp,length(p)];

for temp=1:length(p)
if p(temp)==tempsum;
w1=temp;
end
end
tempfinal=[w1,tempfinal]; % Find the place where tempsum has been inserted
display(p);
end
sizeb(1:2)=size(b);
tempdisplay=0;
temp2=[];

for i= 1:sizeb(2)
temp2=[temp2,b(1,i)];
end
sumarray=[0,sumarray];
var=[];
e=1;
for ifinal= 1:sizeb(2)
code=[s(ifinal),' ']
for j=1:sizeb(1)
tempdisplay=0;

for i1=1:sizeb(2)
if( b(j,i1)==temp2(e))
tempdisplay=b(j,i1);
end
if(tempdisplay==0 & b(j,i1)==sumarray(j))
tempdisplay=b(j,i1);
end
end
var=[var,tempdisplay];
if tempdisplay==b(j,lengthp(j)) %assign 0 & 1
code=[code,'1'];
elseif tempdisplay==b(j,lengthp(j)-1)
code=[code,'0'];
else
code=[code,''];
end
temp2(e)=tempdisplay;
end
display(code) %display final codeword
e=e+1;
end
lbc

#include<stdio.h>
#include<conio.h>
void main() {
inti,j,n,k,M[10],C[10],G[10][10],par[10][10];
clrscr();
printf(" For A Generator Matrix Enter :\n");
printf("1. The No Of Message Bits(n)\t: ");
scanf("%d",&n);
printf("2. The No Of Parity Bits(k)\t: ");
scanf("%d",&k);
printf("\nEnter The Elements Of Generator Matrix:\n");
for(i=0;i<k;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
printf("\nEnter The Message Bits\n");
for(i=0;i<k;i++)
scanf("%d",&M[i]);
printf("\t\t\tResult\n");
printf("____________________________________________________");
printf("\n\tThe Generator Matrix Is :\n\t");
for(i=0;i<k;i++) {
for(j=0;j<n;j++)
printf("%d\t",G[i][j]);
printf("\n\t");
}
printf("\n\tThe Parity Matrix Is :\n\t");
for(i=0;i<k;i++) {
for(j=0;j<n-k;j++) {
par[i][j]=G[i][j+k];
printf("%d\t",par[i][j]); }
printf("\n\t");
}
printf("\n\tTheMessege Bits :\n\t");
for(i=0;i<k;i++)
printf("%d ",M[i]);
printf("\n\n\tThe Parity Bits :");
for(i=0;i<n-k;i++)
{
C[i] = 0;

for(j=0;j<k;j++)

C[i]=(C[i] + M[j] * par[j][i])%2;


printf("\n\tC%d = %d",i+1,C[i]);
}
printf("\n\n\tCode Word For Given Message Bit:\n\t");
for(i=0;i<k;i++)
printf("%d ",M[i]);
for(i=0;i<n-k;i++)
printf("%d ",C[i]);
printf("\n_____________________________________");

getch(); }
crc
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main() {
int i,j,keylen,msglen;
char input[100], key[30],temp[30],quot[100],rem[30],key1[30];
clrscr();
printf("Enter Data: ");
gets(input);
printf("Enter Key: ");
gets(key);
keylen=strlen(key);
msglen=strlen(input);
strcpy(key1,key);
for (i=0;i<keylen-1;i++) {
input[msglen+i]='0';
}
for (i=0;i<keylen;i++)
temp[i]=input[i];
for (i=0;i<msglen;i++) {
quot[i]=temp[0];
if(quot[i]=='0')
for (j=0;j<keylen;j++)
key[j]='0'; else
for (j=0;j<keylen;j++)
key[j]=key1[j];
for (j=keylen-1;j>0;j--) {
if(temp[j]==key[j])
rem[j-1]='0'; else
rem[j-1]='1';
}
rem[keylen-1]=input[i+keylen];
strcpy(temp,rem);
}
strcpy(rem,temp);
printf("\nQuotient is ");
for (i=0;i<msglen;i++)
printf("%c",quot[i]);
printf("\nRemainder is ");
for (i=0;i<keylen-1;i++)
printf("%c",rem[i]);
printf("\nFinal data is: ");
for (i=0;i<msglen;i++)
printf("%c",input[i]);
for (i=0;i<keylen-1;i++)
printf("%c",rem[i]);
getch();
}
scc

#include< stdio.h>
#include< conio.h>
void main()
{
int i,j,k,n,gb,mb,g[10][10],x[10][10],c[10],m[10],t[10];
clrscr();
for(i=0;i< 10;i++)
{ for(j=0;j< 10;j++)
g[i][j]=0;x[i][j]=0;c[i]=0;m[i]=0;t[i]=0;
}
printf("\t\t\t¦ Convolutional Code ¦");
printf("\n\nEnter The No. Of Generator Sequences\t: ");
scanf("%d",&n);
printf("Enter The No. Of Bits In Each Generator Sequence\t: ");
scanf("%d",&gb);
for(i=0;i< n;i++)
{
printf("\n\nEnter The Values Of G%d\t:\n",i);
for(j=0;j< gb;j++)
scanf("%dt",&g[i][j]);
}
printf("\nEnter The No. Of Message Bits\t: ");
scanf("%d",&mb);
printf("Enter The Message Bits\t:\n");
for(i=0;i< mb;i++)
scanf("%d",&c[i]);
for(i=0;i< mb;i++)
{
for(j=0;j< gb;j++)
{
t[j]=m[j];
if(j==0)
m[j]=c[i];
else
m[j]=t[j-1];
}
for(k=0;k< gb;k++)
{
for(j=0;j< gb;j++)
{ if(g[k][j]==1)
x[k][i]=x[k][i]^m[j]; } } }
printf("\n\n¦ The Code Vectors Are: ¦\n");
for(i=0;i< mb;i++)
{ printf("¦ \n¦ ");
for(j=0;j< gb;j++)
printf("%d\t",x[j][i]); }
getch(); }

You might also like