Even & Odd Parity Check, Generate & Add Parity Bit, in a Bin Sequence Using MATLAB

A parity bit is a bit added to the end of a sequence of bits (zeros or ones) that indicates whether the number of bits in the string with the value one is even or odd. Parity bits are used as the simplest form of error detecting code.

Assume, for example, that two devices are communicating with even parity, the transmitting device sends data, it counts the number of set bits in each group of bits. If the number of set bits is even, it sets the parity bit to 0; if the number of set bits is odd, it sets the parity bit to 1. In this way, every sequence have an even number of set bits. On the receiving side, the device checks to make sure that it has an even number of set bits. If it finds an odd number of set bits, the receiver knows there was an error during transmission.

NOTE:
The sender and receiver must both agree to use parity checking and to agree on whether parity is to be odd or even. If the two sides are not configured with the same parity sense, communication will be impossible.

MATLAB Code for Even Parity Check & Adding Parity Bit, in an User Dfined Bin Sequence

clc;
close all;
clear all;
x = input('Enter the bit sequence to test for Even parity: ');
t = 0;
for i = 1:length(x)
%can replace this 'for' loop just by t=sum(x)        
if(x(i))
        t = t + 1;
%increment by one if a bit is one
        end
end

if(mod(t,2)~=0) %check if not even then attach another '1' to make the parity even
   y = [x 1]; disp('Parity bit generated : 1');
else
%check if even then attach another '0' to let the parity be even
  y = [x 0]; disp('Parity bit generated : 0');
end
disp('Input bit sequence:');
disp(x);
%display the input bit sequence
disp('Bit sequence with parity (Even) bit : ');
disp(y);
%display the resultant bit sequence after parity bit addition


Sample input output for  Even Parity Check & Adding Parity Bit
 (1)
Enter the bit stream : [1 0 1 1 0 1 1]
Input bit stream :      1     0     1     1     0     1     1
Parity bit generated : 1
Transmitting message sequence including parity bit :     1     0     1     1     0     1     1     1

(2)
Enter the bit sequence to test for Even parity: [1 0 1 1 0 1 0]
Input bit sequence:     1     0     1     1     0     1     0
Parity bit generated : 0
Bit sequence with parity (Even) bit :     1     0     1     1     0     1     0     0


MATLAB Code for Odd Parity Check & Adding Parity Bit, in an User Dfined Bin Sequence

clc;
close all;
clear all;
x = input('Enter the bit sequence to test for Odd parity: ');
t = 0;
for i = 1:length(x)
%can replace this 'for' loop just by t=sum(x)        
if(x(i))
        t = t + 1;
%increment by one if a bit is one
        end
end

if(mod(t,2)~=1) %check if not odd then attach another '1' to make the parity odd
   y = [x 1]; disp('Parity bit generated : 1');
else %check if odd then attach another '0' to let the parity be odd
  y = [x 0]; disp('Parity bit generated : 0');
end
disp('Input bit sequence:');
disp(x);
%display the input bit sequence
disp('Bit sequence with parity (Odd) bit : ');
disp(y);
%display the resultant bit sequence after parity bit addition


Sample input output for  Odd Parity Check & Adding Parity Bit
 (1)
Enter the bit sequence to test for Odd parity: [1 0 1 1 0 1 0]
Input bit sequence:     1     0     1     1     0     1     0
Parity bit generated : 1
Bit sequence with parity (Odd) bit:      1     0     1     1     0     1     0     1
(2)
Enter the bit sequence to test for Odd parity: [1 0 1 1 0 1 1]
Input bit sequence:
     1     0     1     1     0     1     1
Parity bit generated : 0
Bit sequence with parity (Odd) bit:
     1     0     1     1     0     1     1     0



0 comments: