ENع
→Quantization and bit depth
الدرس 12 · Sampling and conversion

Quantization and bit depth

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

12 دقيقة9 خطوات5 أسئلةBeginner
عن هذا الدرس

Sampling picks the moments; quantization picks the values. Follow one tone through a 3-bit converter: each sample falls into one of 8 slices 4 mV tall, is rounded to the slice's middle and stored as a 3-bit number. You will see the smooth wave turn into a staircase, measure the rounding error (never more than half a step), and learn why each extra bit halves that error, about 6 dB less noise. It builds on the lessons "Sampling: taking snapshots" and "Binary and Boolean logic".

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 12 · تفاعلي
01 / 09

A converter has a fixed set of levels

Sampling gave exact values such as 3.39 mV. A converter can only store a fixed set of numbers. A 3-bit converter splits −16 to +16 mV into 2³ = 8 equal slices. Each slice is Δ = 32 / 8 = 4 mV tall.

full-scale range  −16 .. +16 mV  (32 mV)
bits              N = 3
levels            2^N = 2 × 2 × 2 = 8
step (1 LSB)      Δ = 32 mV / 8 = 4 mV

slice  covers            stands for
  7    +12 .. +16 mV     +14 mV
  6     +8 .. +12 mV     +10 mV
  5     +4 ..  +8 mV      +6 mV
  4      0 ..  +4 mV      +2 mV
  3     −4 ..   0 mV      −2 mV
  ...
  0    −16 .. −12 mV     −14 mV
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. A converter has a fixed set of levels

    Sampling gave exact values such as 3.39 mV. A converter can only store a fixed set of numbers. A 3-bit converter splits −16 to +16 mV into 2³ = 8 equal slices. Each slice is Δ = 32 / 8 = 4 mV tall.

    full-scale range  −16 .. +16 mV  (32 mV)
    bits              N = 3
    levels            2^N = 2 × 2 × 2 = 8
    step (1 LSB)      Δ = 32 mV / 8 = 4 mV
    
    slice  covers            stands for
      7    +12 .. +16 mV     +14 mV
      6     +8 .. +12 mV     +10 mV
      5     +4 ..  +8 mV      +6 mV
      4      0 ..  +4 mV      +2 mV
      3     −4 ..   0 mV      −2 mV
      ...
      0    −16 .. −12 mV     −14 mV
  2. A sample finds its slice

    Sample x[1] = 3.39 mV arrives. The converter compares it with the ruler: it is above 0 mV and below 4 mV, so it falls in slice 4.

    x[n] = 10·sin(2π·440·n / 8000) mV
    x[1] = 3.39 mV
    
    which slice?
      0 mV ≤ 3.39 mV < 4 mV
      slice = floor(3.39 / 4) + 4
            = floor(0.847) + 4
            = 0 + 4 = 4
  3. Round to the middle of the slice

    Every value in a slice is replaced by the slice's middle, so 3.39 mV becomes 2 mV. The difference, 2 − 3.39 = −1.39 mV, is the quantization error. It is never more than half a step: 2 mV.

    slice 4 covers 0 .. 4 mV
    its middle: 2 mV
    
    q[1] = 2 mV
    e[1] = q[1] − x[1]
         = 2 − 3.39 = −1.39 mV
    
    worst case: half a step
      |e| ≤ Δ/2 = 2 mV
    (as long as x stays inside −16 .. +16 mV;
     beyond that the converter clips)
  4. Each level is a pattern of bits

    The converter outputs the slice number, not the voltage. Slice 4 is written in binary as 100. Three bits give 2 × 2 × 2 = 8 patterns, 000 to 111: one per slice. Bits per sample is the bit depth.

    slice  bits     slice  bits
      0    000        4    100
      1    001        5    101
      2    010        6    110
      3    011        7    111
    
    4 = 1·4 + 0·2 + 0·1  ->  100
    
    bit depth N = bits per sample
    N bits -> 2^N levels
    (binary numbers: see the lesson on
     binary and Boolean logic)
  5. One code per sample

    Each sample goes through the same two moves: find the slice, keep its number. The first five samples are stored as 100, 100, 101, 110, 110. The memory now holds 3 bits per sample instead of an exact value.

    n   x[n] mV   slice  bits   q[n] mV
    0    0.00     4     100     +2
    1    3.39     4     100     +2
    2    6.37     5     101     +6
    3    8.61     6     110    +10
    4    9.82     6     110    +10
    
    x[0] = 0 sits exactly on a threshold:
    the floor puts it in slice 4 (+2 mV)
  6. The rounded wave is a staircase

    Draw every rounded sample and hold it until the next one: the smooth wave becomes a staircase. Only 8 heights are allowed, so it follows the shape, but coarsely.

    40 samples in 5 ms
    each q[n] is one of 8 heights:
      −14 −10 −6 −2 +2 +6 +10 +14 mV
    
    this 10 mV wave never reaches ±12 mV,
    so it uses only slices 1 .. 6
    
    held between samples (drawn as steps)
    to make the comparison easy to see
  7. The error looks like noise

    Plot the error e[n] = q[n] − x[n]. It jumps between −2 and +2 mV and looks random over these 40 samples. With many bits and a busy signal such as music, it sounds like a faint hiss, so it is called quantization noise.

    e[n] = q[n] − x[n]
    
    largest |e| here: 2.00 mV  (= Δ/2)
    every error lies in −2 .. +2 mV
    
    the signal = what we want
    the error  = noise added on top
    q[n] = x[n] + e[n]
    
    this pure tone repeats every 200 samples
    (440/8000 = 11/200), so its error repeats
    too: heard as a buzz, not a hiss
  8. One more bit halves the error

    With 4 bits there are 16 slices of 2 mV, and the error stays within ±1 mV. Each extra bit doubles the levels and halves the error: about 6 dB less noise. CDs use 16 bits: 65,536 levels.

    bits  levels   step Δ     max |e|
      3        8   4 mV       2 mV
      4       16   2 mV       1 mV
      8      256   0.125 mV   0.0625 mV
     16   65,536   0.49 µV    0.24 µV
    
    half the error -> noise power ÷ 4
    10·log10(4) = 20·log10(2) = 6.02 dB per bit
    
    full-scale sine: SQNR ≈ 6.02·N + 1.76 dB
    16 bits (CD): about 98 dB
  9. Quantizing in Python

    With numpy, x is the array of 40 samples. np.floor finds each slice, + 4 makes the numbers start at 0, np.clip keeps them within 0 to 7, and the last line turns slices back into millivolts.

    import numpy as np
    n = np.arange(40)
    x = 10*np.sin(2*np.pi*440*n/8000)
    bits = 3; d = 32 / 2**bits
    i = np.floor(x / d) + 4
    i = np.clip(i, 0, 2**bits - 1)
    q = (i - 3.5) * d
    
    >>> i[:5]
    array([4., 4., 5., 6., 6.])
    >>> q[:5]
    array([ 2.,  2.,  6., 10., 10.])
    >>> abs(q - x).max()
    np.float64(2.0)
    
    (Python 3 with numpy)

اختبار يتاح بعد الخطوة الأخيرة.