Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168732
Image: ubuntu2004

Christos Saragiotis

In this worksheet I present two interactive examples of Fourier series. The aim is to demonstrate 

  1. the more terms included in the truncated Fourier Series, the better the approximation of the original signal and    
  2. the Gibbs phenomenon close to the discontinuities.

1. Square wave

We define the square wave with period T0T_0 as

p(t)=Asign[sin(2πtT0)] p(t) = A\mathrm{sign}\left[\sin\left(2\pi \frac{t}{T_0}\right)\right]  

pi = float(pi) A=1 P=2 def pulse(t): # Periodic pulse train. # A: amplitude # P: period return A*sgn(sin(2*pi*t/P)) plot(pulse,(-5,5), ymin=-2*A, ymax=2*A)

The Fourier series coefficients of p(t)p(t) are

  • a0=2T0T0p(t)  dt=0a_0 = \displaystyle\frac{2}{T_0}\int_{T_0}p(t)\;dt = 0,
  • an=0a_n = 0, for n1n\geq1 due to the odd symmetry and
  • bn=2T0T0p(t)sin(nω0t)  dt=2A(1cos(nπ))nπb_n = \displaystyle\frac{2}{T_0}\int_{T_0}p(t)\sin\left(n\omega_0t\right)\;dt = \frac{2A\left(1-\cos(n\pi)\right)}{n\pi}, for n1n \geq 1.
def Pulse(N): # FS coeffs of pulse a0 = 0 an = [A*0 for n in [1..N]] bn = [2*A/(pi*n)*(1 - cos(n*pi)) for n in [1..N]] return (a0,an,bn)

The Fourier Series sum is

p(t)n=1bnsin(nω0t)=n=1bnsin(n2πT0t)p(t) \sim \sum_{n=1}^\infty b_n\sin\left(n\omega_0t\right) = \sum_{n=1}^\infty b_n\sin\left(n\frac{2\pi}{T_0}t\right)

and the partial sum of NN terms

pN(t)=n=1Nbnsin(n2πT0t)p_N(t) = \sum_{n=1}^N b_n\sin\left(n\frac{2\pi}{T_0}t\right)

def Partial_sum(t, coeffs): # Evaluate the Fourier series with coefficients coeffs. partial(t) = coeffs[0] N = len(coeffs[1]) for n,an,bn in zip(range(1,N+1), coeffs[1],coeffs[2]): partial(t) = partial(t) + an*cos(n*2*pi/P*t) + bn*sin(n*2*pi/P*t) return partial @interact def show_sum(N=(0,(0..100))): coeffs = Pulse(N) fn = Partial_sum(t, coeffs) p1 = plot(pulse, (-2.5,2.5), color='blue') p2 = plot(fn, (-2.5,2.5), color='green') txt = text("pulse train and FS partial sum", (3,2*A), horizontal_alignment='right', color='black') show(p2+p1+txt)

2. Sawtooth wave

Let the sawtooth wave be

s(t)=A(tT0tT0),tR s(t) = A\left(\frac{t}{T_0} - \left\lfloor \frac{t}{T_0}\right\rfloor\right),\quad t\in\mathbb{R}

var('t') A = 1 P = 2 def sawtooth(t): return A*((t/P) - floor((t/P))) plot(sawtooth, (-5,5), ymax=A+1)

The Fourier series coefficients of s(t)s(t) are

  • a0=2T0T0s(t)  dt=2T00T0AtT0  dt=A2a_0 = \displaystyle\frac{2}{T_0}\int_{T_0}s(t)\;dt = \frac{2}{T_0}\int_0^{T_0}\frac{At}{T_0}\;dt =\frac{A}{2},
  • an=0a_n = 0, for n1n\geq 1 due to the (hidden) odd symmetry.
  • bn=2T0T0s(t)sin(nω0t)  dt=Anπb_n = \frac{2}{T_0}\displaystyle\int_{T_0}s(t)\sin\left(n\omega_0t\right)\;dt = -\frac{A}{n\pi}, for n1n \geq 1.
def Sawtooth(N): # FS coeffs of pulse a0 = A/2 an = [A*0 for n in [1..N]] bn = [-A/(pi*n) for n in [1..N]] return (a0,an,bn)

The Fourier Series sum is

s(t)a02+n=1bnsin(nω0t)=n=1bnsin(n2πT0t)s(t) \sim \frac{a_0}{2} + \sum_{n=1}^\infty b_n\sin\left(n\omega_0t\right) = \sum_{n=1}^\infty b_n\sin\left(n\frac{2\pi}{T_0}t\right)

and the partial sum of NN terms

sN(t)=n=1Nbnsin(n2πT0t)s_N(t) = \sum_{n=1}^N b_n\sin\left(n\frac{2\pi}{T_0}t\right)

@interact def show_sum(N=(0,(0..100))): coeffs = Sawtooth(N) fn = Partial_sum(t, coeffs) p3 = plot(sawtooth, (-2.5,2.5), color='blue') p4 = plot(fn, (-2.5,2.5), color='green') txt = text("sawtooth wave and FS partial sum", (3,A+1), horizontal_alignment='right', color='black') show(p3+p4+txt)