Building a Square Wave

DonH50

Member Sponsor & WBF Technical Expert
Jun 23, 2010
3,869
233
500
Monument, CO
Since I used a square wave as an example in another thread, I realized that what is common knowledge for hairy-knuckled engineers such as I and high-brow scientists like some of the other folk here, may still be mysterious to many audiophiles. I thought it might be worth recreating a simple set of plots that shows how we can make a square wave from a bunch of simple sine waves, i.e. single frequency tones.

For starters, we must realize that any signal can be represented by an infinite sum of single tones of the right amplitude and phase. This is a fundamental principle upon which all signal processing is based. Problems arise, like in many areas of life, when reality hits the theory... In this case, it's impossible for a real system to have infinite frequency response, sampled or not, and of course getting all those tones' amplitudes and phases just right when we add them up is a real problem. Sampling means we only have a limited bandwidth to work with, so the number of frequencies we can sum is fixed by the fundamental signal frequency and the sampling frequency.

A square wave is comprised of an infinite sum of only odd harmonics of the signal. That is, a 1 kHz square wave includes 1 kHz, 3 kHz, 5 kHz, and so on out to infinity. No even harmonics! The amplitude of the odd harmonics decreases as the ratio 2/(n*pi) where n is the harmonic number and pi = 3.14159... (that old circle number). So, the third harmonic is only 1/3 the amplitude of the fundamental, the fifth is 1/5, etc. It doesn't take a whole lot of harmonics before we can't really see much difference between an ideal square wave and one with only a few components. Digital systems routinely work with limited bandwidth to reduce noise. Noise just keeps getting larger as you increase the system bandwidth, so it makes sense to use as little bandwidth as required to capture the signal and thus maximize the SNR.

For this example, I went back to a 1 kHz signal, and started added harmonics as required to build a square wave. The top plot is the 1 kHz sine wave, a nice single tone. I added a bit of 3rd harmonic, and you can see the top is already starting to flatten. Added in the 5th, and it actually starts to look like a square wave! As I add the 7th and 9th harmonic terms, it looks like the flat top ripple is a little worse, but the edges are starting to shape up now. By the time I add in the first 11 terms we have a pretty decent square wave. The bottom curve is the first 101 terms (fundamental plus 100 harmonics) and is about as close to a perfect square wave as made sense to me to plot.

squarewave_stack..JPG

This next plot shows the single (fundamental) 1 kHz tone and sums to the 3rd, 5th, and 101st harmonic. You can see that even with only 5 terms the edges are nearly as fast as using 101, at least to the eye (and usually to the ear as well).

squarewave_overl&#9.JPG

What does this mean? Well, there aren't many square waves in music, but there are a lot of harmonics. If we were to reproduce a square wave, and decide we need at least 5 terms, then the highest square wave we can produce on a CD is about 20 kHz/5 = 4 kHz.

Hope this is useful! - Don
 
  • Like
Reactions: ZionPOWER

muralman1

New Member
Jul 7, 2010
479
0
0
Sacramento Ca
Frantz, I am neither a scientist or an audio techy. All i know is, if you leave the poor complex 2nd, 3rd, 4th, etc... alone to begin with.... You hear them. It is simply delicious to hear a Japanese bell rung on my system, or Yo Yo Ma playing his marvelous cello. You get the richness that requires the extra harmonics to sound out the beautiful wave tones making the recordings ultimately enjoyable.
 

FrantzM

Member Sponsor & WBF Founding Member
Apr 20, 2010
6,455
15
405
Frantz, I am neither a scientist or an audio techy. All i know is, if you leave the poor complex 2nd, 3rd, 4th, etc... alone to begin with.... You hear them. It is simply delicious to hear a Japanese bell rung on my system, or Yo Yo Ma playing his marvelous cello. You get the richness that requires the extra harmonics to sound out the beautiful wave tones making the recordings ultimately enjoyable.

uh .. Ids this a reply to another thread ... I still don't understand what you mean ...

By he way if you are a fan of Yo-yo Ma, check out Jacqueline Dupre .. Yo-yo Ma is currently using her former Stradivarius. She is considered one of the greatest cellist, if not the best, to ever grace the face of the earth .. Some of her renditions are considered "definitive" e.g The Edgar Cello Concerto in E minor among others ...
 

DonH50

Member Sponsor & WBF Technical Expert
Jun 23, 2010
3,869
233
500
Monument, CO
As long as we're off topic, I worked with a guy who was friends with Yo-yo Ma. Got us in backstage to meet him once, what a great guy! - Don
 

DonH50

Member Sponsor & WBF Technical Expert
Jun 23, 2010
3,869
233
500
Monument, CO
I don't recall if he was wearing a watch when I met him, it was a long time ago, and I wouldn't have paid attention to his watch anyway, sorry.
 

DonH50

Member Sponsor & WBF Technical Expert
Jun 23, 2010
3,869
233
500
Monument, CO
I looked at a lot of Rolex watches in various shops when we visited Switzerland last year. Wow, nice, but I needed a new car instead... :(
 

Vincent Kars

WBF Technical Expert: Computer Audio
Jul 1, 2010
860
1
0
I like this
We are using digital as a source for playback.
The rise and fall of this square wave is used to construct our logical 1 and 0
But a square waves don’t exist in nature.
What about explaining the inverse, a square wave is just a bunch of sinuses and the impact of this on reproducing digital audio?
 

DonH50

Member Sponsor & WBF Technical Expert
Jun 23, 2010
3,869
233
500
Monument, CO
I like this
We are using digital as a source for playback.
The rise and fall of this square wave is used to construct our logical 1 and 0
But a square waves don’t exist in nature.
What about explaining the inverse, a square wave is just a bunch of sinuses and the impact of this on reproducing digital audio?

Sorry, I am not sure I understand the question. A DAC can produce a square wave with much higher-frequency content than an ADC at the same rate can acquire, because the output of a DAC occurs after sampling (see the sampling thread for more about sampling). In the digital domain, e.g. for the bit stream into our DAC, as long as the data can be correctly captured (the 1's and 0's), there's no problem with limited bandwidth. Just sample at the center of the data period and you are OK (usually). There is an issue with jitter if the sampling clock is not precise and low-noise, however. I am still working on a Jitter 101 thread.

At the output of a DAC, the waveform is not 0 or 1, but some number (value) related to the resolution of the DAC. For a 16-bit DAC, it can take on values from 0 to 65,635 (2^16-1), scaled appropriately.

A square can be constructed from, and represented as, a series of sinusoids (sinuses make your nose run), but that's not what happens in a DAC -- it simply switches from one value to another. We can use a series of sinusoids to construct the square wave, and to show what the waveform looks like in the frequency domain.

Could you please restate your question?

Thanks - Don
 

DonH50

Member Sponsor & WBF Technical Expert
Jun 23, 2010
3,869
233
500
Monument, CO
Thanks again! Same thing at work and when I teach at the local university (rare); take away my white board and I am silent... :)

As for a square wave, Fourier tells us it can be described as an infinite series of sine waves, and in fact any signal can be described as a proper combination of sine waves, so in one sense they are "really there". However, you are right that when we make a square wave, we just switch between two levels as fast as we can; we do not sum a bunch of sine waves to do it (though we could).
 

Mark (Basspig) Weiss

Well-Known Member
Aug 3, 2010
660
13
370
New Milford, CT
www.basspig.com
...Some of her renditions are considered "definitive" e.g The Edgar Cello Concerto in E minor among others ...

I think you mean Sir Edward Elgar's Cello Concerto. One of my recording clients, a rising star of a cellist in the symphony world, recorded this concerto recently in Europe, so I'm quite familiar with it. The Cello is a passionate instrument.
 

DonH50

Member Sponsor & WBF Technical Expert
Jun 23, 2010
3,869
233
500
Monument, CO
The Cello is a passionate instrument.

As a musician myself, I have to throw out my opinion that the player has a little something to do with it, passionate or not... ;) For all its passion, every cello I have ever seen just sits there until somebody picks it up and plays it. It never fails to evoke a passionate response, though in less capable hands it's probably not the type of passion the player desires... :D - Don (brass player so what do I know from strings?)
 

Mark (Basspig) Weiss

Well-Known Member
Aug 3, 2010
660
13
370
New Milford, CT
www.basspig.com
Well, I'll say that few other instruments evoke a sense of passion quite as well as the cello. A good cello concerto gives me goosebumps. Can't say I've ever been moved like that by a trumpet, however great the player may be. ;)
 

Vincent Kars

WBF Technical Expert: Computer Audio
Jul 1, 2010
860
1
0
At the output of a DAC, the waveform is not 0 or 1, but some number (value) related to the resolution of the DAC. For a 16-bit DAC, it can take on values from 0 to 65,635 (2^16-1), scaled appropriately.
Thanks - Don

Let's try to be less unclear
The output of a DAC is analogue of course, it is the analogue equivalent of the binary value.
The analogue output however is stepped.
What I (mis?)understand is that because of this all kind of harmonics are generated.
More or less what you described in reverse, as it is impossible to have a infinite rise time, you end up with harmonics.
Likewise with processors, the RFI they generate consist of harmonics.
Again, it is a chopper, a square wave but in practice you end up with these harmonics.

Sinus, plural=sinuses in my language (Dutch). Typical those moments where my spellchecker and me got fooled.
 

DonH50

Member Sponsor & WBF Technical Expert
Jun 23, 2010
3,869
233
500
Monument, CO
Yes, you are correct that those steps generate wide-band harmonics, and that's why a filter is needed on the output of a DAC. If that picture I showed was for a 20 kHz signal, then it would be a sine wave into the ADC (due to the ADC's input anti-alias filter), and to get the "right" output we'd need an output filter after the DAC to prevent a string of high-frequency spikes from being passed on to the rest of the system. That high-frequency content reaches well above the audio and can cause all sorts of problems. Is that what you meant?

Technically, the DAC's output filter is not an anti-alias filter since there is no aliasing at that point, though it shares the same characteristics and could be identical to the input anti-alias filter. At lower frequencies, we get more samples per cycle, so the steps are smaller, and the relative energy contribution of those little spikes from the smaller steps is much less.

Sorry, did not know about the language barrier. In English sinus cavities are around our noses, and sinuses is plural for those. I was talking about sine waves, a mathematical term. Square waves are made of sinusoids, sinusoidal waves with particular amplitude, frequency, and phase.
 
Last edited:

sbo6

Well-Known Member
May 19, 2014
1,356
356
240
Round Rock, TX
Since I used a square wave as an example in another thread, I realized that what is common knowledge for hairy-knuckled engineers such as I and high-brow scientists like some of the other folk here, may still be mysterious to many audiophiles. I thought it might be worth recreating a simple set of plots that shows how we can make a square wave from a bunch of simple sine waves, i.e. single frequency tones.

For starters, we must realize that any signal can be represented by an infinite sum of single tones of the right amplitude and phase. This is a fundamental principle upon which all signal processing is based. Problems arise, like in many areas of life, when reality hits the theory... In this case, it's impossible for a real system to have infinite frequency response, sampled or not, and of course getting all those tones' amplitudes and phases just right when we add them up is a real problem. Sampling means we only have a limited bandwidth to work with, so the number of frequencies we can sum is fixed by the fundamental signal frequency and the sampling frequency.

A square wave is comprised of an infinite sum of only odd harmonics of the signal. That is, a 1 kHz square wave includes 1 kHz, 3 kHz, 5 kHz, and so on out to infinity. No even harmonics! The amplitude of the odd harmonics decreases as the ratio 2/(n*pi) where n is the harmonic number and pi = 3.14159... (that old circle number). So, the third harmonic is only 1/3 the amplitude of the fundamental, the fifth is 1/5, etc. It doesn't take a whole lot of harmonics before we can't really see much difference between an ideal square wave and one with only a few components. Digital systems routinely work with limited bandwidth to reduce noise. Noise just keeps getting larger as you increase the system bandwidth, so it makes sense to use as little bandwidth as required to capture the signal and thus maximize the SNR.

For this example, I went back to a 1 kHz signal, and started added harmonics as required to build a square wave. The top plot is the 1 kHz sine wave, a nice single tone. I added a bit of 3rd harmonic, and you can see the top is already starting to flatten. Added in the 5th, and it actually starts to look like a square wave! As I add the 7th and 9th harmonic terms, it looks like the flat top ripple is a little worse, but the edges are starting to shape up now. By the time I add in the first 11 terms we have a pretty decent square wave. The bottom curve is the first 101 terms (fundamental plus 100 harmonics) and is about as close to a perfect square wave as made sense to me to plot.

View attachment 573

This next plot shows the single (fundamental) 1 kHz tone and sums to the 3rd, 5th, and 101st harmonic. You can see that even with only 5 terms the edges are nearly as fast as using 101, at least to the eye (and usually to the ear as well).

View attachment 574

What does this mean? Well, there aren't many square waves in music, but there are a lot of harmonics. If we were to reproduce a square wave, and decide we need at least 5 terms, then the highest square wave we can produce on a CD is about 20 kHz/5 = 4 kHz.

Hope this is useful! - Don
If I can ask - is what you are trying to convey with your last statement that with digital and specifically CDs are limited to be able to capture adequate harmonics only <=4KHz?

BTW, my sinuses are killing me being in TX with seasonal cedar allergies. :)
 

About us

  • What’s Best Forum is THE forum for high end audio, product reviews, advice and sharing experiences on the best of everything else. This is THE place where audiophiles and audio companies discuss vintage, contemporary and new audio products, music servers, music streamers, computer audio, digital-to-analog converters, turntables, phono stages, cartridges, reel-to-reel tape machines, speakers, headphones and tube and solid-state amplification. Founded in 2010 What’s Best Forum invites intelligent and courteous people of all interests and backgrounds to describe and discuss the best of everything. From beginners to life-long hobbyists to industry professionals, we enjoy learning about new things and meeting new people, and participating in spirited debates.

Quick Navigation

User Menu

Steve Williams
Site Founder | Site Owner | Administrator
Ron Resnick
Site Co-Owner | Administrator
Julian (The Fixer)
Website Build | Marketing Managersing