# Special case: perfect sine waves

Let’s say we want to test the output of a DAC while it passes a pure sine wave. The “pure sine wave” requirement is a bit more difficult than it might seem. In the general case, it’s not possible to create a sine wave that is free of quantization error. You could scale the wave so that the amplitude peaks are on exact values, but other samples of the wave would fall between quantization levels and require numerical rounding. This is not an overwhelming problem, typically, because all of the error is confined to the ± 1 lsb area. For a signal at or near full scale and 24-bit, the total error is well below the noise level of any analog audio circuit playing it back. But the signal to noise ratio necessarily gets worse as the signal level is reduced, so a sine at the lowest possible levels has terrible S/N.

If we were to do similar tests with square waves (more precisely, bandlimited square waves, to satisfy the sample theorem), we could do much better—if we select frequencies with strict limitations. Specifically, by synchronizing the frequency to use periods that are multiples of the sampling period, and with the phase aligned so that the square wave transitions occur exactly half-way between samples. In order words, data patterns like 1,1,-1,-1,1,1,-1,-1… Put more generally, switching between a value of A for n samples, and -A for n samples, where A is any exact bit value, and n is an integer number of samples.

For example, toggling between a positive value for four samples and it’s corresponding negative value for four samples yields a perfect bandlimited square wave of frequency one-eighth of the sample rate and no DC offset—6 kHz for a 48 kHz sample rate. The bandlimited square wave would have three total harmonics below half the sample rate (a fourth would sit right on the Nyquist frequency).

### Sine waves

That should seem obvious, for square waves, but what about sines? First, let’s consider a square wave as described, but for higher frequencies, which would have fewer harmonics—if we can get to the point of a single harmonic below Nyquist, we’d have a pure sine wave! The highest frequency obtainable with the square wave method we’re using would be alternating samples, A,-A,A,-A… But that would be a square wave at the Nyquist rate, equivalent to a sine wave at the Nyquist rate, and we’d expect a proper converter to wipe that out. Next up is A,A,-A,-A…, half Nyquist. That would indeed be a single harmonic—the fundamental at one-quarter the sample rate, 12 kHz for 48 kHz SR. And completely free of quantization. Here’s a representation of how the sine wave relates to the samples:

Note that once converted to analog, the resulting sine wave has inter-sample peaks greater than we’d expect from the digital values. Inter-sample peaks are always a fact of life with digital audio, I’ll publish an article on them soon. But in this case, we could change the phase and align the peaks on exact sample boundaries, resulting in peaks that are exactly represented by the sample values, while retaining the error-free quality:

Now, we’re identified one perfect sign wave, at one-quarter the sample rate. Any others? Not from square waves, because we can’t go higher, and if we go lower we introduce more harmonics. But let’s think about the sine and why we have a perfect one. The answer is that one-quarter works because it features two things we need: it produces a sine wave, and it only uses values that are an exact bit value—either an exact level of our choosing and it’s negative, or zero.

Ultimately, we have only a few choices that can produce a sine from only 0, A, and -A. In addition to one-fourth the sample rate, one-third and one-sixth satisfy the requirements:

What about other integer factors? We have to skip 2 (Nyquist), we’ve seen 3, 4, and 6; 5 doesn’t work, because although it’s symmetrical, it produces two levels per side—both can’t be exact sample values. If you try anything over 6 (3 samples per half-cycle) you’ll find it provides too many sample points per half cycle to maintain a single value (±) along with zero.

So that’s it—1/3, 1/4, and 1/6. For a 48 kHz sample rate, we can have perfect 16, 12, or 8 kHz sines. The A can be any value, from a single lsb to the maximum word size. But remember, positive for DACs doesn’t go quite to “1.0”, so balance the maximum negative level accordingly, and also take into account inter-sample peaks. However, it seems these special cases of sines are mainly useful only for very small amplitudes, so those two caveats can be ignored in that case. For full-scale sines and 24-bit converters, the imperfections from calculation and quantization are small, and in particular much smaller than the minimum analog circuitry noise, so we would normally calculate our sine tones with traditional sine computation (we might use a sine function, or an FFT especially for multiple harmonic tones).

### Calculating values

To calculate the exact values needed for a sine wave of amplitude ± 1.0, use $$sin(\frac{2πx}{div})$$, where x is the integer sample number (0, 1, 2…), and div is the divisor of the sample rate (3, 4, or 6 for “perfect” sine waves). You can multiply the sample values by a constant to scale them to the desired signal size. The idea, of course, is to round them to exact samples values, symmetrically around zero. You’ll get this automatically with rounding towards zero. And don’t dither these special cases—they have no noise, so you’ll only introduce noise with dither.

This article was the result of thought and discussion here.

This entry was posted in Digital Audio, Fun Facts. Bookmark the permalink.