Final Dc Manual 20 39
Final Dc Manual 20 39
clc;
clear all;
close all;
% Generating a random binary bit stream
numBits = 256; % Number of bits
binaryData = randint(numBits,1);
Matlab Code
//crc candetect all single bit error, double bit, odd bits of error and burst error
#include <stdio.h>
#include<string.h>
#include<conio.h>
char t[28],cs[28],g[30];
int a,i,j,N;
void xor()
{
for(j=1; j<N; j++)
cs[j] = ((cs[j]==g[j])?'0':'1');
}
void crc()
{
for(i=0;i<N;i++) cs[i]=t[i];
do
{
if(cs[0]=='1')
xor();
for(j=0;j<N-1;j++)
cs[j]=cs[j+1];
GEC Raichur Dept. of ECE
Digital Communication Lab (BECL504) Page 15
cs[j]=t[i++];
}while(i<=a+N-1);
}
int main()
{
printf("\n message to be send is :");
scanf("%s",&t);
printf("\n ");
printf("\nGenerating polynomial : ");
scanf("%s",&g);
a=strlen(t); N=strlen(g);
for(i=a;i<(a+N-1);i++)
t[i]='0';
printf("\n after appendig zero's to message : %s",t);
printf("\n ");
crc();
printf("\n checksum is :%s",cs); for(i=a;i<(a+N-1);i++)
t[i]=cs[i-a];
printf("\n ");
printf("\nfinal codeword(message+checksum)to be transmitted is : %s",t); printf("\n ");
printf("\nEnter received message"); scanf("%s",&t);
crc();
printf("\n REMAINDER : %s",cs); for(i=0;(i<N-1)&&(cs[i]!='1');i++); if(i<N-1)
printf("\n\n error detected\n\n"); else
printf("\n\n No error detected\n\n"); getch();
return 0;
}
Result:
Cyclic redundancy check was simulated using Matlab.
Introduction:
Huffman coding is a source coding technique. Huffman coding is a lossless data
compression technique. Lossless data compression means original data can be perfectly
reconstructed from the compressed data with no loss of information. Huffman code is a prefix code,
meaning - no codeword appears at the beginning of another codeword. It is a variable length code,
meaning - source symbols are mapped into a variable number of bits. In Huffman coding, more
probable symbols are assigned fewer bits and less probable symbols are assigned more bits.
A source generates 4 symbols {𝐴, 𝐵, 𝐶, 𝐷} with probabilities {0.4, 0.35, 0.2, 0.05}. Construct a binary
code using Huffman coding.
𝐴 ∶ 0.4
𝐵 ∶ 0.35
𝐶 ∶ 0.2
𝐷 ∶ 0.05
Step 2 : Generate a binary tree from left to right taking the two least probable symbols and putting
them together to form another equivalent symbol having a probability that equals the sum of the
probability of two symbols.
Step 4 : Assign Bit 0 to the upper branch and Bit 1 to the lower branch of every stage of the tree.
Step 5 : Traverse the branches from right to left until you reach the first stage. Accumulate all the
bits on the way. You will get the code for each symbol.
0
A:0.4
0
B:0.35 1
1
0
C:0.2 0.6
1
1 0.25
D:0.05
𝐴∶0
𝐵 ∶ 10
𝐶 ∶ 110
𝐷 ∶ 111
Matlab code
X = input ("Enter the numbes of Symbol:");
disp ('the number of Symbols are N:’);
disp (N);
P=imput ‘'Enter the Probabilities = ‘);
disp (the ‘Probabilites are !’);
disp (P);
$= sort(P, ‘descend’);
disp (' the sorted Probabilities are: ");
disp (s):
[dict, avglen] = huffmandict (N, S);
disp (‘ the average length of the code is:’);
disp (arglen);
H=0
for i=1:X
H = H+(P(i) *log2(P1/(i)));
End
disp C'entropy is :');
disp (H);
disp ('bits/ msg');
E = (H/avglen) * 100; /efficiency |
disp (! Efficiency is :');
disp (E);
codewold = huffmanenco(N, dict);
disp ('The codewords are:");
disp (codewold);
decode= Huffmandeco(codewold, dict);
disp ('Decoded output is: ");
disp (decode);
Output
Enter the number of Symbols; 5
1 23 4 5
Enter the Probabilities = [0.1, 0.1.0.2, 0.2,0.4]
Probabilities= 0.1000, 0.1000, 0. 2000, 0.200,0.4000
"the solted probabilities are..
0.4000. 0.2000
0.2000 0.10000.1000
The average length of the code is
2.200
Entropy is 2.1219 bits/msg
Efficiency
is 96.4513
Introduction:
Hamming code is a type of error correcting code. It is a type of linear code meaning - modulo 2 sum of any two
codewords is also a codeword. It is a type of block code meaning - it takes fixed size data blocks and encodes them.
Hamming code can correct single-bit errors and detect the presence of two-bit errors in a data block. A (7,4) Hamming
code means - it encodes four data bits into seven bits by adding three parity bits.
Solution :
𝐷 = [1 0 1 1]
1 0 0 0 0 1 1
0 1 0 0 1 0 1
𝐶 = [1 0 1 1] [ ]
0 0 1 0 1 1 0
0 0 0 1 1 1 1
Received bits, 𝑟 = [1 0 1 1 0 1 0]
Step 1: Construct the parity check matrix (𝐻).
0 1 1 1 1 0 0
𝐻 = [𝑃𝑇 ]
𝐼 = [1 0 1 1 0 1 0]
1 1 0 1 0 0 1
Step 2 : Calculate syndrome (𝑆)
0 1 1
𝖥1 0 11
I1 1 0I
𝑆 = 𝑟𝐻𝑇 = [ I I
]
1 0 1 1 0 1 0 I1 1 1I
I1 0 0I
I0 1 0I
[0 0 1]
= [0 0 0]
If all bits of syndrome matrix are 0, that means the received bits represent a valid codeword. Then we take the first 4 bits
as data bits. Hence, the decoded data is [1 0 1 1]
Decoding assuming that the 3rd bit is received in error due to noise in the channel :
Received bits, 𝑟 = [1 0 0 1 0 1 0]
Step 1: Construct the parity check matrix.
0 1 1 1 1 0 0
𝐻 = [𝑃𝑇 ]
𝐼 = [1 0 1 1 0 1 0]
1 1 0 1 0 0 1
If all bits of syndrome matrix are NOT 0s that means some error has occurred.
The calculated syndrome is [1 1 0]. It appears in the 3rd row of 𝐻𝑇 matrix. This indicates to the receiver that the 3rd bit is
received wrongly.
Matlab Code
n = 7%# of codeword bits per block
k = 4%# of message bits per block
A = [ 1 1 1;1 1 0;1 0 1;0 1 1 ];%Parity submatrix-Need binary(decimal combination of 7,6,5,3)
G = [ eye(k) A ]%Generator matrix
H = [ A' eye(n-k) ]%Parity-check matrix
% ENCODER%
msg = [ 0 0 1 1 ] %Message block vector-change to any 4 bit sequence
code = mod(msg*G,2)%Encode message
% CHANNEL ERROR(add one error to code)%
%code(1)= ~code(1);
%code(2)= ~code(2);
%code(3)= ~code(3);
%code(4)= ~code(4);%Pick one,comment out others
code(5)= ~code(5);
%code(6)= ~code(6);
%code(7)= ~code(7);
recd = code %Received codeword with error
% DECODER%
syndrome = mod(recd * H',2)
%Find position of the error in codeword (index)
find = 0;
for ii = 1:n
if ~find
errvect = zeros(1,n);
errvect(ii) = 1;
search = mod(errvect * H',2);
if search == syndrome
find = 1;
index = ii;
end
end
end
disp(['Position of error in codeword=',num2str(index)]);
Output
haming simulation n=7 k=4
G=
1 0 0 0 1 1 1
0 1 0 0 1 1 0
0 0 1 0 1 0 1
H=
1 1 1 0 1 0 0
1 1 0 1 0 1 0
1 0 1 1 0 0 1
msg= 1 1 1 1
code 1 1 1 1 1 1 1
=
recd= 1 0 1 1 1 1 1
syndrome = 1 1 0
Position of error in codeword = 2
corrected code= 1 1 1 1 1 1 1
msg_decoded= 1 1 1 1
Result:
Hamming code was simulated using Matlab.
Matlab Code
//crc candetect all single bit error, double bit, odd bits of error and burst error
#include <stdio.h>
#include<string.h>
#include<conio.h>
char t[28],cs[28],g[30];
int a,i,j,N;
void xor()
{
for(j=1; j<N; j++)
cs[j] = ((cs[j]==g[j])?'0':'1');
}
void crc()
{
for(i=0;i<N;i++) cs[i]=t[i];
do
{
if(cs[0]=='1')
xor();
for(j=0;j<N-1;j++)
cs[j]=cs[j+1];
GEC Raichur Dept. of ECE
Digital Communication Lab (BECL504) Page 26
cs[j]=t[i++];
}while(i<=a+N-1);
}
int main()
{
printf("\n message to be send is :");
scanf("%s",&t);
printf("\n ");
printf("\nGenerating polynomial : ");
scanf("%s",&g);
a=strlen(t); N=strlen(g);
for(i=a;i<(a+N-1);i++)
t[i]='0';
printf("\n after appendig zero's to message : %s",t);
printf("\n ");
crc();
printf("\n checksum is :%s",cs); for(i=a;i<(a+N-1);i++)
t[i]=cs[i-a];
printf("\n ");
printf("\nfinal codeword(message+checksum)to be transmitted is : %s",t); printf("\n ");
printf("\nEnter received message"); scanf("%s",&t);
crc();
printf("\n REMAINDER : %s",cs); for(i=0;(i<N-1)&&(cs[i]!='1');i++); if(i<N-1)
printf("\n\n error detected\n\n"); else
printf("\n\n No error detected\n\n"); getch();
return 0;
}
Result:
Cyclic redundancy check was simulated using Matlab.
Introduction:
Convolutional code is a form of error-correcting code. Convolutional codes are particularly effective in
combating burst errors. Convolutional codes are characterized by their code rate. For ex. a code rate of ⅓ means
that for every data bit three code bits will be generated. Another parameter that characterizes convolutional code
is constraint length. A constraint length of 3 indicates that there are three stages in the shift register used to
encode the data. Longer constraint lengths generally provide better error-correcting capabilities but may require
more complex decoding. The most common method for decoding convolutional codes is the Viterbi algorithm.
This algorithm involves comparing the received sequence with all possible transmitted sequences and selecting
the one with the highest likelihood.
Convolutional codes differ from block codes in the following perspectives. Block codes segment a large
data sequence into smaller blocks and individually encode each block. In convolutional coding, an entire data
sequence is mapped into a single code sequence. Secondly, the present output of a convolutional encoder not
only depends on the present input, but also on the past inputs. Hence, a convolutional encoder is a system with
memory. Block coders do not have memory.
Example of Convolutional Code :
Construct the codeword for the data "11101" using rate ½ convolutional coder as follows.
V1
u FF1 FF2
V2
Fig. 1. Rate 1 convolutional encoder
2
- 0 0 0 0
1 0 0 1 1
1 1 0 1 0
1 1 1 0 1
0 1 1 1 0
1 0 1 0 0
0 1 0 0 1
0 0 1 1 1
Matlab Code
clc;
clear;
n=input('Enter the number of outputs:');
k=input('Enter the number of input bits given at a time:');
m=input('Enter the number of flip-flops:');
g=input('Enter all the impulse response in order:');
u=input("Enter the input sequence:");
s=length(u); %length of the input sequence
l=n*(s+m); %total length of the output sequence
for i=1:n
vn(i,:)=mod(conv(u,g(i,:)),2); %convolution of input and generator matrix
end
for i=1:n
for j=1:(l/n)
GEC Raichur Dept. of ECE
Digital Communication Lab (BECL504) Page 30
v(1,(n*(j-1))+i)=vn(i,j); %rearranging the encoded vector
end
end
disp("The convolution code without using built-in function is:");
disp(v); %displaying the encoded code
%Using built-in function
cg=str2num(dec2base(bin2dec(int2str(g)),8)); %converting the impluse response to
octal values
t=poly2trellis((m+1),cg');
x=[u,zeros(1,((l/n)-s))];
code=convenc(x,t); %built-in function for convolutional codes
disp('The convolution code using built-in function is:');
disp(code);
rec=input("Enter the recieved code:- ");
decoded=vitdec(rec,t,s,'trunc','hard'); %built-in function for decoding
decoded=decoded(1:s);
disp('The decoded code is:');
disp(decoded);
Output
u=
1 0 1 1 1
1 1 1 0 1 0 1 0 0 1 0 1 1 1 0 0 0 1 0
Columns 20 through 21
1 1