<s>[INST] Find the Fourier Series for $f(x)=$ { $1$ from $-\frac{\pi}{2} < x < \frac{\pi}{2}$ , $-1$ from $\frac{\pi}{2} < x < \frac{3\pi}{2}$ } I'm trying to find the Fourier Series for the following function, but I'm having trouble at some point. I'm hoping someone can give me a hand...
$
f(x) = 
     \begin{cases}
       1 \text{,} & \text{if }\quad -\frac{\pi}{2} < x < \frac{\pi}{2}\\
       -1 \text{,} & \text{if }\quad \frac{\pi}{2} < x < \frac{3\pi}{2}\\
     \end{cases}
$
Here is what I tried so far:
$f(x) = a_0 + \sum_{n=1}^{\inf}[a_n \cdot cos(n \cdot x) + b_n \cdot sin(n \cdot x)]$

Part (1): Finding $a_0$
$a_0 = \frac{1}{2\pi} \cdot \int_{-\pi}^{\pi}f(x) dx$
$a_0 = \frac{1}{2\pi} \cdot [\int_{\frac{-\pi}{2}}^{\frac{\pi}{2}}f(x)dx + \int_{\frac{\pi}{2}}^{\frac{3\pi}{2}}f(x)dx]$
$a_0 = \frac{1}{2\pi} \cdot [(1) \cdot(\frac{\pi}{2}) - (1) \cdot(-\frac{\pi}{2}) + (-1) \cdot (\frac{3\pi}{2}) - (-1) \cdot(\frac{\pi}{2})]$
$a_0 = \frac{1}{2\pi} \cdot [\frac{\pi}{2} + \frac{\pi}{2} - \frac{3\pi}{2} + \frac{\pi}{2}]$
$a_0 = 0$

Part (2): Finding $a_n$
$a_n = \frac{1}{\pi} \cdot \int_{-\pi}^{\pi}f(x) \cdot cos(n \cdot x) dx$
$a_n = \frac{1}{\pi} \cdot [\int_{\frac{-\pi}{2}}^{\frac{\pi}{2}}f(x) \cdot cos(n \cdot x) dx + \int_{\frac{\pi}{2}}^{\frac{3\pi}{2}}f(x) \cdot cos(n \cdot x) dx]$
$a_n = \frac{1}{\pi} \cdot [\int_{\frac{-\pi}{2}}^{\frac{\pi}{2}}(1) \cdot cos(n \cdot x) dx + \int_{\frac{\pi}{2}}^{\frac{3\pi}{2}}(-1) \cdot cos(n \cdot x) dx]$
$a_n = \frac{1}{\pi} \cdot [\frac{1}{n} \cdot sin(n \cdot \frac{\pi}{2}) - \frac{1}{n} \cdot sin(n \cdot \frac{-\pi}{2}) + (-1) \cdot \frac{1}{n} \cdot sin(n \cdot \frac{3\pi}{2}) - (-1) \cdot \frac{1}{n} \cdot sin(n \cdot \frac{\pi}{2})]$
$a_n = \frac{1}{n\pi} \cdot [sin(n \cdot \frac{\pi}{2}) - sin(n \cdot \frac{-\pi}{2}) - sin(n \cdot \frac{3\pi}{2}) + sin(n \cdot \frac{\pi}{2})]$
since
$sin(n \cdot \frac{-\pi}{2}) = sin(n \cdot \frac{\pi}{2})$
and
$sin(n \cdot \frac{3\pi}{2}) = sin(n \cdot \frac{\pi}{2})$
then
$a_n = 0$

Part (3): Finding $b_n$
$b_n = \frac{1}{\pi} \cdot \int_{-\pi}^{\pi}f(x) \cdot sin(n \cdot x) dx$
$b_n = \frac{1}{\pi} \cdot [\int_{\frac{-\pi}{2}}^{\frac{\pi}{2}}f(x) \cdot sin(n \cdot x) dx + \int_{\frac{\pi}{2}}^{\frac{3\pi}{2}}f(x) \cdot sin(n \cdot x) dx]$
$b_n = \frac{1}{\pi} \cdot [\int_{\frac{-\pi}{2}}^{\frac{\pi}{2}}(1) \cdot sin(n \cdot x) dx + \int_{\frac{\pi}{2}}^{\frac{3\pi}{2}}(-1) \cdot sin(n \cdot x) dx]$
$b_n = \frac{1}{\pi} \cdot [\frac{1}{n} \cdot -cos(n \cdot \frac{\pi}{2}) - \frac{1}{n} \cdot -cos(n \cdot \frac{-\pi}{2}) + (-1) \cdot \frac{1}{n} \cdot -cos(n \cdot \frac{\pi}{2}) - (-1) \cdot \frac{1}{n} \cdot -cos(n \cdot \frac{3\pi}{2})]$
$b_n = \frac{1}{n\pi} \cdot [-cos(n \cdot \frac{\pi}{2}) + cos(n \cdot \frac{-\pi}{2}) + cos(n \cdot \frac{\pi}{2}) - cos(n \cdot \frac{\pi}{2})]$
since
$cos(n \cdot \frac{-\pi}{2}) = cos(n \cdot \frac{3\pi}{2})$
then
$b_n = 0$

I can't figure out where did I missed something. I was able to do a similar problem, but in the case where $f(x)$ is defined in two sets from $-\pi < x < 0$ and $0 < x < \pi$, but in such case, some terms got simplified because of the presence of the $0$. But here, this doesn't happen...
 [/INST] Thanks to MyGlasses I was able to finish the exercise. I'll leave the full answer here for future reference.
$
f(x) = 
     \begin{cases}
       1 \text{,} & \text{if }\quad -\frac{\pi}{2} < x < \frac{\pi}{2}\\
       -1 \text{,} & \text{if }\quad \frac{\pi}{2} < x < \frac{3\pi}{2}\\
     \end{cases}
$
$f(x) = a_0 + \sum_{n=1}^{\inf}[a_n \cdot cos(n \cdot x) + b_n \cdot sin(n \cdot x)]$

Part (1): Finding $a_0$
$a_0 = \frac{1}{2\pi} \cdot \int_{-\pi}^{\pi}f(x) dx$
$a_0 = \frac{1}{2\pi} \cdot [\int_{\frac{-\pi}{2}}^{\frac{\pi}{2}}f(x)dx + \int_{\frac{\pi}{2}}^{\frac{3\pi}{2}}f(x)dx]$
$a_0 = \frac{1}{2\pi} \cdot [(1) \cdot(\frac{\pi}{2}) - (1) \cdot(-\frac{\pi}{2}) + (-1) \cdot (\frac{3\pi}{2}) - (-1) \cdot(\frac{\pi}{2})]$
$a_0 = \frac{1}{2\pi} \cdot [\frac{\pi}{2} + \frac{\pi}{2} - \frac{3\pi}{2} + \frac{\pi}{2}]$
$a_0 = 0$

Part (2): Finding $a_n$
$a_n = \frac{1}{\pi} \cdot \int_{-\pi}^{\pi}f(x) \cdot cos(n \cdot x) dx$
$a_n = \frac{1}{\pi} \cdot [\int_{\frac{-\pi}{2}}^{\frac{\pi}{2}}f(x) \cdot cos(n \cdot x) dx + \int_{\frac{\pi}{2}}^{\frac{3\pi}{2}}f(x) \cdot cos(n \cdot x) dx]$
$a_n = \frac{1}{\pi} \cdot [\int_{\frac{-\pi}{2}}^{\frac{\pi}{2}}(1) \cdot cos(n \cdot x) dx + \int_{\frac{\pi}{2}}^{\frac{3\pi}{2}}(-1) \cdot cos(n \cdot x) dx]$
$a_n = \frac{1}{\pi} \cdot [\frac{1}{n} \cdot sin(n \cdot \frac{\pi}{2}) - \frac{1}{n} \cdot sin(n \cdot \frac{-\pi}{2}) + (-1) \cdot \frac{1}{n} \cdot sin(n \cdot \frac{3\pi}{2}) - (-1) \cdot \frac{1}{n} \cdot sin(n \cdot \frac{\pi}{2})]$
$a_n = \frac{1}{n\pi} \cdot [sin(n \cdot \frac{\pi}{2}) - sin(n \cdot \frac{-\pi}{2}) - sin(n \cdot \frac{3\pi}{2}) + sin(n \cdot \frac{\pi}{2})]$
since
$sin(-x) = -sin(x) \implies sin(n \cdot \frac{-\pi}{2}) = -sin(n \cdot \frac{\pi}{2})$
and
$sin(n \cdot \frac{3\pi}{2}) = sin(n \cdot 2 \cdot \pi - n \cdot \frac{\pi}{2}) = sin(n \cdot 2 \cdot \pi) \cdot cos(n \cdot \frac{\pi}{2}) - cos(n \cdot 2 \cdot \pi) \cdot sin(n \cdot \frac{\pi}{2}) = -sin(n \cdot \frac{\pi}{2}) $
then
$a_n = \frac{1}{n\pi} \cdot [sin(n \cdot \frac{\pi}{2}) - (-sin(n \cdot \frac{\pi}{2})) - (-sin(n \cdot \frac{\pi}{2})) + sin(n \cdot \frac{\pi}{2})]$
$a_n = \frac{4}{n \cdot \pi}*sin(n \cdot \frac{\pi}{2})$

Part (3): Finding $b_n$
$b_n = \frac{1}{\pi} \cdot \int_{-\pi}^{\pi}f(x) \cdot sin(n \cdot x) dx$
$b_n = \frac{1}{\pi} \cdot [\int_{\frac{-\pi}{2}}^{\frac{\pi}{2}}f(x) \cdot sin(n \cdot x) dx + \int_{\frac{\pi}{2}}^{\frac{3\pi}{2}}f(x) \cdot sin(n \cdot x) dx]$
$b_n = \frac{1}{\pi} \cdot [\int_{\frac{-\pi}{2}}^{\frac{\pi}{2}}(1) \cdot sin(n \cdot x) dx + \int_{\frac{\pi}{2}}^{\frac{3\pi}{2}}(-1) \cdot sin(n \cdot x) dx]$
$b_n = \frac{1}{\pi} \cdot [\frac{1}{n} \cdot -cos(n \cdot \frac{\pi}{2}) - \frac{1}{n} \cdot -cos(n \cdot \frac{-\pi}{2}) + (-1) \cdot \frac{1}{n} \cdot -cos(n \cdot \frac{\pi}{2}) - (-1) \cdot \frac{1}{n} \cdot -cos(n \cdot \frac{3\pi}{2})]$
$b_n = \frac{1}{n\pi} \cdot [-cos(n \cdot \frac{\pi}{2}) + cos(n \cdot \frac{-\pi}{2}) + cos(n \cdot \frac{\pi}{2}) - cos(n \cdot \frac{3\pi}{2})]$
since
$cos(-x) = cos(x) \implies cos(n \cdot \frac{-\pi}{2}) = cos(n \cdot \frac{\pi}{2})$
and
$cos(n \cdot \frac{3 \pi}{2}) = cos(n \cdot 2\pi - n \cdot \frac{\pi}{2}) = cos(n \cdot 2\pi) \cdot cos(n \cdot \frac{\pi}{2}) - sin(n \cdot 2\pi) \cdot sin(n \cdot \frac{\pi}{2}) = cos(n \cdot \frac{\pi}{2})$
then
$b_n = \frac{1}{n\pi} \cdot [-cos(n \cdot \frac{\pi}{2}) + cos(n \cdot \frac{\pi}{2}) + cos(n \cdot \frac{\pi}{2}) - cos(n \cdot \frac{\pi}{2})]$
$b_n = 0$

Solution plot
from matplotlib import pyplot as plt
import numpy as np

def f(x):
    return np.where(
        np.logical_and(-np.pi / 2.0 < x, x < np.pi / 2.0),
        1.0,
        -1.0
    )

def f_(x, N):
    a0 = 0.0

    r = a0
    for n in range(1,N):
        an = 4. / (n * np.pi) * np.sin(n * np.pi / 2.)
        bn = 0.0
        r += an * np.cos(n * x) + bn * np.sin(n * x)

    return r

x = np.linspace(-np.pi / 2.0, 3 * np.pi / 2.0, 100)

y = f(x)
plt.plot(x, y)

y = f_(x, N=3)
plt.plot(x, y, '--')
y = f_(x, N=10)
plt.plot(x, y, '--')
y = f_(x, N=100)
plt.plot(x, y, '--')

plt.show()


</s>