Fourier Series Calculator
What Is a Fourier Series?
A Fourier series represents a periodic function as a weighted sum of simple sine and cosine waves. Instead of working directly with a complicated waveform, you describe it using its harmonics — pure tones at integer multiples of a base frequency. This calculator focuses on functions defined on the interval [−π, π] and assumes they extend periodically with period 2π.
For a function f(x) that is 2π-periodic and integrable on [−π, π], its Fourier series can be written in the form
f(x) = a₀/2 + Σn=1∞ [aₙ cos(nx) + bₙ sin(nx)].
The coefficients aₙ and bₙ tell you how much of each cosine and sine harmonic appears in the signal. Low-order terms capture coarse features; higher-order terms capture sharper details and rapid oscillations.
Core Formulas
The standard coefficient formulas on [−π, π] are
- a₀ = (1/π) ∫−ππ f(x) dx
- aₙ = (1/π) ∫−ππ f(x) cos(nx) dx, for n ≥ 1
- bₙ = (1/π) ∫−ππ f(x) sin(nx) dx, for n ≥ 1
Using these coefficients, the full Fourier series is
In practice you almost never sum infinitely many terms. Instead, you use a truncated series of order N:
SN(x) = a₀/2 + Σn=1N [aₙ cos(nx) + bₙ sin(nx)].
This calculator computes such partial sums SN(x) by approximating the integrals numerically.
How This Fourier Series Calculator Works
The tool expects an input function f(x) defined on [−π, π] using math.js syntax (for example, x^2, abs(x), or sin(3*x) + 0.5*cos(2*x)). It then:
- Samples f(x) at many points between −π and π.
- Uses numerical integration (Simpson’s rule with a fine grid) to approximate each integral for a₀, aₙ, and bₙ.
- Computes the truncated series up to the chosen order N.
- Outputs the coefficients and, depending on implementation, may also provide the reconstructed series expression or series values at grid points.
Numerical integration trades exact symbolic formulas for speed and flexibility. You can use almost any reasonable function that math.js can evaluate, including ones without elementary antiderivatives.
How to Use the Fourier Series Calculator
To compute a truncated Fourier series on [−π, π]:
- Enter the function f(x). Use valid math.js syntax. Examples that typically work are:
x^2abs(x)sin(3*x) + 0.5*cos(2*x)
- Choose the number of terms N. This is the highest harmonic index used in the truncated series. Typical choices:
- N = 3–5 for a very rough approximation or quick intuition
- N = 10–20 for smoother approximations of many functions
- Higher N only if you understand the numerical trade-offs
- Run the calculation. Click the compute button to let the script evaluate the numerical integrals and assemble the Fourier coefficients.
- Review the coefficients. Inspect a₀, aₙ, and bₙ for 1 ≤ n ≤ N. You can plug these into your own software, plot partial sums, or compare against hand calculations.
The series output helps you see which harmonics dominate the signal and how many terms are required to capture its main shape.
Interpreting Results and Harmonics
Each coefficient corresponds to a specific frequency component:
- n = 0: The term a₀/2 is the average value (DC component) of the function over a period.
- n = 1: The fundamental frequency. Its cosine coefficient a₁ and sine coefficient b₁ capture the primary oscillation.
- n ≥ 2: Higher harmonics (multiples of the fundamental). Large coefficients at high n typically indicate sharper features, corners, or discontinuities in f(x).
Symmetry greatly simplifies the interpretation:
- If f(x) is even (f(−x) = f(x)), then all sine terms vanish: bₙ = 0 for all n. The series contains only cosines.
- If f(x) is odd (f(−x) = −f(x)), then all cosine terms vanish: a₀ = 0 and aₙ = 0 for all n. The series contains only sines.
The truncated series SN(x) is an approximation. As N increases, SN(x) generally converges to f(x) at points where f is continuous, and to the midpoint of the left and right limits at jump discontinuities.
Worked Example: f(x) = |x| on [−π, π]
Consider f(x) = |x| on [−π, π]. This function is even, so the Fourier series should contain only cosine terms. To reproduce this example with the calculator:
- In the function field, enter
abs(x). - Set the number of terms N, for example N = 10.
- Run the computation to obtain a₀ and aₙ for n = 1, …, 10. All bₙ coefficients should be numerically close to zero.
The resulting truncated series has the form
SN(x) = a₀/2 + Σn=1N aₙ cos(nx),
with decreasing cosine coefficients as n increases. For relatively small N you already see a good approximation to the characteristic “V” shape of |x|. As N grows, the approximation becomes closer, especially away from the endpoints ±π. Because |x| is continuous but has a sharp corner at 0, you may observe mild overshoot and ripples near that point, a manifestation of Gibbs-type behavior associated with non-smooth features.
Comparison: Different Function Types
The behavior of the Fourier coefficients depends strongly on the nature of f(x). The table below summarizes some typical patterns:
| Function type | Symmetry | Nonzero coefficients | Typical coefficient decay | Qualitative behavior of SN(x) |
|---|---|---|---|---|
| Smooth (many derivatives), e.g., sin(x) | Depends on example | Both aₙ and bₙ may appear | Often faster than 1/n² | Very rapid convergence; small N gives high accuracy |
| Piecewise smooth with corners, e.g., |x| | Even or odd possible | Typically one family (cosines for even, sines for odd) | About 1/n² | Good convergence but visible ripples near non-smooth points |
| Functions with jump discontinuities, e.g., square wave | Often odd or even when centered | Usually only sines for odd, only cosines for even | About 1/n | Convergence with persistent overshoot near jumps (Gibbs phenomenon) |
By comparing the coefficients produced by the calculator to these patterns, you can quickly infer whether your function is smooth, has sharp corners, or has jumps.
Applications and Typical Use Cases
This calculator is intended for students, instructors, and engineers who need a quick way to:
- Verify hand-computed Fourier coefficients for homework or exams.
- Explore how the truncated series SN(x) converges as N increases.
- Analyze the harmonic content of a custom waveform before building filters.
- Generate coefficient tables for use in programming assignments, simulations, or numerical experiments.
Common application domains include signal processing, vibration analysis, acoustics, heat conduction, and other areas where periodic phenomena appear.
Assumptions and Limitations
To interpret the results correctly, keep the following assumptions and limitations in mind:
- Periodicity: The method assumes f(x) is extended periodically with period 2π. The calculator only sees values on [−π, π] and interprets them as one full period.
- Function behavior: The numerical integration works best for functions that are reasonably smooth and bounded on [−π, π]. Highly oscillatory, discontinuous, or singular functions may lead to less accurate coefficients.
- Truncation order N: Very large N values increase computation time and can amplify numerical noise. For exploratory work, moderate N (about 5–30) is usually sufficient.
- Gibbs phenomenon: For functions with jumps or sharp corners, the partial sums SN(x) will exhibit overshoot and ringing near those points that does not disappear completely as N increases.
- Numerical approximation: Integrals are approximated using Simpson’s rule with a fixed grid. While this is typically accurate for well-behaved functions, it does not guarantee exact results, especially for functions with very rapid oscillations or complicated singular behavior.
- Input syntax: The function must be valid math.js syntax. Unsupported functions, undefined variables, or syntax errors will cause evaluation problems. When in doubt, test a simpler expression first.
- Not for critical design decisions: The tool is primarily educational and exploratory. For safety-critical engineering designs, always corroborate results with more specialized analysis tools and domain-specific checks.
Understanding these assumptions helps you use the calculator confidently while remaining aware of its scope. The Fourier series it produces are powerful aids for intuition, visualization, and preliminary analysis, but they should be combined with mathematical judgment and, where necessary, more rigorous verification.
