Line codes_explanation
Line codes_explanation
Initialization
clc
clear all
close all
• disp('Generated Random data is as follows'): Displays a message indicating the random data
that is about to be shown.
• randi([0,1],1,8): Generates a random row vector of length 8, with values randomly chosen
between 0 and 1.
• disp(data): Displays the generated random data vector.
nbits = 10;
bit_duration=ones(1,nbits); % This decides the duration of each bit
• nbits = 10: Defines the number of bits for the encoding process.
• bit_duration = ones(1, nbits): Creates a vector of length nbits where each element is 1. This
represents a bit duration of 1 for each bit.
4. Check for Even/Odd nbits and Adjust Bit Duration for RZ Encoding
if (mod(nbits,2) == 0)
bit_duration_rz = [ones(1,nbits/2), zeros(1,nbits/2)]; % This decides the duration of each bit
else
bit_duration_rz = [ones(1, floor(nbits/2)), zeros(1, ceil(nbits/2))];
end
• This checks whether nbits is even or odd using mod (nbits, 2).
o If nbits is even, it creates a vector of 1s for half of the duration, followed by 0s for the
other half.
o If nbits is odd, it splits the duration into approximately equal parts (with the floor and
ceiling of half the bits).
data_mach = (data*2)-1;
bit_duration_mach = (bit_duration_rz*2)-1;
• data_mach = (data*2)-1: Converts the data vector (which contains 0s and 1s) into -1 and 1 by
multiplying by 2 and subtracting 1. This transformation is required for Manchester encoding.
• bit_duration_mach = (bit_duration_rz*2)-1: Similarly transforms the bit duration vector
bit_duration_rz to -1 and 1 for use with Manchester encoding.
Unipolar NRZ:
• kron(data, bit_duration): The Kronecker product is used to repeat each bit in data for the
length of bit_duration. In this case, data is a sequence of 1s and 0s, and bit_duration
determines how long each bit lasts. uni_nrz holds the resulting sequence.
Polar NRZ:
data_p = data*2-1; % first convert the data into 1's and -1's
pol_nrz = kron(data_p, bit_duration);
• data_p = data*2 - 1: Converts the data from 0s and 1s to -1 and 1 for polar encoding.
• pol_nrz = kron(data_p, bit_duration): Performs the Kronecker product to create the polar
NRZ encoding.
Unipolar RZ:
• kron(data, bit_duration_rz): Creates the unipolar RZ encoding by repeating the data using
bit_duration_rz.
Polar RZ:
data_p = data*2-1; % first convert the data into 1's and -1's
pol_rz = kron(data_p, bit_duration_rz);
• This creates the polar RZ encoding by converting the data to -1 and 1 and then performing the
Kronecker product with bit_duration_rz.
• The Kronecker product is a mathematical operation on two matrices (or vectors),
resulting in a larger matrix (or vector). It’s denoted by the symbol ⊗ and is used to
"expand" or "stretch" the dimensions of data in a particular way.
• In the context of the MATLAB code, the Kronecker product is used to repeat each bit
of the data sequence for a certain duration.
•
subplot(5,1,1) % to display both unipolar and polar once can use subplot
plot(uni_nrz)
axis([0,length(uni_nrz),min(uni_nrz)-1,max(uni_nrz)+1])
title('Unipolar NRZ')
• subplot(5,1,1): This sets the layout of the plot to display 5 subplots (rows x columns) and
selects the first position.
• plot(uni_nrz): Plots the unipolar NRZ line code.
• axis(...): Adjusts the x and y-axis for better visibility.
• title('Unipolar NRZ'): Sets the title for this subplot.
subplot(5,1,2)
plot(pol_nrz)
axis([0,length(pol_nrz),min(pol_nrz)-1,max(pol_nrz)+1])
title('Polar NRZ')
• Similarly, this creates the second subplot for the polar NRZ encoding.
subplot(5,1,3)
plot(uni_rz)
title('Unipolar RZ')
axis([0,length(uni_rz),min(uni_rz)-1,max(uni_rz)+1])
subplot(5,1,4)
plot(pol_rz)
title('Polar RZ')
axis([0,length(pol_rz),min(pol_rz)-1,max(pol_rz)+1])
subplot(5,1,5)
plot(mach)
axis([0,length(mach),min(mach)-1,max(mach)+1])
title('Manchester coding')