ENع
→Sampling
الدرس 08 · Sampling and conversion

Sampling: taking snapshots

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

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

A computer cannot store a smooth wave, because it has a value at every instant. So it takes snapshots. Follow a 1000 Hz tone as a clock ticks 10,000 times a second, a sample-and-hold freezes the voltage at each tick, and a converter writes the number to memory: x[n] = x(n·Ts). Then see what changes when the clock runs faster or too slowly, and take the same snapshots in Python with numpy. The lesson "What a signal is" introduces x(t) and x[n]; this one shows how the snapshots are taken.

ستتعلّم

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

شكل 8 · تفاعلي
01 / 10

A smooth wave never stops

A signal source puts a smooth 1000 Hz wave on a wire: x(t) = sin(2π·1000·t) volts. It has a value at every instant, so even one millisecond holds infinitely many values. No computer can store them all.

x(t) = A·sin(2π·f0·t)
     A  = 1 V
     f0 = 1000 Hz  (1000 cycles a second)
     one cycle = 1/1000 s = 1 ms

between t = 0 and t = 1 ms there are
infinitely many instants, each with a value

plot: x(t) in volts against t in ms
motion in the figure: slowed down a lot
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. A smooth wave never stops

    A signal source puts a smooth 1000 Hz wave on a wire: x(t) = sin(2π·1000·t) volts. It has a value at every instant, so even one millisecond holds infinitely many values. No computer can store them all.

    x(t) = A·sin(2π·f0·t)
         A  = 1 V
         f0 = 1000 Hz  (1000 cycles a second)
         one cycle = 1/1000 s = 1 ms
    
    between t = 0 and t = 1 ms there are
    infinitely many instants, each with a value
    
    plot: x(t) in volts against t in ms
    motion in the figure: slowed down a lot
  2. A clock ticks every 0.1 ms

    So we only look now and then. A clock ticks fs = 10,000 times a second: the sampling rate. The time between ticks is the sampling period, Ts = 1/fs = 0.1 ms. Each tick closes a switch for a moment.

    sampling rate    fs = 10000 per second
                        = 10 kHz
    sampling period  Ts = 1 / fs
                        = 1 / 10000 s
                        = 0.1 ms = 100 µs
    
    ticks at t = 0, 0.1, 0.2, 0.3 ... ms
    (evenly spaced: periodic sampling)
  3. Sample and hold: the snapshot

    At each tick the switch joins the wire to a small capacitor, which charges to the voltage at that instant. Then the switch opens and the capacitor holds that voltage steady. This is a sample and hold: a snapshot.

    tick      switch closes: capacitor follows x(t)
    after     switch opens:  capacitor keeps the value
    
    held value stays flat until the next tick
    (the steps in the plot)
    
     t = 0.0 ms  holds 0.000 V
     t = 0.1 ms  holds 0.588 V
     t = 0.2 ms  holds 0.951 V
    
    why hold? the converter needs a steady
    voltage while it measures
  4. The converter writes a number

    While the value is held, the converter (ADC) measures it and writes the number to memory. Snapshot n is taken at t = n·Ts, so x[n] = x(n·Ts). For example x[2] = x(0.2 ms) = 0.951 V.

    x[n] = x(n·Ts),  Ts = 0.1 ms
    
     x[0] = x(0.0 ms) = 0.000 V
     x[1] = x(0.1 ms) = 0.588 V
     x[2] = x(0.2 ms) = 0.951 V
     x[3] = x(0.3 ms) = 0.951 V
     x[4] = x(0.4 ms) = 0.588 V
    
    (the numbers are shown to 3 decimals;
     rounding them to a fixed set of levels is
     the lesson on quantization)
  5. Now the wave is a list

    The smooth wave has become a list of numbers: 0, 0.588, 0.951, 0.951, 0.588 ... volts. The whole number n counts the ticks. This list x[n] is the discrete-time signal, and it is what the computer keeps.

    continuous time   x(t)   a value at every t
    discrete time     x[n]   a value per tick
    
    n = 0, 1, 2, ... 20  (whole numbers only)
    in 2 ms: 21 snapshots (t = 0 to 2.0 ms)
    
    written with square brackets: x[n]
    see also: "What a signal is"
  6. Snapshots per cycle

    How many snapshots are enough? The wave repeats 1000 times a second and the clock ticks 10,000 times, so each cycle gets fs/f0 = 10 snapshots. Phone calls are sampled 8000 times a second; CDs, 44,100.

    snapshots per cycle = fs / f0
                        = 10000 / 1000 = 10
    
    real sampling rates
      telephone (G.711)   8 000 per second
      audio CD           44 100 per second
    
    the rate is chosen for the fastest wiggle
    the signal may contain
  7. Nothing between the ticks

    Between two ticks the sampler does not look. Halfway between x[0] and x[1] the wave is at x(0.05 ms) = 0.309 V, but that value is not in the list. Sampling keeps only the instants t = n·Ts.

    x(0.05 ms) = sin(2π × 1000 × 0.00005)
               = sin(18°)
               = 0.309 V
    
    the list has x[0] = 0 and x[1] = 0.588
    and nothing in between
    
    can the in-between values be rebuilt?
    only if the clock is fast enough:
    that is the Nyquist rate lesson
  8. Faster: closer, but more data

    Tick four times faster, fs = 40,000 a second, and each cycle gets 40 snapshots. The dots follow the wave closely, but there are four times as many numbers to store and send. More detail costs more data.

    fs = 40000 per second
    Ts = 1 / fs = 0.025 ms = 25 µs
    per cycle: 40000 / 1000 = 40
    
    in 2 ms: 81 snapshots (was 21)
    
    numbers per second = fs
    faster clock -> more memory, more bandwidth
  9. Too slow: a different wave

    Tick too slowly, fs = 1500 a second, and each cycle gets only 1.5 snapshots. The dots now sit exactly on a slower 500 Hz wave. From the list alone, you cannot tell which wave made them.

    fs = 1500 per second, Ts = 0.667 ms
    per cycle: 1500 / 1000 = 1.5
    
    snapshots: 0, −0.866, 0.866, 0 V
    
    sin(2π·1000·n/1500) = −sin(2π·500·n/1500)
    for every whole n: the 1000 Hz wave and an
    upside-down 500 Hz wave give the same list
    
    this is aliasing; how fast is fast enough
    is the next lesson, The Nyquist rate
  10. Snapshots in Python

    In Python with numpy, np.arange(21) numbers the ticks 0 to 20, n/fs turns each into a time in seconds, and np.sin takes every snapshot in one line. The first five match the memory.

    import numpy as np
    fs = 10000
    n = np.arange(21)
    x = np.sin(2*np.pi*1000*n/fs)
    
    >>> x.round(3)[:5]
    array([0.   , 0.588, 0.951, 0.951, 0.588])
    >>> len(x)
    21
    
    n/fs is the tick time in seconds
    np.sin takes radians
    (Python 3 with numpy)

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