The goal is to recreate a drawing with Fourier series. The drawing is represented by a function \(f\) that takes as values all points of the drawing represented by complex numbers. A point \((x,y)\) is represented by the complex number \(x+iy\). In a drawing composed of \(N\) points forming segments \([f_0, f_1]\), \([f_1, f_2]\), ..., \([f_{N-2}, f_{N-1}]\), a last segment is added to close the figure: \([f_{N-1}, f_0]\). The function representation is a concatenation of linear interpolations between the endpoints of the segments. The period is set to \(2\pi\), meaning the function completes one full cycle as \(t\) goes from \(0\) to \(2\pi\).
\(\displaystyle f(t) = \begin{cases} f_0 \cdot \frac{t_1 - t}{t_1} + f_1 \cdot \frac{t}{t_1} & \text{if } t \in [0, t_1] \\ \vdots \\ f_i \cdot \frac{t_{i+1} - t}{t_{i+1} - t_i} + f_{i+1} \cdot \frac{t - t_i}{t_{i+1} - t_i} & \text{if } t \in [t_i, t_{i+1}] \\ \vdots \\ f_n \cdot \frac{1 - t}{1 - t_n} + f_0 \cdot \frac{t - t_n}{1 - t_n} & \text{if } t \in [t_n, 1] \\ \end{cases} \)
Or, regrouping by \(t\):
\(\displaystyle f(t) = \begin{cases} t \cdot \frac{f_1 - f_0}{t_1} + f_0 & \text{if } t \in [0, t_1] \\ \vdots \\ t \cdot \frac{f_{i+1} - f_i}{t_{i+1} - t_i} + \frac{t_{i+1} \cdot f_i - t_i \cdot f_{i+1}}{t_{i+1} - t_i} & \text{if } t \in [t_i, t_{i+1}] \\ \vdots \\ t \cdot \frac{f_0 - f_n}{1 - t_n} + \frac{f_n - t_n \cdot f_0}{1 - t_n} & \text{if } t \in [t_n, 1] \\ \end{cases} \)
In the implementation, with \(N\) points total, the time parameterization uses uniform spacing: \(t_j = j \cdot \Delta t\) where \(\Delta t = \frac{\text{period}}{N-1}\) and \(j \in [0, N-1]\). The period is set to \(2\pi\), so \(\Delta t = \frac{2\pi}{N-1}\).
The Fourier series reconstruction \(f_r(t)\) approximates the original function using a finite number of harmonics:
\(\displaystyle f_r(t) = \sum_{n=-N}^{N} c_n \cdot e^{i \cdot 2n\pi t / \text{period}} \)
The Fourier coefficients \(c_n\) are computed using the integral:
\(\displaystyle c_n = \frac{1}{\text{period}} \int_{0}^{\text{period}} f(t) \cdot e^{-i \cdot 2n\pi t / \text{period}} \, dt \)
For the piecewise linear function, the integral is computed analytically for each segment. On each segment \(j\) from \(t_j\) to \(t_{j+1}\), the function is linear: \(f_j(t) = f_j^a \cdot t + f_j^b\), where \(f_j^a = \frac{f_{j+1} - f_j}{t_{j+1} - t_j}\) and \(f_j^b = \frac{t_{j+1} \cdot f_j - t_j \cdot f_{j+1}}{t_{j+1} - t_j}\).
The integral over segment \(j\) becomes:
\(\displaystyle \int_{t_j}^{t_{j+1}} f_j(t) \cdot e^{-i \cdot 2n\pi t / \text{period}} \, dt = f_j^a \int_{t_j}^{t_{j+1}} t \cdot e^{w_n t} \, dt + f_j^b \int_{t_j}^{t_{j+1}} e^{w_n t} \, dt \)
where \(w_n = -i \cdot \frac{2n\pi}{\text{period}}\). Using standard integration formulas:
\(\displaystyle \int e^{w_n t} \, dt = \frac{e^{w_n t}}{w_n}, \quad \int t \cdot e^{w_n t} \, dt = \frac{(w_n t - 1) \cdot e^{w_n t}}{w_n^2} \)
(When \(w_n = 0\), these simplify to \(t\) and \(\frac{t^2}{2}\) respectively.)
The coefficient is then computed as the sum over all segments:
\(\displaystyle c_n = \frac{1}{\text{period}} \sum_{j=0}^{N-2} \left[ f_j^a \left( \frac{(w_n t_{j+1} - 1) e^{w_n t_{j+1}} - (w_n t_j - 1) e^{w_n t_j}}{w_n^2} \right) + f_j^b \left( \frac{e^{w_n t_{j+1}} - e^{w_n t_j}}{w_n} \right) \right] \)
This analytical approach avoids numerical integration errors and provides exact results for piecewise linear functions.