As a hobby project I'm trying to implement something with a Fourier series for an arbitrary function. I have a library that can integrate real functions numerically. I'm using the complex notation for a Fourier series, so I had to rewrite the expression for $c_n$ to: $$ c_n = \int_0^1 r(t) \cos{\lambda_n(t)} dt + i \int_0^1 r(t) \sin{\lambda_n(t)} dt $$ where $\lambda_n(t) \equiv \phi(t) - 2 \pi n t$ and some original function $f(t) = a(t) + i b(t) = r(t) \exp{i \phi(t)}$.
Then, I calculate some number $N$ coefficients and determine: $$ g(t) = \sum_{n=-N}^{+N} c_n \exp{2 \pi i n t} $$
Plotting this function $g(t)$ in white and the original function $f(t)$ in red, this results in:

As can be seen, the approximation has some weird curvature. I suspect this is caused by the boundary conditions or the fact that it is periodic and "jumps" from start to end. Any idea what is wrong here? I would like to achieve a similar result as in the last 3B1B video.
Edit: Additional discovery: for $N >> 1$ this effect blows up. See this for $N=75$:


This issue was caused by using a wrong integration algorithm. Just using https://en.wikipedia.org/wiki/Trapezoidal_rule with small steps (1e-4) MASSIVELY improves the results.