Skip to content

Instantly share code, notes, and snippets.

@ecosdelfuturo
Last active July 29, 2024 09:51
Show Gist options
  • Save ecosdelfuturo/42697a221c045653d8b18e410379352a to your computer and use it in GitHub Desktop.
Save ecosdelfuturo/42697a221c045653d8b18e410379352a to your computer and use it in GitHub Desktop.
Fourier series of a square wave
from matplotlib import pyplot as plt
import numpy as np

def func(x,n):
    i = 0
    sum = 0
    while i <= n:
        sum = sum + np.sin((2*i+1)*x)/(2*i+1)
        i += 1
    return sum


def plotfunc(nn):
    for i in range (0,len(nn)):
         plt.plot(x, np.array([func(xp, nn[i]) for xp in x]), label=r'$\sum$ i ='+str(nn[i]),c = (np.random.random(), np.random.random(), np.random.random()))
    return

x=np.arange(0,2*np.pi,.01)

plotfunc([1,3,5,7])
plt.legend()
plt.show()

fourier

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment