Using summation gives widly different function approximation than integration

83 Views Asked by At

I am trying to create a discrete fourier series that describes a data set.

You may have to look at this desmos I made to fully understand what I am talking about here.

When I calculate coefficients like

$$ \begin{align} a_0&=\frac{1}{2\pi }\sum _{i=1}^{N-1}y_i\Delta x \\ a_k&=\frac{1}{\pi }\sum _{i=1}^{N-1}y_i\cos \left(kx_i\right)\Delta x \\ b_k&=\frac{1}{\pi }\sum _{i=1}^{N-1}y_i\sin \left(kx_i\right)\Delta x \\ \end{align} $$

where $N$ is number of data points I have, and $x$ and $y$ are my data values, in my case:

$$ \begin{array}{|c|c|} \hline \text{x} & \text{y} \\ \hline -3.14 & -8.87 \\ \hline -2.44 & -4.97 \\ \hline -1.75 & -2.05 \\ \hline -1.05 & -0.10 \\ \hline -0.35 & 0.88 \\ \hline 0.35 & 0.88 \\ \hline 1.05 & -0.10 \\ \hline 1.75 & -2.05 \\ \hline 2.44 & -4.97 \\ \hline 3.14 & -8.87 \\ \hline \end{array} $$

But, using these values for the coefficients gives a really ugly $F(x)$:

$$ F\left(x\right)=a_0+\sum _{k=1}^8\left(a_k\cos \left(kx\right)+b_k\sin \left(kx\right)\right) $$ enter image description here

However, if I change the summations to (IMO, equivalent) integrations, the approximation becomes sooooo much better:

$$ \begin{align} a_0&=\frac{1}{2\pi }\int _{-\pi }^{\pi }f\left(x\right)dx \\ a_k&=\frac{1}{\pi }\int _{-\pi }^{\pi }f\left(x\right)\cos \left(kx\right)dx \\ b_k&=\frac{1}{\pi }\int _{-\pi }^{\pi }f\left(x\right)\sin \left(kx\right)dx \\ \end{align} $$ enter image description here


What am I doing wrong in the case of the summations to get that weird result? How do I fix this?