Created
August 29, 2015 10:00
-
-
Save mick001/2cbd9b38625bc9e6eeac to your computer and use it in GitHub Desktop.
Fourier series and square wave approximation. Full article at http://www.firsttimeprogrammer.blogspot.com/2015/04/fourier-series-and-square-wave.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
plt.style.use("ggplot") | |
# Setup | |
x_ = np.linspace(-20,20,10000) | |
T = 8 | |
armonics = 10 | |
def squareWave(x): | |
global T | |
lowerBoundLeft = (-T/2) | |
lowerBoundRight = 0 | |
upperBoundLeft = 0 | |
upperBoundRight = (T/2) | |
one = 1 | |
negativeOne = -1 | |
while True: | |
if (x >= lowerBoundLeft) and (x <= lowerBoundRight): | |
return negativeOne | |
elif (x >= upperBoundLeft) and (x <= upperBoundRight): | |
return one | |
else: | |
lowerBoundLeft -= T/2 | |
lowerBoundRight -= T/2 | |
upperBoundLeft += T/2 | |
upperBoundRight += T/2 | |
if one == 1: | |
one = -1 | |
negativeOne = 1 | |
else: | |
one = 1 | |
negativeOne = -1 | |
# Bn coefficients | |
def bn(n): | |
n = int(n) | |
if (n%2 != 0): | |
return 4/(np.pi*n) | |
else: | |
return 0 | |
# Wn | |
def wn(n): | |
global T | |
wn = (2*np.pi*n)/T | |
return wn | |
# Fourier Series function | |
def fourierSeries(n_max,x): | |
a0 = 0 | |
partialSums = a0 | |
for n in range(1,n_max): | |
try: | |
partialSums = partialSums + bn(n)*np.sin(wn(n)*x) | |
except: | |
print("pass") | |
pass | |
return partialSums | |
y = [] | |
f = [] | |
for i in x_: | |
y.append(squareWave(i)) | |
f.append(fourierSeries(armonics,i)) | |
plt.plot(x_,y,color="blue",label="Signal") | |
plt.plot(x_,f,color="red",label="Fourier series approximation") | |
plt.title("Fourier Series approximation number of armonics: "+str(armonics)) | |
plt.legend() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment