As Don Antonio as spoken about, Fourier series exist for periodic functions. However, if you are only trying to represent a function $f$ on a finite interval $[a,b]$, you can just pretend that $f$ is periodic and $[a,b]$ is contained in one period.
For simplicity, assume that $f$ has period $2\pi$, and you want to represent it on $[-\pi, \pi]$. If $f$ is representable as a Fourier series, then $$f(\theta) = \sum_{n=0}^\infty A_n\cos n\theta + \sum_{n=1}^\infty B_n\sin n\theta$$
Now there are some useful trigonometric integral formulas for integer $m,n$:
$$\int_{-\pi}^\pi\cos n\theta\,d\theta = \begin{cases}0& n \ne 0\\2\pi & n = 0\end{cases}$$
$$\int_{-\pi}^\pi\sin n\theta\,d\theta = 0$$
$$\int_{-\pi}^\pi\cos n\theta\cos m\theta\,d\theta =\int_{-\pi}^\pi\sin n\theta\sin m\theta\,d\theta = \begin{cases}0& n\ne m\\\pi & n = m\ne 0\end{cases}$$
$$\int_{-\pi}^\pi\cos n\theta\sin m\theta\,d\theta = 0$$
(Because sine and cosine are periodic, these actually hold for any interval of integration of width $2\pi$.) This happens because other than the exceptional cases, the integrands are all sinusoidal with $y=0$ as the midline, and symmetry matches the area below the midline to the area above, so it all cancels out.
So if we integrate
$$\begin{align}\int_{-\pi}^\pi f(\theta)\,d\theta &= \sum_{n=0}^\infty A_n\int_{-\pi}^\pi\cos n\theta\,d\theta + \sum_{n=1}^\infty B_n\int_{-\pi}^\pi\sin n\theta\,d\theta \\&= 2\pi A_0 + 0\end{align}$$
and for $m > 0$,
$$\begin{align}\int_{-\pi}^\pi f(\theta)\cos m\theta\,d\theta &= \sum_{n=0}^\infty A_n\int_{-\pi}^\pi\cos n\theta\cos m\theta\,d\theta + \sum_{n=1}^\infty B_n\int_{-\pi}^\pi\sin n\theta\cos m\theta\,d\theta\\&=A_m\pi + 0\end{align}$$
$$\begin{align}\int_{-\pi}^\pi f(\theta)\sin m\theta\,d\theta &= \sum_{n=0}^\infty A_n\int_{-\pi}^\pi\cos n\theta\sin m\theta\,d\theta + \sum_{n=1}^\infty B_n\int_{-\pi}^\pi\sin n\theta\sin m\theta\,d\theta\\&=0 + B_m\pi\end{align}$$
So you can find the coefficients by $$A_0 = \frac 1{2\pi} \int_{-\pi}^\pi f(\theta)\,d\theta\\ A_m = \frac 1\pi \int_{-\pi}^\pi f(\theta)\cos m\theta\,d\theta, \quad m > 0$$ and $$B_m = \frac 1\pi \int_{-\pi}^\pi f(\theta)\sin m\theta\,d\theta, \quad m > 0$$
For functions with different periods, you just have to introduce a scaling factor.
I know that two functions are orthogonal if their product integral over interval is 0 and hence -pi to pi is chosen. However what interval would you recommend? is there better intervals?
– tryst with freedom May 24 '20 at 11:11