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 \(a_n\) and \(b_n\) are the trigonometric series coefficients of and \(N \in \mathbb{N}\) is a finite positive integer. The complex form is \[ y(t) \approx \sum_{n=-N}^N c_n e^{j \omega_n t}, \] where \(c_n\) are the complex series coefficients of .
Partial sums are often necessary approximations for concrete calculations.
Consider the function y(t) one period of which shown in figure 7.2. Compute a partial sum approximation and plot it.
We proceed in Python. First, load packages.
import numpy as np
import sympy as sp
import matplotlib.pyplot as pltOver 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 / TDefine 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 cn (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()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 yNow 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()Online Resources for Section 7.2
No online resources.