Saturday 12 July 2014

The Square Waveform

In digital audio, a simple square wave has some interesting properties. On the face of it, you can actually represent the square wave digitally with no error whatsoever. The quantization error, such as it is, becomes merely a scaling factor. If you adjust the input gain, you can get it so that the quantization error is zero for every single recorded value.

Imagine that - a waveform captured digitally with no loss whatsoever. Even though it is of no practical value (who wants to listen to recorded square waves?) you would imagine that it would be useful for test and measurement purposes if nothing else. And people do imagine that. In fact, it is not unusual to see reproductions of square waves, captured on an oscilloscope, as part of a suite of measurements on a DAC. But in digital audio things are seldom quite what they may logically appear to be. Lets take a closer look at square waves and see what we come up with.

A true and perfect square wave can be analyzed mathematically and broken down into what we call its Fourier components. These are nothing more scary than simple sine waves, each one having its own frequency, phase, and amplitude. The Fourier components (sine waves) which make up a square wave comprise the fundamental frequency plus each of its odd harmonics. The fundamental frequency is simply the frequency of the square wave itself. The odd harmonics are just frequencies which are odd multiples of the fundamental frequency (3x, 5x, 7x, 9x, 11x, etc). The amplitudes of each Fourier component are inversely proportional to the frequency of the harmonic. We’ll ignore phase.

If that sounds complicated, it really isn’t. Start with a sine wave, at the frequency of the square wave which I will refer to as ‘F’. Now add to it a sine wave of its third harmonic, which is three times the fundamental frequency, ‘3F’. This sine wave should have an amplitude 1/3 of the amplitude of the fundamental sine wave. OK? Next we add a sine wave of the fifth harmonic, 5F, with an amplitude 1/5. Then the seventh harmonic (7F) with an amplitude 1/7, the ninth harmonic with amplitude 1/9, and so on, ad infinitum. The more of these harmonics you add into the mix, the closer the result approaches a perfect square wave. A truly perfect square wave comprises an infinite number of these Fourier components. Here is a link to an animated illustration which might help make this clearer.

Some interesting things happen as we add more and more Fourier components to build up our square wave. The ripple on the waveform gets smaller in magnitude, and its frequency gets higher. The ‘spikes’ at the start and end of the flat portions of the waveform decay more rapidly. These things can all be seen clearly on the web page I mentioned. However, one surprising thing is that the ‘spikes’ don’t ever disappear, no matter how many Fourier components you add in. In fact, their magnitude doesn’t even get smaller. As you add Fourier components, the 'spikes' converge to about 9% of the amplitude of the square wave, something referred to as the ‘Gibbs Phenomenon’. The spike itself ends up approximating a rapidly decaying oscillation at the highest frequency Fourier component present.

So now lets go back to the idea at the start of this post, that we can easily encode a square wave with absolute precision in a digital audio stream. If this were actually true, it would imply that we are encoding Fourier components comprising an infinite bandwidth of frequencies. But Nyquist-Shannon sampling theory tells us that any frequencies that we attempt to encode which are higher than one half of the sampling rate actually end up encoding ‘alias’ frequencies, which are things that are seriously to be avoided. The square wave that we thought we had encoded perfectly, and with absolute precision, turns out to contain a whole bunch of unwanted garbage! And that, to many people, is bizarrely counter-intuitive.

We’ve gone over this in previous posts, but the bottom line is that any waveform that you wish to capture digitally - square waves included - need to be first passed through a low-pass filter called an ‘anti-aliasing’ filter, which blocks all frequencies above one half of the sampling rate. For the purpose of this post, lets assume that the anti-aliasing filter is ‘perfect’ and does nothing except remove all frequencies above half the sampling frequency. What this filter does to our square wave is, in effect, to strip off the vast majority of the square wave’s Fourier components, leaving only a few behind.

Lets look at what this means in the context of Red Book (44.1kHz sample rate) audio. If the square wave is, for example a 1kHz square wave, then its first ten Fourier components (the 3rd through 21st harmonics) are left intact. However, if the square wave is a 10kHz square wave, not one single odd harmonic component makes it through the filter! This means that if you attempt to record a 10kHz square wave, all you could actually ever capture would be a pure 10kHz sine wave.

What does this mean for DAC reviews where you are presented with a screenshot illustrating the attempted reproduction of a square wave? The bottom line here is that for such a screenshot to make any sense at all, you need to know exactly what a perfectly reproduced square wave ought to look like. For example, it ought to have leading and trailing edge ‘spikes’ which approximate to half cycles of an approximate 20kHz sin wave, and with an amplitude which is about 9% higher than the amplitude of the square wave itself. It needs to have ripple. I could be a lot more specific about those things, but the fine details end up being governed by issues such as the frequency of the square wave, and - far more complex - the characteristics of the anti-aliasing filter used to encode the original signal. Without this information a square-wave screenshot is of limited value. If nothing else, it can provide a level of confidence that the person conducting the test knows what they are talking about. Not to mention avoiding allowing the reader to infer or imply erroneous conclusions of their own.