Skip to main content
Home
A key to conquer the (open source) world.

Main navigation

  • Home
  • About Us
    • About this website
    • About_me
    • Family rivalry
  • Gurus
  • Tutorials
User account menu
  • Log in

Breadcrumb

  1. Home
  2. Tutorials

📊🌊 3.4 Computing the Fourier series of a piecewise function.

Hero image for keyhole tutorials.

📊🌊 Breaking complex shapes into waves.
In this section we compute the Fourier series of a simple piecewise function. Step by step, we determine the Fourier coefficients and see how sine and cosine waves combine to reproduce a function with sharp changes.

Fourier series explained main picture.

Goal

In this section we compute the Fourier series of a simple piecewise function.

  • A piecewise function is a function defined by different formulas on different intervals of the input variable.
  • General form
    A piecewise function is usually written like this:
    \( f(x)=
    \begin{cases}
    \text{formula}_1 & \text{if condition}_1 \\
    \text{formula}_2 & \text{if condition}_2 \\
    \text{formula}_3 & \text{if condition}_3
    \end{cases} \)

Given Function

Consider the function:

\[ \begin{array}{rcl}
f(x) & = & c_{1} \quad \text{if } -\pi < x < 0 \\
& = & c_{2} \quad \text{if } 0 < x < \pi \\
& & \text{and the function is periodic with period } 2\pi \\
& & c_{1} \neq c_{2}
\end{array} \]

This function is piecewise constant and jumps at x = 0.

Graphically it looks like a square step wave.

Our goal is to represent \(f(x) \) as a Fourier series.

Piecewise constant function that jumps at x = 0

Step 1 — Compute \( a_{0} \)

In a Fourier series, \( a_{0} \) is the coefficient that represents the average value of the function over one period.

It determines the constant (baseline) level of the Fourier series.

\[ a_{0}=\frac{1}{\pi}\int_{-\pi}^{\pi}f(x)dx \]

Split the integral:

\[ a_{0}=
\frac{1}{\pi}\int_{-\pi}^{0}c_{1}dx
+
\frac{1}{\pi}\int_{0}^{\pi}c_{2}dx \]

Compute the integrals:

\[ a_{0}=
\frac{c_{1}}{\pi}\int_{-\pi}^{0}dx
+
\frac{c_{2}}{\pi}\int_{0}^{\pi}dx \]

Integrate:

\[ a_{0}=\frac{c_{1}}{\pi}\left[ x \right]^0_{-\pi}+\frac{c_{2}}{\pi}\left[ x \right]^\pi_0 \]

Apply the limits:

\[ a_{0}=\frac{c_{1}}{\pi}(0-(-\pi))+\frac{c_{2}}{\pi}(\pi-0) \]

The result:

\[ a_{0}=c_{1}+c_{2} \]

Step 2 — Compute \( a_{n} \)

In a Fourier series, \( a_{n} \) is the coefficient that determines the strength (amplitude) of the cosine waves in the series.

\[ a_{n}=\frac{1}{\pi}\int_{-\pi}^{\pi}f(x)\cos(nx)dx \]

Split the integral:

\[ a_{n}=
\frac{1}{\pi}\int_{-\pi}^{0}c_{1}\cos(nx)dx
+
\frac{1}{\pi}\int_{0}^{\pi}c_{2}\cos(nx)dx \]

Place the constants outside the integrals:

\[ =
\frac{c_1}{\pi}\int_{-\pi}^{0}\cos(nx)\,dx
+
\frac{c_2}{\pi}\int_{0}^{\pi}\cos(nx)\,dx \]

Recall the derivative rule:

\[ \frac{d}{dx}\sin(nx)=n\cos(nx) \]

So the antiderivative is:

\[ \int_{}^{}cos(nx)dx=\frac{\sin(nx)}{n} \]

Replace the integral by its antiderivative:

\[ =
\frac{c_1}{\pi}\int_{-\pi}^{0}\cos(nx)\,dx
+
\frac{c_2}{\pi}\int_{0}^{\pi}\cos(nx)\,dx \\
=
\frac{c_1}{\pi}\left[\frac{\sin(nx)}{n}\right]_{-\pi}^{0}
+
\frac{c_2}{\pi}\left[\frac{\sin(nx)}{n}\right]_{0}^{\pi} \]

Rewrite the constants:

\[ a_{n}=
\frac{c_1}{n\pi}\left[\sin(nx)\right]_{-\pi}^{0}
+
\frac{c_2}{n\pi}\left[\sin(nx)\right]_{0}^{\pi}=0 \]

Explanation:

\( \sin(n\pi)=0 \) and \( \sin(0)=0 \)

Step 3 — Compute \( b_{n} \)

In a Fourier series, \( b_{n} \) tells how much of the sine wave \( \sin(nx) \) is present in the function.

\[ b_{n}=\frac{1}{\pi}\int_{-\pi}^{\pi}f(x)\sin(nx)dx \]

Split the integral:

\[ =
\frac{1}{\pi}\int_{-\pi}^{0}c_{1}\sin(nx)dx
+
\frac{1}{\pi}\int_{0}^{\pi}c_{2}\sin(nx)dx \]

Place the constants outside the integrals:

\[ =
\frac{c_{1}}{\pi}\int_{-\pi}^{0}\sin(nx)dx
+
\frac{c_{2}}{\pi}\int_{0}^{\pi}\sin(nx)dx \]

Remember the antiderivative:

\[ \int \sin(nx)\,dx=-\frac{\cos(nx)}{n} \]

Substitute the integrals with their antiderivatives:

\[ =
-\frac{c_{1}}{n\pi}\left[\cos(nx)\right]^{0}_{-\pi}
-
\frac{c_{2}}{n\pi}\left[\cos(nx)\right]^{\pi}_{0} \]

Evaluate the limits:

\[ =
-\frac{c_{1}}{n\pi}(\cos 0-\cos(-n\pi))
-
\frac{c_{2}}{n\pi}(\cos(n\pi)-\cos 0) \]

Remember that:

\[ \cos(0)=1 \\
\cos(n\pi)=\cos(-n\pi)=(-1)^n \]

Proof:

  • Cosinus is an even function, as a result \( \cos(-n\pi)=\cos(n\pi) \)
  • \( \cos(n\pi)=(-1)^{n} \)
cos(n΀) = (-1) to the power n

After inserting this into the equation:

\[ b_n=
-\frac{c_1}{n\pi}(1-(-1)^n)
-
\frac{c_2}{n\pi}((-1)^n-1) \]

Even and Odd Values of n

If n is even

  • n = 2m
  • \( (-1)^{n}=1 \)
  • \( b_n=-\frac{c_1}{n\pi}(1-1)-\frac{c_2}{n\pi}(1-1)=0 \)

If n is odd

  • \( n=2m-1 \) for some integer \( m=1,2,3,\cdots \)
  • Substitute \( n=2m-1 \) into \( (-1)^{n} \)
    \( (-1)^n=(-1)^{2m-1} \)
  • Split the power:
    \( (-1)^{2m-1}=(-1)^{2m}(-1)^{-1} \)
  • Any even power of −1 equals 1.
    \( (-1)^{2m}=1 \)
  • A number raised to a negative power is equal to the reciprocal of that number raised to the corresponding positive power:
    \( a^{-n}=\frac{1}{a^{n}} \)
  • As a result:
    \( (-1)^{2m-1}=-1 \)
  • Because \( n=2m-1 \)
    \( (-1)^{n}=-1 \)
  • Substitute \( (-1)^{n}=-1 \) in the fourier series equation:
    \( b_n=
    -\frac{c_1}{n\pi}(1-(-1)^n)
    -
    \frac{c_2}{n\pi}((-1)^n-1)\\ =
    -\frac{c_1}{n\pi}(1-(-1))-\frac{c_2}{n\pi}((-1)-1) \)
  • Simplify
    \( b_{n}=-\frac{2c_{1}}{n\pi}+\frac{2c_{2}}{n\pi}=-\frac{2(c_{1}-c_{2})}{n\pi} \)
  • Replace \( n \) by \( n=2m-1 \)
    \( b_{2m-1}=-\frac{2(c_1-c_2)}{(2m-1)\pi} \)

đŸ”Ĩ This is why the Fourier series contains only odd sine terms:

\[ \sin(x),\sin(3x),\sin(5x),\sin(7x),\cdots \]

Conclusion:

For odd n \((n=2m-1) \):

\[ b_{2m-1}=-\frac{2(c_1-c_2)}{(2m-1)\pi} \]

For even n \((n=2m) \):

\[ b_{n}=0 \]

The Fourier Series becomes

1. Start from the general Fourier series

For a 2Ī€-periodic function:

\[ f(x)=\frac{a_0}{2}+\sum_{n=1}^{\infty}\left(a_n\cos(nx)+b_n\sin(nx)\right) \]

2. Insert the coefficients

From the previous calculations:

Constant term

\[ a_0=c_1+c_2 \]

This results in:

\[ \frac{a_0}{2}=\frac{c_1+c_2}{2} \]

Cosine coefficients

\[ a_n=0 \]

đŸ”Ĩ So the cosine part disappears.

Sine coefficients

For even n

\[ b_{n}=0 \]

For odd n

\[ b_{2m-1}=-\frac{2(c_1-c_2)}{(2m-1)\pi} \]

3. Replace \( n \) by \( 2m-1 \) in the sinus term

Since only odd terms remain, we rewrite the sum using:

\[ n=2m-1 \]

Then the sine terms become

\[ \sin(nx)=\sin((2m-1)x) \]

4. Insert the coefficient inside the sum

The sine part becomes

\[ \sum_{n=1}^{\infty }b_{n}\sin(nx)=\sum_{m=1}^{\infty }-\frac{2(c_{1}-c_{2})}{(2m-1)\pi}\sin((2m-1)x) \]

We are allowed to switch from n to m because only odd values of n remain, and the formula n = 2m − 1 conveniently generates all odd integers.

The switch from \( n\to m \) is allowed because the index of summation is just a dummy variable.

\[ \sum_{n=1}^{\infty}\frac1n
=
\sum_{k=1}^{\infty}\frac1k \]

5. Factor constants outside the sum

The constant factor

\[ -\frac{2(c_{1}-c_{2})}{\pi} \]

can be placed in front of the equation:

\[ -\frac{2(c_1-c_2)}{\pi}
\sum_{m=1}^{\infty}
\frac{\sin((2m-1)x)}{2m-1} \]

6. Final Fourier series

Adding the constant term gives

\[ f(x)=
\frac{c_1+c_2}{2}
-
\frac{2(c_1-c_2)}{\pi}
\sum_{m=1}^{\infty}
\frac{\sin((2m-1)x)}{2m-1} \]

7. Interpretation

The Fourier series contains:

  • a constant level
    \( \frac{c_1+c_2}{2} \)
    (the average value of the function)
  • only odd sine waves
    \( \sin(x),\sin(3x),\sin(5x),\sin(7x),\cdots \)

This combination of odd sine waves builds the jump between \( c_{1} \) and \( c_{2} \)

Important Remark

This Fourier series represents \(f(x) \):

  • in \(]-\pi,0[ \)
  • in \( ]0,-\pi[ \)

At x = 0, the series converges to

\[ \frac{c_1+c_2}{2} \]

This value is the average of the left and right limits.

The same happens at \(x=-\pi \) and \(x=\pi \).

Special Case

Let \( c_2=-c_1=a \)

The function becomes odd, and the Fourier series simplifies to

\[ \frac{4a}{\pi}
\sum_{m=1}^{\infty}
\frac{\sin((2m-1)x)}{2m-1} \]

Evaluating the Series

Let

\[ x=\frac{\pi}{2} \]

Since \( 0 < x < \pi \) the function simplifies to \( f(x)=c_2=a \)

As a result:

\[ f(x)=a=\frac{4a}{\pi}\sum_{m=1}^{\infty}\frac{\sin\!\left((2m-1)\frac{\pi}{2}\right)}{2m-1} \]

1. Divide both sides by a

Since \( a\# 0 \), we can divide by a:

\[ 1=\frac{4}{\pi}\sum_{m=1}^{\infty}\frac{\sin\!\left((2m-1)\frac{\pi}{2}\right)}{2m-1} \]

2. Multiply both sides by \( \frac{\pi}{4} \)

\[ \frac{\pi}{4}=
\sum_{m=1}^{\infty}\frac{\sin\!\left((2m-1)\frac{\pi}{2}\right)}{2m-1} \]

3. Evaluate the sine values

Factoring \( \sin((2m-1)\frac{\pi}{2}) \)

Compute the first few values:

Evaluate the sine values

Conclusion:

\[ \sin\left((2m-1)\frac{\pi}{2}\right)=
\begin{cases}
1 & m \text{ odd} \\
-1 & m \text{ even}
\end{cases} \]

This creates an alternating sign.

4. Substitute into the sum

\[ \frac{\pi}{4}=
\sum_{m=1}^{\infty}\frac{\sin\!\left((2m-1)\frac{\pi}{2}\right)}{2m-1}=
1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\frac{1}{9}-\cdots \]

5. Final famous result

Multiplying both sides by 4:

\[ \pi=4\left(1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\cdots\right) \]

This is the Leibniz series for \( \pi \).

✅ Meaning

A Fourier series of a square-like function leads to the remarkable identity

\[ \frac{\pi}{4}=
1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\frac{1}{9}-\cdots \]

So Fourier analysis gives a way to compute \( \pi \) from an infinite alternating series.

Key Insight

Fourier series are not only useful for describing functions.

They also allow us to compute the sums of certain numerical series, such as

\[ 1-\frac{1}{3}+\frac{1}{5}-\frac{1}{7}+\frac{1}{9}-\cdots \]

which equals

\[ \frac{\pi}{4} \]

Back to the Fourier series explained tutorial list

Back to the tutorial list

Author

 

Categories
Science
Wed, 04/01/2026 - 15:32

Managed ad1

RSS feed
Powered by Drupal