Fourier Series & THD Analyzer

Interactive demonstration of square wave reconstruction and harmonic spectrum


Total Harmonic Distortion (THD): 42.44%
Harmonic Spectrum (Normalized Magnitude)

Mathematical Derivations

For a square wave $v(t)$ with amplitude $V$ and fundamental period $T$, we define the angular frequency as $\omega_0 = \frac{2\pi}{T}$. The waveform over one period $T$ is defined as:

$$ v(t) = \begin{cases} V, & 0 < t < \frac{T}{2} \\ -V, & \frac{T}{2} < t < T \end{cases} $$

1. Conventional (Trigonometric) Form

The trigonometric Fourier series is given by:

$$ v(t) = a_0 + \sum_{n=1}^{\infty} \left( a_n \cos(n\omega_0 t) + b_n \sin(n\omega_0 t) \right) $$

Final Trigonometric Series:

$$ v(t) = \sum_{n=1,3,5,\dots}^{\infty} \frac{4V}{n\pi} \sin(n\omega_0 t) $$

Note: The normalized harmonic magnitude plotted in the spectrum graph above is calculated as the ratio of the $n$-th harmonic magnitude to the fundamental magnitude, which evaluates exactly to $\frac{1}{n}$.


2. Complex (Exponential) Form

The complex Fourier series is given by:

$$ v(t) = \sum_{n=-\infty}^{\infty} c_n e^{j n \omega_0 t} $$

Where the complex coefficients $c_n$ are calculated as:

$$ c_n = \frac{1}{T} \int_{0}^{T} v(t) e^{-j n \omega_0 t} dt $$ $$ c_n = \frac{1}{T} \left[ \int_{0}^{T/2} V e^{-j n \omega_0 t} dt - \int_{T/2}^{T} V e^{-j n \omega_0 t} dt \right] $$ Evaluating the integrals yields: $$ c_n = \frac{V}{-j n \omega_0 T} \left[ e^{-j n \omega_0 T/2} - 1 \right] - \frac{V}{-j n \omega_0 T} \left[ e^{-j n \omega_0 T} - e^{-j n \omega_0 T/2} \right] $$ Substituting $\omega_0 T = 2\pi$: $$ c_n = \frac{V}{-j n 2\pi} \left[ e^{-j n \pi} - 1 - e^{-j n 2\pi} + e^{-j n \pi} \right] $$ Using Euler's identity ($e^{-j n \pi} = \cos(n\pi)$ and $e^{-j n 2\pi} = 1$): $$ c_n = \frac{V}{-j n 2\pi} \left[ 2\cos(n\pi) - 2 \right] = \frac{V}{j n \pi} \left( 1 - \cos(n\pi) \right) $$

For even $n$, $\cos(n\pi) = 1$, so $c_n = 0$.
For odd $n$, $\cos(n\pi) = -1$, so $c_n = \frac{2V}{j n \pi}$.

Final Complex Series:

$$ v(t) = \sum_{n=-\infty, \text{ odd}}^{\infty} \frac{2V}{j n \pi} e^{j n \omega_0 t} $$