# Q 5B
import numpy as np
from scipy.integrate import quad, simps
from scipy.signal import square, sawtooth
import matplotlib.pyplot as plt
x = np.linspace(-np.pi, np.pi, 1001)
f1 = abs(np.sin(x))
L = 2*np.pi
# Evaluation of Fourier coefficients
a0 = (2.0/L)*simps(f1, x)
an = lambda n: (2.0/L) * simps(f1*np.cos(n*x), x)
bn = lambda n: (2.0/L) * simps(f1*np.sin(n*x), x)
# Summing the serier
R = 10
xn = np.linspace(-R, R, 1001)
s = 0.5*a0 + sum([an(n)*np.cos(n*xn) + bn(n)*np.sin(n*xn) for n in range(1, 50)])
signal = abs(np.sin(xn))
#Plotting the Functions
plt.plot(xn, signal,'r-' ,label="Original Signal")
plt.plot(xn, s,'ko',ms=2, label = "Fourier Approximation")
plt.legend()
plt.show()
Comments
Post a Comment