Published: 2015-07-11 | Category: [»] Electronics.

Every student knows that square waves can be made out of a sum of sine waves. If you don’t, just have a look a Figure 1, you will see that it is very straightforward. This is usually presented as an introductory example to Fourier series and, later, Fourier transforms. The question here is: is-it possible to make sine waves from square waves? And this is precisely what this post is all about.

Figure 1 - Generating a square wave from a sum of sine waves.

You may ask why we need yet another method to create sine waves since there are a lot of chips over the web that will synthesize almost perfect sine waves and that we have also presented a [»] method to make sine waves from RC circuits a few years ago. Well the idea behind all that is to ask, in the end, if any signals can be made out of square waves. The answer is: yes, at least theoretically.

In theory, it is possible to express any signal into any basis: sine waves, square waves, triangle waves… But square waves have an interesting property: they are easy to generate with digital electronics. With simple astable oscillators or a microcontroller you can create square waves with precise –and stable– frequency. This is not the case with sine waves, triangles waves and others. So the question is not that silly at all because it might have important return value.

But let first come back to square waves and Fourier series. It is possible to show that a square wave of frequency f can be written as an infinite sum of sine waves of odd terms (i.e.: 1f, 3f, 5f, 7f…):

If you look at the Fourier transform of a square wave you will get something like Figure 2 with a fundamental peak at f, then a peak at 3f of one third the intensity, then a peak at 5f of one fifth the intensity etc. For the sake of clarity, I will now call fn the product of f and n (with n a positive integer). So f1 is f, f3 is 3f, f5 is 5f and so on. f1 will be called the fundamental peak and fn>1 the harmonics. Remember that all of these peaks are pure sine waves because we are working with classical Fourier series.

Figure 2 - 1 Hz Square wave. Top: temporal, bottom: frequency spectrum.

What is important to understand (what you should have a gut feeling for) is that the harmonics is what makes the signal look like a square wave and not as a sine wave corresponding to the fundamental f1. If you remove all the harmonics, you will end up with a pure sine wave of frequency f1 (that is, f). And this is precisely what we are going to do.

The easiest method to remove all the harmonics is to use a very sharp low-pass filter with a cut-off frequency slightly above the frequency of your square wave. You can try it, it works really nice but you need a filter of order 8 or so to remove as much harmonics as possible. If you use a single RC filter you won’t be able to remove enough of the first harmonics at f3 and f5 and you will end up with a distorted sine wave. In a square wave, the first harmonics have large intensities (1/3 and 1/5 for f3 and f5) and so you really need to get rid of them by using a sharp filter.

Here, we won’t use low-pass filters because it’s not easy to build a tuneable low-pass filter that is sharp enough (at least, at low-cost). The idea is to subtract newer square waves from the original one to get rid of all the harmonics.

Think of a square wave of frequency 3f for example. Its fundamental will be 3f and it will have harmonics at 3*3f, 5*3f and so on. Now we will scale this square wave by 1/3 (using a resistor divider circuit) and subtract it from the original square wave. If we write all the peaks of the spectrum of the original wave and the scaled new square wave we get (I have limited the development to the fourth first harmonics):

So we got rid of the harmonic f3 and f9. Actually, you can show that we got rid of all the f3n terms.

We now have a wave that behaves like

We will then repeat the process but with a square wave at 5f scaled down to one fifth:

which get rid of all f5n terms. However, something gets wrong here for frequencies that are both multiple of 3 and 5. Let’s take f15 for example. Because f15 is a multiple of 3, we already got rid of it using the first subtraction. Indeed, the 3f square wave had harmonics at f9, f15, f21 and so on. So when we next subtract f15 with the 5f wave, we end up with a non-zero term that should be cancelled with further subtraction. So, be careful when applying the method because you should watch the effect of every subtraction.

We may continue by subtracting a 7f square wave scale down to one seventh which yields:

As a consequence, with only three subtractions we have cleaned the four first harmonics of our signal which now looks like Figure 3.

Figure 3 - 1 Hz Square wave with 4 harmonics removed. Top: temporal, bottom: frequency spectrum.

This already looks more or less like a sine wave but it’s still not really that good. By subtracting harmonics up to f21 we end up with a wave like on Figure 4. It’s already much better.

Figure 4 - 1 Hz Square wave with 12 harmonics removed. Top: temporal, bottom: frequency spectrum.

But wait… how much harmonics should we remove until we get a perfect sine wave? The theoretical answer is an infinite number of them because the Fourier series is infinite. This is where the low-pass filter comes into play. You may want it or not but somewhere in your circuit an element will limit the bandwidth to a maximum frequency fbw. To avoid non-reproducible results due to uncontrolled low-pass effect in components, let’s say we have a sharp low-pass filter at 100 kHz that will cut-off any frequency above that. We have seen that if we use a square wave with a frequency slightly below that we will get a perfect sine wave but if we lower the frequency too much we will leave the first harmonics intact. So we don’t have much choice and we can only play with a frequency of about 100 kHz.

In our example, we showed that it is possible to get rid of the four first harmonics by subtracting three square waves. That means the first non-zero harmonics is f11. What we would like is that all harmonics above f11 be removed by the low-pass filter. This will be accomplished with any output signal between 10 kHz and 100 kHz approximately. If we continue subtracting square waves up to, say, f101, then we could get any signal working from about 1 kHz to 100 kHz and so on.

This is precisely where the system comes handy: it widens the frequency range available with a simple, non-tuneable, sharp low-pass filter.

The final circuit will then look something like this: a square wave input will be fed into the interrupt pin of a microcontroller that will produce harmonics square wave that are then subtracted using a classical op-amp circuit and low-pass filtered to yield a tuneable sine wave output.

But here comes the problem: how to generate the harmonics? The only solution I could think off, at the moment, is to use a master frequency and to divide that master frequency to create the harmonics. The question becomes: which master frequency to use? The answer is simple but problematic at the same time because it is the smallest integer that is divisible by all the 2n+1 terms (n integer positive). For instance, 105 is divisible by 1, 3, 5 and 7. How did I get it? Sadly enough, by multiplying 3, 5 and 7 together.

So if you run a master frequency at 105 Hz, you can get all the four first terms: f1 if 105/105, f3 is 105/35, f5 is 105/21 and f7 is 105/15. If we use the input frequency as the master frequency or “clock” then we can create a fundamental at f/105 and its harmonics f3, f5 and f7 by dividing the master clock by 105, 35, 21 and 15 respectively. As a consequence, we have downscaled the input frequency by 105 to create a sine wave that looks only like Figure 3. If we would like to get a better shape we have to downscale the input frequency even more!

With the method used to generate the various harmonics we will quickly run into some troubles because the clock divider increases like a factorial: 1*3*5*7*9*11*13*… We may clean the divider up a little bit because 9 is 3 times 3, 15 is 3 times 5 and things like that but it won’t change the fact that the number will grow out of control very quickly.

Even worse than that, let’s say we only use the four first harmonics and so a clock divider of 105. The question is: wouldn’t it be more profitable to use a digital-to-analog converter to produce a step-wise approximation to a sine function? If we divide the period of a sine by 105 we could build a signal like the one on Figure 5 with the same number of output pins (4 bits) and it already looks much nicer than our subtracted square waves…

Figure 5 - Sine wave with 105 elements per period, 4 bits DAC.

So unless you manage to find a better way to create the harmonics, there is very little usage to the technique presented here. Still, I wanted to show you the technique because I found it interesting to get a better “gut” feeling on waves and Fourier spectrum.

And if someday you manage to improve the harmonic generation, send me an e-mail ;-)

[⇈] Top of Page

You may also like:

[»] Better SNR for Square Wave Signals

[»] Sine Wave Oscillator with Fewer Op-Amp

[»] Introduction to Heterodyne Systems

[»] Generic 8th Order Sallen-Key Filter

[»] In-line Absorption and Fluorescence Sensor