0
0
RosHow-ToBeginner · 4 min read

Fourier Series of Square Wave: Formula and Python Example

The Fourier series of a square wave is an infinite sum of sine waves with odd harmonics and decreasing amplitudes. It is given by f(t) = (4/π) ∑ (1/n) sin(nωt) for odd n, where ω is the fundamental frequency.
📐

Syntax

The Fourier series for a square wave with period T and angular frequency ω = 2π/T is:

f(t) = (4/π) ∑_{n=1,3,5,...} (1/n) sin(nωt)

  • n: odd integers (1, 3, 5, ...)
  • ω: fundamental angular frequency
  • sin(nωt): sine wave at nth harmonic
  • 4/π: scaling factor for amplitude
math
f(t) = (4/π) * sum_{n=1,3,5,...} (1/n) * sin(n * ω * t)
💻

Example

This example shows how to compute and plot the Fourier series approximation of a square wave using Python. It sums the first 7 odd harmonics to approximate the wave.

python
import numpy as np
import matplotlib.pyplot as plt

def square_wave_fourier(t, harmonics=7):
    result = np.zeros_like(t)
    for n in range(1, 2*harmonics, 2):  # odd harmonics
        result += (1/n) * np.sin(n * t)
    return (4/np.pi) * result

# Time values from -pi to pi
x = np.linspace(-np.pi, np.pi, 1000)
y = square_wave_fourier(x, harmonics=7)

plt.plot(x, y, label='Fourier Series Approximation')
plt.title('Square Wave Fourier Series Approximation')
plt.xlabel('Time (t)')
plt.ylabel('Amplitude')
plt.grid(True)
plt.legend()
plt.show()
Output
A plot window showing a square wave approximation with smooth transitions and flat tops
⚠️

Common Pitfalls

  • Including even harmonics: The square wave Fourier series only uses odd harmonics (1, 3, 5, ...).
  • Not scaling by 4/π: This factor is essential for correct amplitude.
  • Using too few harmonics: The approximation will be rough and have noticeable ripples (Gibbs phenomenon).
  • Ignoring the fundamental frequency ω: It sets the wave's period.
python
import numpy as np
import matplotlib.pyplot as plt

def wrong_square_wave(t, harmonics=7):
    result = np.zeros_like(t)
    for n in range(1, harmonics + 1):  # Incorrect: includes even harmonics
        result += (1/n) * np.sin(n * t)  # Missing 4/π scaling
    return result

x = np.linspace(-np.pi, np.pi, 1000)
y_wrong = wrong_square_wave(x)

plt.plot(x, y_wrong, label='Wrong Approximation')
plt.title('Incorrect Fourier Series Approximation')
plt.grid(True)
plt.legend()
plt.show()
Output
A plot showing a distorted wave that does not resemble a square wave
📊

Quick Reference

Remember these key points for the square wave Fourier series:

  • Use only odd harmonics: 1, 3, 5, ...
  • Amplitude scales as 4/π * 1/n
  • Sum sine waves: sin(nωt)
  • More harmonics = better approximation but more computation

Key Takeaways

The square wave Fourier series sums only odd sine harmonics scaled by 4/π and 1/n.
Including even harmonics or missing the scaling factor leads to incorrect results.
More harmonics improve the approximation but cause ripples near edges (Gibbs phenomenon).
The fundamental frequency ω defines the wave's period and must be used correctly.
Python can easily visualize the series to understand how harmonics build the square wave.