Die Phase der Sinuskurve spielt keine Rolle: Eine Phasenverschiebung einer Sinuskurve entspricht einer zeitlichen Verschiebung, die zu einer Zeitverschiebung sowohl der quantisierten Sinuskurve als auch des Quantisierungsfehlers führt. Das Leistungsspektrum ist gegenüber Zeitverschiebungen unveränderlich . Wir arbeiten mit SinusAcos(x).
Optimalität durch def. 1
Entsprechend der Maximierung des Signal-Rausch-Verhältnisses (SNR) können wir den quadratischen Mittelwertquantisierungsfehler relativ zum quadratischen Mittelwert minimieren 1/2−−−√A der Sinuskurve, RRMSE = 1/SNR−−−−−−√. Es reicht aus, die Analyse in der ersten Viertelperiode der Kosinuswelle durchzuführen, da der Rest sowohl der Kosinuswelle als auch der Quantisierungsfehler bis zu einem Vorzeichenwechsel und / oder einer Zeitumkehr mit ihrer ersten Viertelperiode identisch sind. Lassenx im ersten Viertel der Kosinuswelle sein, 0<x<π/2. In Anlehnung an Gl. 2 meiner Antwort auf eine verwandte Frage erreicht die quantisierte Kosinuswelle einen ganzzahligen Wertk∈0…round(A) wann:
0<x<acos(round(A)−0.5A),acos(k+0.5A)<x<acos(k−0.5A),acos(0.5A)<x<π2,if k=round(A),if 1≤k≤round(A)−1,if k=0.(1)
Jeder Wert von k gibt den relativen mittleren quadratischen Fehler an RelMSE ein additiver Beitrag von:
2MSEkA2=2A21π/2∫x1x0(Acos(x)−k)2dx=2sin(x1)cos(x1)−2sin(x0)cos(x0)π+8k(sin(x0)−sin(x1))πA+4k2(x1−x0)πA2+2x1−2x0π,(2)
wo x0 und x1 bezeichnen, wie für jeden separat definiert k, die Grenzen x0<x<x1gegeben durch Gl. 1. Der gesamte relative mittlere quadratische Quantisierungsfehler ist dann:
RelMSE=2MSEA2=∑k=0round(A)2MSEkA2=2asin(12A)π−4A2−1−−−−−−√2πA2−(2A2+4round(A)2)asin(2round(A)−12A)πA2−(6round(A)+1)4A2−(2round(A)−1)2−−−−−−−−−−−−−−−−−−−√−2π(A2+2round(A)2)2πA2+1πA2∑k=1round(A)−1((2A2+4k2)(asin(2k+12A)−asin(2k−12A))+(6k−1)4A2−(2k+1)2−−−−−−−−−−−−−√−(6k+1)4A2−(2k−1)2−−−−−−−−−−−−−√2),A>0.5.(3)
Für die Optimalität per Definition 1 besteht die Aufgabe darin, den Wert von zu finden A das minimiert den relativen quadratischen mittleren Quantisierungsfehler RRMSE =RelMSE−−−−−−−√. unter der BedingungA≤2m−1−0.5. Gl. 3 kann in Python mithilfe der mpmath
Bibliothek mit beliebiger Genauigkeit ausgewertet werden :
import mpmath as mp
def RelMSE(A): # valid for A >= 0.5
A = mp.mpf(A)
return 2*mp.asin(1/(2*A))/mp.pi - mp.sqrt(4*A**2-1)/(2*mp.pi*A**2) - (2*A**2 + 4*mp.floor(A + 0.5)**2)*mp.asin((2*mp.floor(A + 0.5) - 1)/(2*A))/(mp.pi*A**2) - ((6*mp.floor(A + 0.5) + 1)*mp.sqrt(4*A**2 - (2*mp.floor(A + 0.5) - 1)**2) - 2*mp.pi*(A**2 + 2*mp.floor(A + 0.5)**2))/(2*mp.pi*A**2) + mp.nsum(lambda k: (2*A**2 + 4*k**2)*(mp.asin((2*k+1)/(2*A)) - mp.asin((2*k-1)/(2*A))) + ((6*k-1)*mp.sqrt(4*A**2 - (2*k + 1)**2) - (6*k + 1)*mp.sqrt(4*A**2 - (2*k - 1)**2))/2, [1, mp.floor(A + 0.5)-1])/(mp.pi*A**2)
RRMSE scheint ein lokales Minimum zwischen jedem Paar aufeinanderfolgender Ganzzahlen zu haben A (Abb. 1).
![enter image description here](https://i.stack.imgur.com/Um8ga.png)
Abbildung 1. RRMSE (blaue durchgezogene Linie und blaue Quadrate) und ihre Annäherung 1/6−−−√/A (orange gestrichelte Linie) basierend auf der Varianz 1/12 einer gleichmäßigen Verteilung der Breite 1 für verschiedene Bereiche von A.
Eine Auswahl von optimalen A sind in Tabelle 1 zusammen mit dem resultierenden RRMSE angegeben, auch für einige andere übliche Werte von A. Bei größermDie RRMSE-Reduzierung durch die optimale Wahl ist marginal. Die Tabelleneinträge, die nur Ziffern anzeigen, die zwischen zwei Berechnungen mit unterschiedlichen Genauigkeitseinstellungen gleich sind, können mit dem folgenden Python-Skript (Fortsetzung) generiert werden, dessen Ausführung Tage dauerte:
def approx_optimal_A(m):
m = mp.mpf(m)
return 2**(m-1) - 1 + mp.mpf("0.156936321399") + mp.exp(-1.2749819017 - 0.3464088917*m) # Eq. 8
# return 2**(m-1) - 1 # This less informed guess gives identical results but slower convergence
def to_max_digits(f, prec_1, prec_2, max_digits): # return the at most max_digits digits of function f that are agreed about by both precision settings
prec = mp.mp.prec
mp.mp.prec = prec_1
y_prec_1 = f()
mp.mp.prec = prec_2
y_prec_2 = f()
digits = max_digits
while mp.nstr(y_prec_1, digits, strip_zeros=False) != mp.nstr(y_prec_2, digits, strip_zeros=False): # Beware: a possible infinite loop
digits -= 1
return mp.nstr(y_prec_2, digits, strip_zeros=False)
prec = mp.mp.prec
double_digits = 15 # Print at most this many digits
dB_digits = 9
for m in range(2, 25):
optimal_A = to_max_digits(lambda: mp.findroot(lambda A: mp.diff(RelMSE, A), approx_optimal_A(m)), 80, 100, double_digits)
RelMSE_optimal = to_max_digits(lambda: 10*mp.log10(RelMSE(mp.mpf(optimal_A))), 80, 100, dB_digits)
RelMSE_1 = to_max_digits(lambda: 10*mp.log10(RelMSE(2**(m-1)-1)), 80, 100, dB_digits)
RelMSE_2 = to_max_digits(lambda: 10*mp.log10(RelMSE(2**(m-1)-0.5)), 80, 100, dB_digits)
print(str(m)+"&"+optimal_A+"&"+RelMSE_optimal+"&"+RelMSE_1+"&"+RelMSE_2+"\\\\")
Tabelle 1. Optimal A per definitionem 1 für verschiedene m≤24 und die resultierende RRMSE, wobei die RRMSE für einige allgemeine Auswahlmöglichkeiten von A zum Vergleich aufgeführt. m=1wurde weggelassen, weil es nicht von Gl. 3 und weil ein einzelnes Bit keine positiven Zahlen als Zweierkomplementdarstellung darstellt. Beachten Sie, dass RRMSE in dB durch einen Vorzeichenwechsel in SNR in dB umgewandelt wird, weilSNR = 1/RelMSE = 1/RRMSE2.
m23456789101112131415161718192021222324optimal A1.268279494615303.238009421210377.2165859792940715.200718133195531.188875671425763.1800835394190127.173613625523255.168894736361511.1654791888901023.163022053772047.161262644844095.160007225168191.1591137160116383.158478966632767.158028642865535.1577094659131071.157483397262143.157323352524287.1572100891048575.157129952097151.157073264194303.157033178388607.15700481RRMSE (dB)A=optimal−11.1128053−18.8206937−25.5167673−31.8127094−37.9364880−43.9858910−50.0049518−56.0134743−62.0200022−68.0278661−74.0380590−80.0505958−86.0651409−92.0812822−98.0986407−104.116904−110.135830−116.155234−122.174984−128.194979−134.215150−140.235446−146.2558312m−1−1−8.92729805−17.9588863−25.0903048−31.5775537−37.7977883−43.9001114−49.9500053−55.9773382−61.9957638−68.0113689−74.0267095−80.0427267−86.0596541−92.0774410−98.0959437−104.115007−110.134493−116.154291−122.174318−128.194509−134.214818−140.23521−146.25572m−1−0.5−10.1764645−17.8882004−24.7375654−31.2013629−37.4726954−43.6414308−49.7527817−55.8307200−61.8884975−67.9337180−73.9708981−80.0028089−86.0312009−92.0572079−98.0815798−104.104821−110.127276−116.149181−122.170701−128.191950−134.213008−140.233931−146.25476
Optimality by def. 2
A periodic function such as a quantized sinusoid has a Fourier series; it is a sum of harmonic sinusoids, that is, sinusoids of harmonic frequencies of a fundamental frequency. Harmonic sinusoids are orthogonal. Therefore, the mean square of the periodic function equals the sum of the mean squares of the harmonic sinusoids. The mean square of the sum of the non-fundamental harmonics can then be calculated by subtracting the mean square of the fundamental from the mean square of the periodic function. For a quantized cosine wave round(Acos(x)) this allows to calculate total harmonic distortion (THD) as:
THD=MS−a21/2a21/2−−−−−−−−−√=MSa21/2−1−−−−−−−−√,(4)
where MS is the mean square of the quantized cosine wave, a21/2 is the mean square of the fundamental, and a1 is the coefficient of the fundamental frequency cosine in the Fourier series of round(Acos(x)), calculated using Eq. 3 of my answer to a related question and simplifying to:
a1=2πA(round(A)4A2−(2round(A)−1)2−−−−−−−−−−−−−−−−−−−−√+∑k=1round(A)−1k(4A2−(2k−1)2−−−−−−−−−−−−−√−4A2−(2k+1)2−−−−−−−−−−−−−√)).(5)
Mean square of the quantized cosine wave is calculated in its first quarter-period by:
MS=1π/2∫π/20round(Acos(x))2dx=round(A)2π/2acos(round(A)−0.5A)+1π/2∑k=1round(A)−1k2(acos(k−0.5A)−acos(k+0.5A))(6)
THD is calculated and minimized by the following Python script (continued), using Eqs. 4, 5, and 6:
def a_1(A):
A = mp.mpf(A)
return 2*(mp.floor(A + 0.5)*mp.sqrt(4*A**2 - (2*mp.floor(A + 0.5) - 1)**2) + mp.nsum(lambda k: k*(mp.sqrt(4*A**2 - (2*k - 1)**2)-mp.sqrt(4*A**2 - (2*k + 1)**2)), [1, mp.floor(A + 0.5) - 1]))/(mp.pi*A)
def MS(A):
A = mp.mpf(A)
return mp.floor(A + 0.5)**2*mp.acos((mp.floor(A + 0.5)-0.5)/A)/(mp.pi/2) + mp.nsum(lambda k: k**2*(mp.acos((k - 0.5)/A) - mp.acos((k + 0.5)/A)), [1, mp.floor(A + 0.5) - 1])/(mp.pi/2)
def STHD(A): # Square of THD
MS_1 = a_1(A)**2/2
return MS(A)/MS_1 - 1
for m in range(2, 25):
optimal_A = to_max_digits(lambda: mp.findroot(lambda A: mp.diff(STHD, A), approx_optimal_A(m)), 80, 100, double_digits)
B = to_max_digits(lambda: a_1(mp.mpf(optimal_A)), 80, 100, double_digits)
THD_optimal = to_max_digits(lambda: 10*mp.log10(STHD(mp.mpf(optimal_A))), 80, 100, dB_digits)
THD_1 = to_max_digits(lambda: 10*mp.log10(STHD(2**(m-1)-1)), 80, 100, dB_digits)
THD_2 = to_max_digits(lambda: 10*mp.log10(STHD(2**(m-1)-0.5)), 80, 100, dB_digits)
print(str(m)+"&"+optimal_A+"&"+B+"&"+THD_optimal+"&"+THD_1+"&"+THD_2+"\\\\")
![enter image description here](https://i.stack.imgur.com/LclTG.png)
Figure 2. THD (dB) as function of unquantized amplitude A. The local maxima are at amplitudes that cause a narrow protrusion to poke out at the extrema of the quantized sinusoid.
![enter image description here](https://i.stack.imgur.com/U5F8k.png)
Figure 3. Amplitude a1 (blue solid line) of the fundamental frequency in the quantization of a cosine wave of amplitude A, with the identity line (orange dashed line) plotted for reference.
Table 2. Optimal A by definition 2 for different m≤24 and the resulting THD, with the THD for some common choices of A listed for comparison. Surprisingly, THD is minimized by the same A that maximize SNR (Table 1), also when tested with much higher precision than what is shown here. If the signal is considered to be a sinusoid of amplitude a1 instead of amplitude A, then SNR values are obtained by flipping the sign of the THD values.
m23456789101112131415161718192021222324optimal A1.268279494615303.238009421210377.2165859792940715.200718133195531.188875671425763.1800835394190127.173613625523255.168894736361511.1654791888901023.163022053772047.161262644844095.160007225168191.1591137160116383.158478966632767.158028642865535.1577094659131071.157483397262143.157323352524287.1572100891048575.157129952097151.157073264194303.157033178388607.15700481a1A=optimal1.170119519486793.195527051451717.1963252507877615.190704465809031.183859747699663.1775601103885127.172343338577255.168255766598511.1651581472981023.162860930522047.161181856974095.159966747888191.1590934470616383.158468821232767.158023566265535.1577069262131071.157482127262143.157322717524287.1572097711048575.157129792097151.157073184194303.157033138388607.15700479THD (dB)A=optimal−10.7629578−18.7633376−25.5045573−31.8098475−37.9357894−43.9857175−50.0049084−56.0134634−62.0199994−68.0278654−74.0380588−80.0505958−86.0651409−92.0812822−98.0986407−104.116904−110.135830−116.155234−122.174984−128.194979−134.215150−140.235446−146.255832m−1−1−10.1492078−18.2533980−25.1895549−31.6159563−37.8138122−43.9071306−49.9531877−55.9788184−61.9964656−68.0117064−74.0268736−80.0428071−86.0596937−92.0774606−98.0959534−104.115011−110.134495−116.154293−122.174319−128.194509−134.21482−140.235212−146.255672m−1−0.5−10.5728562−18.3370094−25.0267366−31.3681507−37.5642711−43.6902709−49.7783344−55.8439127−61.8952456−67.9371468−73.9726322−80.0036830−86.0316405−92.0574286−98.0816905−104.104877−110.127304−116.149195−122.170708−128.191953−134.213010−140.233932−146.25476
The equivalence of the two optimality definitions holds even when numerical precision is increased significantly, here with m=4 at least to 200 decimal places, in Python (continued):
m = 4
mp.mp.dps = 200
mp.findroot(lambda A: mp.diff(RelMSE, A), 2**(m-1)-1+0.157)
mp.findroot(lambda A: mp.diff(STHD, A), 2**(m-1)-1+0.157)
which outputs numerically identical optimal values for A for the two optimality definitions:
mpf('7.21658597929406951556806247230383254685067097032105786583650636819627678717747461433940963299310318715204551609940031954265317274195597248077934451075855527')
mpf('7.21658597929406951556806247230383254685067097032105786583650636819627678717747461433940963299310318715204551609940031954265317274195597248077934451075855527')
Limit m→∞
At large m it becomes difficult to directly optimize m numerically, so another approach is desirable. A Taylor approximation of the sinusoid about its peak, where it most differs from a linear function, is a quadratic polynomial. This can be used to analyze the effects of quantization in the limit m→∞ ⇒ A→∞. The difference between the MS quantization error of a sinusoid with an amplitude A and the MS quantization error 1/12 of a linear function is proportional to (Fig. 4):
MS−112∝f(a)=∫4a+2√/20((x2−a)2−112)dx+∑k=1∞∫4a+4k+2√/24a+4k−2√/2((x2−a−k)2−112)dx=160(4a+2−−−−−√16a2−4a−1+∑k=1∞(4a+4k+2−−−−−−−−−√(16a2+4a(8k−1)+16k2−4k−1)−4a+4k−2−−−−−−−−−√(16a2+4a(8k+1)+16k2+4k−1))),(7)
when the amplitude A is an integer →∞ plus a real number −0.5<a≤0.5. The sum in Eq. 7 appears to converge, whereas leaving out the term −112 would result in the sequence of partial sums growing without bound, indicating that no matter which value of a is chosen, (MS−112)/MS→0 and MS→112 as m→∞.
![enter image description here](https://i.stack.imgur.com/uCNc2.png)
Figure 4. f(a) and f′(a) of Eq. 7. The shape of f(a) looks identical to the shape of RRMSE for large m in Fig. 1.
By symbolically differentiating f(a) defined in Eq. 7 with respect to a (Fig. 4) and by finding the zero of f′(a) near a=0.157, the optimal a at m→∞ can be computed to quite a bit of precision, in Python (continued):
def f(a):
a = mp.mpf(a)
return (mp.sqrt(4*a + 2)*(16*a**2 - 4*a - 1) + mp.nsum(lambda k: (mp.sqrt(4*a + 4*k + 2)*(16*a**2 + 4*a*(8*k - 1) + 16*k**2 - 4*k - 1) - mp.sqrt(4*a + 4*k - 2)*(16*a**2 + 4*a*(8*k + 1) + 16*k**2 + 4*k - 1)), [1, mp.inf]))/60
def Df(a): # Derivative of f(a)
a = mp.mpf(a)
return mp.sqrt(2)*(16*a**2 + 4*a - 1)/(12*mp.sqrt(2*a + 1)) + mp.nsum(lambda k: (mp.sqrt(4*a + 4*k - 2)*(16*a**2 + 4*a*(8*k + 1) + 16*k**2 + 4*k - 1) - mp.sqrt(4*a + 4*k + 2)*(16*a**2 + 4*a*(8*k - 1) + 16*k**2 - 4*k - 1))/(12*mp.sqrt(2*a + 2*k + 1)*mp.sqrt(2*a + 2*k - 1)), [1, mp.inf])
to_max_digits(lambda: mp.findroot(lambda a: Df(a), 0.157), 63, 83, double_digits)
which gives a≈0.156936321399. This can be used to create an approximation of the optimal A as function of m, intended for large m≥20:
A≈2m−1−1+0.156936321399+e−1.2749819017−0.3464088917m,(8)
where the coefficients in the exponent were calculated by a linear fit at m∈{21,22} to a linearization of optimal A values of Table 1 or equivalently Table 2. The error from using the approximation is shown in Fig. 5.
![enter image description here](https://i.stack.imgur.com/ktAHJ.png)
Figure 5: Absolute error in the approximation of optimal A by Eq. 8 as function of m. For m∈{21,22}, which were used for fitting, and for m∈{23,24}, the absolute approximation error was less than 10−8.
Out of curiosity, I also computed the worst-case a that gives the largest MS quantization error as m→∞, by finding the zero of f′(a) near a=−0.43, which turned out to be at a≈−0.433510875868.
Conclusion
The two definitions of optimality appear to be equivalent, to convincing numerical precision. As m→∞, the optimal value of A approaches approximately 2m−1−1+0.156936321399 (or more accurately for large finite m the approximation of Eq. 8) and the quantization error reduction (by def. 1 SNR or def. 2 THD) in dB from choosing the optimal value approaches zero, compared to choosing another large value such as A=2m−1−1 or the nearly worst-case choice A=2m−1−0.5.
At the optimal amplitude A, the least squares (LS) sinusoid is of the same frequency and phase as the sinusoid being quantized, but has a somewhat lower amplitude a1 given in Table 2. This is somewhat counterintuitive. To minimize THD (or to maximize SNR with the sinusoid being approximated as the signal) of approximating a sinusoid of amplitude a1 using a waveform of m-bit numbers with values in range −2m−1…2m−1−1 that is constructed by quantizing (by rounding to the nearest integer) a sinusoid of amplitude A, one must choose optimal a1 and A that are not equal.
The results are applicable to continuous time without sampling, or to sampling in the limit f→fs, where f is the sinusoid frequency and fs is the sampling frequency. In general, optimality is not preserved by sampling. Random sampling with random times of the samples will preserve definition 1 SNR optimality, if the distribution of the sinusoid phase (modulo 2π) at the samples is uniform. Also, for irrational f/fs, the same optimality is preserved for the same reason, see equidistribution theorem. Definition 1 SNR optimality vanishes with rational f/fs, because the distribution of sinusoid phases will not be uniform.