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:

PWM (Pulse Width Modulation) Using MATLAB

Pulse Width Modulation (PWM) is one method of reducing the perceived luminance in displays, which it achieves by cycling the backlight on and off very rapidly. This generally means that at 100% brightness a constant voltage is applied to the backlight and it is continuously lit. As you lower the brightness control the perceived luminance for the user reduces due to a number of possible controlling factors:

PWM (Pulse Width Modulation) Using MATLAB
How the LED LUMA depends on the pulse fed into it, (coursey: tft central)
In short PWM simply does modulation of the duty cycle in accordance with the amplitude of message signal & a comparator reference sawtooth wave. In addition to the Display use, it find use in communication systems, voltage regulators such as Switched Mode Power Supplies (SMPS) to control the power delivered to the load, LED circuits (particularly of fading Effect), Motor Speed Control etc.

PWM (Pulse Width Modulation) Using MATLAB
PWM generation process (Courtesy: Wikipedia)
 MATLAB Code for PWM (Pulse Width Modulation):


fs=input('Comparator Sawtooth frequency:');
fm=input('Message frequency(Assuming it to be a sine wave):');
a=input('Enter Amplitude of Message:');

t=0:0.0001:1; %sampling rate of 10kHz

stooth=1.01*a.*sawtooth(2*pi*fs*t);
%generating a sawtooth wave
%to make the two non zero lobes of pwm not to overlap the amplitude of
%sawtooth wave must be atleast more than a bit to the message amplitude

subplot(3,1,1);
plot(t,stooth);
% plotting the sawtooth wave
title('Comparator Wave');

msg=a.*sin(2*pi*fm*t);
%generating message wave

subplot(3,1,2);
plot(t,msg);
%plotting the sine message wave
title('Message Signal');


for i=1:length(stooth)
if (msg(i)>=stooth(i))
    pwm(i)=1;
%is message signal amplitude at i th sample is greater than
    %sawtooth wave amplitude at i th sample
else
    pwm(i)=0;
end
end

subplot(3,1,3);
plot(t,pwm,'r');
title('PWM');
axis([0 1 0 1.1]);
%to keep the pwm visible during plotting.

Sample Input:

Comparator Sawtooth frequency:10
Message frequency(Assuming it to be a sine wave):1
Enter Amplitude of Message:5

Results of MATLAB code for PWM (Pulse Width Modulation):
PWM (Pulse Width Modulation) Using MATLAB
PWM (Pulse Width Modulation) Using MATLAB


0 comments: