-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFourier Square Wave.py
56 lines (30 loc) · 1.06 KB
/
Fourier Square Wave.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import square
from scipy.integrate import quad
from math import* #import all function from math
x=np.arange(-np.pi,np.pi,0.001) # x axis has been chosen from –π to +π, value of 1 smallest square along x axis is 0.001
y=square(x) #defining square wave function 𝑦 =−1, 𝑓𝑜𝑟 − 𝜋 ≤ 𝑥 ≤ 0
#y= +1, 𝑓𝑜𝑟 0 ≤ 𝑥 ≤ 𝜋
#define fuction
fc=lambda x:square(x)*cos(i*x) #i :dummy index
fs=lambda x:square(x)*sin(i*x)
n=50 #max value of I, not taken infinity, better result with high value
An=[] # defining array
Bn=[]
sum=0
for i in range(n):
an=quad(fc,-np.pi,np.pi)[0]*(1.0/np.pi)
An.append(an)
for i in range(n):
bn=quad(fs,-np.pi,np.pi)[0]*(1.0/np.pi)
Bn.append(bn) #putting value in array Bn
for i in range(n):
if i==0.0:
sum=sum+An[i]/2
else:
sum=sum+(An[i]*np.cos(i*x)+Bn[i]*np.sin(i*x))
plt.plot(x,sum,'g')
plt.plot(x,y,'r--')
plt.title("fourier series for square wave")
plt.show()