Signals and Systems Part 2: Fourier Series

Lesson 5: The Fourier Coefficients and Series


Hacking away at the squarewave

In the previous lesson we saw that there is a sinewave, literally speaking, contained within a square wave. Quantitatively, we can reduce the energy of a signal by subtracting off an appropriately-scaled sinewave (with the same period). But how far can we go with this? Are there other sinewaves of different frequencies hiding in our square wave? Let's find out. Let's take our left-over signal, and use the equation we figured out in the previous lesson to answer this question. Remember that the signal we are working with has a period of \(2\pi\), so I'm going to set \(P\) equal to \(2\pi\). Then, if we want to find out whether the left-over signal has any higher-frequency sinewaves in it, we just need to use the formula we figured out last time:

\begin{equation} \frac{1}{\pi}\langle (sq(x)-sin(x),sin(2x) \rangle \end{equation}

But since integration is linear, the inner product is also a linear operation, and so we can separate it out into two parts:

\begin{equation} \frac{1}{\pi}\langle sq(x),sin(2x) \rangle - \frac{1}{\pi}\langle (sin(x), sin(2x) \rangle \end{equation}

Fortunately, one of these terms we already know (the right-most one)- it's zero! Remember that sinewaves of integer multiple frequencies are orthogonal to each other. Whew! I thought we were actually going to have to do math. So we're just left with the following equation for how much \(sin(2x)\) there is in our left over signal.

\begin{equation} \frac{1}{\pi}\langle sq(x),sin(2x) \rangle \end{equation}

Notice that this is identical to the amount of \(sin(2x)\) in our original signal. So if we want to slowly chip off one sinewave after another from our square wave, we can just work directly with the original signal, rather than having to subtract off one more part each time. That makes life much easier! Let's plot these two signals and see if we can guess how much there is in our square wave:

Look at this closely - does this remind you of anything? Maybe something back in our lesson on orthogonal functions? It looks like if you were to multiply these two signals, for every positive region there is an equal and opposite negative region, and so they cancel each other out when you do the integration. You can check this for yourself mathematically, just to make sure the integral is actually zero. You should find that it is.

On to the next one!

What about \(sin(3x)\)? \(sin(4x)\)? We can do this all day. But how about we save ourselves some time, and let's find how much of the n-th sinewave \(sin(n x)\) is contained in our square wave.

\begin{equation} \frac{1}{\pi}\langle sq(x),sin(nx) \rangle = \frac{1}{\pi}\int_{0}^{2\pi} sq(x)*sin(nx)dx \end{equation}

If you actually do this integral, and simplify it a bit since n is an integer, you'll get

\begin{equation} b_n = \frac{2}{n \pi}\left(1 - cos(n\pi) \right) \end{equation}

Let's just check real quick to make sure this makes sense with what we have already tried - for \(n=1\), which we went over in the previous lesson, we know the answer should be \(4/\pi\), and indeed, that is what this spits out. If you try \(n=2\) you will find that this gives you \(0\), also as expected from just above. These are called Fourier Coefficients, and denoted with the symbol \(b_n\). Why \(b\)? Well, because there's also an \(a\) and a \(c\), which we will get to later :).

What are the Fourier coefficients \(b_5\) and \(b_6\)?





Now for the really crazy part. Imagine you keep subtracting higher and higher frequency sinewaves. Eventually, in the limit as you subtract an infinite number of sinewaves, the remaining signal energy is exactly zero. Not close to zero. Actually zero. This means that our square wave (which, let me remind you in case you forgot is a SQUARE), is actually made up of an infinite number of sinewaves. This means not only can we subtract the sinewaves from the original signal to make it zero energy, but we can add a bunch of sinewaves up to create a square wave. Don't believe me? Watch this. First, let's just plot our square wave and compare it to the first sinewave we found, with \(b_1 = \frac{4}{\pi}\):

Now let's add in \(b_3 * sin(3x)\):

And \(b_5 * sin(5x)\):

Notice it's starting to look more and more like a square wave! Let's go bigger, adding in the sinewaves for \(b_7\) through \(b_{25}\):

Now let's go really big, let's add up to \(b_{200}\):

That's pretty darn close - I can't even see the underlying orange curve any more. Notice there is a bit of ringing at the edges. That never goes away, it's called the Gibbs Phenomenon. Fortunately, the energy of that ringing approaches zero as you add more and more sinewaves, so we usually don't have to worry about it.

If you've never seen this before, it's absolutely bizarre. It's so counterintuitive that the man who discovered it Joseph Fourier had to fight his fellow mathematicians tooth and nail to convince them. You might now be asking yourself if any periodic signal can be represented as a sum of sinewaves. Or you might be so much in shock you can't think (I was in this latter camp). The answer turns out to be yes, we can, but first we need to cover one more subtlety, the difference between even and odd signals.




If you found this content helpful, it would mean a lot to me if you would support me on Patreon. Help keep this content ad-free, get access to my Discord server, exclusive content, and receive my personal thanks for as little as $2. :)

Become a Patron!