# Plot graphs using Matplotlib

Solution

q2-1.py

from math import exp, sin, pi, e

importnumpy as np

importmatplotlib.pyplot as plt

t0 = 0

f0 = 10

sigma = 10

t = np.linspace(-10,10,100)

def y(t):

return (np.exp(-((t-t0)**2)/(2*(sigma)**2))*np.sin(2*pi*f0*t))

plt.plot(t, y(t).astype(np.float))

plt.show()

q2-2.py

from math import exp, sin, pi, e

importnumpy as np

importmatplotlib.pyplot as plt

t = np.linspace(-10,10,1000)

def y(t, sigma, f0, t0):

return (np.exp(-((t-t0)**2)/(2*(sigma)**2))*np.sin(2*pi*f0*t))

plt.plot(t, y(t, 0.5, 2, -5).astype(np.float)) #plot1 blue

plt.plot(t, y(t, 0.5, 3, 0).astype(np.float)) #plot2 red

plt.plot(t, y(t, 0.5, 4, 5).astype(np.float)) #plot3 green

plt.xlabel(‘Time(t)’)

plt.ylabel(‘y(t)’)

plt.title(‘Three Modulated Sine Waves’)

plt.grid(True)

plt.show()

q2-3.py

from math import exp, sin, pi, e

importnumpy as np

importmatplotlib.pyplot as plt

fromnumpy.fft import fft

sigma = 0.5

dt = 1/1000

t = np.linspace(-10,10,1000)

y = ((np.exp(-((t+5)**2)/(2*(sigma)**2))*np.sin(2*pi*2*t)) +

(np.exp(-((t)**2)/(2*(sigma)**2))*np.sin(2*pi*3*t)) +

(np.exp(-((t-5)**2)/(2*(sigma)**2))*np.sin(2*pi*4*t)))

yfft = (fft(y)) #fft

#Amplitude Spectra

plt.plot(abs(yfft))

plt.xlabel(‘Frequency’)

plt.ylabel(‘Magnitude(Volt/sqrt(Hertz))’)

plt.title(‘FFT of Sum of Three Modulated Sine Waves(Amplitude Spectra)’)

plt.grid(True)

plt.show()

plt.plot(abs(yfft))

plt.xlabel(‘Frequency’)

plt.ylabel(‘Magnitude(Volt/sqrt(Hertz))’)

plt.title(‘Zoomed Amplitude Spectra’)

plt.grid(True)

plt.xlim(0, 100) #Zooming

plt.show()

plt.savefig(‘/home/vishal/Desktop/Amplitude Spectra.png’, transparent = False)

#Phase Spectra

plt.plot(np.angle(yfft))

plt.xlabel(‘Frequency’)

plt.ylabel(‘Phase’)

plt.title(‘FFT of Sum of Three Modulated Sine Waves(Phase Spectra)’)

plt.grid(True)

plt.show()

plt.plot(np.angle(yfft))

plt.xlabel(‘Frequency’)

plt.ylabel(‘Phase’)

plt.title(‘Zoomed Phase Spectra’)

plt.grid(True)

plt.xlim(0, 100) #Zooming

plt.show()

plt.savefig(‘/home/vishal/Desktop/Phase Spectra.png’, transparent = False)