0% found this document useful (0 votes)
4 views17 pages

DC Assignment

The document provides Python code implementations for various line coding techniques including Unipolar NRZ, Polar NRZ-L, Polar NRZ-I, Polar RZ, Manchester Encoding, Differential Manchester, AMI, Pseudoternary, 2B1Q, 8B6T, and 4D-PAM5. Each section includes an aim, code, and a plot of the resulting signal. The implementations utilize numpy and matplotlib for signal generation and visualization.

Uploaded by

jpalak388
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)
4 views17 pages

DC Assignment

The document provides Python code implementations for various line coding techniques including Unipolar NRZ, Polar NRZ-L, Polar NRZ-I, Polar RZ, Manchester Encoding, Differential Manchester, AMI, Pseudoternary, 2B1Q, 8B6T, and 4D-PAM5. Each section includes an aim, code, and a plot of the resulting signal. The implementations utilize numpy and matplotlib for signal generation and visualization.

Uploaded by

jpalak388
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

UNIPOLAR NRZ

AIM:
Python code to implement Unipolar NRZ

CODE:
import numpy as np
import [Link] as plt

def unipolar_nrz(bit_sequence, bit_duration):


time = [Link](0, len(bit_sequence) * bit_duration, bit_duration / 1000)
signal = [Link](len(time))
for i, bit in enumerate(bit_sequence):
if bit == 1:
signal[i * 1000:(i + 1) * 1000] = 1

return time, signal


bit_sequence = [1, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0]
bit_duration = 1

time, signal = unipolar_nrz(bit_sequence, bit_duration)


[Link](figsize=(10, 4))
[Link](time, signal, where='post', linewidth=2)
[Link]('Unipolar NRZ Signal')
[Link]('Time (s)')
[Link]('Amplitude')
[Link](-0.1, 1.1)
[Link](0, len(bit_sequence) * bit_duration)
[Link]()
[Link]()

OUTPUT:
POLAR NRZ - L
AIM:
Python code to implement Polar NRZ-L

CODE:
import numpy as np
import [Link] as plt
def polar_nrz_l(bit_sequence, bit_duration):
time = [Link](0, len(bit_sequence) * bit_duration, bit_duration / 1000)
signal = [Link](len(time))
for i, bit in enumerate(bit_sequence):
if bit == 1:
signal[i * 1000:(i + 1) * 1000] = 1 # High level for '1'
else:
signal[i * 1000:(i + 1) * 1000] = -1 # Low level for '0'

return time, signal


bit_sequence = [1, 0, 1, 1, 0, 1, 1, 1, 0, 1]
bit_duration = 1
time, signal = polar_nrz_l(bit_sequence, bit_duration)
[Link](figsize=(10, 4))
[Link](time, signal, where='post', linewidth=2)
[Link]('Polar NRZ-L Signal')
[Link]('Time (s)')
[Link]('Amplitude')
[Link](-1.5, 1.5)
[Link](0, len(bit_sequence) * bit_duration)
[Link]()
[Link](0, color='black', linewidth=0.5, linestyle='--')
[Link]()
OUTPUT:
POLAR NRZ - I
AIM:
Python code to implement Polar NRZ-I

CODE:
import numpy as np
import [Link] as plt
def polar_nrz_i(bit_sequence, bit_duration):
time = [Link](0, len(bit_sequence) * bit_duration, bit_duration / 1000)
signal = [Link](len(time))

current_level = 1
for i, bit in enumerate(bit_sequence):
if bit == 1:
current_level *= -1
signal[i * 1000:(i + 1) * 1000] = current_level

return time, signal


bit_sequence = [1, 0, 1, 1, 0, 1, 0, 0, 1, 1]
bit_duration = 1
time, signal = polar_nrz_i(bit_sequence, bit_duration)
[Link](figsize=(10, 4))
[Link](time, signal, where='post', linewidth=2, color='orange')
[Link]('Polar NRZ-I Signal')
[Link]('Time (s)')
[Link]('Amplitude')
[Link](-1.5, 1.5)
[Link](0, len(bit_sequence) * bit_duration)
[Link]()
[Link](0, color='black', linewidth=0.5, linestyle='--')
[Link]()

OUTPUT:
POLAR RZ
AIM:
Python code to implement Polar RZ

CODE:
import numpy as np
import [Link] as plt
def polar_rz(bit_sequence, bit_duration):
time = [Link](0, len(bit_sequence) * bit_duration, bit_duration / 1000)
signal = [Link](len(time))

for i, bit in enumerate(bit_sequence):


if bit == 1:
signal[i * 1000:(i * 1000 + 500)] = 1

return time, signal


bit_sequence = [1, 0, 1, 1, 0, 1, 1, 0, 1, 0]
bit_duration = 1
time, signal = polar_rz(bit_sequence, bit_duration)
[Link](figsize=(10, 4))
[Link](time, signal, where='post', linewidth=2)
[Link]('Polar RZ Signal')
[Link]('Time (s)')
[Link]('Amplitude')
[Link](-0.5, 1.5)
[Link](0, len(bit_sequence) * bit_duration)
[Link]()
[Link](0, color='black', linewidth=0.5, linestyle='--')
[Link]()

OUTPUT:
BIPHASE- MANCHESTER
AIM:
Python code to implement Manchester Biphase Line Coding

CODE:
import numpy as np
import [Link] as plt
def manchester_encoding(bit_sequence, bit_duration):
time = [Link](0, len(bit_sequence) * bit_duration * 2, bit_duration / 1000)
signal = [Link](len(time))

for i, bit in enumerate(bit_sequence):


if bit == 1:
signal[i * 2000:(i * 2000 + 1000)] = 0.5 # Low to High for '1'
signal[i * 2000 + 1000:(i + 1) * 2000] = 1.0
else:
signal[i * 2000:(i * 2000 + 1000)] = 1.0 # High to Low for '0'
signal[i * 2000 + 1000:(i + 1) * 2000] = 0.5
return time, signal
bit_sequence = [1, 0, 1, 1, 0, 1, 0, 1, 1, 0]
bit_duration = 1 # Duration of each bit in seconds
# Generate the Manchester encoded signal
time, signal = manchester_encoding(bit_sequence, bit_duration)
[Link](figsize=(10, 4))
[Link](time, signal, where='post', linewidth=2)
[Link]('Manchester Encoding Signal')
[Link]('Time (s)')
[Link]('Amplitude')
[Link](-0.5, 1.5)
[Link](0, len(bit_sequence) * bit_duration * 2)
[Link]()
[Link](0, color='black', linewidth=0.5, linestyle='--')
[Link]()

OUTPUT:
DIFFERENTIAL MANCHESTER
AIM:
Python code to implement Differential Manchester Biphase Line Coding

CODE:
import numpy as np
import [Link] as plt
def differential_manchester_encoding(bit_sequence, bit_duration):
time = [Link](0, len(bit_sequence) * bit_duration * 2, bit_duration / 1000)
signal = [Link](len(time))
current_level = 1
for i, bit in enumerate(bit_sequence):
signal[i * 2000 + 1000] = current_level
if bit == 1:
signal[i * 2000:i * 2000 + 1000] = current_level
else:
current_level *= -1
signal[i * 2000:i * 2000 + 1000] = current_level
current_level *= -1
signal[i * 2000 + 1000:(i + 1) * 2000] = current_level
return time, signal
bit_sequence = [1, 0, 1, 1, 0, 1, 0, 1, 1, 0]
bit_duration = 1
time, signal = differential_manchester_encoding(bit_sequence, bit_duration)
[Link](figsize=(10, 4))
[Link](time, signal, where='post', linewidth=2)
[Link]('Differential Manchester Encoding Signal')
[Link]('Time (s)')
[Link]('Amplitude')
[Link](-1.5, 1.5)
[Link](0, len(bit_sequence) * bit_duration * 2)
[Link]()
[Link](0, color='black', linewidth=0.5, linestyle='--')
[Link]()

OUTPUT:
BIPOLAR AMI (Alternative Mark
Inversion)
AIM:
Python code to implement AMI Bipolar Line Coding

CODE:
import numpy as np
import [Link] as plt
def ami_encoding(bit_sequence, bit_duration):
time = [Link](0, len(bit_sequence) * bit_duration, bit_duration / 1000)
signal = [Link](len(time))

current_level = 1
for i, bit in enumerate(bit_sequence):
if bit == 1:
signal[i * 1000:(i + 1) * 1000] = current_level
current_level *= -1
return time, signal
bit_sequence = [1, 0, 1, 1, 0, 1, 0, 1, 1, 0]
bit_duration = 1
time, signal = ami_encoding(bit_sequence, bit_duration)
[Link](figsize=(10, 4))
[Link](time, signal, where='post', linewidth=2)
[Link]('Alternate Mark Inversion (AMI) Signal')
[Link]('Time (s)')
[Link]('Amplitude')
[Link](-1.5, 1.5)
[Link](0, len(bit_sequence) * bit_duration)
[Link]()
[Link](0, color='black', linewidth=0.5, linestyle='--')
[Link]()

OUTPUT:
PSEUDOTERNARY
AIM:
Python code to implement Pseudoternary Bipolar Line Coding

CODE:
import numpy as np
import [Link] as plt

def pseudoternary_encoding(bit_sequence, bit_duration):


time = [Link](0, len(bit_sequence) * bit_duration, bit_duration / 1000)
signal = [Link](len(time))

current_level = 1
for i, bit in enumerate(bit_sequence):
if bit == 0:
signal[i * 1000:(i + 1) * 1000] = current_level
current_level *= -1
return time, signal

bit_sequence = [1, 0, 1, 1, 0, 1, 0, 1, 1, 0]
bit_duration = 1
time, signal = pseudoternary_encoding(bit_sequence, bit_duration)
[Link](figsize=(10, 4))
[Link](time, signal, where='post', linewidth=2)
[Link]('Pseudoternary Encoding Signal')
[Link]('Time (s)')
[Link]('Amplitude')
[Link](-1.5, 1.5)
[Link](0, len(bit_sequence) * bit_duration)
[Link]()
[Link](0, color='black', linewidth=0.5, linestyle='--')
[Link]()
OUTPUT:
MULTILEVEL - 2B1Q
AIM:
Python code to implement 2B1Q Multilevel Line Coding

CODE:
import numpy as np
import [Link] as plt
def two_b_one_q_encoding(bit_sequence, bit_duration):
time = [Link](0, len(bit_sequence) // 2 * bit_duration, bit_duration / 1000)
signal = [Link](len(time))
for i in range(0, len(bit_sequence), 2):
if i + 1 < len(bit_sequence): # Check if we have a pair
bits = bit_sequence[i:i + 2]
if bits == [0, 0]:
signal[i // 2 * 1000:(i // 2 + 1) * 1000] = 1
elif bits == [0, 1]:
signal[i // 2 * 1000:(i // 2 + 1) * 1000] = 2
elif bits == [1, 0]:
signal[i // 2 * 1000:(i // 2 + 1) * 1000] = 3
elif bits == [1, 1]:
signal[i // 2 * 1000:(i // 2 + 1) * 1000] = 4
return time, signal
bit_sequence = [1, 0, 0, 1, 1, 0, 1, 1, 0, 0]
bit_duration = 1
time, signal = two_b_one_q_encoding(bit_sequence, bit_duration)
[Link](figsize=(10, 4))
[Link](time, signal, where='post', linewidth=2)
[Link]('2B1Q Multilevel Encoding Signal')
[Link]('Time (s)')
[Link]('Amplitude')
[Link](0, 5)
[Link](0, len(bit_sequence) // 2 * bit_duration)
[Link]([0, 1, 2, 3], ['00', '01', '10', '11'])
[Link]()
[Link](0, color='black', linewidth=0.5, linestyle='--')
[Link]()

OUTPUT:
8B6T
AIM:
Python code to implement 8B6T Multilevel Line Coding

CODE:
import numpy as np
import [Link] as plt
def eight_b_six_t_encoding(bit_sequence, bit_duration):
time = [Link](0, len(bit_sequence) // 8 * 6 * bit_duration, bit_duration / 1000)
signal = [Link](len(time))
encoding_map = {
'00000000': [0, 0, 0],
'00000001': [0, 0, 1],
'00000010': [0, 1, 0],
'00000011': [0, 1, 1],
'00000100': [1, 0, 0],
'00000101': [1, 0, 1],
'00000110': [1, 1, 0],
'00000111': [1, 1, 1],
}
for i in range(0, len(bit_sequence), 8):
bits = ''.join(map(str, bit_sequence[i:i + 8]))
if bits in encoding_map:
symbols = encoding_map[bits]
for j in range(3):
signal[(i // 8 * 3) * 1000 + j * 1000:(i // 8 * 3 + 1) * 1000 + j * 1000] = symbols[j] -1
return time, signal
bit_sequence = [0, 0, 0, 0, 0, 0, 0, 0,0, 0, 0, 0, 0, 0, 0, 1,0, 0, 0, 0, 0, 0, 1, 0]
bit_duration = 1
time, signal = eight_b_six_t_encoding(bit_sequence, bit_duration)
[Link](figsize=(10, 4))
[Link](time, signal, where='post', linewidth=2)
[Link]('8B6T Multilevel Encoding Signal')
[Link]('Time (s)')
[Link]('Amplitude')
[Link](-1.5, 1.5)
[Link](0, len(bit_sequence) // 8 * 6 * bit_duration)
[Link]()
[Link]()

OUTPUT:
4D-PAM5
AIM:
Python code to implement 4D-PAM5 Multilevel Line Coding

CODE:
import numpy as np
import [Link] as plt
def pam5_encoding(bit_sequence, bit_duration):
time = [Link](0, len(bit_sequence) // 4 * bit_duration, bit_duration / 1000)
signal = [Link](len(time))
encoding_map = {
'0000': -2,
'0001': -1,
'0010': 0,
'0011': 1,
'0100': 2,
}
for i in range(0, len(bit_sequence), 4):
bits = ''.join(map(str, bit_sequence[i:i + 4]))
if bits in encoding_map:
amplitude = encoding_map[bits]
signal[i // 4 * 1000:(i // 4 + 1) * 1000] = amplitude
return time, signal
bit_sequence = [0, 0, 0, 0,
0, 0, 0, 1,
0, 1, 0, 0,
0, 1, 1, 0]
bit_duration = 1
time, signal = pam5_encoding(bit_sequence, bit_duration)
[Link](time, signal, where='post', linewidth=2)
[Link]('4D-PAM5 Multilevel Encoding Signal')
[Link]('Time (s)')
[Link]('Amplitude')
[Link](-3, 3)
[Link](0, len(bit_sequence) // 4 * bit_duration)
[Link]()
[Link]()

OUTPUT:
MULTI-TRANSITION (MLT3)
AIM:
Python code to implement MLT3 Multi-transition Line Coding

CODE:
import numpy as np
import [Link] as plt
def mlt3_encoding(bit_sequence, bit_duration):
time = [Link](0, len(bit_sequence) * bit_duration, bit_duration / 1000)
signal = [Link](len(time))
current_index = 1
level_map = [1, 0, -1]
for i, bit in enumerate(bit_sequence):
if bit == 1:
current_index = (current_index + 1) % len(level_map)
signal[i * 1000:(i + 1) * 1000] = level_map[current_index]
return time, signal
bit_sequence = [1, 0, 1, 1, 0, 1, 0, 1]
bit_duration = 1
time, signal = mlt3_encoding(bit_sequence, bit_duration)
[Link](figsize=(10, 4))
[Link](time, signal, where='post', linewidth=2)
[Link]('MLT-3 Encoding Signal')
[Link]('Time (s)')
[Link]('Amplitude')
[Link](-2, 2)
[Link](0, len(bit_sequence) * bit_duration)
[Link]()
[Link]()

OUTPUT:
SCRAMBLING
B8ZS (Bipolar with 8 Zero's Substitution)
AIM:
Python code to implement B8ZS Scrambling Technique
CODE:
import numpy as np
import numpy as np
import [Link] as plt
def b8zs_encoding(bit_sequence):
signal = []
last_signal = 1
zero_count = 0
for bit in bit_sequence:
if bit == 1:
[Link](last_signal)
last_signal *= -1
zero_count = 0
else:
zero_count += 1
if zero_count == 8:
if last_signal == 1:
[Link]([0, -1, 0, 1, 0, -1])
else:
[Link]([0, 1, 0, -1, 0, 1])
zero_count = 0

else:
[Link](0)

return signal
bit_sequence = [1, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 1, 1, 0, 0, 0, 0, 0, 0, 0]
signal = b8zs_encoding(bit_sequence)
bit_duration = 1
time = [Link](0, len(signal) * (bit_duration / 1000), bit_duration / 1000)
[Link](figsize=(10, 4))
[Link](time, signal, where='post', linewidth=2)
[Link]('B8ZS Encoding Signal')
[Link]('Time (s)')
[Link]('Amplitude')
[Link](0, color='black', linewidth=0.5, linestyle='--')
[Link]()
[Link]()

OUTPUT:
HDB3 (High Density Bipolar 3-Zero's Substitution)
AIM:
Python code to implement HDB3 Scrambling Technique

CODE:
import numpy as np
import [Link] as plt

def hdb3_encoding(bit_sequence):
signal = []
last_signal = 1
zero_count = 0
violation_count = 0

for bit in bit_sequence:


if bit == 1:
[Link](last_signal)
last_signal *= -1
zero_count = 0
else:
zero_count += 1
if zero_count == 4:
if violation_count % 2 == 0:
[Link]([0, -last_signal, 0, last_signal])
else:
[Link]([0, last_signal, 0, -last_signal])
violation_count += 1
zero_count = 0
else:
[Link](0) return signal

def plot_hdb3(signal, bit_duration):


time = [Link](0, len(signal) * (bit_duration / 1000), bit_duration / 1000)

[Link](figsize=(10, 4))
[Link](time, signal, where='post', linewidth=2)
[Link]('HDB3 Encoding Signal')
[Link]('Time (s)')
[Link]('Amplitude')
[Link](-2, 2)
[Link](0, color='black', linewidth=0.5, linestyle='--')
[Link]()
[Link]()

bit_sequence = [1, 0, 0, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 0, 0, 0, 0]

signal = hdb3_encoding(bit_sequence)

bit_duration = 1

plot_hdb3(signal, bit_duration)

OUTPUT:
Some Block coding Techniques

You might also like