# Fourier series of some sawtooth functions

Given a periodic function $$f:[0,2\pi] \rightarrow \mathbb{R}$$ one calculates the Fourier coefficients $$a_k$$ and $$b_k$$ by

$$a_k \sim \int_0^{2\pi}f(t)\cos(kt)\mathrm{d}t$$

$$b_k \sim \int_0^{2\pi}f(t)\sin(kt)\mathrm{d}t$$

In turn one has $$f(t) = a(t) + b(t)$$ with

$$a(t) \sim \sum_{k=0}^\infty a_k\cos(kt)$$

$$b(t) \sim \sum_{k=0}^\infty b_k\sin(kt)$$

This is a case of round-tripping, i.e. $$a(t)$$ and $$b(t)$$ and thus $$f(t)$$ are perfectly reproduced.

But then I found a case where this seems to break, and I wonder if a) I made a mistake in my calculations (or assumptions) or b) this is an exceptional case which has an explanation.

For the triangle function $$f_\triangle(t) = a_\triangle(t) + b_\triangle(t)$$ (shown below in the middle) everything looks fine.

The impurities in the "reconstructions" are due to the fact that I evaluated the sum only up to $$k = 100$$.
$$a$$ corresponds to red, $$b$$ corresponds to blue. The curve shown above in the middle is $$a(t) + ib(t)$$.

Also for the square function $$f_\square(t) = a_\square(t) + b_\square(t)$$ everything looks fine.

But for the sawtooth function $$f_{/\!|}(t) = a_{/\!|}(t) + b_{/\!|}(t)$$ samething strange happens. While $$b_{/\!|}(t)$$ (blue) is reproduced, $$a_{/\!|}(t)$$ (red) isn't:

How can this be?

Once again:

Did I make a mistake in my calculations (or assumptions) or is this a somehow exceptional case which has an explanation?

This is how I defined the sawtooth function $$f_{/\!|}(t) = a_{/\!|}(t) + b_{/\!|}(t)$$ (black, for the sake of convenience with $$f:[0,1] \rightarrow \mathbb{R}$$):

$$b_{/\!|}(t) = \begin{cases} t & \text{ for } t < \frac{1}{2}\\ t-1 & \text{ otherwise} \end{cases}$$

$$a_{/\!|}(t)$$ (red) is just $$b_{/\!|}(t)$$ (blue) shifted by $$\frac{1}{4}$$ to the left:

$$a_{/\!|}(t) = \begin{cases} t + \frac{1}{4} & \text{ for } t < \frac{1}{4}\\ t - \frac{3}{4} & \text{ otherwise} \end{cases}$$

• You should provide the definition you take for the sawtooth function : is it $f(t)=t$ on $(-\pi,\pi)$ ? i.e., an odd function or do you take it as an even function ? Commented Apr 3, 2019 at 11:10
• @JeanMarie: I guess, only my sawtooth function $b$ is an odd function, while $a$ is neither odd nor even, and so is $f$ (see my edit). May this be the "problem"? Commented Apr 3, 2019 at 11:59
• I am puzzled by the period you take : is it $T=1$ (as in your last picture) or $T=2\pi$ as in the previous figures ? Commented Apr 3, 2019 at 12:30
• @JeanMarie: I noted in the edit, that I switched to $T=1$ to simply the formulas, so I made the replacement $t \rightarrow t/2\pi$. This should not pose any severe problem, does it? Commented Apr 3, 2019 at 12:39
• I imagine that you have obtained explicitly formulas for coefficients $a_k$ and $b_k$ ? What are they ? Commented Apr 3, 2019 at 15:34

As it is rather hard for me to enter into your conventions, I thought the best thing I could do is to show you how I compute the coefficients of such a series.

I am going to stick to formulas (Eqn. 2) and (Eqn. 4) for a periodic function $$f$$ with period $$P$$ that can be found in the excellent Wikipedia article : https://en.wikipedia.org/wiki/Fourier_series.

I copy them here :

\displaystyle {\begin{aligned}a_{n}&={\frac {2}{P}}\int _{x_{0}}^{x_{0}+P}f(x)\cdot \cos \left({\tfrac {2\pi nx}{P}}\right)\ dx\\b_{n}&={\frac {2}{P}}\int _{x_{0}}^{x_{0}+P}f(x)\cdot \sin \left({\tfrac {2\pi nx}{P}}\right)\ dx,\end{aligned}}

giving the truncated series at rank $$N$$ equal to :

\displaystyle {\begin{aligned}s_{N}(x)&=a_{0}/2+\sum _{n=1}^{N}\left(a_{n}\cos \left({\tfrac {2\pi nx}{P}}\right)+b_{n}\sin \left({\tfrac {2\pi nx}{P}}\right)\right).\end{aligned}}

Let us define our sawtooth function $$f$$ as the periodic function with period $$P=1$$ defined on $$[-\frac12,+\frac12]$$ by $$f(x)=x$$ (see Fig. below).

As this function is odd in general and on this interval in particular, all $$a_n$$ coefficients are zero. The $$b_n$$ coefficients are readily computed (using integration by parts) as

$$b_n=\dfrac{(-1)^{n+1}}{\pi n}$$

(we have taken $$x_0=0$$).

For example, when we take the 3 first harmonics :

$$s_3(x)=\dfrac{1}{\pi}\left(\sin(2\pi x)-\dfrac{1}{2}\sin(4\pi x)+\dfrac{1}{3}\sin(6\pi x)\right)$$

Here is the graphical representation of $$s_3$$, in black, and the reference curve of $$f$$ in red :

Edit : If you shift function $$f$$ by $$1/4$$, I think that the error not to be done is to compute for example $$a_n$$ coefficients by the following formula :

$$a_n = 2 \int_{-1/2}^{1/2}(x+1/4) \cos(2 \pi n x) dx$$

because in this case, you aren't anymore working with a function whose values are in $$[-1/2,1/2]$$ but in a different interval. Instead, you have to take

$$a_n = 2 \int_{-1/2}^{1/4}(x+1/4) \cos(2 \pi n x) dx+ 2\int_{1/4}^{1/2}(x-3/4) \cos(2 \pi n x) dx$$

I find the following coefficients in this case :

$$a_n=\frac{2}{\pi n}\sin(\pi n/2)\sin(\pi n/4)^2$$

$$b_n=\frac{1}{2\pi n}(1+(-1)^n)$$

I have checked that they give the adequate result.

• Thanks for taking the effort. But to be honest: $b_n$ was not the problem. Can you please do the same with $f$ shifted by $1/4$ to the left? Commented Apr 4, 2019 at 6:12
• I guess, it was not the wrong integration interval I had chosen but the wrong function I integrated over (which you could not know and see): I calculate $a_k = \int a(t)\cos(kt)\mathrm{d}t$ instead of $a_k = \int (a(t)+b(t))\cos(kt)\mathrm{d}t$ (same for $b_k$). Nevertheless you gave me a hint on that in the comment you later removed. (It was a good explanation!) Commented Apr 4, 2019 at 9:45
• Happy end, anyhow. Commented Apr 4, 2019 at 10:18
• Yes, that's true. Thanks again! Commented Apr 4, 2019 at 10:19

When - correctly - calculating $$a_k$$ and $$b_k$$ by

$$a_k \sim \int_0^{2\pi}(a(t) + b(t))\cos(kt)\mathrm{d}t$$

$$b_k \sim \int_0^{2\pi}(a(t) + b(t))\sin(kt)\mathrm{d}t$$

and not by

$$a_k \sim \int_0^{2\pi}a(t)\cos(kt)\mathrm{d}t$$

$$b_k \sim \int_0^{2\pi}b(t)\sin(kt)\mathrm{d}t$$

(as I did), the pictures look better:

It's $$f(t) = a(t) + b(t)$$ that is reproduced, not $$a(t)$$ and $$b(t)$$ separately.

Interestingly, for the triangle and the square case (with odd and even $$a(t)$$, $$b(t)$$) it doesn't make a difference.