# Q 4A
# Fourier Series Sawtooth
import numpy as np
from scipy.signal import sawtooth
from scipy.integrate import simps
import matplotlib.pyplot as plt
t = np.linspace(0,1, 1001)
f1 = sawtooth(2*np.pi*2*t,1)
T = 0.5
#Calculation Of Coefficients
a0 = (2./T) * simps(f1, t)
an = lambda n : (2./T) * simps(f1*np.cos(2*np.pi*2*n*t), t)
bn = lambda n : (2./T) * simps(f1*np.sin(2*np.pi*2*n*t), t)
tn = np.linspace(0, 6, 1001)
s =0.5*a0 + sum([an(n)*np.cos(2*np.pi*2*n*tn) + bn(n)*np.sin(2*np.pi*2*n*tn) for n in range(10)])
plt.plot(tn, s, 'r',label="Fourier approximation")
plt.plot(tn, 2*sawtooth(2*np.pi*2*tn,1) ,label= "Original Signal")
plt.legend()
plt.show()
Comments
Post a Comment