0% found this document useful (0 votes)
23 views6 pages

Digital Communication - 06082025.EXPERIMENT - 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

Digital Communication - 06082025.EXPERIMENT - 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

EXPERIMENT - 2

DATE: 06-08-2025​ ​ ​ ​ ​ ​

AIM:
Write a python code to get one binary stream as input and generate Waveforms of
Unipolar NRZ, Unipolar RZ, Polar NRZ, Polar RZ, Alternate Mark Inversion and
Manchester line codes. Generate the spectrum in frequency domain for each of the above
codes as well.

SOFTWARE USED:
Google Colab

THEORY:
In digital communication systems, binary data must be represented in the form of electrical
waveforms for transmission over a medium. This process is achieved using line coding
techniques, which convert a binary sequence into a suitable format of voltage pulses. Different
line codes have unique characteristics in terms of bandwidth requirement, synchronization
capability, error detection, and DC content.

●​ Unipolar NRZ represents a logic ‘1’ by a constant positive voltage and logic ‘0’ by zero
voltage throughout the bit duration.​

●​ Unipolar RZ uses a positive voltage for ‘1’ only in the first half of the bit interval and
returns to zero in the second half, ensuring a mid-bit transition.​

●​ Polar NRZ assigns positive voltage to ‘1’ and negative voltage to ‘0’ for the entire bit
duration.​

●​ Polar RZ uses positive or negative voltage for the first half of the bit duration depending
on the bit value, and returns to zero for the second half.​

●​ Alternate Mark Inversion (AMI) represents binary ‘1’ by alternating between positive
and negative voltages, while ‘0’ is represented by zero. This reduces DC content.​

●​ Manchester coding combines data and clock by representing ‘1’ as a high-to-low


transition and ‘0’ as a low-to-high transition in the middle of the bit interval.​
By generating both time-domain waveforms and their corresponding frequency spectra, we
can analyze the performance of each line coding scheme. This experiment helps in understanding
trade-offs between bandwidth efficiency, synchronization, and error performance, which are
crucial for designing practical digital communication systems.

CODE:

import numpy as np
import matplotlib.pyplot as plt

# Function to plot frequency spectrum


def plot_spectrum(signal, samples, title):
freq = np.fft.fftfreq(len(signal), d=1/samples)
spectrum = np.fft.fft(signal)
plt.plot(freq, np.abs(spectrum))
plt.title(f"Frequency Spectrum - {title}")
plt.xlabel("Frequency (Hz)")
plt.ylabel("Magnitude")
plt.grid(True)

# Input binary stream


binary_stream = [1, 0, 1, 1, 0, 0, 1]
samples = 100 # samples per bit

# Time axis
t = np.linspace(0, len(binary_stream), len(binary_stream) * samples,
endpoint=False)

# --- Generate Line Codes ---


def unipolar_nrz(data):
signal = np.repeat(data, samples)
return signal

def unipolar_rz(data):
signal = np.zeros(len(data) * samples)
for i, bit in enumerate(data):
if bit == 1:
signal[i*samples : i*samples + samples//2] = 1
return signal
def polar_nrz(data):
signal = np.repeat([1 if bit == 1 else -1 for bit in data],
samples)
return signal

def polar_rz(data):
signal = np.zeros(len(data) * samples)
for i, bit in enumerate(data):
if bit == 1:
signal[i*samples : i*samples + samples//2] = 1
else:
signal[i*samples : i*samples + samples//2] = -1
return signal

def ami(data):
signal = np.zeros(len(data) * samples)
polarity = 1
for i, bit in enumerate(data):
if bit == 1:
signal[i*samples : i*samples + samples] = polarity
polarity *= -1
return signal

def manchester(data):
signal = np.zeros(len(data) * samples)
for i, bit in enumerate(data):
if bit == 1:
signal[i*samples : i*samples + samples//2] = 1
signal[i*samples + samples//2 : (i+1)*samples] = -1
else:
signal[i*samples : i*samples + samples//2] = -1
signal[i*samples + samples//2 : (i+1)*samples] = 1
return signal

# --- Plotting ---


codes = {
"Unipolar NRZ": unipolar_nrz(binary_stream),
"Unipolar RZ": unipolar_rz(binary_stream),
"Polar NRZ": polar_nrz(binary_stream),
"Polar RZ": polar_rz(binary_stream),
"AMI": ami(binary_stream),
"Manchester": manchester(binary_stream)
}

plt.figure(figsize=(12, 16))

for idx, (name, signal) in enumerate(codes.items()):


# Time domain
plt.subplot(len(codes), 2, 2*idx+1)
plt.plot(t, signal)
plt.title(f"{name} - Time Domain")
plt.xlabel("Time")
plt.ylabel("Amplitude")
plt.grid(True)

# Frequency domain
plt.subplot(len(codes), 2, 2*idx+2)
plot_spectrum(signal, samples, name)

plt.tight_layout()
plt.show()
RESULT:
CONCLUSION:

In this experiment, various line coding techniques such as Unipolar NRZ, Unipolar RZ, Polar
NRZ, Polar RZ, Alternate Mark Inversion (AMI), and Manchester coding were successfully
implemented using Python. The corresponding time-domain waveforms were generated for a
given binary input, and their frequency-domain spectra were also plotted. From the results, it can
be observed that each coding scheme has distinct waveform characteristics and spectral
properties, making them suitable for different communication scenarios. This experiment
highlights the importance of line coding in digital communication systems for reliable
transmission and efficient bandwidth utilization.

You might also like