26 June 2014

Fourier fun 1: Sums and square waves

Periodic functions

Please note that this post will assume a familiarity with summation notation. A brief explanation is given in my post on divergent series, which can be found here.

What is a Fourier series? In the simplest of terms, a Fourier series is a method of re-expressing (or approximating) any arbitrary periodic function, that is, a function that repeats itself over and over again (the length of the repeated interval is known as the period). This might sound very abstract, but it is an extremely powerful tool that asserts itself in very many fields, a couple of examples of which will be given later on. So how does it work? Let me show you.

In some sense, the most basic periodic function$^1$ is the sine function: $\sin$, although equally we could say it is the cosine function: $\cos$, which is the same as the sine function but shifted a quarter of a period across (see fig. 1). The natural expression of sine ($\sin(t)$) has a period of $2\pi$, or mathematically $\sin(t+2\pi)=\sin(t)$, but the function can be stretched or compressed to have an arbitrary period $T$ such that $\sin\left(\frac{2\pi}{T}t+T\right)=\sin\left(\frac{2\pi}{T}t\right)$. Note that for the specific case of $T=2\pi$ this reverts to the "natural" expression as expected. With this in mind, we can define a cosine with period $T$ in terms of sine as $\cos\left(\frac{2\pi}{T}t\right)=\sin\left(\frac{2\pi}{T}t+\frac{T}{4}\right)$.


Figure 1: $\sin(t)$ is shown in blue and $\cos(t)$ is shown in red. In this plot (and all
others except otherwise noted), the $t$-axis is shown in units of $\pi$ so that
$1.0$ corresponds to $\pi$, $2.0$ corresponds to $2\pi$, and so on. The fact that the
cosine function is related to the sine function by a quarter-period shift in the
negative direction is clearly demonstrated (recall that for $\sin(t)$ and $\cos(t)$ the
period $T$ is $2\pi$, so a quarter period shift is given by $T/4=2\pi/4=\pi/2$ or
$0.5$ in units of $\pi$ as shown on the plot).

This is all well and good, but there are many other periodic functions we might be interested in besides sine and cosine. One simple example is the square wave $S(t)$, which is the signal you get when, for example, you switch a DC power source on and off (and on and off and so on) at regular intervals. This is an example of a digital signal, and mathematically we would represent it as a function that oscillates evenly between $1$ and $-1$ over every period of length $T$ (see fig. 2)$^2$. What we will make special note of, however, is that the square wave looks very much like a flattened, widened $\sin(t)$ (assuming $S(t)$ is defined such that $T=2\pi$). This suggests the question of if and how we might get be able to get from a sine wave to a square wave.


Figure 2: $S(t)$ is shown in blue and $\sin(t)$ is shown in red. In this plot,
the similarity between the square wave (as we have defined it) and the
sine wave is apparent.

If we think additively, to get to $S(t)$ from $\sin(t)$ we need to add to the function near the zeroes (but not at them!) to amplify those regions and subtract from the function near the peaks to flatten them out. What's more, whatever function we add to $\sin(t)$ will also have to be periodic with a period that's an integer multiple of $2\pi$ so that the alterations we make occur everywhere at the right places (so that, for example, we don't add anything to our zero points). It just so happens that we find an excellent candidate in $\sin(3t)$—if we scale $\sin(t)$ and $\sin(3t)$ just right and then add them together, we get a little bit closer to $S(t)$. Repeating the process, we find we get closer if we add an appropriately scaled $\sin(5t)$ and closer again by adding more and more similar sine functions (see fig. 3).


Figure 3: For appropriately chosen amplitudes, the square wave $S(t)$ (shown in blue in the left column)
can be approximated by a sum of sine functions $\sin(nt)$ (shown in red in the left column). The right
column shows the sine functions used for each summation to the immediate left (the Fourier components).
The summations used (a) two components; (b) three components; (c) four components; and (d) five
components.

The square wave

As adding more and more sine functions improves our approximation to the square wave, we can reasonably assume$^3$ that as the number of sine functions we add approaches infinity, the approximation to the square wave becomes arbitrarily accurate. As we noted in the previous section, however, we are only interested in sine functions $\sin(nt)$ with odd values of $n$. Thus we can write our definition of the square wave as
\begin{equation} \label{eq:square1} S(t)=\sum_{n\mathrm{ odd}}b_n\sin(nt), \end{equation}
or equivalently,
\begin{equation} \label{eq:square2} S(t)=\sum^\infty_{m=1}b_m\sin\big((2m-1)t\big), \end{equation}
where in the second equation we have just replaced the positive odd numbers $n$ with the positive integers $m$ such that $2m-1=n$ is still always odd. The terms $b_n$ are the Fourier coefficients (think scaling factors or amplitudes) corresponding to each sine function $\sin(nt)$ such that the infinite sum can be expanded as $b_1\sin(t)+b_3\sin(3t)+b_5\sin(5t)+\ldots$.

Equations \ref{eq:square1} and \ref{eq:square2} are both examples of Fourier series. However, they are not very useful as we haven't found the scaling factors $b_n$. How do we find them? It turns out that's a little more complicated. The derivation for these terms involve a little bit of calculus and so is probably a little beyond the scope of this post, but I strongly encourage the interested reader to turn to the notes where I have given a sketched-out version.$^4$ The result we find, however, is that
\begin{equation} \label{eq:b} b_n = \frac{2}{T}\int_{t_0}^{t_0+T} \! f(t)\sin\left(\frac{2\pi nt}{T}\right) \, \mathrm{d}t \end{equation}
for the general case of a periodic function $f(t)$ with period $T$, where $t_0$ is any arbitrary $t$-value of our choosing. Note that as this applies to the general case, the $n$ in this expression could be odd or even.

Let us now calculate the $b_n$ terms for the square wave [A warning: this section requires a little calculus. It isn't as full-on as that required for the derivation which is why I have decided to include it here. If you have vague memories of calculus from high school you should be able to make it through with some care, but do not worry if you have some trouble or you can't follow it]. The first thing we must note is that both $S(t)$ and $\sin(2\pi nt/T)$ are odd functions, that is, they look upside-down when reflected across the $y$-axis (this is expressed mathematically as $f(-t)=-f(t)$). The product of two odd functions is an even function, that is, a function that looks the same when reflected across the $y$-axis (mathematically, $f(-t)=f(t)$; the cosine function is an example of an even function). That means that the product $S(t)\sin(2\pi nt/T)$ must be an even function. We can use this fact to our advantage! When we evaluate our integral from equation \ref{eq:b} we can choose $t_0=-T/2$ so that the integral ranges from $-T/2$ to $T/2$. Because the integrand is even, and therefore symmetric about $t=0$, the integral from $-T/2$ to $T/2$ is double the integral from $0$ to $T/2$: $\int_{-T/2}^{T/2}S(t)\sin(2\pi nt/T)\mathrm{d}t=2\int_{0}^{T/2}S(t)\sin(2\pi nt/T)\mathrm{d}t$ (recall that the integral calculates the area under the function). Using this fact and substituting in $T=2\pi$, equation \ref{eq:b} becomes
\begin{align} b_n &= \frac{4}{T}\int_{0}^{T/2} \! S(t)\sin\left(\frac{2\pi nt}{T}\right) \, \mathrm{d}t \nonumber \\ &= \frac{2}{\pi}\int_{0}^{\pi} \! S(t)\sin(nt) \, \mathrm{d}t. \end{align}
However, as we can see from fig. 2, between $t=0$ and $t=\pi$, $S(t)=1$. This equation now becomes simple to solve:
\begin{align*}b_n &= \frac{2}{\pi}\int_{0}^{\pi} \! \sin(nt) \, \mathrm{d}t \\&= \frac{2}{\pi}\bigg[-\cos(nt)\bigg]_0^\pi \\&= -\frac{2}{\pi}\big(\cos(\pi n)-\cos(0)\big).\end{align*}
As $\cos(0)=1$ and $\cos(\pi n)=\pm 1$ depending on whether or not $n$ is odd or even, we find that
\[b_n=\begin{cases}4/\pi n & \text{when } n \text{ is odd} \\0 & \text{when } n \text{ is even}\end{cases}\]
Thus, as expected, only odd $n$ terms contribute to the sum -- a very neat result to fall out! Now that we know the Fourier coefficients we know the Fourier series for the square wave (varying from $-1$ to $1$ with period $T=2\pi$) in full:
\begin{equation} S(t)=\frac{4}{\pi}\sum_{n\textrm{ odd}} \frac{1}{n} \sin(nt). \end{equation}

This is the first part in a 3-part series on some of the basics of Fourier analysis. Part 2 can be found here.

Notes

$1$. Here "most basic" refers both to the fact that sine and cosine are typically the only periodic functions taught in school and the more fundamental significance of its relationship to the unit circle.

$2$. Of course, the amplitude of the square wave is only a matter of convention (likewise the period); my preferred representation varies between $0$ and $1$ instead of $-1$ and $1$, but the transformation from the main body text version to my preference is simple: $S(t)\mapsto(S(t)+1)/2$. In the body text I used the $-1$ to $1$ form only because it is more immediately relatable to $\sin(t)$ and therefore, I hope, the Fourier sum becomes less conceptually difficult.

$3$. Of course, the convergence of Fourier series has been proven rigorously, but I leave the investigation of that topic to the interested reader. For the examples shown in this post, convergence can be inferred reasonably safely from the appearance of an ever-improving approximation, except notably at non-differentiable (sharp or discontinuous) points.

$4$. Suppose we have a function $f(t)$ that can be expressed purely as a sum of sine functions, similar to the case of $S(t)$. For this arbitrary periodic function we then have \begin{equation*}f(t)=b_1\sin(\omega_1t)+b_2\sin(\omega_2t)+b_3\sin(\omega_3t)+\ldots,\end{equation*} where $\omega_n=2\pi n/T$. Multiplying both sides by $\sin(\omega_nt)$ gives
\begin{align*}f(t)\sin(\omega_nt)&=(b_1\sin(\omega_1t)+b_2\sin(\omega_2t)+b_3\sin(\omega_3t)+\ldots)\sin(\omega_nt) \\ &=b_1\sin(\omega_1t)\sin(\omega_nt)+b_2\sin(\omega_2t)\sin(\omega_nt)+b_3\sin(\omega_3t)\sin(\omega_nt)+\ldots\end{align*}
We then wish to integrate both sides over one period $T$, giving
\begin{equation*} \int_{t_0}^{t_0+T} \! f(t)\sin(\omega_nt) \, \mathrm{d}t=\int_{t_0}^{t_0+T} \! \left(b_1\sin(\omega_1t)\sin(\omega_nt)+b_2\sin(\omega_2t)\sin(\omega_nt)+\ldots\right) \, \mathrm{d}t\end{equation*}
where $t_0$ is any arbitrary $t$-value we wish to choose to begin our integration at (because the function is periodic and the integration is over a period, the choice of starting point is not important).
We now consider the trigonometric identity $\sin(\theta)\sin(\phi)=(\cos(\theta-\phi)-\cos(\theta+\phi))/2$. Using $\omega_n$ and $\omega_m$ in place of $\theta$ and $\phi$ respectively we can integrate over a period beginning at $t_0=0$ (without loss of generality) to give
\begin{align*}\int_{0}^{T} \! \sin(\omega_nt)\sin(\omega_mt) \, \mathrm{d}t &= \int_{0}^{T} \! \sin\left(\frac{2\pi nt}{T}\right)\sin\left(\frac{2\pi mt}{T}\right) \, \mathrm{d}t \\ &= \frac{1}{2} \int_{0}^{T} \! \cos\left(\frac{2\pi (n-m)t}{T}\right)-\cos\left(\frac{2\pi (n+m)t}{T}\right) \, \mathrm{d}t.\end{align*}
We now consider two cases:
$1) \: n=m$
\begin{align*}\int_{0}^{T} \! \sin^2(\omega_nt) \, \mathrm{d}t &= \frac{1}{2}\int_{0}^{T} \! 1-\cos\left(\frac{4\pi nt}{T}\right) \, \mathrm{d}t \\ &= \frac{1}{2}\left[t-\frac{T}{4\pi n}\sin\left(\frac{4\pi nt}{T}\right)\right]^T_0 \\ &= \frac{T}{2}-\frac{T}{8\pi n}\sin(4\pi n) = \frac{T}{2} \end{align*}
as $n$ is a non-zero integer and so $\sin(4\pi n)=0$ for any $n$.
$2) \: n\neq m$
\begin{align*} \int_{0}^{T} \! \sin(\omega_nt)\sin(\omega_mt) \, \mathrm{d}t &= \frac{1}{2}\int_{0}^{T} \! \cos\left(\frac{2\pi (n-m)t}{T}\right)-\cos\left(\frac{2\pi (n+m)t}{T}\right) \, \mathrm{d}t \\ &= \frac{1}{2}\left[\frac{T}{2\pi (n-m)}\sin\left(\frac{2\pi (n-m)t}{T}\right)\right. \\ &\qquad \left.-\frac{T}{2\pi (n+m)}\sin\left(\frac{2\pi (n+m)t}{T}\right)\right]^T_0 \\ &= \frac{T}{4\pi}\left(\frac{1}{(n-m)}\sin(2\pi (n-m))-\frac{1}{(n+m)}\sin(2\pi (n+m))\right) \\ &=0 \end{align*}
as $n$ and $m$ are non-zero integers and so $\sin(2\pi (n\pm m))=0$ for any $n$ and $m$.
These two cases can be summarised as $\int_{0}^{T} \! \sin(\omega_nt)\sin(\omega_mt) \, \mathrm{d}t = T\delta_{nm}/2$ where $\delta_{nm}$ is the Kronecker delta, which gives $1$ when $n=m$ and $0$ otherwise. This property is known as 'orthogonality', and the set of sine functions of the form $\sin(\omega_nt)$ provides an example of such orthogonal functions.
We can use this orthogonality result to evaluate the integral:
\begin{align*} \int_{t_0}^{t_0+T} \! f(t)\sin(\omega_nt) \, \mathrm{d}t &= \int_{t_0}^{t_0+T} \! \left(b_1\sin(\omega_1t)\sin(\omega_nt)+b_2\sin(\omega_2t)\sin(\omega_nt)+\ldots\right) \, \mathrm{d}t \\ &=\int_{t_0}^{t_0+T} \! b_n\sin^2(\omega_nt)  \, \mathrm{d}t \\ &= b_n\frac{T}{2} \\ \Rightarrow b_n &= \frac{2}{T}\int_{t_0}^{t_0+T} \! f(t)\sin(\omega_nt) \, \mathrm{d}t \end{align*}
where in going to the second line we have used the fact that $\int_{t_0}^{t_0+T} \! b_n\sin(\omega_nt)\sin(\omega_mt)  \, \mathrm{d}t=0$ for all $n\neq m$, leaving only the $n=m$ case relevant, the result from which is used in going to the third line.

0 comments :

Post a Comment