Fourier Series Calculator | aₙ, bₙ & Spectrum
Compute Fourier coefficients aₙ and bₙ for any periodic function using 2000-point Simpson's rule. Supports square wave, sawtooth, triangle, rectifiers, and custom expressions. Shows amplitude spectrum, coefficient table, partial sum, and Gibbs phenomenon detection.
Preset Waveforms
f(x) = 1 if x > 0, −1 if x < 0, f(x) = ±1, odd function; bₙ = 4/(nπ) for odd n only
What Is the Fourier Series Calculator | aₙ, bₙ & Spectrum?
A Fourier series decomposes any periodic function into an infinite sum of sines and cosines, the harmonics. The key insight is that any repeating waveform, no matter how complicated its shape, can be expressed as a weighted combination of simple sinusoids at integer multiples of a fundamental frequency. The weights are the Fourier coefficients aₙ and bₙ.
The DC term a₀/2 is simply the mean value of the function over one period. The fundamental harmonic (n = 1) oscillates once per period. The second harmonic (n = 2) oscillates twice per period, and so on. Together, they reconstruct the original waveform with increasing fidelity as more terms are added.
The Dirichlet conditions guarantee convergence: if f is piecewise continuous with finitely many discontinuities per period, the Fourier series converges to f(x) at every continuity point and to the average of the left and right limits at each jump discontinuity. This covers virtually every waveform encountered in engineering and physics.
This calculator computes each coefficient numerically using 2000-point composite Simpson's rule, accurate to about 8 significant figures for smooth functions. The method is the same numerical quadrature used in professional scientific software. All computation runs live in your browser with no server round-trip.
Formula
| Symbol | Name | Description |
|---|---|---|
| f(x) | Periodic function | Assumed periodic with period T = 2L; the series converges to f(x) where f is continuous |
| L | Half-period | The function is defined on [−L, L]; period = 2L. Use L = π for the standard case. |
| a₀ | DC coefficient | (1/L) ∫ f(x) dx; a₀/2 equals the average (mean) value of f over one period |
| aₙ | Cosine coefficient | (1/L) ∫ f(x)cos(nπx/L) dx; zero for odd functions; dominant for even functions |
| bₙ | Sine coefficient | (1/L) ∫ f(x)sin(nπx/L) dx; zero for even functions; dominant for odd functions |
| |cₙ| | Harmonic amplitude | √(aₙ² + bₙ²), the amplitude of the nth harmonic in the spectrum |
| φₙ | Phase angle | atan2(bₙ, aₙ) in degrees, the phase offset of the nth harmonic |
| N | Number of terms | Partial sum uses harmonics 1 through N; more terms → better approximation |
| ω₀ | Fundamental frequency | π/L radians per unit x; the lowest frequency component in the series |
How to Use
- 1Select a preset waveform: Click a preset (Square wave, Sawtooth, Triangle, Full-wave rectifier, Half-wave rectifier, or Parabola) to auto-load a well-known function. Each preset note explains the expected coefficient pattern.
- 2Or enter a custom function: Click "Custom f(x)" and type any expression in x, e.g. x^2, sin(x)*x, abs(x)-0.5. The parser supports sin, cos, tan, exp, ln, sqrt, abs, pi, and standard arithmetic operators.
- 3Set the half-period L: Enter the half-period L so the function is treated as defined on [−L, L] with period 2L. For the standard trigonometric Fourier series, use L = π (pre-filled for most presets).
- 4Choose the number of terms N: Set N between 1 and 20. More terms give a closer approximation. For smooth functions, N = 8–10 is usually sufficient to capture the main behaviour.
- 5Optionally enter an evaluation point x: If you enter a value in "Evaluate S_N(x) at", the partial sum is computed at that point and shown in the result banner. Leave blank to skip evaluation.
- 6Press Calculate or Enter: Click "Calculate" or press Enter. All N + 1 integrals are computed (a₀ and n = 1..N), giving aₙ, bₙ, amplitude, and phase for each harmonic.
- 7Read the amplitude spectrum: The bar chart shows the amplitude |cₙ| for each harmonic. Tall bars indicate dominant frequency components. The DC bar uses a lighter colour. Hover over any bar to see the exact value.
- 8Expand the coefficient table: Click "Coefficient table" to see every aₙ, bₙ, amplitude, and phase angle. Click "Copy results" to copy all coefficients as tab-separated text for use in a spreadsheet or script.
Example Calculation
Example 1: Square wave on [−π, π]
The square wave is defined as +1 for x > 0 and −1 for x < 0. It is an odd function, so all cosine coefficients aₙ are zero. The exact coefficients are known analytically.
Example 2: Triangle wave on [−π, π]
The triangle wave f(x) = 1 − |x|/π is an even function. Only cosine terms appear, and the coefficients fall off as 1/n², much faster than the square wave's 1/n decay.
Example 3: Parabola f(x) = x² on [−1, 1]
A non-trigonometric custom function. Even function, L = 1. The exact Fourier series is known and serves as a benchmark for the numerical integration accuracy.
Understanding Fourier Series | aₙ, bₙ & Spectrum
Exact Coefficients for Common Waveforms
These exact analytic results let you verify numerical output and understand the relationship between waveform shape and spectrum:
| Waveform | aₙ (cosine) | bₙ (sine) |
|---|---|---|
| Square wave (sign(x)) | aₙ = 0 | bₙ = 4/(nπ), odd n; 0 even n |
| Sawtooth (x/π) | aₙ = 0 | bₙ = 2(−1)ⁿ⁺¹/(nπ) |
| Triangle (1−|x|/π) | aₙ = 8/(nπ)², odd n; 0 even | bₙ = 0 |
| Full-wave rect. (|sin x|) | a₀ = 4/π; aₙ = −8/(π(4n²−1)) | bₙ = 0 |
| Half-wave rect. (max(0,sin x)) | a₀ = 2/π; a₁ = 1/2; aₙ = 2/(π(1−4n²)) for n≥2 | b₁ = 1/2; bₙ = 0 for n≠1 |
| Parabola (x²) on [−L,L] | a₀ = 2L²/3; aₙ = 4L²(−1)ⁿ/(nπ)² | bₙ = 0 |
The Gibbs Phenomenon
At every jump discontinuity in f, the partial sum S_N overshoots the true value by approximately 8.9% of the jump height, regardless of how many terms N you include. This was first described by Henry Wilbraham in 1848 and rediscovered by J. Willard Gibbs in 1899.
- ›The overshoot does not vanish as N → ∞, it merely moves closer to the discontinuity.
- ›At the jump midpoint, the series converges to the average of the left and right limits, by the Dirichlet theorem.
- ›Smooth functions (no jumps) have no Gibbs phenomenon and converge exponentially fast.
- ›Practical fix: Lanczos σ-factors or windowing functions (Hann, Hamming) can suppress the overshoot at the cost of spectral resolution.
Dirichlet Convergence Conditions
The Fourier series of f converges to f(x) at every continuity point, and to the midpoint of the jump at every discontinuity, provided:
- ›Bounded variation: f has finitely many maxima, minima, and discontinuities in one period.
- ›Absolute integrability: ∫|f(x)| dx over one period is finite.
- ›One-sided limits: f(x⁺) and f(x⁻) exist at every point.
These conditions cover virtually every waveform in engineering. More sophisticated convergence results (L² convergence via Parseval, uniform convergence for smooth f) apply in specialised contexts.
Parseval's Theorem
Parseval's identity connects the energy of a signal to its frequency content:
Applications in Science and Engineering
- ›Signal processing: Every audio waveform can be decomposed into frequency components. Equalizers adjust the amplitude of each harmonic. The Fourier transform generalises this to aperiodic signals.
- ›Electrical engineering: AC circuit analysis uses Fourier series to find the steady-state response to periodic non-sinusoidal voltages (square waves, PWM signals). Total harmonic distortion (THD) is the ratio of harmonic power to fundamental power.
- ›Heat equation: Joseph Fourier invented the series to solve ∂u/∂t = α ∂²u/∂x² on a finite rod. The solution is a Fourier series in x, with each coefficient decaying exponentially in time at rate α(nπ/L)².
- ›Vibrations and acoustics: A plucked guitar string vibrates at its fundamental and harmonic frequencies simultaneously. The relative amplitudes of the harmonics determine the timbre.
- ›Image compression: JPEG uses the discrete cosine transform (DCT), a close relative of the Fourier cosine series, to represent image blocks as sums of spatial frequency components.
- ›Quantum mechanics: The Fourier series connects position-space and momentum-space wave functions for particles in a box (infinite square well). Energy levels correspond to harmonics of the fundamental.
Fourier Series vs. Fourier Transform
The Fourier series applies to periodic functions and produces a discrete spectrum, countably many coefficients at integer multiples of the fundamental frequency. The Fourier transform extends this to aperiodic (non-repeating) functions and produces a continuous spectrum. The two are related: the Fourier transform of a periodic function is a train of Dirac deltas at the harmonic frequencies, with weights proportional to the Fourier coefficients.
- ›Fourier series (this calculator): periodic f, discrete spectrum {aₙ, bₙ}, integral over one period.
- ›Fourier transform: aperiodic f, continuous spectrum F(ω) = ∫ f(t) e^(−iωt) dt over all t.
- ›DFT / FFT: sampled and periodic signal, discrete spectrum, the foundation of digital signal processing.
Frequently Asked Questions
What is a Fourier series and what does it calculate?
A Fourier series represents any periodic function as an infinite sum of sines and cosines. This calculator computes the coefficients aₙ and bₙ that define each term:
- • a₀ is proportional to the mean value of f over one period (the DC component).
- • aₙ is the cosine coefficient for the nth harmonic, how much of cos(nπx/L) is in f.
- • bₙ is the sine coefficient, how much of sin(nπx/L) is in f.
The partial sum S_N(x) uses N harmonics and approximates f(x). As N → ∞, the partial sum converges to f at every continuity point by the Dirichlet theorem.
What is the half-period L and how do I set it?
The half-period L means the function is defined on [−L, L] and extended periodically with period T = 2L.
- • Standard case L = π: the period is 2π and the harmonics are cos(nx) and sin(nx), the textbook Fourier series.
- • L = 1: period = 2, used for functions defined on [−1, 1].
- • General L: the harmonics are cos(nπx/L) and sin(nπx/L), with angular frequency ω₀ = π/L.
Preset waveforms auto-set L to the most natural value (π for trigonometric waveforms, 1 for the parabola).
What is the Gibbs phenomenon?
The Gibbs phenomenon is a permanent overshoot that occurs near jump discontinuities in the Fourier partial sum.
- • The partial sum S_N overshoots by approximately 8.9% of the jump height at each discontinuity.
- • Adding more terms does not eliminate the overshoot, it only moves it closer to the jump point.
- • At the jump itself, the series converges to the average of the left and right limits.
- • The square wave and sawtooth presets both exhibit this behaviour.
To suppress Gibbs ringing, engineers apply window functions (Hann, Hamming, Lanczos) that taper the higher-order coefficients.
Why are some coefficients exactly zero?
Symmetry of f(x) forces entire groups of coefficients to vanish:
- • Odd function f(−x) = −f(x): all a₀ = aₙ = 0. Only sine terms remain. Examples: square wave, sawtooth.
- • Even function f(−x) = f(x): all bₙ = 0. Only cosine terms remain. Examples: triangle wave, parabola, full-wave rectifier.
- • Half-wave symmetry f(x + L) = −f(x): all even harmonics vanish (a₂ = b₂ = a₄ = b₄ = … = 0). Square wave and triangle wave both have this.
The calculator auto-detects even and odd symmetry from the computed coefficients and displays a note when found.
How accurate is the numerical integration?
This calculator uses composite Simpson's rule with 2000 subintervals, a standard, well-tested quadrature method.
- • Smooth functions: error ≈ O(h⁴) ≈ 10⁻¹³ for typical waveforms, far better than needed.
- • Piecewise smooth functions (square wave, sawtooth): error at discontinuity points is handled gracefully, the integrand is bounded, so Simpson's rule still converges at O(h²).
- • Benchmark: the square wave preset should give b₁ ≈ 1.273239 (exact: 4/π) and b₂ ≈ 0, the numerical output matches to 8+ decimal places.
All computation runs live in your browser using JavaScript's 64-bit floating-point arithmetic (IEEE 754 double precision).
What is Parseval's theorem and why does it matter?
Parseval's theorem states that the total energy (power) of a signal equals the sum of the squares of its Fourier coefficients:
- • (1/L) ∫|f|² dx = (a₀/2)² + (1/2)Σ(aₙ² + bₙ²)
- • Verification tool: compute both sides independently. If they match, your coefficients are correct.
- • Power spectrum: aₙ² + bₙ² is proportional to the power at harmonic n. This is the basis of spectral analysis.
- • Mathematical identity: Parseval applied to f(x) = x on [−π, π] gives the famous Basel result Σ 1/n² = π²/6.
What is the difference between Fourier series and the Fourier transform?
They are related but suited to different types of signals:
- • Fourier series (this tool): for periodic functions. Produces a discrete set of coefficients at integer multiples of ω₀ = π/L.
- • Fourier transform: for aperiodic (non-repeating) signals. Produces a continuous function F(ω) for all frequencies ω.
- • DFT/FFT: for finite, sampled data. Uses a finite number of discrete frequency bins, standard in audio, image processing, and communications.
A periodic function's Fourier transform is an impulse train at the harmonic frequencies with impulse magnitudes equal to the Fourier series coefficients.
Where does this calculator get its mathematical definitions?
All coefficient formulas and mathematical conventions follow the standard references used in engineering and applied mathematics:
- • NIST Digital Library of Mathematical Functions (DLMF), Section 1.8: Fourier Series, for the coefficient integral definitions and Dirichlet convergence theorem.
- • Abramowitz & Stegun, Chapter 2, for the orthogonality relations used to derive the coefficient formulas.
- • The exact analytic results for common waveforms (square, sawtooth, triangle, rectifiers) shown in the table are independently derived and cross-checked against the computed output.
All computation runs entirely in your browser, no data is transmitted to any server.