I am trying to approximate the function $\delta(x)$ on an interval $L=[-a,a]\subset[-\pi,\pi]$ using a linear combination of $A=\{1, \cos(x),\cos(2x),\dots,\cos(nx)\}$. I want to have a better approximation than this by restricting the approximation to be within $L$.
If $L=[-\pi,\pi]$, I can use the Fourier series. Inspired by this, I used the Gram-Schmidt algorithm to find an orthonormal basis using $A$ on $[-a,a]$. I then tried to expand these into an orthogonal series to approximate $\delta(x)$ in the same way the Fourier series coefficients are found.
However, summing the series with $a=0.5$ gave a flat region without a central peak and huge peaks at around $\pm \pi$, at least according to the program I wrote.
Why does this technique not work? Can I do something to remedy it?
I'm not super sure what you exactly did, but what I think you are looking for is known as the Dirichlet kernel:
$$ D_n(x) = \sum_{k=-n}^{+n}e^{ikx} = 1+2\sum_{k=1}^n \cos(kx)$$
Then $\frac{1}{2\pi}D_n$ converges against a $2\pi$ period verion of the Dirac delta aka Dirac comb.