ENع
→Interpolation
الدرس 47 · Multirate and random signals

Interpolation: raising the sample rate

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

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

The Decimation lesson lowered a sample rate; this one raises it. Take a 1000 Hz tone sampled at 8 kHz and make it 32 kHz digitally, in two stages: put 3 zeros after every sample, then low-pass filter. Watch the zeros create images in the spectrum, see why the filter's cutoff is the old Nyquist frequency and its gain is L, check that the old samples pass through untouched, and learn the polyphase trick that skips the zeros and does 4 times less work. It builds on the lessons on the DTFT, windowed FIR filters (FIR design by windowing) and DACs and reconstruction.

ستتعلّم

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

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

The goal: four times as many samples

A 1 kHz tone was sampled at 8 kHz: 8 samples per cycle. We want the same tone at 32 kHz, 4 times as many samples, for example to feed a faster DAC. Interpolation by L = 4 does it digitally.

old rate   fs  = 8000 Hz   (Ts = 125 µs)
new rate   L·fs = 32000 Hz
factor     L = 4

x[n] = cos(2π·1000·n/8000) = cos(2πn/8)
 x[0] = 1.000  x[1] = 0.707
 x[2] = 0.000  x[3] = −0.707

the recipe (two stages):
  1. put L−1 zeros between samples
  2. low-pass filter, gain L
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. The goal: four times as many samples

    A 1 kHz tone was sampled at 8 kHz: 8 samples per cycle. We want the same tone at 32 kHz, 4 times as many samples, for example to feed a faster DAC. Interpolation by L = 4 does it digitally.

    old rate   fs  = 8000 Hz   (Ts = 125 µs)
    new rate   L·fs = 32000 Hz
    factor     L = 4
    
    x[n] = cos(2π·1000·n/8000) = cos(2πn/8)
     x[0] = 1.000  x[1] = 0.707
     x[2] = 0.000  x[3] = −0.707
    
    the recipe (two stages):
      1. put L−1 zeros between samples
      2. low-pass filter, gain L
  2. Stage 1: three zeros after each sample

    The upsampler, written ↑4, passes each sample on and then sends 3 zeros. So xe[m] = x[m/4] when m is a multiple of 4, and 0 otherwise. Same token speed, 4 times as many tokens: 32 kHz.

    xe[m] = x[m/L]   if m = 0, ±L, ±2L ...
          = 0        otherwise
    
     m :  0     1  2  3  4     5  6  7  8
    xe :  1.000 0  0  0  0.707 0  0  0  0.000
    
    new sample spacing Ts/L = 31.25 µs
    the zeros carry no information yet
  3. The spectrum now shows images

    A sampled spectrum repeats every 8 kHz. At the new rate we can see up to 16 kHz, so besides the 1 kHz tone there are copies at 7, 9, 15 kHz. These copies are called images. They are the fast wiggles the zeros add.

    DTFT of the expander output:
      Xe(e^jω) = X(e^jωL)
      the old spectrum, squeezed by L = 4
    
    in hertz (new Nyquist 16 kHz):
      tone     1 kHz
      images   8 − 1 = 7 kHz
               8 + 1 = 9 kHz
              16 − 1 = 15 kHz
    
    DFT: 128 samples, bins every 250 Hz
  4. Each line is a quarter as tall

    Three of every 4 numbers are now zero. As tones, xe is the 1 kHz tone plus its 3 images, each 1/L as tall: 0.25 instead of 1.00. xe has a quarter of the power, split equally over 4 lines.

    the comb 1, 0, 0, 0, 1, 0, 0, 0 ...
      = (1/L)·Σ e^{j2πrm/L},  r = 0..L−1
    so xe[m] = x[m/L] × comb
      = 4 copies of the tone, each × 1/4
    
    DFT amplitudes (one-sided):
      x  : 1 kHz -> 1.00
      xe : 1, 7, 9, 15 kHz -> 0.25, 0.25, 0.25, 0.25
  5. Stage 2: a low-pass filter

    Keep 0 to 4 kHz, the band the old samples could hold, and remove the rest. The cutoff is the old Nyquist frequency, fs/2 = 4 kHz, or π/L radians per sample. The filter runs at the new rate.

    ideal interpolation filter (new rate):
      gain L = 4   for |ω| < π/L
      gain 0         for π/L < |ω| ≤ π
      π/L  <->  16 kHz / 4 = 4 kHz
    
    ideal h[m] = sinc(m/L)  (endless)
    real  h[q] = sinc(q/4)·hamming,  q = −16..16
           33 taps
    
    plot: gain ÷ 4, so 1.0 = gain 4
    |H| at 4 kHz = 2.00 (half of 4)
  6. Gain L puts the height back

    In its passband the filter's gain is about L = 4, so the 1 kHz line grows from 0.25 back to 1.00. At 7 kHz the gain is only 0.0019, so that image shrinks to 0.0005: 67 dB below the tone.

    |H| at  1 kHz = 4.008
    |H| at  7 kHz = 0.0019
    |H| at  9 kHz = 0.0035
    |H| at 15 kHz = 0.0025
    
    output lines:
      1 kHz  0.25 × 4.008 = 1.00
      7 kHz  0.25 × 0.0019 = 0.0005
      ratio  -67 dB
    
    why L: each line of xe is 1/L as tall
           (power 1/L²); gain L restores
           the amplitude
  7. The zeros are filled in

    Out come 32000 samples a second that follow the tone. y[1] = 0.983, and the true tone at that instant is cos(2π/32) = 0.981. Over a whole cycle the largest error is 0.003.

    y[m] = Σ h[q]·xe[m − q],  q = −16..16
    
     m   y[m]    cos(2πm/32)
     0   1.000   1.000
     1   0.983   0.981
     2   0.926   0.924
     3   0.833   0.831
     4   0.707   0.707
     5   0.557   0.556
     6   0.384   0.383
     7   0.195   0.195
    
    largest |error| over a cycle: 0.003
    centred filter shown; a causal one gives
    the same values 16 samples (0.5 ms) later
  8. Old samples pass straight through

    Here h[0] = 1, and h is 0 at every other multiple of 4. Lined up at m = 16, it meets just one non-zero input, so y[16] = x[4] = −1.000 exactly. Only the 3 new values between old samples are computed.

    h[q] = sinc(q/4)·w:
      h[0] = 1
      h[±4] = h[±8] = ... = 0   (sinc(±1), sinc(±2) ...)
    
    y[4n] = x[n]·h[0] = x[n]
      y[0]  = 1.000   x[0] = 1.000
      y[4]  = 0.707   x[1] = 0.707
      y[16] = −1.000   x[4] = −1.000
    
    a filter like this is called
    a Nyquist (L-th band) filter
  9. Skip the zeros: polyphase

    To make y[17] the 33 taps line up with 33 inputs, but only 8 are not zero. Skipping zeros splits h into 4 small filters. In general that is 8.25 multiplications per output, not 33. Here phase 0 is a plain copy, so just 6.

    phase r = m mod 4: taps q with (m − q) a multiple of 4
      r = 0:  9 taps  (-16, -12, -8, -4, 0, 4, 8, 12, 16)
      r = 1:  8 taps  (-15, -11, -7, -3, 1, 5, 9, 13)
      r = 2:  8 taps  (-14, -10, -6, -2, 2, 6, 10, 14)
      r = 3:  8 taps  (-13, -9, -5, -1, 3, 7, 11, 15)
    
    any 33-tap filter: 9 + 8 + 8 + 8 = 33 per 4 outputs
      = 8.25 per output, ÷ 4 vs 33
    this Nyquist filter: phase 0 taps are
      0 except h[0] = 1, so y[4n] = x[n] (a copy)
      0 + 8 + 8 + 8 = 24 per 4 outputs
      = 6 per output, ÷ 5.5 vs 33
    
    the 4 sub-filters run at the OLD rate
  10. Interpolation in Python

    With numpy: make zeros, copy x into every 4th slot, then convolve with the windowed sinc. Mode "valid" keeps only full overlaps, so y[0] lines up with x[0]. scipy.signal.resample_poly(x, 4, 1) does the same job, polyphase, with a Kaiser window, so its numbers differ slightly.

    import numpy as np          # Python 3 + numpy
    n = np.arange(-4, 36)
    x = np.cos(2*np.pi*n/8)
    xe = np.zeros(160); xe[::4] = x
    q = np.arange(-16, 17)
    h = np.sinc(q/4)*np.hamming(33)
    y = np.convolve(xe, h, "valid")
    
    >>> len(y)      # 160 − 33 + 1
    128
    >>> y[:8].round(3)
    array([1.   , 0.983, 0.926, 0.833, 0.707, 0.557, 0.384, 0.195])
    
    (n starts at −4 so the filter sees
     4 old samples before x[0])

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