0% found this document useful (0 votes)
5 views14 pages

Source code

The document contains multiple source code snippets that demonstrate various digital signaling techniques including Unipolar NRZ, Polar NRZ, Unipolar RZ, Bipolar RZ, Manchester encoding, and modulation techniques like ASK, FSK, PSK, and QPSK. Each code snippet generates a corresponding signal based on binary input data and visualizes it using matplotlib. The document serves as a practical guide for understanding and implementing different digital communication methods.

Uploaded by

hossainemon5821
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)
5 views14 pages

Source code

The document contains multiple source code snippets that demonstrate various digital signaling techniques including Unipolar NRZ, Polar NRZ, Unipolar RZ, Bipolar RZ, Manchester encoding, and modulation techniques like ASK, FSK, PSK, and QPSK. Each code snippet generates a corresponding signal based on binary input data and visualizes it using matplotlib. The document serves as a practical guide for understanding and implementing different digital communication methods.

Uploaded by

hossainemon5821
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/ 14

Source code:

import numpy as np
import matplotlib.pyplot as plt

# Binary input (you can change this)


data = [1, 0, 1, 1, 0, 0, 1]

# Duration setup
bit_duration = 1 # 1 second per bit (for simplicity)
samples_per_bit = 100 # Resolution (samples per bit)
total_samples = len(data) * samples_per_bit

# Time axis
t = np.linspace(0, len(data) * bit_duration, total_samples)

# Signal generation
signal = []

for bit in data:


if bit == 1:
signal.extend([1] * samples_per_bit)
else:
signal.extend([0] * samples_per_bit)

# Plotting
plt.figure(figsize=(10, 3))
plt.plot(t, signal, linewidth=2, color='blue')
plt.title("Unipolar NRZ Signaling")
plt.xlabel("Time")
plt.ylabel("Amplitude")
plt.ylim(-1.5, 1.5)
plt.grid(True)
# plt.xticks(np.arange(0, len(data) + 1, 1))
# plt.yticks([0, 1])
plt.show()
Source Code:

import numpy as np
import matplotlib.pyplot as plt

# Input binary data


data = [1, 0, 1, 1, 0, 0, 1]
bit_duration = 1 # Assume 1 second per bit
samples_per_bit = 100
t = np.linspace(0, bit_duration * len(data), samples_per_bit * len(data))

# Generate Polar NRZ signal


signal = np.zeros(len(t))
for i, bit in enumerate(data):
start = i * samples_per_bit
end = start + samples_per_bit
if bit == 1:
signal[start:end] = 1
else:
signal[start:end] = -1

# Plot the waveform


plt.figure(figsize=(10, 3))
plt.plot(t, signal, linewidth=2)
plt.title("Polar NRZ Signaling")
plt.xlabel("Time")
plt.ylabel("Amplitude")
plt.ylim([-1.5, 1.5])
plt.grid(True)
plt.xticks(np.arange(0, len(data)+1, 1))
plt.yticks([-1, 0, 1])
plt.show()
Source Code:

import numpy as np
import matplotlib.pyplot as plt

# Binary input (you can change this)


data = [1, 0, 1, 1, 0, 0, 1]

# Parameters
bit_duration = 1 # seconds per bit
samples_per_bit = 100 # resolution
total_samples = len(data) * samples_per_bit
t = np.linspace(0, len(data) * bit_duration, total_samples)

# Signal generation
signal = []

for bit in data:


if bit == 1:
# First half high, second half zero
signal.extend([1] * (samples_per_bit // 2))
signal.extend([0] * (samples_per_bit // 2))
else:
# Full bit duration zero
signal.extend([0] * samples_per_bit)

# Plotting
plt.figure(figsize=(10, 3))
plt.plot(t, signal, linewidth=2, color='green')
plt.title("Unipolar RZ (Return-to-Zero) Signaling")
plt.xlabel("Time")
plt.ylabel("Amplitude")
plt.ylim(-0.5, 1.5)
plt.grid(True)
plt.xticks(np.arange(0, len(data) + 1, 1))
plt.yticks([0, 1])
plt.show()
Source Code:

import numpy as np
import matplotlib.pyplot as plt

# Input binary data


data = [1, 0, 1, 1, 0, 0, 1]
bit_duration = 1 # Assume 1 second for simplicity
samples_per_bit = 100
t = np.linspace(0, bit_duration * len(data), samples_per_bit * len(data))

# Generate bipolar RZ signal


signal = np.zeros(len(t))
polarity = 1 # Start with positive voltage for first '1'
for i, bit in enumerate(data):
start = i * samples_per_bit
mid = start + samples_per_bit // 2
if bit == 1:
signal[start:mid] = polarity
polarity *= -1 # Alternate polarity for next 1

# Plot the waveform


plt.figure(figsize=(10, 3))
plt.plot(t, signal, linewidth=2)
plt.title("Bipolar RZ Signaling")
plt.xlabel("Time")
plt.ylabel("Amplitude")
plt.ylim([-1.5, 1.5])
plt.grid(True)
plt.xticks(np.arange(0, len(data)+1, 1))
plt.yticks([-1, 0, 1])
plt.show()
Source Code:

import numpy as np
import matplotlib.pyplot as plt

# Input binary data


data = [1, 0, 1, 1, 0, 0, 1]
bit_duration = 1 # Duration of one bit (in seconds)
samples_per_bit = 100 # Number of samples per bit

# Time vector for entire signal


t = np.linspace(0, bit_duration * len(data), samples_per_bit * len(data))

# Generate Manchester encoded signal


signal = np.zeros(len(t))
for i, bit in enumerate(data):
start = i * samples_per_bit
mid = start + samples_per_bit // 2
end = start + samples_per_bit
if bit == 1:
# For '1': High-to-Low transition
signal[start:mid] = 1
signal[mid:end] = -1
else:
# For '0': Low-to-High transition
signal[start:mid] = -1
signal[mid:end] = 1

# Plotting the Manchester waveform


plt.figure(figsize=(10, 3))
plt.plot(t, signal, linewidth=2)
plt.title("Split-Phase (Manchester) Signaling")
plt.xlabel("Time")
plt.ylabel("Amplitude")
plt.ylim([-1.5, 1.5])
plt.grid(True)
plt.xticks(np.arange(0, len(data) + 1, 1))
plt.yticks([-1, 0, 1])
plt.show()
Source Code:

import numpy as np
import matplotlib.pyplot as plt

data_str = input("Enter binary data (e.g., 1010101): ")


x = [int(bit) for bit in data_str if bit in '01']

bp = 1e-6
A1, A2 = 10, 5
f = (1 / bp) * 10
samples = 100
t_bit = np.linspace(0, bp, samples)
t_total = np.linspace(0, bp * len(x), len(x) * samples)
digital_signal = np.repeat(x, samples)
modulated = np.concatenate([
(A1 if bit else A2) * np.cos(2 * np.pi * f * t_bit)
for bit in x
])
carrier = np.cos(2 * np.pi * f * t_bit)
recovered = [
1 if (2 * np.trapz(modulated[i:i+samples] * carrier, t_bit) / bp) > 7.5 else 0
for i in range(0, len(modulated), samples)
]
received_signal = np.repeat(recovered, samples)

plt.figure(figsize=(12, 8))
plt.subplot(3, 1, 1)
plt.plot(t_total, digital_signal, lw=2.5)
plt.title("Transmitting Information as Digital Signal")
plt.ylabel("Amplitude"); plt.grid(True); plt.ylim(-0.5, 1.5)

plt.subplot(3, 1, 2)
plt.plot(t_total, modulated)
plt.title("Waveform for Binary ASK Modulation")
plt.ylabel("Amplitude"); plt.grid(True)

plt.subplot(3, 1, 3)
plt.plot(t_total, received_signal, lw=2.5)
plt.title("Received Information as Digital Signal")
plt.xlabel("Time (sec)"); plt.ylabel("Amplitude"); plt.grid(True); plt.ylim(-0.5, 1.5)

plt.tight_layout()
plt.show()

print("Transmitted Data: ", x)


print("Received Data: ", recovered)
Source Code:

import numpy as np
import matplotlib.pyplot as plt

data_str = input("Enter binary data (e.g., 1011001): ")


x = [int(bit) for bit in data_str if bit in '01']

bp = 1e-6
A=5
f1 = 10 / bp
f2 = 5 / bp
samples = 100
t_bit = np.linspace(0, bp, samples, endpoint=False)
t_total = np.linspace(0, bp * len(x), len(x) * samples, endpoint=False)

digital_signal = np.repeat(x, samples)

modulated = np.concatenate([
A * np.cos(2 * np.pi * (f1 if bit else f2) * t_bit) for bit in x
])

recovered = []
for i in range(0, len(modulated), samples):
segment = modulated[i:i+samples]
corr1 = np.trapz(segment * np.cos(2 * np.pi * f1 * t_bit), t_bit)
corr2 = np.trapz(segment * np.cos(2 * np.pi * f2 * t_bit), t_bit)
recovered.append(1 if corr1 > corr2 else 0)

received_signal = np.repeat(recovered, samples)

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

plt.subplot(3, 1, 1)
plt.plot(t_total, digital_signal, lw=2.5)
plt.title("Transmitting Information as Digital Signal")
plt.ylabel("Amplitude")
plt.grid(True)
plt.ylim(-0.5, 1.5)

plt.subplot(3, 1, 2)
plt.plot(t_total, modulated)
plt.title("Waveform for Binary FSK Modulation")
plt.ylabel("Amplitude")
plt.grid(True)

plt.subplot(3, 1, 3)
plt.plot(t_total, received_signal, lw=2.5)
plt.title("Received Information as Digital Signal")
plt.xlabel("Time (sec)")
plt.ylabel("Amplitude")
plt.grid(True)
plt.ylim(-0.5, 1.5)

plt.tight_layout()
plt.show()

print("Transmitted Data: ", x)


print("Received Data: ", recovered)
Source Code:

import numpy as np
import matplotlib.pyplot as plt

data_str = input("Enter binary data (e.g., 1100101): ")


x = [int(bit) for bit in data_str if bit in '01']

bp = 1e-6
A=5
fc = 2 / bp
samples = 100
t_bit = np.linspace(0, bp, samples, endpoint=False)
t_total = np.linspace(0, bp * len(x), len(x) * samples, endpoint=False)

digital_signal = np.repeat(x, samples)

modulated = np.concatenate([
A * np.cos(2 * np.pi * fc * t_bit) if bit == 1 else
-A * np.cos(2 * np.pi * fc * t_bit) for bit in x
])

recovered = []
for i in range(0, len(modulated), samples):
segment = modulated[i:i+samples]
ref = A * np.cos(2 * np.pi * fc * t_bit)
product = segment * ref
integral = np.trapz(product, t_bit)
recovered.append(1 if integral > 0 else 0)

received_signal = np.repeat(recovered, samples)

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

plt.subplot(3, 1, 1)
plt.plot(t_total, digital_signal, lw=2.5)
plt.title("Transmitting Information as Digital Signal")
plt.ylabel("Amplitude")
plt.grid(True)
plt.ylim(-0.5, 1.5)

plt.subplot(3, 1, 2)
plt.plot(t_total, modulated)
plt.title("Waveform for Binary PSK Modulation")
plt.ylabel("Amplitude")
plt.grid(True)

plt.subplot(3, 1, 3)
plt.plot(t_total, received_signal, lw=2.5)
plt.title("Received Information as Digital Signal")
plt.xlabel("Time (sec)")
plt.ylabel("Amplitude")
plt.grid(True)
plt.ylim(-0.5, 1.5)

plt.tight_layout()
plt.show()

print("Transmitted Data: ", x)


print("Received Data: ", recovered)
Source Code:

import numpy as np
import matplotlib.pyplot as plt

binary_input = input("Enter binary data (e.g., 10100101): ")


x = np.array([int(bit) for bit in binary_input])
print("Input bits:", x)

p = np.where(x == 0, -1, 1)

even_seq = p[::2]
odd_seq = p[1::2]

bit_duration = 2
t = np.arange(0, len(p), 0.01)
even_ps = np.zeros_like(t)
odd_ps = np.zeros_like(t)

for i in range(len(even_seq)):
start = int((bit_duration * i) / 0.01)
end = int((bit_duration * (i + 1)) / 0.01)
even_ps[start:end] = even_seq[i]

for i in range(len(odd_seq)):
start = int((bit_duration * i) / 0.01)
end = int((bit_duration * (i + 1)) / 0.01)
odd_ps[start:end] = odd_seq[i]

plt.figure(figsize=(10, 6))

plt.subplot(2,1,1)
plt.plot(t, even_ps, 'r')
plt.title('NRZ Polar Line Coded Signal - Even Bits')
plt.ylabel('Amplitude')
plt.grid(True)

plt.subplot(2,1,2)
plt.plot(t, odd_ps, 'b')
plt.title('NRZ Polar Line Coded Signal - Odd Bits')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.grid(True)

plt.tight_layout()
plt.show()
c1 = np.cos(2 * np.pi * 1 * t)
c2 = np.sin(2 * np.pi * 1 * t)

plt.figure(figsize=(10, 4))

plt.subplot(2,1,1)
plt.plot(t, c1, 'r')
plt.title('Cosine Carrier Signal')
plt.grid(True)

plt.subplot(2,1,2)
plt.plot(t, c2, 'b')
plt.title('Sine Carrier Signal')
plt.grid(True)

plt.tight_layout()
plt.show()

r1 = even_ps * c1
r2 = odd_ps * c2
qpsk = r1 - r2

plt.figure(figsize=(10, 6))

plt.subplot(3,1,1)
plt.plot(t, r1, 'r')
plt.title('I-component (Even × Cosine)')
plt.grid(True)

plt.subplot(3,1,2)
plt.plot(t, r2, 'b')
plt.title('Q-component (Odd × Sine)')
plt.grid(True)

plt.subplot(3,1,3)
plt.plot(t, qpsk, 'k')
plt.title('QPSK Signal (I - Q)')
plt.xlabel('Time')
plt.grid(True)

plt.tight_layout()
plt.show()
Source Code:

import numpy as np
import matplotlib.pyplot as plt

# Define parameters
fs = 1000 # Sampling frequency
t = np.arange(0, 1, 1/fs) # Time vector
fm = 5 # Message signal frequency
fc = 50 # Carrier signal frequency

# Generate message signal (sine wave)


message_signal = np.sin(2 * np.pi * fm * t)

# Generate carrier signal (pulse train)


carrier_signal = np.zeros_like(t)
pulse_width = 0.01 # Adjust pulse width as needed
for i in range(len(t)):
if t[i] % (1/fc) < pulse_width:
carrier_signal[i] = 1

# Perform PAM
pam_signal = message_signal * carrier_signal

# Plot signals
plt.figure(figsize=(12, 6))

plt.subplot(3, 1, 1)
plt.plot(t, message_signal)
plt.title('Message Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')

plt.subplot(3, 1, 2)
plt.plot(t, carrier_signal)
plt.title('Carrier Signal (Pulse Train)')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')

plt.subplot(3, 1, 3)
plt.plot(t, pam_signal)
plt.title('PAM Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')

plt.tight_layout()
plt.show()

You might also like