Problem with Fourier series I'm trying to find the Fourier series of the function defined on the interval $(-2,2)$
$$
f(x)=\begin{cases}
0,& \,\,\, |x| <1 \\
1, & \,\,\, 1<|x|<2
\end{cases}
$$
This should be trivial. I have calculated the components to be
\begin{eqnarray*}
a_0&=&\frac{1}{4}\int _{-2}^2f(x)\mathrm{d}x=\frac{1}{4}\left(\int _{-2}^{-1}\mathrm{d}x+\int _1^2\mathrm{d}x\right)=\frac{1}{4}((-1-(-2))+2-1)=\frac{1}{2}\\
a_n&=&\frac{1}{2}\int _{-2}^2f(x)\cos \frac{n\pi x}{2}\mathrm{d}x=\frac{1}{2}\left(\int _{-2}^{-1}\cos \frac{n\pi x}{2}\mathrm{d}x+\int _1^2 \cos \frac{n\pi x}{2}\mathrm{d}x\right)\\
&=&\frac{1}{2}\cdot \frac{2}{n\pi }\left(-\sin \frac{n\pi }{2}+\sin  n\pi +\sin n\pi -\sin \frac{n \pi }{2}\right)=-\frac{2(-1)^{n+1}}{n\pi }=\frac{2(-1)^n}{n\pi }
\end{eqnarray*}
which give the following series expansion
$$f(x)=\frac{1}{2}+\frac{2}{\pi }\sum _{n=1}^{\infty }\frac{(-1)^n\cos \frac{n\pi x}{2}}{n}$$
But when I plot the function for the first 1000 coefficients I get

which doesn't look good. I have went through the calculation many times and I can't find the mistake.

$$\frac{1}{2}\int_{1}^{2}\sin\left(\frac{\pi n x}{2}\right)\,dx = \frac{1}{n\pi}\left(\cos\frac{n\pi}{2}-\cos(n\pi)\right)$$
$$\frac{1}{2}\int_{1}^{2}\cos\left(\frac{\pi n x}{2}\right)\,dx = -\frac{1}{n\pi}\,\sin\frac{n\pi}{2}$$
hence:

$$\begin{eqnarray*} f(x) = \frac{1}{4}&+&\sum_{m\geq 0}\frac{(-1)^{m+1}}{(2m+1)\pi}\,\cos\left(\frac{(2m+1)\pi x}{2}\right)\\&+&\sum_{m\geq 0}\left(\frac{\sin\frac{(4m+1)\pi x}{2}}{(4m+1)\pi}-\frac{\sin\frac{(4m+2)\pi x}{2}}{(2m+1)\pi}+\frac{\sin\frac{(4m+3)\pi x}{2}}{(4m+3)\pi}\right).\end{eqnarray*}$$

Here it is the partial sum till $m=10$:
$\hspace1in$
