{"id":1327,"date":"2023-01-15T16:53:37","date_gmt":"2023-01-16T00:53:37","guid":{"rendered":"https:\/\/www.earlevel.com\/main\/?p=1327"},"modified":"2023-01-16T10:39:47","modified_gmt":"2023-01-16T18:39:47","slug":"special-case-perfect-sine-waves","status":"publish","type":"post","link":"https:\/\/www.earlevel.com\/main\/2023\/01\/15\/special-case-perfect-sine-waves\/","title":{"rendered":"Special case: perfect sine waves"},"content":{"rendered":"\n<p>Let\u2019s say we want to test the output of a DAC while it passes a pure sine wave. The \u201cpure sine wave\u201d requirement is a bit more difficult than it might seem. In the general case, it\u2019s 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 \u00b1 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.<\/p>\n\n\n\n<p>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\u2014if 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\u2026 Put more generally, switching between a value of <em>A<\/em> for <em>n<\/em> samples, and <em>-A<\/em> for <em>n<\/em> samples, where <em>A<\/em> is any exact bit value, and <em>n<\/em> is an integer number of samples.<\/p>\n\n\n\n<p>For example, toggling between a positive value for four samples and it\u2019s corresponding negative value for four samples yields a perfect bandlimited square wave of frequency one-eighth of the sample rate and no DC offset\u20146 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).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sine waves<\/h3>\n\n\n\n<p>That should seem obvious, for square waves, but what about sines? First, let\u2019s consider a square wave as described, but for higher frequencies, which would have fewer harmonics\u2014if we can get to the point of a single harmonic below Nyquist, we\u2019d have a pure sine wave! The highest frequency obtainable with the square wave method we\u2019re using would be alternating samples, <em>A,-A,A,-A\u2026<\/em> But that would be a square wave at the Nyquist rate, equivalent to a sine wave at the Nyquist rate, and we\u2019d expect a proper converter to wipe that out. Next up is <em>A,A,-A,-A\u2026<\/em>, half Nyquist. That would indeed be a single harmonic\u2014the fundamental at one-quarter the sample rate, 12 kHz for 48 kHz SR. And completely free of quantization. Here\u2019s a representation of how the sine wave relates to the samples:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.earlevel.com\/main\/wp-content\/uploads\/2023\/01\/4.png\"><img loading=\"lazy\" decoding=\"async\" width=\"685\" height=\"229\" src=\"https:\/\/www.earlevel.com\/main\/wp-content\/uploads\/2023\/01\/4.png\" alt=\"\" class=\"wp-image-1331\" srcset=\"https:\/\/www.earlevel.com\/main\/wp-content\/uploads\/2023\/01\/4.png 685w, https:\/\/www.earlevel.com\/main\/wp-content\/uploads\/2023\/01\/4-300x100.png 300w\" sizes=\"(max-width: 685px) 100vw, 685px\" \/><\/a><\/figure>\n\n\n\n<p>Note that once converted to analog, the resulting sine wave has inter-sample peaks greater than we\u2019d expect from the digital values. Inter-sample peaks are always a fact of life with digital audio, I\u2019ll 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:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.earlevel.com\/main\/wp-content\/uploads\/2023\/01\/4-1.png\"><img loading=\"lazy\" decoding=\"async\" width=\"685\" height=\"230\" src=\"https:\/\/www.earlevel.com\/main\/wp-content\/uploads\/2023\/01\/4-1.png\" alt=\"\" class=\"wp-image-1332\" srcset=\"https:\/\/www.earlevel.com\/main\/wp-content\/uploads\/2023\/01\/4-1.png 685w, https:\/\/www.earlevel.com\/main\/wp-content\/uploads\/2023\/01\/4-1-300x101.png 300w\" sizes=\"(max-width: 685px) 100vw, 685px\" \/><\/a><\/figure>\n\n\n\n<p>Now, we\u2019re identified one perfect sign wave, at one-quarter the sample rate. Any others? Not from square waves, because we can\u2019t go higher, and if we go lower we introduce more harmonics. But let\u2019s 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\u2014either an exact level of our choosing and it\u2019s negative, or zero.<\/p>\n\n\n\n<p>Ultimately, we have only a few choices that can produce a sine from only 0, <em>A<\/em>, and <em>-A<\/em>. In addition to one-fourth the sample rate, one-third and one-sixth satisfy the requirements:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.earlevel.com\/main\/wp-content\/uploads\/2023\/01\/3.png\"><img loading=\"lazy\" decoding=\"async\" width=\"684\" height=\"230\" src=\"https:\/\/www.earlevel.com\/main\/wp-content\/uploads\/2023\/01\/3.png\" alt=\"\" class=\"wp-image-1333\" srcset=\"https:\/\/www.earlevel.com\/main\/wp-content\/uploads\/2023\/01\/3.png 684w, https:\/\/www.earlevel.com\/main\/wp-content\/uploads\/2023\/01\/3-300x101.png 300w\" sizes=\"(max-width: 684px) 100vw, 684px\" \/><\/a><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.earlevel.com\/main\/wp-content\/uploads\/2023\/01\/6.png\"><img loading=\"lazy\" decoding=\"async\" width=\"684\" height=\"229\" src=\"https:\/\/www.earlevel.com\/main\/wp-content\/uploads\/2023\/01\/6.png\" alt=\"\" class=\"wp-image-1334\" srcset=\"https:\/\/www.earlevel.com\/main\/wp-content\/uploads\/2023\/01\/6.png 684w, https:\/\/www.earlevel.com\/main\/wp-content\/uploads\/2023\/01\/6-300x100.png 300w\" sizes=\"(max-width: 684px) 100vw, 684px\" \/><\/a><\/figure>\n\n\n\n<p>What about other integer factors? We have to skip 2 (Nyquist), we\u2019ve seen 3, 4, and 6; 5 doesn\u2019t work, because although it\u2019s symmetrical, it produces two levels per side\u2014both can\u2019t be exact sample values. If you try anything over 6 (3 samples per half-cycle) you\u2019ll find it provides too many sample points per half cycle to maintain a single value (\u00b1) along with zero.<\/p>\n\n\n\n<p>So that\u2019s it\u20141\/3, 1\/4, and 1\/6. For a 48 kHz sample rate, we can have perfect 16, 12, or 8 kHz sines. The <em>A<\/em> can be any value, from a single lsb to the maximum word size. But remember, positive for DACs doesn\u2019t go quite to \u201c1.0\u201d, 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).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Calculating values<\/h3>\n\n\n\n<p>To calculate the exact values needed for a sine wave of amplitude \u00b1 1.0, use \\(sin(\\frac{2\u03c0x}{div})\\), where x is the integer sample number (0, 1, 2&#8230;), and <em>div<\/em> is the divisor of the sample rate (3, 4, or 6 for \u201cperfect\u201d 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\u2019ll get this automatically with rounding towards zero. And don\u2019t dither these special cases\u2014they have no noise, so you\u2019ll only introduce noise with dither.<\/p>\n\n\n\n<p>This article was the result of thought and discussion <a href=\"https:\/\/www.audiosciencereview.com\/forum\/index.php?threads\/how-good-is-your-pcm-sine-wave-test-tone-nivens-theorem-is-your-friend.40376\/\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let\u2019s say we want to test the output of a DAC while it passes a pure sine wave. The \u201cpure sine wave\u201d requirement is a bit more difficult than it might seem. In the general case, it\u2019s not possible to &hellip; <a href=\"https:\/\/www.earlevel.com\/main\/2023\/01\/15\/special-case-perfect-sine-waves\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,47],"tags":[],"_links":{"self":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts\/1327"}],"collection":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/comments?post=1327"}],"version-history":[{"count":9,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts\/1327\/revisions"}],"predecessor-version":[{"id":1341,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/posts\/1327\/revisions\/1341"}],"wp:attachment":[{"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/media?parent=1327"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/categories?post=1327"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.earlevel.com\/main\/wp-json\/wp\/v2\/tags?post=1327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}