Fourier series coefficients proof

Starting with $$f(x) = \frac{a_0}{2}+\sum_{n=1}^\infty [a_n \cos(nx) + b_n \sin(nx)],$$ to find the $b_n$, multiply both sides of the equation by $\sin(mx)$ for an arbitrary but fixed positive integer $m$, and integrate from $x=0$ to $x=\pi$. Using the fact that $$\int_0^\pi \sin(nx)\sin(mx)\,dx=\begin{cases}0, &n\not=m,\\ {\pi\over 2}, &n=m,\end{cases}$$ you get $b_m={2\over \pi}\int_0^\pi f(x)\sin(mx)\,dx$ for each $m=1,2,\dots$ Since $m$ was arbitrary, you can change this to an $n$ and get the formula for the coefficients for the sine terms.

A similar argument for the cosine terms establishes the formula for the $a_n$. Here, use the fact that $$\int_0^\pi \cos(nx)\cos(mx)\,dx=\begin{cases} 0, &n\not=m,\\ {\pi\over 2}, &n=m,\end{cases}$$ which leads to $$a_n={2\over \pi}\int_0^\pi f(x)\cos(nx)\,dx, \quad n=0,1,2,\dots$$

Note: The only purpose of the factor ${1\over 2}$ on the $a_0$ term is so that the formula for $a_0$ will match the pattern of the formula for $n=1,2,\dots$