Fourier Series Calculator

Stephanie Ben-Joseph headshot Stephanie Ben-Joseph

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

Using these coefficients, the full Fourier series is

f(x) = a02 + n=1 [ an cos(nx) + bn sin(nx) ]

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:

  1. Samples f(x) at many points between −π and π.
  2. Uses numerical integration (Simpson’s rule with a fine grid) to approximate each integral for a₀, aₙ, and bₙ.
  3. Computes the truncated series up to the chosen order N.
  4. 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 [−π, π]:

  1. Enter the function f(x). Use valid math.js syntax. Examples that typically work are:
    • x^2
    • abs(x)
    • sin(3*x) + 0.5*cos(2*x)
  2. 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
  3. Run the calculation. Click the compute button to let the script evaluate the numerical integrals and assemble the Fourier coefficients.
  4. 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:

Symmetry greatly simplifies the interpretation:

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:

  1. In the function field, enter abs(x).
  2. Set the number of terms N, for example N = 10.
  3. 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:

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:

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.

Enter a function and order.

Embed this calculator

Copy and paste the HTML below to add the Fourier Series Calculator – Compute Truncated Series on [−π, π] to your website.