Engineering Math

Partial Sums of Fourier Series

A partial sum of a Fourier series is the truncated synthesis \[ y(t) \approx \frac{a_0}{2} + \sum_{n=1}^N a_n \cos(\omega_n t) + b_n \sin(\omega_n t), \] where and are the trigonometric series coefficients of and is a finite positive integer. The complex form is \[ y(t) \approx \sum_{n=-N}^N c_n e^{j \omega_n t}, \] where are the complex series coefficients of .

Partial sums are often necessary approximations for concrete calculations.

Example 6.2

Consider the function one period of which shown in figure 6.2. Compute a partial sum approximation and plot it.

 Figure 6.2
Figure 6.2: A period of the function .

We proceed in Python. First, load packages.

import numpy as np
import sympy as sp
import matplotlib.pyplot as plt

Over the period $[-T/2, T/2)$, the function $y(t)$ is defined as: $$ y(t) = \begin{cases} (4 A/T) t + A & -T/2 \leq t < -T/4 \\ 0 & -T/4 \leq t < T/4 \\ (4 A/T) t - A & T/4 \leq t < T/2. \end{cases} $$ We will use a partial sum of the complex Fourier series to approximate the function $y(t)$.

We begin symbolically. Define the symbolic variables.

A, T = sp.symbols('A, T', real=True, positive=True)
t = sp.symbols('t', real=True, nonnegative=True)
n = sp.symbols('n', integer=True)
w_n = 2 * sp.pi * n / T

Define the function $y(t)$.

y_list = []
y_list.append(4 * A / T * t + A)  # -T/2 <= t < -T/4
y_list.append(0 * A)  # -T/4 <= t < T/4
y_list.append(4 * A / T * t - A)  # T/4 <= t < T/2
lims = [-T/2, -T/4, T/4, T/2]

Compute the complex Fourier series coefficients $c_n$ (i.e., perform the Fourier series analysis).

c_n = 0  # Initialize the coefficient
for i in range(len(y_list)):
  c_n += (1 / T) * sp.integrate(
     y_list[i] * sp.exp(-sp.I * w_n * t), (t, lims[i], lims[i + 1])
  )
c_n = c_n.simplify()

Letting $A = 1$ and $T = 1$, we can compute the harmonic amplitude and phase spectrum.

N = 10
params = {A: 1, T: 1}
c_n_num = np.full(2 * N + 1, np.nan, dtype=complex)
n_num = np.arange(-N, N + 1)
for n_ in n_num:
    i = n_ + N
    c_n_num[i] = c_n.subs({n: n_}).evalf(subs=params)

Let’s plot the amplitude and phase spectrum.

fig, ax = plt.subplots(2, 1, sharex=True)
ax[0].stem(n_num, np.abs(c_n_num))
ax[0].set_title('Amplitude spectrum')
ax[1].stem(n_num, np.angle(c_n_num))
ax[1].set_title('Phase spectrum')
ax[1].set_xlabel('Harmonic $n$')
plt.draw()
 Figure 6.3
Figure 6.3: Amplitude and phase spectrum of the complex Fourier series.

Now, we will compute the partial sum of the complex Fourier series up to the $N$-th harmonic.

y_N = sp.Sum(c_n * sp.exp(sp.I * w_n * t), (n, -N, N)).doit()

Let’s plot the function $y(t)$ and the partial sum of the complex Fourier series. First we define the function $y(t)$ beyond the interval $[-T/2, T/2)$ with the use of the periodicity of the function.

def y_num(t, params, T, A):
    T_ = params[T]
    t = np.mod(t + T_ / 2, T_) - T_ / 2
    y = np.zeros_like(t)
    i1 = (-T_ / 2 <= t) & (t < -T_ / 4)
    i2 = (-T_ / 4 <= t) & (t < T_ / 4)
    i3 = (T_ / 4 <= t) & (t < T_ / 2)
    y[i1] = 4 * params[A] / T_ * t[i1] + params[A]
    y[i2] = 0
    y[i3] = 4 * params[A] / T_ * t[i3] - params[A]
    return y

Now we plot the function $y(t)$ and the partial sum of the complex Fourier series.

t_num = np.linspace(-1, 1, 1001)
y_N_num = sp.lambdify(t, y_N.subs(params), 'numpy')(t_num)
fig, ax = plt.subplots()
ax.plot(t_num, y_N_num, label='Partial sum')
ax.plot(t_num, y_num(t_num, params, T, A), label='Actual $y(t)$')
ax.set_xlabel('$t$')
ax.set_ylabel('$y(t)$')
ax.legend()
plt.show()
 Figure 6.4
Figure 6.4: Function and partial sum of the complex Fourier series.

Online Resources for Section 6.2

No online resources.