Derivation (Fourier series from complex Fourier series)
We split the complex Fourier series up into $\Z = \set{n<0} \cup \set{n=0} \cup \set{n>0}$.
$$ \begin{align*} \sum_{n\in\Z} \hat f_n e^{2\pi in\theta/L} &= \hat f_0 + \sum_{n=1}^\infty \left(\hat f_n e^{2\pi i n \theta/L} + \hat f_{-n} e^{-2\pi in\theta/L}\right) \\ &= \hat f_0 + \sum_{n=1}^\infty \left((\hat f_n + \hat f_{-n}) \cos \frac{2\pi i n\theta}{L} + i(\hat f_n - \hat f_{-n}) \sin \frac{2\pi i n \theta}{L}\right) \end{align*} $$
We define for $n\geq 0$ $$ \begin{align*} a_n &= \hat f_n + \hat f_{-n}, \\ b_n &= i(\hat f_n - \hat f_{-n}). \end{align*} $$