Skip to content

Instantly share code, notes, and snippets.

@AmirAlahmedy
Created March 28, 2020 17:11
Show Gist options
  • Save AmirAlahmedy/190fa7de980fbfdedb7be86e84d8c3c2 to your computer and use it in GitHub Desktop.
Save AmirAlahmedy/190fa7de980fbfdedb7be86e84d8c3c2 to your computer and use it in GitHub Desktop.
Simulation of some modulation techniques using Python.
import matplotlib.pyplot as plt
from numpy import arange, cos, sin, fft
from math import pi
from scipy.signal import hilbert
# Givens
t = arange(0, 1e-2, 1/2e4)
f = fft.fftfreq(t.shape[-1])
f_m, f_c = 1e3, 1e6
A_c = 10
modulatingSignal = cos(2*pi*f_m*t)
# DSB-SC
unmodulatedCarrier = A_c*cos(2*pi*t*f_c)
modulatedSignal = modulatingSignal*unmodulatedCarrier
spectrum = fft.fft(modulatedSignal)
plt.plot(f, spectrum.real, f, spectrum.imag)
plt.title('Spectrum of the DSB-SC modulated signal')
plt.show()
line1, = plt.plot(t, modulatingSignal, 'r')
line2, = plt.plot(t, modulatedSignal, 'b')
plt.legend((line1, line2), ('modulating signal','modulated signal'))
plt.title('Double Side Band Suppressed Carrier')
plt.show()
# DSB-LC
modulatedSignal = (A_c + modulatingSignal)*cos(2*pi*t*f_c)
spectrum = fft.fft(modulatedSignal)
plt.plot(f, spectrum.real, f, spectrum.imag)
plt.title('Spectrum of the DSB-LC modulated signal')
plt.show()
line1, = plt.plot(t, modulatingSignal, 'r')
line2, = plt.plot(t, modulatedSignal, 'b')
plt.legend((line1, line2), ('modulating signal','modulated signal'))
plt.title('Double Side Band Large Carrier')
plt.show()
# SSB
modulatingSignal_h = hilbert(modulatingSignal)
USB = modulatingSignal*cos(2*pi*f_c*t) - modulatingSignal_h*sin(2*pi*f_c*t)
LSB = modulatingSignal*cos(2*pi*f_c*t) + modulatingSignal_h*sin(2*pi*f_c*t)
# Frequency Domain
specFig = plt.figure()
plt.title('Single Side Band Spectrum')
u_spectrum = fft.fft(USB)
ax0 = specFig.add_subplot(211)
ax0.plot(f, u_spectrum.real, f, u_spectrum.imag)
l_spectrum = fft.fft(LSB)
ax1 = specFig.add_subplot(212)
ax1.plot(f, l_spectrum.real, f, l_spectrum.imag)
# Time Domain
fig = plt.figure()
plt.title('Single Side Band')
ax0 = fig.add_subplot(211)
ax0.plot(t, USB, 'y', label='USB')
ax0.legend()
ax1 = fig.add_subplot(212)
ax1.plot(t, LSB, 'c', label='LSB')
ax1.legend()
plt.show()
# FM and PM
b = 3 # modulation index
modulatedSignal = A_c*cos(f_c*t + b*sin(f_m*t))
spectrum = fft.fft(modulatedSignal)
plt.plot(f, spectrum.real, f, spectrum.imag)
plt.title('Spectrum of the frequency modulated signal')
plt.show()
plt.plot(t, modulatedSignal, 'g')
plt.title('Frequency Modulation')
plt.show()
@AmirAlahmedy
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment