1.
Initialization
clc
clear all
close all
• clc: Clears the command window, removing any previous output.
• clear all: Clears all variables from the workspace.
• close all: Closes all figure windows.
2. Generate Random Data
disp('Generated Random data is as follows')
data = randi([0,1], 1, 8); % This will generate a random data of 1's and 0's of length 8.
disp(data);
• disp('Generated Random data is as follows'): Displays a message indicating the generated
random data.
• data = randi([0,1], 1, 8): Generates a random binary vector of length 8, containing only 0s and
1s.
• disp(data): Displays the randomly generated binary vector.
3. Set Bit Duration and Noise Parameters
nbits = 10; % decides the length of each bit
bit_duration = ones(1, nbits); % This decides the duration of each bit
• nbits = 10: Sets the number of time units per bit.
• bit_duration = ones(1, nbits): Creates a vector of ones representing the duration of each bit.
Here, each bit will have a length of 10.
noise = input('Do you want to add noise of normal distributed press y: ', 's');
sigma = 0.1;
• noise = input('Do you want to add noise of normal distributed press y: ', 's'): Prompts the user
to decide if they want to add noise to the signal. If the user enters 'y', noise will be added.
• sigma = 0.1: Sets the standard deviation (σ) for the normal distribution used to generate the
noise.
4. Generate NRZ Line Codes
Unipolar NRZ
uni_nrz = kron(data, bit_duration); % performs Kronecker product and stores in uni_nrz
• kron(data, bit_duration): The Kronecker product is used to repeat each bit in the data vector
according to the bit_duration vector. For example, if data = [1 0 1] and bit_duration = [1 1 1],
the resulting uni_nrz would repeat each bit for the specified duration.
Adding Noise to Unipolar NRZ
if noise == 'y'
uni_nrz = uni_nrz + (sigma .* randn(1, length(uni_nrz)));
end
• if noise == 'y': Checks if the user chose to add noise.
• randn(1, length(uni_nrz)): Generates random noise with a normal distribution, with the same
length as uni_nrz.
• sigma .* randn(...): Scales the generated noise by the standard deviation sigma.
• uni_nrz + noise: Adds the noise to the uni_nrz signal.
Matched Filter Output for Unipolar NRZ
mf_uni_out = conv(fliplr(bit_duration), uni_nrz);
• fliplr(bit_duration): Flips the bit_duration vector left-to-right. This is done to create a matched
filter impulse response.
• conv(...): Performs a convolution between the flipped bit_duration and the uni_nrz signal. The
convolution simulates the response of a matched filter, which is used to extract the data from
the noisy signal.
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 binary data (0 and 1) into polar values (-1 and 1).
• kron(data_p, bit_duration): Similar to unipolar NRZ, the Kronecker product is used to repeat
each bit in the data_p vector according to bit_duration.
Adding Noise to Polar NRZ
if noise == 'y'
pol_nrz = pol_nrz + (sigma .* randn(1, length(pol_nrz)));
end
• Similar to the unipolar NRZ section, this adds noise to the pol_nrz signal if the user chose to
add noise.
Matched Filter Output for Polar NRZ
mf_pol_out = conv(fliplr(bit_duration), pol_nrz);
• Similar to the unipolar NRZ section, this performs a convolution of the flipped bit_duration
with the noisy pol_nrz signal to simulate the matched filter output for the polar NRZ
encoding.
5. Plot the Results
subplot(4, 1, 1) % to display unipolar data
plot(uni_nrz)
title('Unipolar NRZ')
• subplot(4, 1, 1): Divides the figure into a 4-row, 1-column grid of subplots and selects the
first subplot.
• plot(uni_nrz): Plots the unipolar NRZ signal.
• title('Unipolar NRZ'): Adds a title to the subplot.
subplot(4, 1, 2)
plot(mf_uni_out)
title('Matched Filter Output for Unipolar NRZ')
• Plots the matched filter output for the unipolar NRZ signal in the second subplot.
subplot(4, 1, 3) % to display polar data
plot(pol_nrz)
title('Polar NRZ')
• Plots the polar NRZ signal in the third subplot.
subplot(4, 1, 4)
plot(mf_pol_out)
title('Matched Filter Output for Polar NRZ')
• Plots the matched filter output for the polar NRZ signal in the fourth subplot.
Summary
This code performs the following steps:
1. Generates Random Data: Creates an 8-bit random sequence of 1s and 0s.
2. Sets Bit Duration: Each bit is associated with a duration of nbits.
3. Adds Noise: The user is prompted to decide whether to add normally distributed noise to the
signals.
4. Generates NRZ Line Codes:
o Unipolar NRZ: Repeats each bit for the specified bit duration.
o Polar NRZ: Converts 0s to -1s and 1s to 1s, then repeats each bit for the specified
duration.
5. Adds Noise: If noise is chosen, Gaussian noise is added to both the unipolar and polar NRZ
signals.
6. Matched Filter: Convolves the noisy signals with a matched filter to simulate the effect of a
matched filter on the noisy signals.
7. Plots: Displays the original signals and their matched filter outputs in subplots.